source: trunk/FACT++/dim/src/dim_jni.c@ 18275

Last change on this file since 18275 was 15282, checked in by tbretz, 12 years ago
Updated to v20r7.
File size: 79.2 KB
Line 
1/*
2 * dim_JNI_Native.c : JNI native code for dim
3 * routines to be used by Java.
4 *
5 * Started on : 2000-08-16
6 * Written by : M.Jonker
7 *
8 * Version history:
9 * 20000911MJJ First Released Version (Not 100% complete yet)
10 * 31-Oct-2007 Adjustments for 64bit platforms Joern Adamczewski, gsi
11 * 03-Dec-2008 Fix in dim_Client releaseService Hans Essel, gsi
12 *
13 */
14/* TODO Remove these kludges */
15#define dim_Dbg_MEMORY dim_Dbg_SERIALIZER
16#define dim_Dbg_MUTABLE_MEMORY dim_Dbg_SERIALIZER
17#define dim_Dbg_MEMORY_ALLOCATE dim_Dbg_SERIALIZER
18
19/* TODO Split the Native and the serialization into two source files */
20/* TODO Release global references when they are not anymore used */
21
22#define DIMLIB
23#include <stdio.h>
24#include "dim.h"
25#include "dic.h"
26#include "dis.h"
27#include "dim_jni.h"
28
29
30#ifdef JNI_VERSION_1_4
31#define JNI_VERSION JNI_VERSION_1_4
32#else
33#ifdef JNI_VERSION_1_3
34#define JNI_VERSION JNI_VERSION_1_3
35#else
36#ifdef JNI_VERSION_1_2
37#define JNI_VERSION JNI_VERSION_1_2
38#else
39#define JNI_VERSION 0x00010001
40#endif
41#endif
42#endif
43#define dim_JNI_version DIM_VERSION_NUMBER
44
45
46// Debug/tracing support ===============================================================================
47// The DBGe (Entry) DBGx (exit) DBGm (middle) DBG (Message only) macros allow for tracing and trapping
48// of native method calls.
49// DBGe, DBGx and DBGm have corresponding trap mask to active entry exits and middle traps
50// By setting DBG_filter to something restrictive, we suppress the code generation of the conditional
51// printouts for all dbg conditions except the conditions specified in the DBG_filter. (Provided the
52// optimiser does its job.)
53#ifndef DBG_filter
54 #ifdef _DEBUG
55 // no filter
56 #define DBG_filter 0xFFFFFFFF
57 #else
58 // filter all but module loading and unloading
59 #define DBG_filter dim_Dbg_MODULE
60 #endif
61#endif
62
63#define DBG(test) if(((test&DBG_filter) & DBG_mask ) !=0) /* etc . */
64#define DBGe(test) if(((test&DBG_filter) & DBGe_trap ) !=0) DBG_Trap(test); DBG(test) /* etc ; */
65#define DBGm(test) if(((test&DBG_filter) & DBGm_trap ) !=0) DBG_Trap(test); DBG(test) /* etc ; */
66#define DBGx(test) if(((test&DBG_filter) & DBGx_trap ) !=0) DBG_Trap(test); DBG(test) /* etc ; */
67
68static int DBG_mask = dim_Dbg_MODULE;
69static int DBGe_trap = 0;
70static int DBGm_trap = 0;
71static int DBGx_trap = 0;
72
73static void DBG_Trap(int code)
74{
75 /* if you set a break point here you can trap all */
76 /* native calls that are activated by the mask DBG_trap */
77// TODO DBG_Trap should invoke the debugger
78 if(code){}
79 return;
80}
81// ===============================================================================Debug/tracing support=
82
83
84// Static module variables
85JavaVM* theJavaVM;
86
87jclass NativeDataMemory;
88jmethodID NativeDataMemory_new;
89jmethodID NativeDataMemory_decodeData;
90jfieldID NativeDataMemory_dataAddress;
91jfieldID NativeDataMemory_dataSize;
92
93jclass ObjectDescriptor;
94
95jclass SendSynchronizer;
96jmethodID SendSynchronizer_new;
97jmethodID SendSynchronizer_setCompletionCode;
98jmethodID SendSynchronizer_getCompletionCode;
99
100jclass ReceiveSynchronizer;
101jmethodID ReceiveSynchronizer_new;
102jmethodID ReceiveSynchronizer_decodeNativeData;
103jmethodID ReceiveSynchronizer_getCompletionCode;
104
105jclass CompletionHandler;
106jmethodID CompletionHandler_setCompletionCode;
107
108jclass NativeDataDecoder;
109jmethodID NativeDataDecoder_decodeNativeData;
110
111jclass NativeDataEncoder;
112jmethodID NativeDataEncoder_encodeNativeData;
113
114jclass NativeDimTimer;
115jmethodID NativeDimTimer_timerHandler;
116
117jclass NativeDimSrvError;
118jmethodID NativeDimSrvError_errorHandler;
119jmethodID NativeDimSrvError_new;
120jobject ourNativeDimSrvError;
121
122jclass NativeDimCltError;
123jmethodID NativeDimCltError_errorHandler;
124jmethodID NativeDimCltError_new;
125jobject ourNativeDimCltError;
126
127jclass NativeDimExit;
128jmethodID NativeDimExit_exitHandler;
129jmethodID NativeDimExit_new;
130jobject ourNativeDimExit;
131
132jobject ourNativeMemoryObject;
133
134#ifdef WIN32
135 DWORD MainThreadId = 0;
136#else
137#ifdef __linux__
138 pthread_t MainThreadId = 0;
139#else
140 int MainThreadId = 0;
141#endif
142#endif
143JNIEnv* TheEnv;
144
145#define NOT_STAMPED 0
146
147// TODO need to get a hook into dim release services so that I can release global references
148// Note: I can test on the user library beeing one of the four DIM_JNI specific callbacks to
149// decide whether the tag was a global reference.
150// I also have to control the copying of the tags by DIM libraries:
151// There may be actually some complications because in certain cases the release service is
152// not invoked but the service is kept on a 'hot-spare' list.
153// In case of DIS, information from the servep structure maybe copied to a dtq structure.
154
155// Forward defintions =====================================================================
156
157void info_service_callback( jobject* _aDataDecoder, void* data, int* _size);
158void info_service_callback_with_cleanup(jobject* _aDataDecoder, void* data, int* _size);
159void send_callback(jobject* _aCompletionHandler, int* _status);
160
161void server_getInfo_callback(jobject* _aDataEncoder, void* *address, int *size);
162void server_setCmnd_callback(jobject* _aDataDecoder, void *address, int *size);
163void timer_callback( jobject* _aDimTimer);
164void server_error_callback(int severity, int code, char *msg);
165void client_error_callback(int severity, int code, char *msg);
166void server_exit_callback(int *code);
167
168
169// DLL load, unload and init ==============================================================
170
171int dim_jni_attachThread(JNIEnv **env)
172{
173#ifdef WIN32
174 DWORD tid;
175 tid = GetCurrentThreadId();
176#else
177#ifdef __linux__
178 pthread_t tid;
179 tid = pthread_self();
180#else
181 int tid = 0;
182#endif
183#endif
184
185 if(tid == MainThreadId)
186 {
187 *env = TheEnv;
188 return 0;
189 }
190 (*theJavaVM)->AttachCurrentThread(theJavaVM, (void *)env, NULL);
191 if(MainThreadId == 0)
192 {
193 MainThreadId = tid;
194 }
195 return 1;
196}
197
198JNIEXPORT jint JNICALL
199JNI_OnLoad(JavaVM* jvm, void* reserved)
200{
201 int bugs =0;
202 JNIEnv *env;
203
204// DBGe(dim_Dbg_MODULE) ; /* trap only, report on exit */
205
206 if(reserved){}
207 theJavaVM = jvm;
208
209 dim_jni_attachThread(&env);
210 TheEnv = env;
211// (*theJavaVM)->AttachCurrentThread(theJavaVM, (void *)&env, NULL);
212
213#ifdef develop_a_better_understanding_of_java
214 {
215 jclass test_cid;
216 jmethodID test_mid;
217
218 test_cid = (*env)->FindClass(env,"dim/test/Ntest$static_class");
219 test_mid = (*env)->GetMethodID(env,test_cid, "instance_method", "()V");
220 test_mid = (*env)->GetMethodID(env,test_cid, "<init>", "()V");
221 test_mid = (*env)->GetStaticMethodID(env,test_cid, "static_method", "()V");
222 if ((*env)->ExceptionOccurred(env)) (*env)->ExceptionDescribe(env);
223
224 test_cid = (*env)->FindClass(env,"dim/test/Ntest$instance_class");
225 test_mid = (*env)->GetMethodID(env,test_cid, "instance_method", "()V");
226 test_mid = (*env)->GetMethodID(env,test_cid, "<init>", "()V");
227 if ((*env)->ExceptionOccurred(env)) (*env)->ExceptionDescribe(env);
228
229 test_cid = (*env)->FindClass(env,"dim/test/Ntest$method_class");
230 test_mid = (*env)->GetMethodID(env,test_cid, "instance_method", "()V");
231 test_mid = (*env)->GetMethodID(env,test_cid, "<init>", "()V");
232 if ((*env)->ExceptionOccurred(env)) (*env)->ExceptionDescribe(env);
233 }
234#endif
235
236 NativeDataMemory = (*env)->FindClass(env, "dim/Memory");
237 NativeDataMemory_new = (*env)->GetMethodID (env, NativeDataMemory, "<init>", "()V");
238 NativeDataMemory_decodeData = (*env)->GetMethodID (env, NativeDataMemory, "decodeData", "(JILdim/DataDecoder;)V");
239 NativeDataMemory_dataAddress = (*env)->GetFieldID (env, NativeDataMemory, "dataAddress", "J");
240 NativeDataMemory_dataSize = (*env)->GetFieldID (env, NativeDataMemory, "highWaterMark", "I");
241 NativeDataMemory = (*env)->NewGlobalRef(env, NativeDataMemory);
242 if ((*env)->ExceptionOccurred(env)) {bugs++; (*env)->ExceptionDescribe(env);}
243
244 ourNativeMemoryObject = (*env)->NewObject(env, NativeDataMemory, NativeDataMemory_new);
245 ourNativeMemoryObject = (*env)->NewGlobalRef(env, ourNativeMemoryObject);
246 if ((*env)->ExceptionOccurred(env)) {bugs++; (*env)->ExceptionDescribe(env);}
247
248 SendSynchronizer = (*env)->FindClass(env, "dim/Client$SendSynchronizer");
249 SendSynchronizer_new = (*env)->GetMethodID (env, SendSynchronizer, "<init>", "(Ldim/CompletionHandler;)V");
250 SendSynchronizer_setCompletionCode = (*env)->GetMethodID (env, SendSynchronizer, "setCompletionCode", "(I)I");
251 SendSynchronizer_getCompletionCode = (*env)->GetMethodID (env, SendSynchronizer, "getCompletionCode", "(I)I");
252 SendSynchronizer = (*env)->NewGlobalRef(env, SendSynchronizer);
253 if ((*env)->ExceptionOccurred(env)) {bugs++; (*env)->ExceptionDescribe(env);}
254
255 ReceiveSynchronizer = (*env)->FindClass(env, "dim/Client$ReceiveSynchronizer");
256 ReceiveSynchronizer_new = (*env)->GetMethodID (env, ReceiveSynchronizer, "<init>", "(Ldim/DataDecoder;)V");
257 ReceiveSynchronizer_decodeNativeData = (*env)->GetMethodID (env, ReceiveSynchronizer, "decodeData", "(Ldim/Memory;)V");
258 ReceiveSynchronizer_getCompletionCode = (*env)->GetMethodID (env, ReceiveSynchronizer, "getCompletionCode","(I)I" );
259 ReceiveSynchronizer = (*env)->NewGlobalRef(env, ReceiveSynchronizer);
260 if ((*env)->ExceptionOccurred(env)) {bugs++; (*env)->ExceptionDescribe(env);}
261
262 CompletionHandler = (*env)->FindClass(env, "dim/CompletionHandler");
263 CompletionHandler_setCompletionCode = (*env)->GetMethodID(env, CompletionHandler, "setCompletionCode", "(I)I");
264 CompletionHandler = (*env)->NewGlobalRef(env, CompletionHandler);
265 if ((*env)->ExceptionOccurred(env)) {bugs++; (*env)->ExceptionDescribe(env);}
266
267 NativeDataDecoder = (*env)->FindClass(env, "dim/DataDecoder");
268 NativeDataDecoder_decodeNativeData = (*env)->GetMethodID(env, NativeDataDecoder, "decodeData", "(Ldim/Memory;)V");
269 NativeDataDecoder = (*env)->NewGlobalRef(env, NativeDataDecoder);
270 if ((*env)->ExceptionOccurred(env)) {bugs++; (*env)->ExceptionDescribe(env);}
271
272 NativeDataEncoder = (*env)->FindClass(env, "dim/DataEncoder");
273 NativeDataEncoder_encodeNativeData = (*env)->GetMethodID(env, NativeDataEncoder, "encodeData", "()Ldim/Memory;");
274 NativeDataEncoder = (*env)->NewGlobalRef(env, NativeDataEncoder);
275 if ((*env)->ExceptionOccurred(env)) {bugs++; (*env)->ExceptionDescribe(env);}
276
277 NativeDimTimer = (*env)->FindClass(env, "dim/DimTimer");
278 NativeDimTimer_timerHandler = (*env)->GetMethodID (env, NativeDimTimer, "timerHandler", "()V");
279 NativeDimTimer = (*env)->NewGlobalRef(env, NativeDimTimer);
280 if ((*env)->ExceptionOccurred(env)) {bugs++; (*env)->ExceptionDescribe(env);}
281
282 NativeDimSrvError = (*env)->FindClass(env, "dim/DimErrorHandler$DimSrvError");
283 NativeDimSrvError_new = (*env)->GetMethodID (env, NativeDimSrvError, "<init>", "()V");
284 NativeDimSrvError_errorHandler = (*env)->GetMethodID (env, NativeDimSrvError, "errorHandler", "(IILjava/lang/String;)V");
285 NativeDimSrvError = (*env)->NewGlobalRef(env, NativeDimSrvError);
286 ourNativeDimSrvError = (*env)->NewObject(env, NativeDimSrvError, NativeDimSrvError_new);
287 ourNativeDimSrvError = (*env)->NewGlobalRef(env, ourNativeDimSrvError);
288 if ((*env)->ExceptionOccurred(env)) {bugs++; (*env)->ExceptionDescribe(env);}
289
290 NativeDimCltError = (*env)->FindClass(env, "dim/DimErrorHandler$DimCltError");
291 NativeDimCltError_new = (*env)->GetMethodID (env, NativeDimCltError, "<init>", "()V");
292 NativeDimCltError_errorHandler = (*env)->GetMethodID (env, NativeDimCltError, "errorHandler", "(IILjava/lang/String;)V");
293 NativeDimCltError = (*env)->NewGlobalRef(env, NativeDimCltError);
294 ourNativeDimCltError = (*env)->NewObject(env, NativeDimCltError, NativeDimCltError_new);
295 ourNativeDimCltError = (*env)->NewGlobalRef(env, ourNativeDimCltError);
296 if ((*env)->ExceptionOccurred(env)) {bugs++; (*env)->ExceptionDescribe(env);}
297
298 NativeDimExit = (*env)->FindClass(env, "dim/DimExitHandler$DimExit");
299 NativeDimExit_new = (*env)->GetMethodID (env, NativeDimExit, "<init>", "()V");
300 NativeDimExit_exitHandler = (*env)->GetMethodID (env, NativeDimExit, "exitHandler", "(I)V");
301 NativeDimExit = (*env)->NewGlobalRef(env, NativeDimExit);
302 ourNativeDimExit = (*env)->NewObject(env, NativeDimExit, NativeDimExit_new);
303 ourNativeDimExit = (*env)->NewGlobalRef(env, ourNativeDimExit);
304 if ((*env)->ExceptionOccurred(env)) {bugs++; (*env)->ExceptionDescribe(env);}
305
306 DBGx(dim_Dbg_MODULE) printf("DimJNI: loaded DLL with dim version %d and JNI %d.%d\n", dim_JNI_version,JNI_VERSION>>16,JNI_VERSION&0xFFFF);
307
308 return(JNI_VERSION);
309}
310
311JNIEXPORT jint JNICALL
312JNI_OnUnLoad(JNIEnv* env, void* reserved)
313{
314// static JNIEnv* env;
315
316// DBGe(dim_Dbg_MODULE) ; /* trap only, report on exit */
317
318 if(reserved){}
319// (*jvm)->AttachCurrentThread(jvm, (void *)&env, NULL);
320 (*env)->DeleteGlobalRef(env, NativeDataMemory);
321 (*env)->DeleteGlobalRef(env, SendSynchronizer);
322 (*env)->DeleteGlobalRef(env, ReceiveSynchronizer);
323 (*env)->DeleteGlobalRef(env, CompletionHandler);
324 (*env)->DeleteGlobalRef(env, NativeDataDecoder);
325 (*env)->DeleteGlobalRef(env, NativeDataEncoder);
326 (*env)->DeleteGlobalRef(env, ourNativeMemoryObject);
327
328 (*env)->DeleteGlobalRef(env, NativeDimTimer);
329 (*env)->DeleteGlobalRef(env, NativeDimSrvError);
330 (*env)->DeleteGlobalRef(env, ourNativeDimSrvError);
331 (*env)->DeleteGlobalRef(env, NativeDimCltError);
332 (*env)->DeleteGlobalRef(env, ourNativeDimCltError);
333 (*env)->DeleteGlobalRef(env, NativeDimExit);
334 (*env)->DeleteGlobalRef(env, ourNativeDimExit);
335
336 DBGx(dim_Dbg_MODULE) printf("DimJNI: DLL unloaded\n");
337 return(0);
338}
339
340
341
342
343/* implementation of dim_native.h =============================================================== */
344
345/*
346 * Class: dim_Native
347 * Method: init
348 * Signature: ()I
349 */
350JNIEXPORT jint JNICALL Java_dim_Native_init
351 (JNIEnv* env, jclass nativeClass)
352{
353 JavaVM* jvm;
354
355 if(nativeClass){}
356 if(theJavaVM!=NULL) return JNI_VERSION;
357 (*env)->GetJavaVM(env, &jvm);
358 return JNI_OnLoad(jvm, 0);
359}
360
361/*
362 * Class: dim_Native
363 * Method: stop
364 * Signature: ()I
365 */
366JNIEXPORT jint JNICALL Java_dim_Native_stop
367 (JNIEnv* env, jclass nativeClass)
368{
369
370 if(nativeClass){}
371 return JNI_OnUnLoad(env, 0);
372}
373
374
375/* implementation of dim_dbg.h ================================================================== */
376
377/*
378 * Class: dim_Dbg
379 * Method: setMask
380 * Signature: (I)V
381 */
382JNIEXPORT void JNICALL Java_dim_Dbg_setMask
383 (JNIEnv *env, jclass nativeClass, jint dbg_mask)
384{
385 if(env){}
386 if(nativeClass){}
387 if(dim_Dbg_TRANSACTIONS & (DBG_mask|dbg_mask))
388 printf("DimJNI: debug mask changed from %08x to %08x\n", DBG_mask, dbg_mask);
389 DBG_mask = dbg_mask;
390}
391
392/*
393 * Class: dim_Dbg
394 * Method: getMask
395 * Signature: ()I
396 */
397JNIEXPORT jint JNICALL Java_dim_Dbg_getMask
398 (JNIEnv *env, jclass nativeClass)
399{
400 if(env){}
401 if(nativeClass){}
402 return DBG_mask;
403}
404
405
406
407
408/* implementation of dim_client.h =============================================================== */
409
410
411/* the send callback */
412
413void send_callback(jobject* _theCompletionHandler, int* _status)
414{
415 jobject theCompletionHandler = *_theCompletionHandler;
416 JNIEnv* env;
417 int doit;
418
419 DBGe(dim_Dbg_SEND_CALLBACK) printf("DimJNI: client SEND_CALLBACK status %08lx:%d\n", (dim_long)_status, *_status);
420
421 doit = dim_jni_attachThread(&env);
422// (*theJavaVM)->AttachCurrentThread(theJavaVM, (void *)&env, NULL);
423
424 (*env)->CallIntMethod(env, theCompletionHandler, CompletionHandler_setCompletionCode, *_status);
425 (*env)->DeleteGlobalRef(env, theCompletionHandler);
426
427 if(doit)
428 (*theJavaVM)->DetachCurrentThread(theJavaVM);
429 return;
430}
431
432/* for debuging messages only, so I do not care about reentrance (which could potentially happen) */
433static char* send_data_format;
434
435/* general send service */
436jint send_data
437 (JNIEnv *env, jstring name, jobject theCompletionHandler, jint mode, jint timeout, void* data_address, int data_size)
438{
439 jint ret;
440 int stamped = 0;
441 void (*callback_funct)();
442 jobject callback_param;
443 jobject theSendSynchronizer;
444
445 extern int request_command(char *, void *, int , void (*)(), dim_long, int);
446
447 const char* cmnd = (*env)->GetStringUTFChars(env, name, 0);
448
449// DBGe(dim_Dbg_SEND_NATIVE) ; /* trap only, report later */
450
451 if(timeout){}
452 if(mode & dim_Native_F_STAMPED) stamped = 1;
453 if(mode & dim_Native_F_WAIT) // note: dim_Native_F_WAIT defined as -2147483648L //(0x80000000)
454 {
455 // Create a SendSynchronizer object using theCompletionHandler
456 theSendSynchronizer = (*env)->NewObject(env, SendSynchronizer, SendSynchronizer_new, theCompletionHandler);
457 callback_param = (*env)->NewGlobalRef(env, theSendSynchronizer);
458 callback_funct = &send_callback;
459 }
460 else if(theCompletionHandler)
461 {
462 // create a global reference of the CompletionHandler if present
463 callback_param = (*env)->NewGlobalRef(env, theCompletionHandler);
464 callback_funct = &send_callback;
465 }
466 else
467 {
468 callback_param = 0;
469 callback_funct = 0;
470 }
471
472 // Send the request
473 ret = request_command((char *)cmnd, data_address, data_size, callback_funct, (dim_long)callback_param, stamped);
474 DBGx(dim_Dbg_SEND_NATIVE) printf("DimJNI: Client.Send(%s,(%s) 0x%x) returns %d \n", cmnd, send_data_format, * (int*) data_address, ret);
475
476 // release the String
477 (*env)->ReleaseStringUTFChars(env, name, cmnd);
478
479//if the send request was not queued and there is a CompletionHandler, we call the send_callback
480 if(!ret && callback_param)
481 {
482 DBGm(dim_Dbg_SEND_NATIVE) printf("DimJNI: Native.send calls callback as bug fix.\n");
483// TODO we do not have to call send_callback(&callback_param, &ret); when not queued? //Apparently we do not need this.
484 }
485
486 if(mode & dim_Native_F_WAIT)
487 {
488 ret=(*env)->CallIntMethod(env, theSendSynchronizer, SendSynchronizer_getCompletionCode, 0);
489 DBGx(dim_Dbg_SEND_NATIVE) printf("DimJNI: SEND returns (after wait) %08x\n",ret);
490 }
491
492 return ret;
493}
494
495
496
497/*
498 * Class: dim_Client
499 * Method: send
500 * Signature: (Ljava/lang/String;Ldim/CompletionHandler;IIZ)I
501 */
502JNIEXPORT jint JNICALL Java_dim_Client_send__Ljava_lang_String_2Ldim_CompletionHandler_2IIZ
503 (JNIEnv *env, jclass This, jstring name, jobject theCompletionHandler, jint mode, jint timeout, jboolean data)
504{
505 DBG(dim_Dbg_SEND_NATIVE) send_data_format = "jboolean";
506 if(This){}
507 return send_data(env, name, theCompletionHandler, mode, timeout, &data, sizeof(data));
508}
509
510
511/*
512 * Class: dim_Client
513 * Method: send
514 * Signature: (Ljava/lang/String;Ldim/CompletionHandler;IIC)I
515 */
516JNIEXPORT jint JNICALL Java_dim_Client_send__Ljava_lang_String_2Ldim_CompletionHandler_2IIC
517 (JNIEnv *env, jclass This, jstring name, jobject theCompletionHandler, jint mode, jint timeout, jchar data)
518{
519 DBG(dim_Dbg_SEND_NATIVE) send_data_format = "jchar";
520 if(This){}
521 return send_data(env, name, theCompletionHandler, mode, timeout, &data, sizeof(data));
522}
523
524
525/*
526 * Class: dim_Client
527 * Method: send
528 * Signature: (Ljava/lang/String;Ldim/CompletionHandler;IIB)I
529 */
530JNIEXPORT jint JNICALL Java_dim_Client_send__Ljava_lang_String_2Ldim_CompletionHandler_2IIB
531 (JNIEnv *env, jclass This, jstring name, jobject theCompletionHandler, jint mode, jint timeout, jbyte data)
532{
533 DBG(dim_Dbg_SEND_NATIVE) send_data_format = "jbyte";
534 if(This){}
535 return send_data(env, name, theCompletionHandler, mode, timeout, &data, sizeof(data));
536}
537
538
539/*
540 * Class: dim_Client
541 * Method: send
542 * Signature: (Ljava/lang/String;Ldim/CompletionHandler;IIS)I
543 */
544JNIEXPORT jint JNICALL Java_dim_Client_send__Ljava_lang_String_2Ldim_CompletionHandler_2IIS
545 (JNIEnv *env, jclass This, jstring name, jobject theCompletionHandler, jint mode, jint timeout, jshort data)
546{
547 DBG(dim_Dbg_SEND_NATIVE) send_data_format = "jshort";
548 if(This){}
549 return send_data(env, name, theCompletionHandler, mode, timeout, &data, sizeof(data));
550}
551
552
553/*
554 * Class: dim_Client
555 * Method: send
556 * Signature: (Ljava/lang/String;Ldim/CompletionHandler;III)I
557 */
558JNIEXPORT jint JNICALL Java_dim_Client_send__Ljava_lang_String_2Ldim_CompletionHandler_2III
559 (JNIEnv *env, jclass This, jstring name, jobject theCompletionHandler, jint mode, jint timeout, jint data)
560{
561 DBG(dim_Dbg_SEND_NATIVE) send_data_format = "jint";
562 if(This){}
563 return send_data(env, name, theCompletionHandler, mode, timeout, &data, sizeof(data));
564}
565
566
567/*
568 * Class: dim_Client
569 * Method: send
570 * Signature: (Ljava/lang/String;Ldim/CompletionHandler;IIJ)I
571 */
572JNIEXPORT jint JNICALL Java_dim_Client_send__Ljava_lang_String_2Ldim_CompletionHandler_2IIJ
573 (JNIEnv *env, jclass This, jstring name, jobject theCompletionHandler, jint mode, jint timeout, jlong data)
574{
575 DBG(dim_Dbg_SEND_NATIVE) send_data_format = "jlong";
576 if(This){}
577 return send_data(env, name, theCompletionHandler, mode, timeout, &data, sizeof(data));
578}
579
580
581/*
582 * Class: dim_Client
583 * Method: send
584 * Signature: (Ljava/lang/String;Ldim/CompletionHandler;IIF)I
585 */
586JNIEXPORT jint JNICALL Java_dim_Client_send__Ljava_lang_String_2Ldim_CompletionHandler_2IIF
587 (JNIEnv *env, jclass This, jstring name, jobject theCompletionHandler, jint mode, jint timeout, jfloat data)
588{
589 DBG(dim_Dbg_SEND_NATIVE) send_data_format = "jfloat";
590 if(This){}
591 return send_data(env, name, theCompletionHandler, mode, timeout, &data, sizeof(data));
592}
593
594
595/*
596 * Class: dim_Client
597 * Method: send
598 * Signature: (Ljava/lang/String;Ldim/CompletionHandler;IID)I
599 */
600JNIEXPORT jint JNICALL Java_dim_Client_send__Ljava_lang_String_2Ldim_CompletionHandler_2IID
601 (JNIEnv *env, jclass This, jstring name, jobject theCompletionHandler, jint mode, jint timeout, jdouble data)
602{
603 DBG(dim_Dbg_SEND_NATIVE) send_data_format = "jdouble";
604 if(This){}
605 return send_data(env, name, theCompletionHandler, mode, timeout, &data, sizeof(data));
606}
607
608
609/*
610 * Class: dim_Client
611 * Method: send
612 * Signature: (Ljava/lang/String;Ldim/CompletionHandler;IILjava/lang/String;)I
613 */
614JNIEXPORT jint JNICALL Java_dim_Client_send__Ljava_lang_String_2Ldim_CompletionHandler_2IILjava_lang_String_2
615 (JNIEnv *env, jclass This, jstring name, jobject theCompletionHandler, jint mode, jint timeout, jstring sdata)
616{
617 jint ret;
618 const char* data = (*env)->GetStringUTFChars(env, sdata, 0);
619
620 DBG(dim_Dbg_SEND_NATIVE) send_data_format = "String";
621 if(This){}
622 ret = send_data(env, name, theCompletionHandler, mode, timeout, (void*) data, (int)strlen(data)+1);
623
624 (*env)->ReleaseStringUTFChars(env,sdata, data);
625 return ret;
626}
627
628
629/*
630 * Class: dim_Client
631 * Method: send
632 * Signature: (Ljava/lang/String;Ldim/CompletionHandler;II[Z)I
633 */
634JNIEXPORT jint JNICALL Java_dim_Client_send__Ljava_lang_String_2Ldim_CompletionHandler_2II_3Z
635 (JNIEnv *env, jclass This, jstring name, jobject theCompletionHandler, jint mode, jint timeout, jbooleanArray dataArray)
636{
637 jboolean* nativeDataArray;
638 jint length;
639 jint ret;
640
641 nativeDataArray = (*env)->GetBooleanArrayElements(env,dataArray,0);
642 length = (jint)(*env)->GetArrayLength(env,dataArray) * (jint)sizeof(*nativeDataArray);
643
644 DBG(dim_Dbg_SEND_NATIVE) send_data_format = "boolean[]";
645 if(This){}
646 ret = send_data(env, name, theCompletionHandler, mode, timeout, nativeDataArray, length);
647
648 (*env)->ReleaseBooleanArrayElements(env,dataArray,nativeDataArray,JNI_ABORT);
649 return ret;
650}
651
652
653/*
654 * Class: dim_Client
655 * Method: send
656 * Signature: (Ljava/lang/String;Ldim/CompletionHandler;II[C)I
657 */
658JNIEXPORT jint JNICALL Java_dim_Client_send__Ljava_lang_String_2Ldim_CompletionHandler_2II_3C
659 (JNIEnv *env, jclass This, jstring name, jobject theCompletionHandler, jint mode, jint timeout, jcharArray dataArray)
660{
661 jchar* nativeDataArray;
662 jint length;
663 jint ret;
664
665 nativeDataArray = (*env)->GetCharArrayElements(env,dataArray,0);
666 length = (jint)(*env)->GetArrayLength(env,dataArray) * (jint)sizeof(*nativeDataArray);
667
668 DBG(dim_Dbg_SEND_NATIVE) send_data_format = "jchar[]";
669 if(This){}
670 ret = send_data(env, name, theCompletionHandler, mode, timeout, nativeDataArray, length);
671
672 (*env)->ReleaseCharArrayElements(env,dataArray,nativeDataArray,JNI_ABORT);
673 return ret;
674}
675
676
677/*
678 * Class: dim_Client
679 * Method: send
680 * Signature: (Ljava/lang/String;Ldim/CompletionHandler;II[B)I
681 */
682JNIEXPORT jint JNICALL Java_dim_Client_send__Ljava_lang_String_2Ldim_CompletionHandler_2II_3B
683 (JNIEnv *env, jclass This, jstring name, jobject theCompletionHandler, jint mode, jint timeout, jbyteArray dataArray)
684{
685 jbyte* nativeDataArray;
686 jint length;
687 jint ret;
688
689 nativeDataArray = (*env)->GetByteArrayElements(env,dataArray,0);
690 length = (jint)(*env)->GetArrayLength(env,dataArray) * (jint)sizeof(*nativeDataArray);
691
692 DBG(dim_Dbg_SEND_NATIVE) send_data_format = "jbyte[]";
693 if(This){}
694 ret = send_data(env, name, theCompletionHandler, mode, timeout, nativeDataArray, length);
695
696 (*env)->ReleaseByteArrayElements(env,dataArray,nativeDataArray,JNI_ABORT);
697 return ret;
698}
699
700
701/*
702 * Class: dim_Client
703 * Method: send
704 * Signature: (Ljava/lang/String;Ldim/CompletionHandler;II[S)I
705 */
706JNIEXPORT jint JNICALL Java_dim_Client_send__Ljava_lang_String_2Ldim_CompletionHandler_2II_3S
707 (JNIEnv *env, jclass This, jstring name, jobject theCompletionHandler, jint mode, jint timeout, jshortArray dataArray)
708{
709 jshort* nativeDataArray;
710 jint length;
711 jint ret;
712
713 nativeDataArray = (*env)->GetShortArrayElements(env,dataArray,0);
714 length = (jint)(*env)->GetArrayLength(env,dataArray) * (jint)sizeof(*nativeDataArray);
715
716 DBG(dim_Dbg_SEND_NATIVE) send_data_format = "jshort[]";
717 if(This){}
718 ret = send_data(env, name, theCompletionHandler, mode, timeout, nativeDataArray, length);
719
720 (*env)->ReleaseShortArrayElements(env,dataArray,nativeDataArray,JNI_ABORT);
721 return ret;
722}
723
724
725/*
726 * Class: dim_Client
727 * Method: send
728 * Signature: (Ljava/lang/String;Ldim/CompletionHandler;II[I)I
729 */
730JNIEXPORT jint JNICALL Java_dim_Client_send__Ljava_lang_String_2Ldim_CompletionHandler_2II_3I
731 (JNIEnv *env, jclass This, jstring name, jobject theCompletionHandler, jint mode, jint timeout, jintArray dataArray)
732{
733 jint* nativeDataArray;
734 jint length;
735 jint ret;
736
737 nativeDataArray = (*env)->GetIntArrayElements(env,dataArray,0);
738 length = (jint)(*env)->GetArrayLength(env,dataArray) * (jint)sizeof(*nativeDataArray);
739
740 DBG(dim_Dbg_SEND_NATIVE) send_data_format = "jint[]";
741 if(This){}
742 ret = send_data(env, name, theCompletionHandler, mode, timeout, nativeDataArray, length);
743
744 (*env)->ReleaseIntArrayElements(env,dataArray,nativeDataArray,JNI_ABORT);
745 return ret;
746}
747
748
749/*
750 * Class: dim_Client
751 * Method: send
752 * Signature: (Ljava/lang/String;Ldim/CompletionHandler;II[J)I
753 */
754JNIEXPORT jint JNICALL Java_dim_Client_send__Ljava_lang_String_2Ldim_CompletionHandler_2II_3J
755 (JNIEnv *env, jclass This, jstring name, jobject theCompletionHandler, jint mode, jint timeout, jlongArray dataArray)
756{
757 jlong* nativeDataArray;
758 jint length;
759 jint ret;
760
761 nativeDataArray = (*env)->GetLongArrayElements(env,dataArray,0);
762 length = (jint)(*env)->GetArrayLength(env,dataArray) * (jint)sizeof(*nativeDataArray);
763
764 DBG(dim_Dbg_SEND_NATIVE) send_data_format = "jlong[]";
765 if(This){}
766 ret = send_data(env, name, theCompletionHandler, mode, timeout, nativeDataArray, length);
767
768 (*env)->ReleaseLongArrayElements(env,dataArray,nativeDataArray,JNI_ABORT);
769 return ret;
770}
771
772
773/*
774 * Class: dim_Client
775 * Method: send
776 * Signature: (Ljava/lang/String;Ldim/CompletionHandler;II[F)I
777 */
778JNIEXPORT jint JNICALL Java_dim_Client_send__Ljava_lang_String_2Ldim_CompletionHandler_2II_3F
779 (JNIEnv *env, jclass This, jstring name, jobject theCompletionHandler, jint mode, jint timeout, jfloatArray dataArray)
780{
781 jfloat* nativeDataArray;
782 jint length;
783 jint ret;
784
785 nativeDataArray = (*env)->GetFloatArrayElements(env,dataArray,0);
786 length = (jint)(*env)->GetArrayLength(env,dataArray) * (jint)sizeof(*nativeDataArray);
787
788 DBG(dim_Dbg_SEND_NATIVE) send_data_format = "jfloat[]";
789 if(This){}
790 ret = send_data(env, name, theCompletionHandler, mode, timeout, nativeDataArray, length);
791
792 (*env)->ReleaseFloatArrayElements(env,dataArray,nativeDataArray,JNI_ABORT);
793 return ret;
794}
795
796
797/*
798 * Class: dim_Client
799 * Method: send
800 * Signature: (Ljava/lang/String;Ldim/CompletionHandler;II[D)I
801 */
802JNIEXPORT jint JNICALL Java_dim_Client_send__Ljava_lang_String_2Ldim_CompletionHandler_2II_3D
803 (JNIEnv *env, jclass This, jstring name, jobject theCompletionHandler, jint mode, jint timeout, jdoubleArray dataArray)
804{
805 jdouble* nativeDataArray;
806 jint length;
807 jint ret;
808
809 nativeDataArray = (*env)->GetDoubleArrayElements(env,dataArray,0);
810 length = (jint)(*env)->GetArrayLength(env,dataArray) * (jint)sizeof(*nativeDataArray);
811
812 DBG(dim_Dbg_SEND_NATIVE) send_data_format = "jdouble[]";
813 if(This){}
814 ret = send_data(env, name, theCompletionHandler, mode, timeout, nativeDataArray, length);
815
816 (*env)->ReleaseDoubleArrayElements(env,dataArray,nativeDataArray,JNI_ABORT);
817 return ret;
818}
819
820
821/*
822 * Class: dim_Client
823 * Method: send
824 * Signature: (Ljava/lang/String;Ldim/CompletionHandler;IIJI)I
825 */
826JNIEXPORT jint JNICALL Java_dim_Client_send__Ljava_lang_String_2Ldim_CompletionHandler_2IIJI
827 (JNIEnv *env, jclass This, jstring name, jobject theCompletionHandler, jint mode, jint timeout, jlong nativeDataBlock, jint nativeDataSize)
828{
829
830 DBG(dim_Dbg_SEND_NATIVE) send_data_format = "nativeDataBlock";
831 if(This){}
832 return send_data(env, name, theCompletionHandler, mode, timeout, (void*) nativeDataBlock, nativeDataSize);
833}
834
835
836
837/* This function is called by callback there is new data to be decoded */
838void decodeData(jobject* _theDataDecoder, void* dataAddress, int* _dataSize, int cleanup)
839{
840 jobject theDataDecoder = *_theDataDecoder;
841 int dataSize = *_dataSize;
842 JNIEnv* env;
843 int doit;
844
845 doit = dim_jni_attachThread(&env);
846// (*theJavaVM)->AttachCurrentThread(theJavaVM, (void *)&env, NULL);
847 if(dataAddress == NULL)
848 {
849 (*env)->CallVoidMethod(env, theDataDecoder, NativeDataDecoder_decodeNativeData, NULL);
850 }
851 else
852 {
853 /* the decode method will further complete the Memory object and call the decodeData method of the DataDecoder object */
854 (*env)->CallVoidMethod(env, ourNativeMemoryObject, NativeDataMemory_decodeData, (jlong) dataAddress, (jint) dataSize, theDataDecoder);
855 }
856
857 /* and cleanup */
858 if(cleanup) (*env)->DeleteGlobalRef(env, theDataDecoder);
859 if(doit)
860 (*theJavaVM)->DetachCurrentThread(theJavaVM);
861
862 return;
863}
864
865
866
867/* This call back is called when there is new data for a client subscription */
868void info_service_callback(jobject* _theDataDecoder, void* dataAddress, int* _dataSize)
869{
870 DBGe(dim_Dbg_INFO_CALLBACK) printf("DimJNI: INFO_CALLBACK(data: %08lx(%08x))\n", (dim_long) dataAddress, *_dataSize);
871
872 decodeData(_theDataDecoder, dataAddress, _dataSize, 0);
873}
874
875
876/* This call back is called when a client once_only subscription completes (so we clean up) */
877void info_service_callback_with_cleanup(jobject* _theDataDecoder, void* dataAddress, int* _dataSize)
878{
879 DBGe(dim_Dbg_INFO_CALLBACK) printf("DimJNI: INFO_CALLBACK/ONCE_ONLY(data: %08lx(%08x))\n", (dim_long)dataAddress, *_dataSize);
880
881 decodeData(_theDataDecoder, dataAddress, _dataSize, 1);
882
883}
884
885/* This function is called by callback when timer fires */
886void callTimerHandler(jobject* _aDimTimer)
887{
888// jobject aDimTimer = *_aDimTimer;
889 JNIEnv* env;
890 int doit;
891
892 doit = dim_jni_attachThread(&env);
893// (*theJavaVM)->AttachCurrentThread(theJavaVM, (void *)&env, NULL);
894//printf("Got callback %08x\n", _aDimTimer);
895 (*env)->CallVoidMethod(env, (jobject)_aDimTimer, NativeDimTimer_timerHandler, NULL);
896 if(doit)
897 (*theJavaVM)->DetachCurrentThread(theJavaVM);
898
899 return;
900}
901
902/* This call back is called when the timer expires */
903void timer_callback(jobject* _aDimTimer)
904{
905 callTimerHandler(_aDimTimer);
906}
907
908/* This function is called by callback when Server Error detected */
909void callServerErrorHandler(int severity, int code, char *msg)
910{
911 JNIEnv* env;
912 int doit;
913
914 doit = dim_jni_attachThread(&env);
915// (*theJavaVM)->AttachCurrentThread(theJavaVM, (void *)&env, NULL);
916 (*env)->CallVoidMethod(env, ourNativeDimSrvError, NativeDimSrvError_errorHandler,
917 (jint)severity, (jint)code, (jstring)(*env)->NewStringUTF(env, msg));
918 if(doit)
919 (*theJavaVM)->DetachCurrentThread(theJavaVM);
920 return;
921}
922
923/* This callback is called when the server gets an error*/
924void server_error_callback(int severity, int code, char *msg)
925{
926 callServerErrorHandler(severity, code, msg);
927}
928
929/* This function is called by callback when Client Error detected */
930void callClientErrorHandler(int severity, int code, char *msg)
931{
932 JNIEnv* env;
933 int doit;
934
935 doit = dim_jni_attachThread(&env);
936// (*theJavaVM)->AttachCurrentThread(theJavaVM, (void *)&env, NULL);
937 (*env)->CallVoidMethod(env, ourNativeDimCltError, NativeDimCltError_errorHandler,
938 (jint)severity, (jint)code, (jstring)(*env)->NewStringUTF(env, msg));
939 if(doit)
940 (*theJavaVM)->DetachCurrentThread(theJavaVM);
941 return;
942}
943
944/* This callback is called when the client gets an error*/
945void client_error_callback(int severity, int code, char *msg)
946{
947 callClientErrorHandler(severity, code, msg);
948}
949
950void callServerExitHandler(int code)
951{
952// jobject aDimTimer = *_aDimTimer;
953 JNIEnv* env;
954 int doit;
955
956 doit = dim_jni_attachThread(&env);
957// (*theJavaVM)->AttachCurrentThread(theJavaVM, (void *)&env, NULL);
958 (*env)->CallVoidMethod(env, ourNativeDimExit, NativeDimExit_exitHandler,
959 (jint)code);
960 if(doit)
961 (*theJavaVM)->DetachCurrentThread(theJavaVM);
962 return;
963}
964
965/* This call back is called when the timer expires */
966void server_exit_callback(int *code)
967{
968 callServerExitHandler(*code);
969}
970
971/*
972 * Class: dim_Client
973 * Method: infoService
974 * Signature: (Ljava/lang/String;Ldim/DataDecoder;II)I
975 */
976JNIEXPORT jint JNICALL Java_dim_Client_infoService
977 (JNIEnv *env, jclass This, jstring name, jobject theNativeDataDecoder, jint mode, jint timeout)
978{
979 jint ret;
980 int no_link;
981 int stamped = 0;
982 int service_type = mode & 0x0FFF;
983 void (*callback_function)();
984 jobject callback_param;
985 jobject theReceiveSynchronizer;
986 const char* info = (*env)->GetStringUTFChars(env, name, 0);
987 extern unsigned request_service(char *, int, int , void *, int , void (*)(),
988 dim_long, void *, int, int);
989
990// DBGe(dim_Dbg_INFO_SERVICE); /* trap only, we report on exit */
991
992 if(This){}
993 if(mode & dim_Native_F_STAMPED) stamped = 1;
994 if(mode & dim_Native_F_WAIT)
995 {
996 // Create a ReceiveSynchronizer object using the dataDecoder
997 theReceiveSynchronizer = (*env)->NewObject(env, ReceiveSynchronizer, ReceiveSynchronizer_new, theNativeDataDecoder);
998 callback_param = (*env)->NewGlobalRef(env, theReceiveSynchronizer);
999 }
1000 else
1001 {
1002 callback_param = (*env)->NewGlobalRef(env, theNativeDataDecoder);
1003 }
1004
1005 if(service_type == dim_Native_ONCE_ONLY)
1006 callback_function = &info_service_callback_with_cleanup;
1007 else
1008 callback_function = &info_service_callback; //TODO who should do the cleanup?
1009
1010
1011 ret = (jint)request_service((char *)info, service_type, timeout, 0, 0, callback_function, (dim_long)callback_param, &no_link, 0, stamped);
1012 DBGx(dim_Dbg_INFO_SERVICE) printf("DimJNI: client infoService(%s, DataDecoder@0x%08lx, mode=%d, timeout=%d ) returns %d\n", info, (dim_long)theNativeDataDecoder, mode, timeout, ret);
1013 (*env)->ReleaseStringUTFChars(env, name, info);
1014
1015 if(mode & dim_Native_F_WAIT)
1016 {
1017 /* we synchronize by calling the getCompletionCode method of the ReceiveSynchronizer */
1018 (*env)->CallIntMethod(env, theReceiveSynchronizer, ReceiveSynchronizer_getCompletionCode, 0);
1019 DBGx(dim_Dbg_INFO_SERVICE) printf("DimJNI: infoService(%s) completed with %08x\n",info, ret);
1020 }
1021
1022 return ret;
1023}
1024
1025/*
1026 * Class: dim_DimTimer
1027 * Method: start
1028 * Signature: (Ldim/DimTimer;I)V
1029 */
1030JNIEXPORT jlong JNICALL Java_dim_DimTimer_start
1031 (JNIEnv *env, jclass This, jobject aDimTimer, jint secs)
1032{
1033 jobject callback_param;
1034 void (*callback_function)();
1035
1036 if(This){}
1037 callback_param = (*env)->NewGlobalRef(env, aDimTimer);
1038 callback_function = &timer_callback; //TODO who should do the cleanup?
1039
1040//printf("Starting timer %d, %08x %08x\n", secs, (long)callback_param, aDimTimer);
1041 dtq_start_timer(secs, callback_function, callback_param);
1042
1043 return (jlong)callback_param;
1044}
1045
1046/*
1047 * Class: dim_DimTimer
1048 * Method: stop
1049 * Signature: (Ldim/DimTimer)V
1050 */
1051JNIEXPORT void JNICALL Java_dim_DimTimer_stop
1052// (JNIEnv *env, jclass This, jobject aDimTimer)
1053 (JNIEnv *env, jclass This, jlong aDimTimer)
1054{
1055 jobject callback_param;
1056 int ret;
1057
1058 if(env){}
1059 if(This){}
1060// callback_param = (*env)->NewGlobalRef(env, aDimTimer);
1061 callback_param = (jobject) aDimTimer;
1062
1063//printf("Stopping timer %08x %08X\n", callback_param, aDimTimer);
1064 ret = dtq_stop_timer((dim_long)callback_param);
1065 //printf("ret = %d\n", ret);
1066
1067 return;
1068}
1069
1070
1071/*
1072 * Class: dim_Client
1073 * Method: releaseService
1074 * Signature: (I)V
1075 */
1076JNIEXPORT void JNICALL Java_dim_Client_releaseService
1077 (JNIEnv* env, jclass This, jint sid)
1078{
1079
1080 DIC_SERVICE *servp;
1081
1082// DBGe(dim_Dbg_INFO_SERVICE) ; /* Trap only, report later */
1083
1084 if(This){}
1085 servp = (DIC_SERVICE *)id_get_ptr(sid, SRC_DIC);
1086
1087/*
1088Hans Essel, 3.12.08
1089Without deleting the global reference, the Java GC could not free the object!
1090Any DimInfo object would stay forever. This is a memory leak.
1091*/
1092 if(servp != NULL)
1093 {
1094 // DBGx(dim_Dbg_INFO_SERVICE) printf("DimJNI: Client.releaseService(%d (%s))\n", sid, servp->serv_name);
1095 servp->user_routine = NULL; // make sure this is not called anymore
1096 (*env)->DeleteGlobalRef(env, (jobject) servp->tag);
1097 servp->tag = 0;
1098 }
1099
1100 dic_release_service(sid);
1101 return;
1102}
1103
1104/*
1105 * Class: dim_Client
1106 * Method: noPadding
1107 * Signature: ()V
1108 */
1109JNIEXPORT void JNICALL Java_dim_Client_noPadding
1110 (JNIEnv* env, jclass This)
1111{
1112
1113 if(env){}
1114 if(This){}
1115 dic_disable_padding();
1116 return;
1117}
1118
1119/*
1120 * Class: dim_Client
1121 * Method: getFormat
1122 * Signature: (I)Ljava/lang/String;
1123 */
1124JNIEXPORT jstring JNICALL Java_dim_Client_getFormat
1125 (JNIEnv* env, jclass This, jint sid)
1126{
1127
1128 if(This){}
1129 return (*env)->NewStringUTF(env, (char*)dic_get_format(sid));
1130}
1131
1132/*
1133 * Class: dim_Client
1134 * Method: stop
1135 * Signature: ()V
1136 */
1137JNIEXPORT void JNICALL Java_dim_Client_stop
1138 (JNIEnv* env, jclass This)
1139{
1140 extern void dim_stop();
1141
1142 if(env){}
1143 if(This){}
1144 dim_stop();
1145 return;
1146}
1147
1148/*
1149 * Class: dim_DimInfo
1150 * Method: getQuality
1151 * Signature: (I)I;
1152 */
1153JNIEXPORT jint JNICALL Java_dim_DimInfo_getQuality
1154 (JNIEnv* env, jclass This, jint sid)
1155{
1156 int ret;
1157
1158 if(env){}
1159 if(This){}
1160 ret = dic_get_quality(sid);
1161 return ret;
1162}
1163
1164/*
1165 * Class: dim_DimInfo
1166 * Method: getTimestamp
1167 * Signature: (I)I;
1168 */
1169JNIEXPORT jint JNICALL Java_dim_DimInfo_getTimestamp
1170 (JNIEnv* env, jclass This, jint sid)
1171{
1172 int mysecs, mymilli;
1173 if(env){}
1174 if(This){}
1175 dic_get_timestamp(sid, &mysecs, &mymilli);
1176 return mysecs;
1177}
1178
1179/*
1180 * Class: dim_DimInfo
1181 * Method: getTimestampMillisecs
1182 * Signature: (I)I;
1183 */
1184JNIEXPORT jint JNICALL Java_dim_DimInfo_getTimestampMillisecs
1185 (JNIEnv* env, jclass This, jint sid)
1186{
1187 int mysecs, mymilli;
1188 if(env){}
1189 if(This){}
1190 dic_get_timestamp(sid, &mysecs, &mymilli);
1191 return mymilli;
1192}
1193
1194/* implementation of dim_server.h =============================================================== */
1195
1196
1197/*
1198 * Class: dim_Server
1199 * Method: startServing
1200 * Signature: (Ljava/lang/String;)I
1201 */
1202JNIEXPORT jint JNICALL Java_dim_Server_startServing
1203 (JNIEnv* env, jclass This, jstring serverName)
1204{
1205 const char* serverNameUTF = (*env)->GetStringUTFChars(env, serverName, 0);
1206
1207 if(This){}
1208
1209 dis_start_serving(serverNameUTF);
1210
1211 DBGe(dim_Dbg_SERVER) printf("DimJNI: Start serving\n");
1212
1213 (*env)->ReleaseStringUTFChars(env, serverName, serverNameUTF);
1214 return 0;
1215}
1216
1217/*
1218 * Class: dim_Server
1219 * Method: stopServing
1220 * Signature: ()V
1221 */
1222JNIEXPORT void JNICALL Java_dim_Server_stopServing
1223 (JNIEnv* env, jclass This)
1224{
1225 DBGe(dim_Dbg_SERVER) printf("DimJNI: Stop serving\n");
1226 if(env){}
1227 if(This){}
1228 dis_stop_serving();
1229 return;
1230}
1231
1232void server_getInfo_callback(jobject* _dataEncoder, void* *address, int *size)
1233{
1234 /* server_getInfo_callback is invoked when dim needs the data for a service.
1235 * The data is obtained by calling the encodeData method of the DataEncoder
1236 * interface which returns us a Memory object. Next we extract the data address
1237 * and size from the returned Memory object.
1238 */
1239 /* Note, the DataEncoder can obtain the identity of the client by calling
1240 * getClient or getClientConnID */
1241
1242 /* thou shall not not use volatile storage to return info to dim */
1243 jobject dataEncoder = *_dataEncoder;
1244 jobject theMemory;
1245 JNIEnv* env;
1246 int doit;
1247
1248// DBGe(dim_Dbg_SERVICE_CALLBACK) ; /* no report, only trap */
1249
1250 doit = dim_jni_attachThread(&env);
1251// (*theJavaVM)->AttachCurrentThread(theJavaVM, (void *)&env, NULL);
1252
1253 theMemory = (*env)->CallObjectMethod(env, dataEncoder, NativeDataEncoder_encodeNativeData);
1254 if(theMemory == NULL)
1255 {
1256 *address = 0;
1257 *size = 0;
1258 }
1259 else
1260 {
1261 *address = (void*) (*env)->GetLongField(env, theMemory, NativeDataMemory_dataAddress);
1262 *size = (*env)->GetIntField(env, theMemory, NativeDataMemory_dataSize);
1263// printf("data address = %x, data size = %d\n",*address, *size);
1264 }
1265 DBGx(dim_Dbg_SERVICE_CALLBACK) printf("DimJNI: server_SERVICE_CALLBACK(dataEncoder=%08lx)\n ==> data: %08lx size %08x\n", (dim_long)dataEncoder, (dim_long) *address, *size);
1266
1267 if ((*env)->ExceptionOccurred(env)) (*env)->ExceptionDescribe(env); // clear any possible exception, if we do not do this, all further methods will fail!!
1268 if(doit)
1269 (*theJavaVM)->DetachCurrentThread(theJavaVM);
1270
1271 return;
1272}
1273
1274
1275/*
1276 * Class: dim_Server
1277 * Method: getClientConnID
1278 * Signature: ()I
1279 */
1280JNIEXPORT jint JNICALL Java_dim_Server_getClientConnID
1281 (JNIEnv* env, jclass This)
1282{
1283 DBGe(dim_Dbg_GETCLIENT) printf("DimJNI: Server.getClientConnID\n");
1284
1285 if(env){}
1286 if(This){}
1287 return dis_get_conn_id();
1288}
1289
1290/*
1291 * Class: dim_Server
1292 * Method: getClient
1293 * Signature: ()Ljava/lang/String;
1294 */
1295JNIEXPORT jstring JNICALL Java_dim_Server_getClient
1296 (JNIEnv* env, jclass This)
1297{
1298 char name[MAX_NODE_NAME+MAX_TASK_NAME+4];
1299
1300 DBGe(dim_Dbg_GETCLIENT) printf("DimJNI: Server.getClient\n");
1301
1302 if(This){}
1303 if(dis_get_client(name))
1304 return (*env)->NewStringUTF(env, name);
1305 else
1306 return NULL;
1307}
1308
1309/*
1310 * Class: dim_Server
1311 * Method: getServices
1312 * Signature: ()Ljava/lang/String;
1313 */
1314JNIEXPORT jstring JNICALL Java_dim_Server_getServices
1315 (JNIEnv* env, jclass This)
1316{
1317 int id;
1318
1319 DBGe(dim_Dbg_GETCLIENT) printf("DimJNI: Server.getClientServices\n");
1320
1321 if(This){}
1322 if( (id = dis_get_conn_id()) )
1323 return (*env)->NewStringUTF(env, dis_get_client_services(id));
1324 else
1325 return NULL;
1326}
1327
1328/*
1329 * Class: dim_Client
1330 * Method: getServerPID
1331 * Signature: ()I
1332 */
1333JNIEXPORT jint JNICALL Java_dim_Client_getServerPID
1334 (JNIEnv* env, jclass This)
1335{
1336 int pid, ret;
1337
1338 if(env){}
1339 if(This){}
1340 ret = dic_get_server_pid(&pid);
1341 if(!ret)
1342 return 0;
1343 return pid;
1344}
1345
1346/*
1347 * Class: dim_Client
1348 * Method: getServerConnID
1349 * Signature: ()I
1350 */
1351JNIEXPORT jint JNICALL Java_dim_Client_getServerConnID
1352 (JNIEnv* env, jclass This)
1353{
1354 if(env){}
1355 if(This){}
1356 return dic_get_conn_id();
1357}
1358
1359/*
1360 * Class: dim_Client
1361 * Method: getServer
1362 * Signature: ()Ljava/lang/String;
1363 */
1364JNIEXPORT jstring JNICALL Java_dim_Client_getServer
1365 (JNIEnv* env, jclass This)
1366{
1367 char name[MAX_NODE_NAME+MAX_TASK_NAME+4];
1368
1369 if(This){}
1370 if(dic_get_server(name))
1371 return (*env)->NewStringUTF(env, name);
1372 else
1373 return NULL;
1374}
1375
1376/*
1377 * Class: dim_Client
1378 * Method: getServices
1379 * Signature: ()Ljava/lang/String;
1380 */
1381JNIEXPORT jstring JNICALL Java_dim_Client_getServices
1382 (JNIEnv* env, jclass This)
1383{
1384 int id;
1385
1386 if(This){}
1387 if( (id = dic_get_conn_id()) )
1388 return (*env)->NewStringUTF(env, dic_get_server_services(id));
1389 else
1390 return NULL;
1391}
1392
1393/*
1394 * Class: dim_Server
1395 * Method: addService
1396 * Signature: (Ljava/lang/String;Ljava/lang/String;Ldim/DataEncoder;)I
1397 */
1398JNIEXPORT jint JNICALL Java_dim_Server_addService
1399 (JNIEnv* env, jclass This, jstring serviceName, jstring serviceType, jobject dataEncoder)
1400{
1401 const char* serviceNameUTF = (*env)->GetStringUTFChars(env, serviceName, 0);
1402 const char* serviceTypeUTF = (*env)->GetStringUTFChars(env, serviceType, 0);
1403 jint sid;
1404
1405// DBGe(dim_Dbg_ADD_SERVICE) ; /* no reporting, for trap only */
1406
1407 if(This){}
1408 dataEncoder = (*env)->NewGlobalRef(env, dataEncoder);
1409 sid = (jint)dis_add_service(serviceNameUTF, serviceTypeUTF, 0, 0, server_getInfo_callback, dataEncoder);
1410
1411 DBGx(dim_Dbg_ADD_SERVICE) printf("DimJNI: Server.addService(%s,%s, @%08lx)=%d\n",serviceNameUTF, serviceTypeUTF, (dim_long)dataEncoder, sid);
1412
1413 (*env)->ReleaseStringUTFChars(env, serviceName, serviceNameUTF);
1414 (*env)->ReleaseStringUTFChars(env, serviceType, serviceTypeUTF);
1415 return sid;
1416}
1417
1418
1419void server_cmnd_callback(jobject* _theDataDecoder, void* dataAddress, int* _dataSize)
1420{
1421
1422 DBGe(dim_Dbg_CMND_CALLBACK) printf("DimJNI: server CMND_CALLBACK(data: %08lx(%08x))\n", (dim_long) dataAddress, *_dataSize);
1423
1424 decodeData(_theDataDecoder, dataAddress, _dataSize, 0);
1425}
1426
1427
1428/*
1429 * Class: dim_Server
1430 * Method: addCommand
1431 * Signature: (Ljava/lang/String;Ljava/lang/String;Ldim/DataDecoder;)I
1432 */
1433JNIEXPORT jint JNICALL Java_dim_Server_addCommand
1434 (JNIEnv* env, jclass This, jstring serviceName, jstring serviceType, jobject dataDecoder)
1435{
1436 const char* serviceNameUTF = (*env)->GetStringUTFChars(env, serviceName, 0);
1437 const char* serviceTypeUTF = (*env)->GetStringUTFChars(env, serviceType, 0);
1438 jint sid;
1439
1440// DBGe(dim_Dbg_ADD_CMND) ; /* trap only, repot later */
1441 if(This){}
1442 dataDecoder = (*env)->NewGlobalRef(env, dataDecoder);
1443 sid = (jint)dis_add_cmnd(serviceNameUTF, serviceTypeUTF, server_cmnd_callback, dataDecoder);
1444
1445 DBGx(dim_Dbg_ADD_CMND) printf("DimJNI: Server.addCmnd(%s,%s, @%08lx) = %d\n",serviceNameUTF, serviceTypeUTF, (dim_long) dataDecoder, sid);
1446
1447 (*env)->ReleaseStringUTFChars(env, serviceName, serviceNameUTF);
1448 (*env)->ReleaseStringUTFChars(env, serviceType, serviceTypeUTF);
1449 return sid;
1450}
1451
1452
1453/*
1454 * Class: dim_Server
1455 * Method: selectiveUpdateService
1456 * Signature: (I[I)I
1457 */
1458JNIEXPORT jint JNICALL Java_dim_Server_selectiveUpdateService
1459 (JNIEnv* env, jclass This, jint sid, jintArray clients)
1460{
1461 jint* clientArray;
1462 extern void do_update_service(unsigned, int *);
1463
1464 if(This){}
1465 if(clients==NULL) clientArray = NULL;
1466 else clientArray = (*env)->GetIntArrayElements(env,clients,0);
1467
1468 DBGe(dim_Dbg_UPDATE_SERVICE) printf("DimJNI: Server.updateService %d\n", sid);
1469 do_update_service((unsigned int)sid, (int *)clientArray);
1470
1471 if(clientArray!=NULL) (*env)->ReleaseIntArrayElements(env,clients,clientArray,JNI_ABORT);
1472
1473
1474 return 0;
1475}
1476
1477/*
1478 * Class: dim_Server
1479 * Method: removeService
1480 * Signature: (I)I
1481 */
1482JNIEXPORT jint JNICALL Java_dim_Server_removeService
1483 (JNIEnv* env, jclass This, jint sid)
1484{
1485 DBGe(dim_Dbg_RELEASE_SERVICE) printf("DimJNI: Server.removedService %d\n", sid);
1486 if(env){}
1487 if(This){}
1488 dis_remove_service(sid);
1489 return 0;
1490}
1491
1492/*
1493 * Class: dim_Server
1494 * Method: noPadding
1495 * Signature: ()V
1496 */
1497JNIEXPORT void JNICALL Java_dim_Server_noPadding
1498 (JNIEnv* env, jclass This)
1499{
1500
1501 if(env){}
1502 if(This){}
1503 dis_disable_padding();
1504 return;
1505}
1506
1507/*
1508 * Class: dim_DimErrorHandler
1509 * Method: addSrvErrorHandler
1510 * Signature: ()V
1511 */
1512JNIEXPORT void JNICALL Java_dim_DimErrorHandler_addSrvErrorHandler
1513 (JNIEnv* env, jclass This)
1514{
1515
1516 void (*callback_function)();
1517
1518 if(env){}
1519 if(This){}
1520 callback_function = &server_error_callback;
1521
1522 dis_add_error_handler( callback_function );
1523
1524 return;
1525}
1526
1527/*
1528 * Class: dim_DimErrorHandler
1529 * Method: addCltErrorHandler
1530 * Signature: ()V
1531 */
1532JNIEXPORT void JNICALL Java_dim_DimErrorHandler_addCltErrorHandler
1533 (JNIEnv* env, jclass This)
1534{
1535
1536 void (*callback_function)();
1537
1538 if(env){}
1539 if(This){}
1540 callback_function = &client_error_callback;
1541
1542 dic_add_error_handler( callback_function );
1543
1544 return;
1545}
1546
1547/*
1548 * Class: dim_DimExitHandler
1549 * Method: addExitHandler
1550 * Signature: ()V
1551 */
1552JNIEXPORT void JNICALL Java_dim_DimExitHandler_addExitHandler
1553 (JNIEnv* env, jclass This)
1554{
1555
1556 void (*callback_function)();
1557
1558 if(env){}
1559 if(This){}
1560 callback_function = &server_exit_callback;
1561
1562 dis_add_exit_handler( callback_function );
1563
1564 return;
1565}
1566
1567/*
1568 * Class: dim_DimServer
1569 * Method: disableAST
1570 * Signature: ()V;
1571 */
1572JNIEXPORT void JNICALL Java_dim_DimServer_disableAST
1573 (JNIEnv* env, jclass This)
1574{
1575
1576 DIM_LOCK
1577 if(env){}
1578 if(This){}
1579 return;
1580}
1581
1582/*
1583 * Class: dim_DimServer
1584 * Method: enableAST
1585 * Signature: ()V;
1586 */
1587JNIEXPORT void JNICALL Java_dim_DimServer_enableAST
1588 (JNIEnv* env, jclass This)
1589{
1590
1591 if(env){}
1592 if(This){}
1593 DIM_UNLOCK
1594 return;
1595}
1596
1597/*
1598 * Class: dim_DimClient
1599 * Method: disableAST
1600 * Signature: ()V;
1601 */
1602JNIEXPORT void JNICALL Java_dim_DimClient_disableAST
1603 (JNIEnv* env, jclass This)
1604{
1605
1606 DIM_LOCK
1607 if(env){}
1608 if(This){}
1609 return;
1610}
1611
1612/*
1613 * Class: dim_DimClient
1614 * Method: enableAST
1615 * Signature: ()V;
1616 */
1617JNIEXPORT void JNICALL Java_dim_DimClient_enableAST
1618 (JNIEnv* env, jclass This)
1619{
1620
1621 if(env){}
1622 if(This){}
1623 DIM_UNLOCK
1624 return;
1625}
1626
1627/*
1628 * Class: dim_DimServer
1629 * Method: setDnsNode
1630 * Signature: (Ljava/lang/String)V;
1631 */
1632JNIEXPORT void JNICALL Java_dim_DimServer_setDnsNode
1633 (JNIEnv* env, jclass This, jstring nodes)
1634{
1635 const char* nodesUTF = (*env)->GetStringUTFChars(env, nodes, 0);
1636
1637 if(env){}
1638 if(This){}
1639 dis_set_dns_node(nodesUTF);
1640 return;
1641}
1642
1643/*
1644 * Class: dim_DimServer
1645 * Method: setDnsPort
1646 * Signature: (I)V;
1647 */
1648JNIEXPORT void JNICALL Java_dim_DimServer_setDnsPort
1649 (JNIEnv* env, jclass This, jint port)
1650{
1651 if(env){}
1652 if(This){}
1653 dis_set_dns_port(port);
1654 return;
1655}
1656
1657/*
1658 * Class: dim_DimClient
1659 * Method: setDnsNode
1660 * Signature: (Ljava/lang/String)V;
1661 */
1662JNIEXPORT void JNICALL Java_dim_DimClient_setDnsNode
1663 (JNIEnv* env, jclass This, jstring nodes)
1664{
1665 const char* nodesUTF = (*env)->GetStringUTFChars(env, nodes, 0);
1666
1667 if(env){}
1668 if(This){}
1669 dic_set_dns_node(nodesUTF);
1670 dic_close_dns();
1671 return;
1672}
1673
1674/*
1675 * Class: dim_DimClient
1676 * Method: setDnsPort
1677 * Signature: (I)V;
1678 */
1679JNIEXPORT void JNICALL Java_dim_DimClient_setDnsPort
1680 (JNIEnv* env, jclass This, jint port)
1681{
1682 if(env){}
1683 if(This){}
1684 dic_set_dns_port(port);
1685 return;
1686}
1687
1688/*
1689 * Class: dim_DimServer
1690 * Method: getDnsNode
1691 * Signature: ()Ljava/lang/String;
1692 */
1693JNIEXPORT jstring JNICALL Java_dim_DimServer_getDnsNode
1694 (JNIEnv* env, jclass This)
1695{
1696 char nodes[255];
1697
1698 if(This){}
1699 dis_get_dns_node(nodes);
1700 return (*env)->NewStringUTF(env, (char*)nodes);
1701}
1702
1703/*
1704 * Class: dim_DimClient
1705 * Method: getDnsNode
1706 * Signature: ()Ljava/lang/String;
1707 */
1708JNIEXPORT jstring JNICALL Java_dim_DimClient_getDnsNode
1709 (JNIEnv* env, jclass This)
1710{
1711 char nodes[255];
1712
1713 if(This){}
1714 dic_get_dns_node(nodes);
1715 return (*env)->NewStringUTF(env, (char*)nodes);
1716}
1717
1718/*
1719 * Class: dim_DimServer
1720 * Method: getDnsPort
1721 * Signature: ()I;
1722 */
1723JNIEXPORT jint JNICALL Java_dim_DimServer_getDnsPort
1724 (JNIEnv* env, jclass This)
1725{
1726
1727 if(env){}
1728 if(This){}
1729 return dis_get_dns_port();
1730}
1731
1732/*
1733 * Class: dim_DimClient
1734 * Method: getDnsPort
1735 * Signature: ()I;
1736 */
1737JNIEXPORT jint JNICALL Java_dim_DimClient_getDnsPort
1738 (JNIEnv* env, jclass This)
1739{
1740
1741 if(env){}
1742 if(This){}
1743 return dic_get_dns_port();
1744}
1745
1746/*
1747 * Class: dim_DimService
1748 * Method: setQuality
1749 * Signature: (II)V;
1750 */
1751JNIEXPORT void JNICALL Java_dim_DimService_setQuality
1752 (JNIEnv* env, jclass This, jint sid, jint qual)
1753{
1754
1755 if(env){}
1756 if(This){}
1757 dis_set_quality(sid, qual);
1758 return;
1759}
1760
1761
1762/*
1763 * Class: dim_DimService
1764 * Method: setTimestamp
1765 * Signature: (III)V;
1766 */
1767JNIEXPORT void JNICALL Java_dim_DimService_setTimestamp
1768 (JNIEnv* env, jclass This, jint sid, jint secs, jint millisecs)
1769{
1770
1771 if(env){}
1772 if(This){}
1773 dis_set_timestamp(sid, secs, millisecs);
1774 return;
1775}
1776
1777/* implementation of dim_memory,h =============================================================== */
1778
1779/*
1780 * Class: dim_Memory
1781 * Method: dumpInternalData
1782 * Signature: (III)V
1783 */
1784JNIEXPORT void JNICALL Java_dim_Memory_dumpInternalData
1785 (JNIEnv *env, jclass nativeClass, jlong internalDataAddress, jint internalDataSize, jint dumpOptions)
1786{
1787 {
1788 int* data = (int*) internalDataAddress;
1789 int leng = (int)internalDataSize/(int)sizeof(int);
1790 int i;
1791
1792 if(env){}
1793 if(dumpOptions){}
1794 if(nativeClass){}
1795 for (i=0;i<leng;i++)
1796 {
1797 if((i%8)==0) printf("%04x:",i);
1798 printf(" %08x", *(data++));
1799 if((i%8)==7) printf("\n");
1800 }
1801 if((leng%8)!=0) printf("\n");
1802 }
1803 return;
1804}
1805
1806
1807/*
1808 * Class: dim_Memory
1809 * Method: getBoolean
1810 * Signature: (I)Z
1811 */
1812JNIEXPORT jboolean JNICALL Java_dim_Memory_getBoolean
1813 (JNIEnv* env, jclass nativeClass, jlong nativeDataAddress)
1814{
1815 if(env){}
1816 if(nativeClass){}
1817 DBGe(dim_Dbg_MEMORY) printf("DimJNI: Memory.getBoolean\n");
1818 return *(jboolean*)nativeDataAddress;
1819}
1820
1821/*
1822 * Class: dim_Memory
1823 * Method: getChar
1824 * Signature: (I)C
1825 */
1826JNIEXPORT jchar JNICALL Java_dim_Memory_getChar
1827 (JNIEnv* env, jclass nativeClass, jlong nativeDataAddress)
1828{
1829 if(env){}
1830 if(nativeClass){}
1831 DBGe(dim_Dbg_MEMORY) printf("DimJNI: Memory.getChar\n");
1832 return *(jchar*)nativeDataAddress;
1833}
1834
1835/*
1836 * Class: dim_Memory
1837 * Method: getByte
1838 * Signature: (I)B
1839 */
1840JNIEXPORT jbyte JNICALL Java_dim_Memory_getByte
1841 (JNIEnv* env, jclass nativeClass, jlong nativeDataAddress)
1842{
1843 if(env){}
1844 if(nativeClass){}
1845 DBGe(dim_Dbg_MEMORY) printf("DimJNI: Memory.getByte\n");
1846 return *(jbyte*)nativeDataAddress;
1847}
1848
1849/*
1850 * Class: dim_Memory
1851 * Method: getShort
1852 * Signature: (I)S
1853 */
1854JNIEXPORT jshort JNICALL Java_dim_Memory_getShort
1855 (JNIEnv* env, jclass nativeClass, jlong nativeDataAddress)
1856{
1857 if(env){}
1858 if(nativeClass){}
1859 DBGe(dim_Dbg_MEMORY) printf("DimJNI: Memory.getShort\n");
1860 return *(jshort*)nativeDataAddress;
1861}
1862
1863/*
1864 * Class: dim_Memory
1865 * Method: getInt
1866 * Signature: (I)I
1867 */
1868JNIEXPORT jint JNICALL Java_dim_Memory_getInt
1869 (JNIEnv* env, jclass nativeClass, jlong nativeDataAddress)
1870{
1871 if(env){}
1872 if(nativeClass){}
1873 DBGe(dim_Dbg_MEMORY) printf("DimJNI: Memory.getInt\n");
1874 return *(jint*)nativeDataAddress;
1875}
1876
1877/*
1878 * Class: dim_Memory
1879 * Method: getLong
1880 * Signature: (I)J
1881 */
1882JNIEXPORT jlong JNICALL Java_dim_Memory_getLong
1883 (JNIEnv* env, jclass nativeClass, jlong nativeDataAddress)
1884{
1885 if(env){}
1886 if(nativeClass){}
1887 DBGe(dim_Dbg_MEMORY) printf("DimJNI: Memory.getLong\n");
1888 return *(jlong*)nativeDataAddress;
1889}
1890
1891/*
1892 * Class: dim_Memory
1893 * Method: getFloat
1894 * Signature: (I)F
1895 */
1896JNIEXPORT jfloat JNICALL Java_dim_Memory_getFloat
1897 (JNIEnv* env, jclass nativeClass, jlong nativeDataAddress)
1898{
1899 if(env){}
1900 if(nativeClass){}
1901 DBGe(dim_Dbg_MEMORY) printf("DimJNI: Memory.getFloat\n");
1902 return *(jfloat*)nativeDataAddress;
1903}
1904
1905/*
1906 * Class: dim_Memory
1907 * Method: getDouble
1908 * Signature: (I)D
1909 */
1910JNIEXPORT jdouble JNICALL Java_dim_Memory_getDouble
1911 (JNIEnv* env, jclass nativeClass, jlong nativeDataAddress)
1912{
1913 if(env){}
1914 if(nativeClass){}
1915 DBGe(dim_Dbg_MEMORY) printf("DimJNI: Memory.getDouble\n");
1916 return *(jdouble*)nativeDataAddress;
1917}
1918
1919
1920/*
1921 * Class: dim_Memory
1922 * Method: getString
1923 * Signature: (I,I)Ljava/lang/String;
1924 */
1925JNIEXPORT jstring JNICALL Java_dim_Memory_getString
1926 (JNIEnv* env, jclass nativeClass, jlong nativeDataAddress, jint maxSize)
1927{
1928 if(env){}
1929 if(nativeClass){}
1930 if(maxSize){}
1931 DBGe(dim_Dbg_MEMORY) printf("DimJNI: Memory.getString\n");
1932 return (*env)->NewStringUTF(env, (char*)nativeDataAddress);
1933}
1934
1935
1936/*
1937 * Class: dim_Memory
1938 * Method: copyIntoBooleanArray
1939 * Signature: (I[ZII)V
1940 */
1941JNIEXPORT void JNICALL Java_dim_Memory_copyIntoBooleanArray
1942 (JNIEnv* env, jclass nativeClass, jlong nativeDataAddress, jbooleanArray array, jint arrayOffset, jint length)
1943{
1944 if(nativeClass){}
1945 DBGe(dim_Dbg_MEMORY) printf("DimJNI: Memory.copyIntoBooleanArray\n");
1946 (*env)->SetBooleanArrayRegion(env, array, arrayOffset, length, (void*) nativeDataAddress);
1947 return ;
1948}
1949
1950/*
1951 * Class: dim_Memory
1952 * Method: copyIntoCharArray
1953 * Signature: (I[CII)V
1954 */
1955JNIEXPORT void JNICALL Java_dim_Memory_copyIntoCharArray
1956 (JNIEnv* env, jclass nativeClass, jlong nativeDataAddress, jcharArray array, jint arrayOffset, jint length)
1957{
1958 if(nativeClass){}
1959 DBGe(dim_Dbg_MEMORY) printf("DimJNI: Memory.copyIntoCharArray\n");
1960 (*env)->SetCharArrayRegion(env, array, arrayOffset, length, (void*) nativeDataAddress);
1961 return ;
1962}
1963
1964/*
1965 * Class: dim_Memory
1966 * Method: copyIntoByteArray
1967 * Signature: (I[BII)V
1968 */
1969JNIEXPORT void JNICALL Java_dim_Memory_copyIntoByteArray
1970 (JNIEnv* env, jclass nativeClass, jlong nativeDataAddress, jbyteArray array, jint arrayOffset, jint length)
1971{
1972 if(nativeClass){}
1973 DBGe(dim_Dbg_MEMORY) printf("DimJNI: Memory.copyIntoByteArray\n");
1974 (*env)->SetByteArrayRegion(env, array, arrayOffset, length, (void*) nativeDataAddress);
1975 return ;
1976}
1977
1978/*
1979 * Class: dim_Memory
1980 * Method: copyIntoShortArray
1981 * Signature: (I[SII)V
1982 */
1983JNIEXPORT void JNICALL Java_dim_Memory_copyIntoShortArray
1984 (JNIEnv* env, jclass nativeClass, jlong nativeDataAddress, jshortArray array, jint arrayOffset, jint length)
1985{
1986 if(nativeClass){}
1987 DBGe(dim_Dbg_MEMORY) printf("DimJNI: Memory.copyIntoShortArray\n");
1988 (*env)->SetShortArrayRegion(env, array, arrayOffset, length, (void*) nativeDataAddress);
1989 return ;
1990}
1991
1992/*
1993 * Class: dim_Memory
1994 * Method: copyIntoIntArray
1995 * Signature: (I[III)V
1996 */
1997JNIEXPORT void JNICALL Java_dim_Memory_copyIntoIntArray
1998 (JNIEnv* env, jclass nativeClass, jlong nativeDataAddress, jintArray array, jint arrayOffset, jint length)
1999{
2000 if(nativeClass){}
2001 DBGe(dim_Dbg_MEMORY) printf("DimJNI: Memory.copyIntoIntArray\n");
2002 (*env)->SetIntArrayRegion(env, array, arrayOffset, length, (void*) nativeDataAddress);
2003 return ;
2004}
2005
2006/*
2007 * Class: dim_Memory
2008 * Method: copyIntoLongArray
2009 * Signature: (I[JII)V
2010 */
2011JNIEXPORT void JNICALL Java_dim_Memory_copyIntoLongArray
2012 (JNIEnv* env, jclass nativeClass, jlong nativeDataAddress, jlongArray array, jint arrayOffset, jint length)
2013{
2014 if(nativeClass){}
2015 DBGe(dim_Dbg_MEMORY) printf("DimJNI: Memory.copyIntoLongArray\n");
2016 (*env)->SetLongArrayRegion(env, array, arrayOffset, length, (void*) nativeDataAddress);
2017 return ;
2018}
2019
2020/*
2021 * Class: dim_Memory
2022 * Method: copyIntoFloatArray
2023 * Signature: (I[FII)V
2024 */
2025JNIEXPORT void JNICALL Java_dim_Memory_copyIntoFloatArray
2026 (JNIEnv* env, jclass nativeClass, jlong nativeDataAddress, jfloatArray array, jint arrayOffset, jint length)
2027{
2028 if(nativeClass){}
2029 DBGe(dim_Dbg_MEMORY) printf("DimJNI: Memory.copyIntoFloatArray\n");
2030 (*env)->SetFloatArrayRegion(env, array, arrayOffset, length, (void*) nativeDataAddress);
2031 return ;
2032}
2033
2034/*
2035 * Class: dim_Memory
2036 * Method: copyIntoDoubleArray
2037 * Signature: (I[DII)V
2038 */
2039JNIEXPORT void JNICALL Java_dim_Memory_copyIntoDoubleArray
2040 (JNIEnv* env, jclass nativeClass, jlong nativeDataAddress, jdoubleArray array, jint arrayOffset, jint length)
2041{
2042 if(nativeClass){}
2043 DBGe(dim_Dbg_MEMORY) printf("DimJNI: Memory.copyIntoDoubleArray\n");
2044 (*env)->SetDoubleArrayRegion(env, array, arrayOffset, length, (void*) nativeDataAddress);
2045 return ;
2046}
2047
2048
2049
2050
2051/* implementation of dim_mutablememory.h ======================================================== */
2052
2053/*
2054 * Class: dim_MutableMemory
2055 * Method: allocateNativeDataBlock
2056 * Signature: (I)I
2057 */
2058JNIEXPORT jlong JNICALL Java_dim_MutableMemory_allocateNativeDataBlock
2059 (JNIEnv* env, jclass nativeClass, jint size)
2060{
2061 jlong address;
2062 if(env){}
2063 if(nativeClass){}
2064// DBGe(dim_Dbg_MEMORY_ALLOCATE) ; /* report only */
2065 address = (jlong) malloc((size_t)size);
2066 DBGx(dim_Dbg_MEMORY_ALLOCATE) printf("DimJNI: MutableMemory.allocateNativeDataBlock of %d bytes at 0x%08lx\n", size, address);
2067 return address;
2068}
2069
2070/*
2071 * Class: dim_MutableMemory
2072 * Method: releaseNativeDataBlock
2073 * Signature: (I)V
2074 */
2075JNIEXPORT void JNICALL Java_dim_MutableMemory_releaseNativeDataBlock
2076 (JNIEnv* env, jclass nativeClass, jlong desc)
2077{
2078 if(env){}
2079 if(nativeClass){}
2080 DBGe(dim_Dbg_MEMORY_ALLOCATE) printf("DimJNI: MutableMemory.releaseNativeDataBlock 0x%08lx\n", desc);
2081 //printf("free %08X\n", desc);
2082 free((void*)desc);
2083 return;
2084}
2085
2086/*
2087 * Class: dim_MutableMemory
2088 * Method: setBoolean
2089 * Signature: (IZ)V
2090 */
2091JNIEXPORT void JNICALL Java_dim_MutableMemory_setBoolean
2092 (JNIEnv* env, jclass nativeClass, jlong nativeDataAddress, jboolean data)
2093{
2094 if(env){}
2095 if(nativeClass){}
2096 DBGe(dim_Dbg_MUTABLE_MEMORY) printf("DimJNI: MutableMemory.setBoolean(0x%08lx, %02x)\n", nativeDataAddress, data);
2097 *(jboolean*)nativeDataAddress = data;
2098}
2099
2100/*
2101 * Class: dim_MutableMemory
2102 * Method: setChar
2103 * Signature: (IC)V
2104 */
2105JNIEXPORT void JNICALL Java_dim_MutableMemory_setChar
2106 (JNIEnv* env, jclass nativeClass, jlong nativeDataAddress, jchar data)
2107{
2108 if(env){}
2109 if(nativeClass){}
2110 DBGe(dim_Dbg_MUTABLE_MEMORY) printf("DimJNI: MutableMemory.setChar(0x%08lx, %02x)\n", nativeDataAddress, data);
2111 *(jchar*)nativeDataAddress = data;
2112}
2113
2114/*
2115 * Class: dim_MutableMemory
2116 * Method: setByte
2117 * Signature: (IB)V
2118 */
2119JNIEXPORT void JNICALL Java_dim_MutableMemory_setByte
2120 (JNIEnv* env, jclass nativeClass, jlong nativeDataAddress, jbyte data)
2121{
2122 if(env){}
2123 if(nativeClass){}
2124 DBGe(dim_Dbg_MUTABLE_MEMORY) printf("DimJNI: MutableMemory.setByte(0x%08lx, %02x)\n", nativeDataAddress, data);
2125 *(jbyte*)nativeDataAddress = data;
2126}
2127
2128/*
2129 * Class: dim_MutableMemory
2130 * Method: setShort
2131 * Signature: (IS)V
2132 */
2133JNIEXPORT void JNICALL Java_dim_MutableMemory_setShort
2134 (JNIEnv* env, jclass nativeClass, jlong nativeDataAddress, jshort data)
2135{
2136 if(env){}
2137 if(nativeClass){}
2138 DBGe(dim_Dbg_MUTABLE_MEMORY) printf("DimJNI: MutableMemory.setShort(0x%08lx, %04x)\n", nativeDataAddress, data);
2139 *(jshort*)nativeDataAddress = data;
2140}
2141
2142/*
2143 * Class: dim_MutableMemory
2144 * Method: setInt
2145 * Signature: (II)V
2146 */
2147JNIEXPORT void JNICALL Java_dim_MutableMemory_setInt
2148 (JNIEnv* env, jclass nativeClass, jlong nativeDataAddress, jint data)
2149{
2150 if(env){}
2151 if(nativeClass){}
2152 DBGe(dim_Dbg_MUTABLE_MEMORY) printf("DimJNI: MutableMemory.setInt(0x%08lx, %0x)\n", nativeDataAddress, data);
2153 *(jint*)nativeDataAddress = data;
2154}
2155
2156/*
2157 * Class: dim_MutableMemory
2158 * Method: setLong
2159 * Signature: (IJ)V
2160 */
2161JNIEXPORT void JNICALL Java_dim_MutableMemory_setLong
2162 (JNIEnv* env, jclass nativeClass, jlong nativeDataAddress, jlong data)
2163{
2164 if(env){}
2165 if(nativeClass){}
2166 DBGe(dim_Dbg_MUTABLE_MEMORY) printf("DimJNI: MutableMemory.setLong(0x%08lx, %08x)\n", nativeDataAddress, (unsigned)data);
2167 *(jlong*)nativeDataAddress = data;
2168}
2169
2170/*
2171 * Class: dim_MutableMemory
2172 * Method: setFloat
2173 * Signature: (IF)V
2174 */
2175JNIEXPORT void JNICALL Java_dim_MutableMemory_setFloat
2176 (JNIEnv* env, jclass nativeClass, jlong nativeDataAddress, jfloat data)
2177{
2178 if(env){}
2179 if(nativeClass){}
2180 DBGe(dim_Dbg_MUTABLE_MEMORY) printf("DimJNI: MutableMemory.setFloat(0x%08lx, %f)\n", nativeDataAddress, data);
2181 *(jfloat*)nativeDataAddress = data;
2182}
2183
2184/*
2185 * Class: dim_MutableMemory
2186 * Method: setDouble
2187 * Signature: (ID)V
2188 */
2189JNIEXPORT void JNICALL Java_dim_MutableMemory_setDouble
2190 (JNIEnv* env, jclass nativeClass, jlong nativeDataAddress, jdouble data)
2191{
2192 if(env){}
2193 if(nativeClass){}
2194 DBGe(dim_Dbg_MUTABLE_MEMORY) printf("DimJNI: MutableMemory.setDouble(0x%08lx, %08x)\n", nativeDataAddress, (unsigned)data);
2195 *(jdouble*)nativeDataAddress = data;
2196}
2197
2198/*
2199 * Class: dim_MutableMemory
2200 * Method: setString
2201 * Signature: (ILjava/lang/String;)V
2202 */
2203JNIEXPORT void JNICALL Java_dim_MutableMemory_setString
2204 (JNIEnv* env, jclass nativeClass, jlong nativeDataAddress, jstring data)
2205{
2206 const char* charData = (*env)->GetStringUTFChars(env, data, 0);
2207
2208 DBGe(dim_Dbg_MUTABLE_MEMORY) printf("DimJNI: MutableMemory.setString(0x%08lx, %s)\n", nativeDataAddress, charData);
2209
2210 if(nativeClass){}
2211 strcpy((char*)nativeDataAddress, charData);
2212 (*env)->ReleaseStringUTFChars(env, data, charData);
2213}
2214
2215
2216/*
2217 * Class: dim_MutableMemory
2218 * Method: copyFromBooleanArray
2219 * Signature: (I[ZII)V
2220 */
2221JNIEXPORT void JNICALL Java_dim_MutableMemory_copyFromBooleanArray
2222 (JNIEnv* env, jclass nativeClass, jlong nativeDataAddress, jbooleanArray array, jint arrayOffset, jint length)
2223{
2224 if(nativeClass){}
2225 DBGe(dim_Dbg_MUTABLE_MEMORY) printf("DimJNI: MutableMemory.copyFromBooleanArray\n");
2226 (*env)->GetBooleanArrayRegion(env, array, arrayOffset, length, (void*) nativeDataAddress);
2227 return ;
2228}
2229
2230/*
2231 * Class: dim_MutableMemory
2232 * Method: copyFromCharArray
2233 * Signature: (I[CII)V
2234 */
2235JNIEXPORT void JNICALL Java_dim_MutableMemory_copyFromCharArray
2236 (JNIEnv* env, jclass nativeClass, jlong nativeDataAddress, jcharArray array, jint arrayOffset, jint length)
2237{
2238 if(nativeClass){}
2239 DBGe(dim_Dbg_MUTABLE_MEMORY) printf("DimJNI: MutableMemory.copyFromCharArray\n");
2240 (*env)->GetCharArrayRegion(env, array, arrayOffset, length, (void*) nativeDataAddress);
2241 return ;
2242}
2243
2244/*
2245 * Class: dim_MutableMemory
2246 * Method: copyFromByteArray
2247 * Signature: (I[BII)V
2248 */
2249JNIEXPORT void JNICALL Java_dim_MutableMemory_copyFromByteArray
2250 (JNIEnv* env, jclass nativeClass, jlong nativeDataAddress, jbyteArray array, jint arrayOffset, jint length)
2251{
2252 if(nativeClass){}
2253 DBGe(dim_Dbg_MUTABLE_MEMORY) printf("DimJNI: MutableMemory.copyFromByteArray\n");
2254 (*env)->GetByteArrayRegion(env, array, arrayOffset, length, (void*) nativeDataAddress);
2255 return ;
2256}
2257
2258/*
2259 * Class: dim_MutableMemory
2260 * Method: copyFromShortArray
2261 * Signature: (I[SII)V
2262 */
2263JNIEXPORT void JNICALL Java_dim_MutableMemory_copyFromShortArray
2264 (JNIEnv* env, jclass nativeClass, jlong nativeDataAddress, jshortArray array, jint arrayOffset, jint length)
2265{
2266 if(nativeClass){}
2267 DBGe(dim_Dbg_MUTABLE_MEMORY) printf("DimJNI: MutableMemory.copyFromShortArray\n");
2268 (*env)->GetShortArrayRegion(env, array, arrayOffset, length, (void*) nativeDataAddress);
2269 return ;
2270}
2271
2272/*
2273 * Class: dim_MutableMemory
2274 * Method: copyFromIntArray
2275 * Signature: (I[III)V
2276 */
2277JNIEXPORT void JNICALL Java_dim_MutableMemory_copyFromIntArray
2278 (JNIEnv* env, jclass nativeClass, jlong nativeDataAddress, jintArray array, jint arrayOffset, jint length)
2279{
2280 if(nativeClass){}
2281 DBGe(dim_Dbg_MUTABLE_MEMORY) printf("DimJNI: MutableMemory.copyFromIntArray\n");
2282 (*env)->GetIntArrayRegion(env, array, arrayOffset, length, (void*) nativeDataAddress);
2283 return ;
2284}
2285
2286/*
2287 * Class: dim_MutableMemory
2288 * Method: copyFromLongArray
2289 * Signature: (I[JII)V
2290 */
2291JNIEXPORT void JNICALL Java_dim_MutableMemory_copyFromLongArray
2292 (JNIEnv* env, jclass nativeClass, jlong nativeDataAddress, jlongArray array, jint arrayOffset, jint length)
2293{
2294 if(nativeClass){}
2295 DBGe(dim_Dbg_MUTABLE_MEMORY) printf("DimJNI: MutableMemory.copyFromLongArray\n");
2296 (*env)->GetLongArrayRegion(env, array, arrayOffset, length, (void*) nativeDataAddress);
2297 return ;
2298}
2299
2300/*
2301 * Class: dim_MutableMemory
2302 * Method: copyFromFloatArray
2303 * Signature: (I[FII)V
2304 */
2305JNIEXPORT void JNICALL Java_dim_MutableMemory_copyFromFloatArray
2306 (JNIEnv* env, jclass nativeClass, jlong nativeDataAddress, jfloatArray array, jint arrayOffset, jint length)
2307{
2308 if(nativeClass){}
2309 DBGe(dim_Dbg_MUTABLE_MEMORY) printf("DimJNI: MutableMemory.copyFromFloatArray\n");
2310 (*env)->GetFloatArrayRegion(env, array, arrayOffset, length, (void*) nativeDataAddress);
2311 return ;
2312}
2313
2314/*
2315 * Class: dim_MutableMemory
2316 * Method: copyFromDoubleArray
2317 * Signature: (I[DII)V
2318 */
2319JNIEXPORT void JNICALL Java_dim_MutableMemory_copyFromDoubleArray
2320 (JNIEnv* env, jclass nativeClass, jlong nativeDataAddress, jdoubleArray array, jint arrayOffset, jint length)
2321{
2322 if(nativeClass){}
2323 DBGe(dim_Dbg_MUTABLE_MEMORY) printf("DimJNI: MutableMemory.copyFromDoubleArray\n");
2324 (*env)->GetDoubleArrayRegion(env, array, arrayOffset, length, (void*) nativeDataAddress);
2325 return ;
2326}
2327
2328/*
2329 * Class: dim_MutableMemory
2330 * Method: copyNativeDataBlock
2331 * Signature: (II)V
2332 */
2333JNIEXPORT void JNICALL Java_dim_MutableMemory_copyNativeDataBlock
2334 (JNIEnv* env, jclass nativeClass, jlong destinationDataAddress, jlong sourceDataAddress, jint length)
2335{
2336 if(env){}
2337 if(nativeClass){}
2338 DBGe(dim_Dbg_MUTABLE_MEMORY) printf("DimJNI: MutableMemory.copyNativeDataBlock\n");
2339 memcpy((void *)destinationDataAddress, (void *)sourceDataAddress, (size_t)length);
2340 return ;
2341}
2342
2343
2344
2345
2346/* implementation of dim_objectdescriptor.h ===================================================== */
2347
2348/* fancy packing methods */
2349
2350typedef enum {f_skip,
2351 f_boolean, f_byte, f_char, f_short, f_int, f_long, f_float, f_double, f_string, f_object,
2352 a_boolean, a_byte, a_char, a_short, a_int, a_long, a_float, a_double, a_string, a_object,
2353 c_boolean, c_byte, c_char, c_short, c_int, c_long, c_float, c_double, c_string, c_object
2354 } FieldType;
2355typedef struct objectDescriptorEntry_struct objectDescriptorEntry_type;
2356struct objectDescriptorEntry_struct
2357{
2358 FieldType type;
2359 int length;
2360 int offset;
2361 jfieldID fieldID;
2362 jarray array;
2363 int arrayOffset;
2364};
2365
2366typedef struct objectDescriptor_struct objectDescriptor_type;
2367struct objectDescriptor_struct
2368{
2369 jclass objectClass;
2370 int entries;
2371 int maxEntries;
2372 objectDescriptorEntry_type* entry;
2373};
2374
2375
2376/*
2377 * Class: dim_ObjectDescriptor
2378 * Method: newObjectDescriptor
2379 * Signature: (Ljava/lang/Class;I)I
2380 */
2381JNIEXPORT jlong JNICALL Java_dim_ObjectDescriptor_newObjectDescriptor
2382 (JNIEnv* env, jclass nativeClass, jclass objectClass, jint maxEntries)
2383{
2384 objectDescriptor_type* descriptor;
2385
2386// DBGe(dim_Dbg_DESCRIPTORS) ; /* trap only, report on exit */
2387 // todo put object descriptor and entry array in the same malloc (for dump purposes)
2388//printf("malloc descriptor\n");
2389 if(env){}
2390 if(nativeClass){}
2391 if(maxEntries==0) maxEntries = 10;
2392 descriptor = (objectDescriptor_type*) malloc(sizeof(objectDescriptor_type));
2393 descriptor->entry = (objectDescriptorEntry_type*) malloc((size_t)(maxEntries * (jint)sizeof(objectDescriptorEntry_type)));
2394 descriptor->objectClass = (*env)->NewGlobalRef(env, objectClass);
2395 descriptor->entries = 0;
2396 descriptor->maxEntries = maxEntries;
2397
2398 DBGx(dim_Dbg_DESCRIPTORS) printf("DimJNI: Native.newObjectDescriptor %08lx\n", (dim_long)descriptor);
2399 return (dim_long) descriptor;
2400}
2401
2402objectDescriptorEntry_type* getNextDescriptorEntry(objectDescriptor_type* descriptor)
2403{
2404 if(descriptor->entries == descriptor->maxEntries)
2405 {
2406 objectDescriptorEntry_type* entry = realloc(descriptor->entry, (size_t)(descriptor->maxEntries+10));
2407//printf("realloc descriptor\n");
2408 if(entry==NULL) return NULL;
2409
2410 descriptor->entry = entry;
2411 descriptor->maxEntries = descriptor->maxEntries+10;
2412 }
2413 return descriptor->entry + descriptor->entries++;
2414}
2415
2416
2417
2418/*
2419 * Class: dim_ObjectDescriptor
2420 * Method: addFieldToObjectDescriptor
2421 * Signature: (ILjava/lang/String;Ljava/lang/String;I)I
2422 */
2423JNIEXPORT jint JNICALL Java_dim_ObjectDescriptor_addFieldToObjectDescriptor
2424 (JNIEnv* env, jclass nativeClass, jlong desc, jstring fieldName, jstring fieldType, jint offset)
2425{
2426// FieldType field_type = f_int;
2427 objectDescriptorEntry_type* entry = getNextDescriptorEntry((objectDescriptor_type*) desc);
2428 const char* name = (*env)->GetStringUTFChars(env, fieldName, 0);
2429 const char* type = (*env)->GetStringUTFChars(env, fieldType, 0);
2430 jfieldID fieldID = (*env)->GetFieldID(env, ((objectDescriptor_type*) desc)->objectClass, name, type);
2431
2432 // TODO throw an error if there is no such FieldID
2433
2434 DBGe(dim_Dbg_DESCRIPTORS) printf("DimJNI: Native.addFieldToObjectDescriptor %08lx Field %s Type %s\n", (dim_long) desc, name, type);
2435 // TODO: if(entry==NULL) throw out-of-memory exception, set length to 0
2436
2437 // TODO: if(fieldType == "I") field_type = f_int; etc
2438
2439 if(nativeClass){}
2440 entry->type =f_skip;
2441 entry->length =0;
2442 entry->offset =offset;
2443 entry->fieldID =fieldID;
2444 entry->array =0;
2445 entry->arrayOffset =0;
2446
2447 switch (*type)
2448 {
2449 case 'Z':
2450 {
2451 entry->type =f_boolean;
2452 entry->length =sizeof(jboolean);
2453 break;
2454 }
2455 case 'B':
2456 {
2457 entry->type =f_byte;
2458 entry->length =sizeof(jbyte);
2459 break;
2460 }
2461
2462 case 'C':
2463 {
2464 entry->type =f_char;
2465 entry->length =sizeof(jchar);
2466 break;
2467 }
2468
2469 case 'S':
2470 {
2471 entry->type =f_short;
2472 entry->length =sizeof(jshort);
2473 break;
2474 }
2475
2476 case 'I':
2477 {
2478 entry->type =f_int;
2479 entry->length =sizeof(jint);
2480 break;
2481 }
2482
2483 case 'J':
2484 {
2485 entry->type =f_long;
2486 entry->length =sizeof(jlong);
2487 break;
2488 }
2489
2490 case 'F':
2491 {
2492 entry->type =f_float;
2493 entry->length =sizeof(jfloat);
2494 break;
2495 }
2496
2497 case 'D':
2498 {
2499 entry->type =f_double;
2500 entry->length =sizeof(jdouble);
2501 break;
2502 }
2503
2504 //case '[':
2505 //{
2506 // // TODO deal with array types
2507 //}
2508 default :
2509 {
2510 printf("DimJNI: addFieldToObjectDescriptor - type %s not yet supported. (field %s)\n", name, type);
2511 }
2512 }
2513
2514
2515 (*env)->ReleaseStringUTFChars(env, fieldName, name);
2516 (*env)->ReleaseStringUTFChars(env, fieldType, type);
2517 return entry->length;
2518}
2519
2520
2521/*
2522 * Class: dim_ObjectDescriptor
2523 * Method: deleteObjectDescriptor
2524 * Signature: (I)V
2525 */
2526JNIEXPORT void JNICALL Java_dim_ObjectDescriptor_deleteObjectDescriptor
2527 (JNIEnv* env, jclass nativeClass, jlong desc)
2528{
2529 objectDescriptor_type* descriptor = (objectDescriptor_type*) desc;
2530
2531 if(nativeClass){}
2532 DBGe(dim_Dbg_DESCRIPTORS) printf("DimJNI: Native.deleteObjectDescriptor %08lx\n", (dim_long)desc);
2533 (*env)->DeleteGlobalRef(env, descriptor->objectClass);
2534//printf("free descriptor\n");
2535 free(descriptor->entry);
2536 free(descriptor);
2537 return;
2538}
2539
2540
2541/*
2542 * Class: dim_ObjectDescriptor
2543 * Method: copyIntoObject
2544 * Signature: (ILjava/lang/Object;I)V
2545 */
2546
2547JNIEXPORT void JNICALL Java_dim_ObjectDescriptor_copyIntoObject
2548 (JNIEnv* env, jclass nativeClass, jlong nativeDataAddress, jobject theObject, jlong desc)
2549{
2550 int i;
2551 objectDescriptorEntry_type* entry;
2552
2553 objectDescriptor_type* descriptor = (objectDescriptor_type*) desc;
2554 jclass objectClass = descriptor->objectClass;
2555
2556 DBGe(dim_Dbg_DESCRIPTORS) printf("DimJNI: Native.copyIntoObject %08lx\n", (dim_long)desc);
2557
2558 if(nativeClass){}
2559 // test if object can be cast to object class
2560 if((*env)->IsInstanceOf(env, theObject, objectClass) != JNI_TRUE)
2561 {
2562 // throw exception
2563 jclass exceptionClass = (*env)->FindClass(env, "java/lang/IllegalArgumentException");
2564 (*env)->ThrowNew(env, exceptionClass, " (Sorry...)");
2565 return;
2566 }
2567
2568 // loop over descriptor entries
2569 entry = descriptor->entry;
2570 for (i=0; i<descriptor->entries; i++)
2571 {
2572 switch (entry->type)
2573 {
2574 case f_boolean:
2575 (*env)->SetBooleanField(env, theObject, entry->fieldID, *(jboolean*) (nativeDataAddress+entry->offset));
2576 break;
2577 case f_byte:
2578 (*env)->SetByteField( env, theObject, entry->fieldID, *(jbyte*) (nativeDataAddress+entry->offset));
2579 break;
2580 case f_char:
2581 (*env)->SetCharField( env, theObject, entry->fieldID, *(jchar*) (nativeDataAddress+entry->offset));
2582 break;
2583 case f_short:
2584 (*env)->SetShortField( env, theObject, entry->fieldID, *(jshort*) (nativeDataAddress+entry->offset));
2585 break;
2586 case f_int:
2587 (*env)->SetIntField( env, theObject, entry->fieldID, *(jint*) (nativeDataAddress+entry->offset));
2588 break;
2589 case f_long:
2590 (*env)->SetLongField( env, theObject, entry->fieldID, *(jlong*) (nativeDataAddress+entry->offset));
2591 break;
2592 case f_float:
2593 (*env)->SetFloatField( env, theObject, entry->fieldID, *(jfloat*) (nativeDataAddress+entry->offset));
2594 break;
2595 case f_double:
2596 (*env)->SetDoubleField( env, theObject, entry->fieldID, *(jdouble*) (nativeDataAddress+entry->offset));
2597 break;
2598 case a_boolean:
2599 (*env)->SetBooleanArrayRegion(env, entry->array, entry->arrayOffset, entry->length, (void*) (nativeDataAddress+entry->offset));
2600 break;
2601 case c_boolean:
2602 (*env)->SetBooleanArrayRegion(env, entry->array, entry->arrayOffset, entry->length, (void*) (nativeDataAddress+entry->offset));
2603 //TODO :: complete this list, including recursive call to this function for objects
2604 break;
2605 default:
2606 break;
2607 }
2608 //TODO :: ?? if ((*env)->ExceptionOccurred(env)) (*env)->ExceptionDescribe(env); // clear any possible exception, if we do not do this, all further methods will fail!!
2609 entry++;
2610 }
2611 return;
2612}
2613
2614/*
2615 * Class: dim_ObjectDescriptor
2616 * Method: copyFromObject
2617 * Signature: (ILjava/lang/Object;I)V
2618 */
2619JNIEXPORT void JNICALL Java_dim_ObjectDescriptor_copyFromObject
2620 (JNIEnv* env, jclass nativeClass, jlong nativeDataAddress, jobject theObject, jlong desc)
2621{
2622 int i;
2623 objectDescriptorEntry_type* entry;
2624
2625 objectDescriptor_type* descriptor = (objectDescriptor_type*) desc;
2626 jclass objectClass = descriptor->objectClass;
2627
2628 DBGe(dim_Dbg_DESCRIPTORS) printf("DimJNI: Native.copyFromObject %08x\n", (int)desc);
2629
2630 if(nativeClass){}
2631 // test if object can be cast to object class
2632 if((*env)->IsInstanceOf(env, theObject, objectClass) != JNI_TRUE)
2633 {
2634 // throw exception
2635 jclass exceptionClass = (*env)->FindClass(env, "java/lang/IllegalArgumentException");
2636 (*env)->ThrowNew(env, exceptionClass, " (Sorry...)");
2637 return;
2638 }
2639
2640 // loop over descriptor entries
2641 entry = descriptor->entry;
2642 for (i=0; i<descriptor->entries; i++)
2643 {
2644 switch (entry->type)
2645 {
2646 case f_boolean:
2647 *(jboolean*) (nativeDataAddress+entry->offset) = (*env)->GetBooleanField(env, theObject, entry->fieldID);
2648 break;
2649 case f_byte:
2650 *(jbyte*) (nativeDataAddress+entry->offset) = (*env)->GetByteField(env, theObject, entry->fieldID);
2651 break;
2652 case f_char:
2653 *(jchar*) (nativeDataAddress+entry->offset) = (*env)->GetCharField(env, theObject, entry->fieldID);
2654 break;
2655 case f_short:
2656 *(jshort*) (nativeDataAddress+entry->offset) = (*env)->GetShortField(env, theObject, entry->fieldID);
2657 break;
2658 case f_int:
2659 *(jint*) (nativeDataAddress+entry->offset) = (*env)->GetIntField(env, theObject, entry->fieldID);
2660 break;
2661 case f_long:
2662 *(jlong*) (nativeDataAddress+entry->offset) = (*env)->GetLongField(env, theObject, entry->fieldID);
2663 break;
2664 case f_float:
2665 *(jfloat*) (nativeDataAddress+entry->offset) = (*env)->GetFloatField(env, theObject, entry->fieldID);
2666 break;
2667 case f_double:
2668 *(jdouble*) (nativeDataAddress+entry->offset) = (*env)->GetDoubleField(env, theObject, entry->fieldID);
2669 break;
2670// case a_boolean:
2671// (*env)->GetBooleanArrayRegion(env, array, 0, length, (void*) nativeDataAddress);
2672// break;
2673// case c_boolean:
2674// *(jbyte*) (nativeDataAddress+entry->offset) = (*env)->GetField(env, theObject, entry->fieldID);
2675// (*env)->SetBooleanArrayRegion(env, entry->array, entry->arrayOffset, entry->length, (void*) (nativeDataAddress+entry->offset));
2676// //TODO :: complete this list, including recursive call to this function for objects
2677// break;
2678 default:
2679 break;
2680 }
2681 //TODO :: ?? if ((*env)->ExceptionOccurred(env)) (*env)->ExceptionDescribe(env); // clear any possible exception, if we do not do this, all further methods will fail!!
2682 entry++;
2683 }
2684 return;
2685}
2686
2687
2688#ifdef zombies
2689
2690
2691// jstring myFormat;
2692// myFormat = (*env)->NewStringUTF(env, "x");
2693// (*env)->CallVoidMethod(env, dataObject, setDimFormatMID, myFormat); // JUST TO TRY
2694
2695// jintArray dataArray;
2696// int data[10]={1,2,3,4,5,6,7,8,9,-1};
2697// dataArray = (*env)->NewIntArray(env,10);
2698// (*env)->SetIntArrayRegion(env, dataArray,0,10,data);
2699// (*env)->CallVoidMethod(env, dataObject, NativeDataDecoder_decodeNativeData, dataArray); // JUST TO TRY
2700
2701// jclass dataClass = (*env)->GetObjectClass(env, dataObject);
2702
2703// (*env)->CallIntMethod(env,dataObject,dimOIlengthMID);
2704// (*env)->CallNonvirtualIntMethod(env,dataObject, dataItemInterface, dimOIlengthMID);// formatString = (jstring) (*env)->CallNonvirtualObjectMethod(env, dataObject, dataItemInterface, getDimFormatMID);
2705
2706 // Print the current thread ID in the Debug Window
2707 // TRACE("Current Thread ID = 0x%X\n", AfxGetThread()->m_nThreadID);
2708 _RPT1(_CRT_ERROR, "Invalid allocation size: %u bytes.\n", nSize);
2709
2710
2711// formatString = (jstring) (*env)->CallObjectMethod(env,dataObject,getDimFormatMID);
2712// format = (*env)->GetStringUTFChars(env, formatString, 0);
2713// printf("Format string in native: %s\n",format);
2714// (*env)->ReleaseStringUTFChars(env, formatString, format);
2715
2716 /* server_getInfo_callback is invoked when dim needs the data for a service.
2717 * The data is obtained by calling the encodeData method of the DataEncoder
2718 * interface which returns us a Memory object. Next we extract the data address
2719 * and size from the returned Memory object.
2720 *
2721 * Alternative:
2722 * I call the following class method: Server.sendMeTheData(theDataEncoder)
2723 * This method is implemented as
2724 * sendMeTheData(DataEncoder theDataEncoder)
2725 * {
2726 * Memory theData = theDataEncoder.encodeData();
2727 * Server.setDataReference(Memory.dataAddress, Memory.DataSize);
2728 * }
2729 * The method setDataReference(int dataAddress, int dataSize) is implemented
2730 * as a native method and receives directly the data which one can store in
2731 * a global variable.
2732 * More complicated, not convinced that it would be more efficient.
2733 */
2734
2735
2736
2737#endif
Note: See TracBrowser for help on using the repository browser.