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.
32 // library header file's path
34 // pregenerated header
35 #include "jorte/org_ocera_orte_Subscription.h"
36 // enable TEST_STAGE run level
37 #include "jorte/4all.h"
39 #include "jorte/jorte_typedefs_defines.h"
40 #include "jorte/jorte_protos_api.h"
42 /* ****************************************************************** *
44 * ****************************************************************** */
47 recvCallBack(const ORTERecvInfo *info,void *vinstance, void *recvCallBackParam)
52 jclass cls = 0; // local reference!
58 jmethodID mid_callback = 0;
60 JORTECallbackContext_t *callback_cont = (JORTECallbackContext_t*)recvCallBackParam;
63 printf("\n\n:c: --------------- recvCallBack called.. --------------- \n");
68 // set local variables from struct
69 if(callback_cont->jvm == 0)
72 printf(":!c: jvm = NULL \n");
76 jvm = callback_cont->jvm;
78 (*jvm)->AttachCurrentThread(jvm, (void **)&env, NULL);
82 printf(":!c: env = NULL \n");
87 if(callback_cont->obj == 0)
90 printf(":!c: obj = NULL \n");
95 obj = callback_cont->obj;
96 rinfo = callback_cont->rinfo;
97 obj_msg = callback_cont->msg;
102 printf(":c: env = %#"PRIxPTR", obj_msg = %#"PRIxPTR" \n", (intptr_t)env, (intptr_t)obj_msg);
110 cls = findClass(env, "org.ocera.orte.types.RecvInfo");
114 printf(":!c: cls = NULL \n");
118 // call object constructor
119 mid = (*env)->GetMethodID(env, cls, "<init>", "()V");
123 printf(":!c: constructor failed! \n");
128 rinfo = (*env)->NewObject(env, cls, mid);
132 printf(":!c: rinfo = NULL \n");
136 // create global reference
137 callback_cont->rinfo = (*env)->NewGlobalRef(env, rinfo);
138 if (callback_cont->rinfo == 0)
141 printf(":!c: callback_cont->rinfo = NULL \n");
146 ////////////////////////////////////////////////////
147 // set RecvInfo instance
148 if(setRecvInfo(env,info,callback_cont->rinfo) == 0)
151 printf(":!c: setRecvInfo() failed! \n");
155 ////////////////////////////////////////////////////
156 // control print - only in TEST_STAGE
158 printf(":c: rinfo created :] \n");
159 printf(":c:----- ORTERecvInfo members ------ \n");
160 printf(":c: recvStatus: %#x \n", info->status);
161 printf(":c: senderGuid: hid = %#"PRIx32", aid = %#"PRIx32", oid = %#"PRIx32" \n",
162 info->senderGUID.hid,info->senderGUID.aid,info->senderGUID.oid);
163 printf(":c: topic: %s \n",info->topic);
164 printf(":c: type: %s \n",info->type);
165 printf(":c: localTimeRecv: sec = %"PRId32", fract = %"PRIu32" \n",
166 info->localTimeReceived.seconds,info->localTimeReceived.fraction);
167 printf(":c: remoteTimePub: sec = %"PRId32", fract = %"PRIu32" \n",
168 info->remoteTimePublished.seconds,info->remoteTimePublished.fraction);
169 printf(":c: seqNr: high = %"PRId32", low = %"PRIu32" \n",info->sn.high,info->sn.low);
170 printf(":c:---------------------------------- \n");
172 ////////////////////////////////////////////////////
173 // update MessageData instance
175 cls_msg = (*env)->GetObjectClass(env, obj_msg);
179 printf(":!c: cls_msg = NULL \n");
183 /////////////////////////////////////////////////////
185 mid = (*env)->GetMethodID(env,
192 printf(":!c: mid = NULL \n");
197 (*env)->CallVoidMethod(env,
201 /* *************************** *
202 * call JAVA CallBack method *
203 * *************************** */
205 printf(":c: call JAVA CallBack method \n");
210 cls = (*env)->GetObjectClass(env,callback_cont->obj);
214 printf(":!c: cls = NULL \n");
219 mid = (*env)->GetMethodID(env,
222 "(Lorg/ocera/orte/types/RecvInfo;Lorg/ocera/orte/types/MessageData;)V");
226 printf(":!c: cls = NULL \n");
233 printf(":c: volam callback metodu.. halo jsi tam?? \n");
235 // call object's method
236 (*env)->CallVoidMethod(env,
237 callback_cont->obj, /*obj*/
239 callback_cont->rinfo,
243 // detach current thread
244 if((*jvm)->DetachCurrentThread(jvm) != 0)
245 printf(":c!: DetachCurrentThread failed! \n");
248 printf(":c: ------------ thats all from recvCallBack ------------ \n\n");
253 /* ****************************************************************** *
255 * ****************************************************************** */
256 JNIEXPORT jlong JNICALL
257 Java_org_ocera_orte_Subscription_jORTESubscriptionCreate
260 jlong dhandle, // appDomain handle
261 jint jsmode, // subs mode
262 jint jstype, // subs type
263 jstring jtopic, // subs topic
264 jstring jtname, // subs typeName
265 jobject jinstance, // direct ByteBuffer
266 jobject obj_msg, // messageData instance
268 jobject jminSeparation,
269 jobject obj_callback,
277 ORTESubscription *s = 0;
279 SubscriptionMode smode;
280 SubscriptionType stype;
282 NtpTime minSeparation;
284 JORTECallbackContext_t *callback_cont;
285 // standart variables
286 const char *topic = 0;
287 const char *typename = 0;
291 // don't forget use free() funct.!!
292 callback_cont = (JORTECallbackContext_t*)malloc(sizeof(JORTECallbackContext_t));
297 // get direct ByteBuffer pointer from Java
298 buffer = (*env)->GetDirectBufferAddress(env, jinstance);
299 // check obj_callback
300 if (obj_callback == 0)
303 printf(":!c: obj_callback = NULL \n");
308 jint b = (*env)->GetJavaVM(env,&jvm);
312 printf(":!c: getJavaVM() failed! \n");
319 printf(":c: getJavaVM succesfull.. \n");
322 callback_cont->jvm = jvm;
323 // create global references
324 callback_cont->obj = (*env)->NewGlobalRef(env, obj_callback);
326 if (callback_cont->obj == 0)
329 printf(":c: global reference not created! \n");
333 // create global references
334 callback_cont->msg = (*env)->NewGlobalRef(env, obj_msg);
336 if (callback_cont->msg == 0)
339 printf(":c: global reference not created! \n");
343 // init RecvInfo pointer
344 callback_cont->rinfo = 0;
346 cls = (*env)->GetObjectClass(env, obj);
350 printf(":!c: cls = NULL \n");
354 // fieldID - callbackContextHandle
355 fid = (*env)->GetFieldID(env,
357 "callbackContextHandle",
362 printf(":!c: fid = NULL \n");
366 (*env)->SetLongField(env,
369 (jlong) callback_cont);
371 printf(":c: ORTESubscriptionCreate() calling..\n");
374 d = (ORTEDomain *) dhandle;
378 printf(":!c: d = NULL [bad domain handle] \n");
383 smode = (SubscriptionMode) jsmode;
384 stype = (SubscriptionType) jstype;
385 topic = (*env)->GetStringUTFChars(env, jtopic, 0);
386 typename = (*env)->GetStringUTFChars(env, jtname, 0);
387 deadline = getNtpTime(env, jdeadline);//
388 minSeparation = getNtpTime(env, jminSeparation);//
389 // call ORTE function
390 s = ORTESubscriptionCreate(d,
399 (void*)callback_cont,
400 (uint32_t) j_multicastIP);
404 printf(":!c: s = NULL [subscription not created] \n");
414 (*env)->ReleaseStringUTFChars(env, jtopic, topic);
415 (*env)->ReleaseStringUTFChars(env, jtname, typename);
416 // returns handle of new created Subscription
417 if(flag_ok == 0) return 0;