00001 #include "../stdafx.h"
00002 #include <stdlib.h>
00003 #include "com_phidgets_Dictionary.h"
00004 #include "../cphidgetdictionary.h"
00005 #include "../csocket.h"
00006 #include "phidget_jni.h"
00007
00008 static int CCONV serverConnect_handler(CPhidgetDictionaryHandle h, void *arg);
00009 static int CCONV serverDisconnect_handler(CPhidgetDictionaryHandle h, void *arg);
00010
00011 static jclass dictionary_class;
00012
00013 static jfieldID dictionary_handle_fid;
00014 static jfieldID nativeServerConnectHandler_fid;
00015 static jfieldID nativeServerDisconnectHandler_fid;
00016
00017 static jmethodID fireServerConnect_mid;
00018 static jmethodID fireServerDisconnect_mid;
00019
00020 void
00021 com_phidgets_Dictionary_OnLoad(JNIEnv *env)
00022 {
00023
00024 if (!(dictionary_class = (*env)->FindClass(env, "com/phidgets/Dictionary")))
00025 JNI_ABORT_STDERR("Couldn't FindClass com/phidgets/Dictionary");
00026 if (!(dictionary_class = (jclass)(*env)->NewGlobalRef(env, dictionary_class)))
00027 JNI_ABORT_STDERR("Couldn't create global ref dictionary_class");
00028
00029 if (!(dictionary_handle_fid = (*env)->GetFieldID(env, dictionary_class, "handle", "J")))
00030 JNI_ABORT_STDERR("Couldn't get Field ID handle from dictionary_class");
00031 if (!(nativeServerConnectHandler_fid = (*env)->GetFieldID(env, dictionary_class, "nativeServerConnectHandler", "J")))
00032 JNI_ABORT_STDERR("Couldn't get Field ID nativeServerConnectHandler from dictionary_class");
00033 if (!(nativeServerDisconnectHandler_fid = (*env)->GetFieldID(env, dictionary_class, "nativeServerDisconnectHandler", "J")))
00034 JNI_ABORT_STDERR("Couldn't get Field ID nativeServerDisconnectHandler from dictionary_class");
00035
00036 if (!(fireServerConnect_mid = (*env)->GetMethodID(env, dictionary_class, "fireServerConnect", "(Lcom/phidgets/event/ServerConnectEvent;)V")))
00037 JNI_ABORT_STDERR("Couldn't get method ID fireServerConnect from dictionary_class");
00038 if (!(fireServerDisconnect_mid = (*env)->GetMethodID(env, dictionary_class, "fireServerDisconnect", "(Lcom/phidgets/event/ServerDisconnectEvent;)V")))
00039 JNI_ABORT_STDERR("Couldn't get method ID fireServerDisconnect from dictionary_class");
00040 }
00041
00042 JNIEXPORT void JNICALL
00043 Java_com_phidgets_Dictionary_enableServerConnectEvents(JNIEnv *env, jobject obj,
00044 jboolean b)
00045 {
00046 jlong gr = updateGlobalRef(env, obj, nativeServerConnectHandler_fid, b);
00047 CPhidgetDictionaryHandle h = (CPhidgetDictionaryHandle)(uintptr_t)(*env)->GetLongField(env,
00048 obj, dictionary_handle_fid);
00049
00050 CPhidgetDictionary_set_OnServerConnect_Handler(h, b ? serverConnect_handler : 0,
00051 (void *)(uintptr_t)gr);
00052 }
00053
00054 static int CCONV
00055 serverConnect_handler(CPhidgetDictionaryHandle h, void *arg)
00056 {
00057 JNIEnv *env;
00058 jobject obj;
00059 jobject serverConnectEvent;
00060
00061 if ((*ph_vm)->AttachCurrentThread(ph_vm, (JNIEnvPtr)&env, NULL))
00062 JNI_ABORT_STDERR("Couldn't AttachCurrentThread");
00063
00064 obj = (jobject)arg;
00065
00066 if (!(serverConnectEvent = (*env)->NewObject(env, serverConnectEvent_class, serverConnectEvent_cons,
00067 obj)))
00068 return -1;
00069 (*env)->CallVoidMethod(env, obj, fireServerConnect_mid, serverConnectEvent);
00070 (*env)->DeleteLocalRef(env, serverConnectEvent);
00071
00072 return 0;
00073 }
00074
00075 JNIEXPORT void JNICALL
00076 Java_com_phidgets_Dictionary_enableServerDisconnectEvents(JNIEnv *env, jobject obj,
00077 jboolean b)
00078 {
00079 jlong gr = updateGlobalRef(env, obj, nativeServerDisconnectHandler_fid, b);
00080 CPhidgetDictionaryHandle h = (CPhidgetDictionaryHandle)(uintptr_t)(*env)->GetLongField(env,
00081 obj, dictionary_handle_fid);
00082
00083 CPhidgetDictionary_set_OnServerDisconnect_Handler(h, b ? serverDisconnect_handler : 0,
00084 (void *)(uintptr_t)gr);
00085 }
00086
00087 static int CCONV
00088 serverDisconnect_handler(CPhidgetDictionaryHandle h, void *arg)
00089 {
00090 JNIEnv *env;
00091 jobject obj;
00092 jobject serverDisconnectEvent;
00093
00094 if ((*ph_vm)->AttachCurrentThread(ph_vm, (JNIEnvPtr)&env, NULL))
00095 JNI_ABORT_STDERR("Couldn't AttachCurrentThread");
00096
00097 obj = (jobject)arg;
00098
00099 if (!(serverDisconnectEvent = (*env)->NewObject(env, serverDisconnectEvent_class, serverDisconnectEvent_cons,
00100 obj)))
00101 return -1;
00102 (*env)->CallVoidMethod(env, obj, fireServerDisconnect_mid, serverDisconnectEvent);
00103 (*env)->DeleteLocalRef(env, serverDisconnectEvent);
00104
00105 return 0;
00106 }
00107
00108 JNIEXPORT jboolean JNICALL
00109 Java_com_phidgets_Dictionary_isAttached(JNIEnv *env, jobject obj)
00110 {
00111 CPhidgetHandle h = (CPhidgetHandle)(uintptr_t)(*env)->GetLongField(env,
00112 obj, dictionary_handle_fid);
00113 int error;
00114 int s;
00115
00116 if ((error = CPhidget_getDeviceStatus(h, &s)))
00117 PH_THROW(error);
00118
00119 return s == PHIDGET_ATTACHED;
00120 }
00121
00122 JNIEXPORT jboolean JNICALL
00123 Java_com_phidgets_Dictionary_isAttachedToServer(JNIEnv *env, jobject obj)
00124 {
00125 CPhidgetDictionaryHandle h = (CPhidgetDictionaryHandle)(uintptr_t)(*env)->GetLongField(env,
00126 obj, dictionary_handle_fid);
00127 int error;
00128 int s;
00129
00130 if ((error = CPhidgetDictionary_getServerStatus(h, &s)))
00131 PH_THROW(error);
00132
00133 return s == PHIDGET_ATTACHED;
00134 }
00135
00136 JNIEXPORT jint JNICALL
00137 Java_com_phidgets_Dictionary_getServerPort(JNIEnv *env, jobject obj)
00138 {
00139 CPhidgetDictionaryHandle h = (CPhidgetDictionaryHandle)(uintptr_t)(*env)->GetLongField(env,
00140 obj, dictionary_handle_fid);
00141 int error;
00142 int port;
00143 const char *addr;
00144
00145 if ((error = CPhidgetDictionary_getServerAddress(h, &addr, &port)))
00146 PH_THROW(error);
00147
00148 return port;
00149 }
00150
00151 JNIEXPORT jstring JNICALL
00152 Java_com_phidgets_Dictionary_getServerAddress(JNIEnv *env, jobject obj)
00153 {
00154 CPhidgetDictionaryHandle h = (CPhidgetDictionaryHandle)(uintptr_t)(*env)->GetLongField(env,
00155 obj, dictionary_handle_fid);
00156 int error;
00157 int port;
00158 const char *addr;
00159
00160 if ((error = CPhidgetDictionary_getServerAddress(h, &addr, &port)))
00161 PH_THROW(error);
00162
00163 return (*env)->NewStringUTF(env, addr);
00164 }
00165
00166 JNIEXPORT jstring JNICALL
00167 Java_com_phidgets_Dictionary_getServerID(JNIEnv *env, jobject obj)
00168 {
00169 CPhidgetDictionaryHandle h = (CPhidgetDictionaryHandle)(uintptr_t)(*env)->GetLongField(env,
00170 obj, dictionary_handle_fid);
00171 int error;
00172 const char *id;
00173
00174 if ((error = CPhidgetDictionary_getServerID(h, &id)))
00175 PH_THROW(error);
00176
00177 return (*env)->NewStringUTF(env, id);
00178 }
00179
00180 JNIEXPORT void JNICALL
00181 Java_com_phidgets_Dictionary_nativeOpenRemote(JNIEnv *env, jobject obj, jstring serverID, jstring pass)
00182 {
00183 int error;
00184 jboolean iscopy;
00185 const char *serverIDcopy = serverID?(*env)->GetStringUTFChars(
00186 env, serverID, &iscopy):0;
00187 const char *passcopy = (*env)->GetStringUTFChars(
00188 env, pass, &iscopy);
00189
00190 CPhidgetDictionaryHandle h = (CPhidgetDictionaryHandle)(uintptr_t)
00191 (*env)->GetLongField(env, obj, dictionary_handle_fid);
00192
00193 if ((error = CPhidgetDictionary_openRemote(h, serverIDcopy, passcopy)))
00194 PH_THROW(error);
00195
00196 if(serverID)
00197 (*env)->ReleaseStringUTFChars(env, serverID, serverIDcopy);
00198 (*env)->ReleaseStringUTFChars(env, pass, passcopy);
00199 }
00200
00201 JNIEXPORT void JNICALL
00202 Java_com_phidgets_Dictionary_nativeOpenRemoteIP(JNIEnv *env, jobject obj, jstring ipAddr, jint port, jstring pass)
00203 {
00204 int error;
00205 jboolean iscopy;
00206 const char *ipAddrcopy = (*env)->GetStringUTFChars(
00207 env, ipAddr, &iscopy);
00208 const char *passcopy = (*env)->GetStringUTFChars(
00209 env, pass, &iscopy);
00210
00211 CPhidgetDictionaryHandle h = (CPhidgetDictionaryHandle)(uintptr_t)
00212 (*env)->GetLongField(env, obj, dictionary_handle_fid);
00213
00214 if ((error = CPhidgetDictionary_openRemoteIP(h, ipAddrcopy, port, passcopy)))
00215 PH_THROW(error);
00216
00217 (*env)->ReleaseStringUTFChars(env, ipAddr, ipAddrcopy);
00218 (*env)->ReleaseStringUTFChars(env, pass, passcopy);
00219 }
00220
00221 JNIEXPORT void JNICALL
00222 Java_com_phidgets_Dictionary_nativeDelete(JNIEnv *env, jobject obj)
00223 {
00224 int error;
00225
00226 CPhidgetDictionaryHandle h = (CPhidgetDictionaryHandle)(uintptr_t)
00227 (*env)->GetLongField(env, obj, dictionary_handle_fid);
00228
00229 if ((error = CPhidgetDictionary_delete(h)))
00230 PH_THROW(error);
00231 }
00232
00233 JNIEXPORT void JNICALL
00234 Java_com_phidgets_Dictionary_nativeClose(JNIEnv *env, jobject obj)
00235 {
00236 int error;
00237
00238 CPhidgetDictionaryHandle h = (CPhidgetDictionaryHandle)(uintptr_t)
00239 (*env)->GetLongField(env, obj, dictionary_handle_fid);
00240
00241 if ((error = CPhidgetDictionary_close(h)))
00242 PH_THROW(error);
00243 }
00244
00245 JNIEXPORT void JNICALL
00246 Java_com_phidgets_Dictionary_nativeAddKey(JNIEnv *env, jobject obj, jstring key, jstring val, jint persistent)
00247 {
00248 int error;
00249
00250 jboolean iscopy;
00251 const char *keyString = (*env)->GetStringUTFChars(
00252 env, key, &iscopy);
00253 const char *valString = (*env)->GetStringUTFChars(
00254 env, val, &iscopy);
00255
00256 CPhidgetDictionaryHandle h = (CPhidgetDictionaryHandle)(uintptr_t)
00257 (*env)->GetLongField(env, obj, dictionary_handle_fid);
00258
00259 if ((error = CPhidgetDictionary_addKey(h, keyString, valString, persistent)))
00260 PH_THROW(error);
00261
00262 (*env)->ReleaseStringUTFChars(env, key, keyString);
00263 (*env)->ReleaseStringUTFChars(env, val, valString);
00264 }
00265
00266 JNIEXPORT void JNICALL
00267 Java_com_phidgets_Dictionary_nativeRemoveKey(JNIEnv *env, jobject obj, jstring pattern)
00268 {
00269 int error;
00270
00271 jboolean iscopy;
00272 const char *patternString = (*env)->GetStringUTFChars(
00273 env, pattern, &iscopy);
00274
00275 CPhidgetDictionaryHandle h = (CPhidgetDictionaryHandle)(uintptr_t)
00276 (*env)->GetLongField(env, obj, dictionary_handle_fid);
00277
00278 if ((error = CPhidgetDictionary_removeKey(h, patternString)))
00279 PH_THROW(error);
00280
00281 (*env)->ReleaseStringUTFChars(env, pattern, patternString);
00282 }
00283
00284 JNIEXPORT jstring JNICALL
00285 Java_com_phidgets_Dictionary_nativeGetKey(JNIEnv *env, jobject obj, jstring key)
00286 {
00287 int error;
00288
00289 char val[1024];
00290
00291 jboolean iscopy;
00292 const char *keyString = (*env)->GetStringUTFChars(env, key, &iscopy);
00293
00294 CPhidgetDictionaryHandle h = (CPhidgetDictionaryHandle)(uintptr_t)
00295 (*env)->GetLongField(env, obj, dictionary_handle_fid);
00296
00297 if ((error = CPhidgetDictionary_getKey(h, keyString, val, sizeof(val))))
00298 {
00299 PH_THROW(error);
00300 (*env)->ReleaseStringUTFChars(env, key, keyString);
00301 return NULL;
00302 }
00303 else
00304 {
00305 (*env)->ReleaseStringUTFChars(env, key, keyString);
00306 return (*env)->NewStringUTF(env, val);
00307 }
00308 }
00309
00310 JNIEXPORT jlong JNICALL
00311 Java_com_phidgets_Dictionary_create(JNIEnv *env, jobject obj)
00312 {
00313 int error;
00314 CPhidgetDictionaryHandle dict;
00315
00316 if ((error = CPhidgetDictionary_create(&dict)))
00317 PH_THROW(error);
00318 return (jlong)(uintptr_t)dict;
00319 }