1 /* JORTESubscriptionCreate.c */
4 * This code provides conversion between JAVA a C environments.
5 * The C functions are calling here and results are send to JAVA
6 * native functions. It uses the header pregenerated by JAVA
7 * (by command 'javah -jni class_with_native_function')
9 * @author Lukas Pokorny (lukas_pokorny@centrum.cz)
10 * @author CTU FEE Prague - Department of Control Engineering (dce.felk.cvut.cz)
11 * @author Project ORTE - OCERA Real Time Ethernet (www.ocera.org)
12 * @author dedication to Kj
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
31 // library header file's path
33 // pregenerated header
34 #include "jorte/org_ocera_orte_Subscription.h"
35 // enable TEST_STAGE run level
36 #include "jorte/4all.h"
38 #include "jorte/jorte_typedefs_defines.h"
39 #include "jorte/jorte_protos_api.h"
41 /* ****************************************************************** *
43 * ****************************************************************** */
46 recvCallBack(const ORTERecvInfo *info,void *vinstance, void *recvCallBackParam)
51 jclass cls = 0; // local reference!
60 jmethodID mid_callback = 0;
68 JORTECallbackContext_t *callback_cont = (JORTECallbackContext_t*)recvCallBackParam;
71 printf("\n\n:c: --------------- recvCallBack called.. --------------- \n");
76 // set local variables from struct
77 if(callback_cont->jvm == 0)
80 printf(":!c: jvm = NULL \n");
84 jvm = callback_cont->jvm;
86 (*jvm)->AttachCurrentThread(jvm, (void **)&env, NULL);
90 printf(":!c: env = NULL \n");
95 if(callback_cont->obj == 0)
98 printf(":!c: obj = NULL \n");
103 obj = callback_cont->obj;
104 rinfo = callback_cont->rinfo;
105 obj_msg = callback_cont->msg;
106 obj_bb = callback_cont->bbuff;
111 printf(":c: env = %p, obj_msg = %p \n", env, obj_msg);
119 cls = (*env)->FindClass(env, "org/ocera/orte/types/RecvInfo");
123 printf(":!c: cls = NULL \n");
127 // call object constructor
128 mid = (*env)->GetMethodID(env, cls, "<init>", "()V");
132 printf(":!c: constructor failed! \n");
137 rinfo = (*env)->NewObject(env, cls, mid);
141 printf(":!c: rinfo = NULL \n");
145 // create global reference
146 callback_cont->rinfo = (*env)->NewGlobalRef(env, rinfo);
147 if (callback_cont->rinfo == 0)
150 printf(":!c: callback_cont->rinfo = NULL \n");
155 ////////////////////////////////////////////////////
156 // set RecvInfo instance
157 if(setRecvInfo(env,info,callback_cont->rinfo) == 0)
160 printf(":!c: setRecvInfo() failed! \n");
164 ////////////////////////////////////////////////////
165 // control print - only in TEST_STAGE
167 printf(":c: rinfo created :] \n");
168 printf(":c:----- ORTERecvInfo members ------ \n");
169 printf(":c: recvStatus: %d \n", info->status);
170 printf(":c: senderGuid: hid = %d, aid = %d, oid = %d \n",
171 info->senderGUID.hid,info->senderGUID.aid,info->senderGUID.oid);
172 printf(":c: topic: %s \n",info->topic);
173 printf(":c: type: %s \n",info->type);
174 printf(":c: localTimeRecv: sec = %d, fract = %d \n",
175 info->localTimeReceived.seconds,info->localTimeReceived.fraction);
176 printf(":c: remoteTimePub: sec = %d, fract = %d \n",
177 info->remoteTimePublished.seconds,info->remoteTimePublished.fraction);
178 printf(":c: seqNr: high = %d, low = %d \n",info->sn.high,info->sn.low);
179 printf(":c:---------------------------------- \n");
181 ////////////////////////////////////////////////////
182 // update MessageData instance
184 cls_msg = (*env)->GetObjectClass(env, obj_msg);
185 //cls_tmp = (*env)->FindClass(env, "org/ocera/orte/types/MessageData");
189 printf(":!c: cls_msg = NULL \n");
196 // get buff's fieldID
197 fid = (*env)->GetFieldID(env,
200 "Ljava/nio/ByteBuffer;");
204 printf(":!c: fid = NULL \n");
209 obj_bb = (*env)->GetObjectField(env, obj_msg, fid);
213 printf(":!c: obj_bb = NULL \n");
217 // create global reference
218 callback_cont->bbuff = (*env)->NewGlobalRef(env, obj_bb);
219 if (callback_cont->bbuff == 0)
222 printf(":!c: callback_cont->bbuff = NULL \n");
227 printf(":c: global reference created.. \n");
230 // get class ByteBuffer
231 cls_bb = (*env)->GetObjectClass(env, obj_bb);
235 printf(":!c: cls_bb = NULL \n");
240 mid = (*env)->GetMethodID(env,
247 printf(":!c: mid = NULL \n");
251 buff_length = (*env)->CallIntMethod(env,
254 // needs rewind buffer before working with it!!
255 // (needs cls Buffer not ByteBuffer!)
257 cls_tmp = (*env)->FindClass(env, "java/nio/Buffer");
261 printf(":!c: cls_tmp = NULL \n");
265 // methodID - rewind()
266 mid = (*env)->GetMethodID(env,
269 "()Ljava/nio/Buffer;");
273 printf(":!c: mid = NULL \n");
278 (*env)->CallObjectMethod(env,
282 printf(":c: buffer successfully rewind..\n");
285 mid = (*env)->GetMethodID(env,
288 "(B)Ljava/nio/ByteBuffer;");
292 printf(":!c: mid = NULL \n");
296 // copy the bytes from C to JAVA buffer
297 /////////////////////////////////////////////////////
298 buffer = (char *) vinstance;
300 for(i = 0; i < buff_length; i++)
303 printf(":c: i = %d kopirovany znak = %c [%d] \n",
307 obj_bb = (*env)->CallObjectMethod(env,
314 /////////////////////////////////////////////////////
316 mid = (*env)->GetMethodID(env,
323 printf(":!c: mid = NULL \n");
328 (*env)->CallVoidMethod(env,
332 /* *************************** *
333 * call JAVA CallBack method *
334 * *************************** */
336 printf(":c: call JAVA CallBack method \n");
341 cls = (*env)->GetObjectClass(env,callback_cont->obj);
345 printf(":!c: cls = NULL \n");
350 mid = (*env)->GetMethodID(env,
353 "(Lorg/ocera/orte/types/RecvInfo;Lorg/ocera/orte/types/MessageData;)V");
357 printf(":!c: cls = NULL \n");
364 printf(":c: volam callback metodu.. halo jsi tam?? \n");
366 // call object's method
367 (*env)->CallVoidMethod(env,
368 callback_cont->obj, /*obj*/
370 callback_cont->rinfo,
376 // detach current thread
377 if((*jvm)->DetachCurrentThread(jvm) != 0)
378 printf(":c!: DetachCurrentThread failed! \n");
381 printf(":c: ------------ thats all from recvCallBack ------------ \n\n");
386 /* ****************************************************************** *
388 * ****************************************************************** */
389 JNIEXPORT jint JNICALL
390 Java_org_ocera_orte_Subscription_jORTESubscriptionCreate
393 jint dhandle, // appDomain handle
394 jint jsmode, // subs mode
395 jint jstype, // subs type
396 jstring jtopic, // subs topic
397 jstring jtname, // subs typeName
398 jint jbufflen, // length of buffer
399 jobject obj_msg, // messageData instance
401 jobject jminSeparation,
402 jobject obj_callback,
410 ORTESubscription *s = 0;
412 SubscriptionMode smode;
413 SubscriptionType stype;
415 NtpTime minSeparation;
417 JORTECallbackContext_t *callback_cont;
418 // standart variables
419 const char *topic = 0;
420 const char *typename = 0;
424 // don't forget use free() funct.!!
425 callback_cont = (JORTECallbackContext_t*)malloc(sizeof(JORTECallbackContext_t));
430 buffer = (char*) malloc((int32_t) jbufflen);
431 // check obj_callback
432 if (obj_callback == 0)
435 printf(":!c: obj_callback = NULL \n");
440 jint b = (*env)->GetJavaVM(env,&jvm);
444 printf(":!c: getJavaVM() failed! \n");
451 printf(":c: getJavaVM succesfull.. \n");
454 callback_cont->jvm = jvm;
455 // create global references
456 callback_cont->obj = (*env)->NewGlobalRef(env, obj_callback);
458 if (callback_cont->obj == 0)
461 printf(":c: global reference not created! \n");
465 // create global references
466 callback_cont->msg = (*env)->NewGlobalRef(env, obj_msg);
468 if (callback_cont->msg == 0)
471 printf(":c: global reference not created! \n");
475 // init RecvInfo pointer
476 callback_cont->rinfo = 0;
477 callback_cont->bbuff = 0;
479 cls = (*env)->GetObjectClass(env, obj);
483 printf(":!c: cls = NULL \n");
487 // fieldID - callbackContextHandle
488 fid = (*env)->GetFieldID(env,
490 "callbackContextHandle",
495 printf(":!c: fid = NULL \n");
499 (*env)->SetIntField(env,
502 (int) callback_cont);
504 printf(":c: ORTESubscriptionCreate() calling..\n");
507 d = (ORTEDomain *) dhandle;
511 printf(":!c: d = NULL [bad domain handle] \n");
516 smode = (SubscriptionMode) jsmode;
517 stype = (SubscriptionType) jstype;
518 topic = (*env)->GetStringUTFChars(env, jtopic, 0);
519 typename = (*env)->GetStringUTFChars(env, jtname, 0);
520 deadline = getNtpTime(env, jdeadline);//
521 minSeparation = getNtpTime(env, jminSeparation);//
522 // call ORTE function
523 s = ORTESubscriptionCreate(d,
532 (void*)callback_cont,
533 (uint32_t) j_multicastIP);
537 printf(":!c: s = NULL [subscription not created] \n");
547 (*env)->ReleaseStringUTFChars(env, jtopic, topic);
548 (*env)->ReleaseStringUTFChars(env, jtname, typename);
549 // returns handle of new created Subscription
550 if(flag_ok == 0) return 0;