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!
57 jmethodID mid_callback = 0;
59 JORTECallbackContext_t *callback_cont = (JORTECallbackContext_t*)recvCallBackParam;
62 printf("\n\n:c: --------------- recvCallBack called.. --------------- \n");
67 // set local variables from struct
68 if(callback_cont->jvm == 0)
71 printf(":!c: jvm = NULL \n");
75 jvm = callback_cont->jvm;
77 (*jvm)->AttachCurrentThread(jvm, (void **)&env, NULL);
81 printf(":!c: env = NULL \n");
86 if(callback_cont->obj == 0)
89 printf(":!c: obj = NULL \n");
94 rinfo = callback_cont->rinfo;
95 obj_msg = callback_cont->msg;
100 printf(":c: env = %#"PRIxPTR", obj_msg = %#"PRIxPTR" \n", (intptr_t)env, (intptr_t)obj_msg);
108 cls = findClass(env, "org.ocera.orte.types.RecvInfo");
112 printf(":!c: cls = NULL \n");
116 // call object constructor
117 mid = (*env)->GetMethodID(env, cls, "<init>", "()V");
121 printf(":!c: constructor failed! \n");
126 rinfo = (*env)->NewObject(env, cls, mid);
130 printf(":!c: rinfo = NULL \n");
134 // create global reference
135 callback_cont->rinfo = (*env)->NewGlobalRef(env, rinfo);
136 if (callback_cont->rinfo == 0)
139 printf(":!c: callback_cont->rinfo = NULL \n");
144 ////////////////////////////////////////////////////
145 // set RecvInfo instance
146 if(setRecvInfo(env,info,callback_cont->rinfo) == 0)
149 printf(":!c: setRecvInfo() failed! \n");
153 ////////////////////////////////////////////////////
154 // control print - only in TEST_STAGE
156 printf(":c: rinfo created :] \n");
157 printf(":c:----- ORTERecvInfo members ------ \n");
158 printf(":c: recvStatus: %#x \n", info->status);
159 printf(":c: senderGuid: hid = %#"PRIx32", aid = %#"PRIx32", oid = %#"PRIx32" \n",
160 info->senderGUID.hid,info->senderGUID.aid,info->senderGUID.oid);
161 printf(":c: topic: %s \n",info->topic);
162 printf(":c: type: %s \n",info->type);
163 printf(":c: localTimeRecv: sec = %"PRId32", fract = %"PRIu32" \n",
164 info->localTimeReceived.seconds,info->localTimeReceived.fraction);
165 printf(":c: remoteTimePub: sec = %"PRId32", fract = %"PRIu32" \n",
166 info->remoteTimePublished.seconds,info->remoteTimePublished.fraction);
167 printf(":c: seqNr: high = %"PRId32", low = %"PRIu32" \n",info->sn.high,info->sn.low);
168 printf(":c:---------------------------------- \n");
170 ////////////////////////////////////////////////////
171 // update MessageData instance
173 cls_msg = (*env)->GetObjectClass(env, obj_msg);
177 printf(":!c: cls_msg = NULL \n");
181 /////////////////////////////////////////////////////
183 mid = (*env)->GetMethodID(env,
190 printf(":!c: mid = NULL \n");
195 (*env)->CallVoidMethod(env,
199 /* *************************** *
200 * call JAVA CallBack method *
201 * *************************** */
203 printf(":c: call JAVA CallBack method \n");
208 cls = (*env)->GetObjectClass(env,callback_cont->obj);
212 printf(":!c: cls = NULL \n");
217 mid = (*env)->GetMethodID(env,
220 "(Lorg/ocera/orte/types/RecvInfo;Lorg/ocera/orte/types/MessageData;)V");
224 printf(":!c: cls = NULL \n");
231 printf(":c: volam callback metodu.. halo jsi tam?? \n");
233 // call object's method
234 (*env)->CallVoidMethod(env,
235 callback_cont->obj, /*obj*/
237 callback_cont->rinfo,
241 // detach current thread
242 if((*jvm)->DetachCurrentThread(jvm) != 0)
243 printf(":c!: DetachCurrentThread failed! \n");
246 printf(":c: ------------ thats all from recvCallBack ------------ \n\n");
251 /* ****************************************************************** *
253 * ****************************************************************** */
254 JNIEXPORT jlong JNICALL
255 Java_org_ocera_orte_Subscription_jORTESubscriptionCreate
258 jlong dhandle, // appDomain handle
259 jint jsmode, // subs mode
260 jint jstype, // subs type
261 jstring jtopic, // subs topic
262 jstring jtname, // subs typeName
263 jobject jinstance, // direct ByteBuffer
264 jobject obj_msg, // messageData instance
266 jobject jminSeparation,
267 jobject obj_callback,
275 ORTESubscription *s = 0;
277 SubscriptionMode smode;
278 SubscriptionType stype;
280 NtpTime minSeparation;
282 JORTECallbackContext_t *callback_cont;
283 // standart variables
284 const char *topic = 0;
285 const char *typename = 0;
289 // don't forget use free() funct.!!
290 callback_cont = (JORTECallbackContext_t*)malloc(sizeof(JORTECallbackContext_t));
295 // get direct ByteBuffer pointer from Java
296 buffer = (*env)->GetDirectBufferAddress(env, jinstance);
297 // check obj_callback
298 if (obj_callback == 0)
301 printf(":!c: obj_callback = NULL \n");
306 jint b = (*env)->GetJavaVM(env,&jvm);
310 printf(":!c: getJavaVM() failed! \n");
317 printf(":c: getJavaVM succesfull.. \n");
320 callback_cont->jvm = jvm;
321 // create global references
322 callback_cont->obj = (*env)->NewGlobalRef(env, obj_callback);
324 if (callback_cont->obj == 0)
327 printf(":c: global reference not created! \n");
331 // create global references
332 callback_cont->msg = (*env)->NewGlobalRef(env, obj_msg);
334 if (callback_cont->msg == 0)
337 printf(":c: global reference not created! \n");
341 // init RecvInfo pointer
342 callback_cont->rinfo = 0;
344 cls = (*env)->GetObjectClass(env, obj);
348 printf(":!c: cls = NULL \n");
352 // fieldID - callbackContextHandle
353 fid = (*env)->GetFieldID(env,
355 "callbackContextHandle",
360 printf(":!c: fid = NULL \n");
364 (*env)->SetLongField(env,
367 (jlong) callback_cont);
369 printf(":c: ORTESubscriptionCreate() calling..\n");
372 d = (ORTEDomain *) dhandle;
376 printf(":!c: d = NULL [bad domain handle] \n");
381 smode = (SubscriptionMode) jsmode;
382 stype = (SubscriptionType) jstype;
383 topic = (*env)->GetStringUTFChars(env, jtopic, 0);
384 typename = (*env)->GetStringUTFChars(env, jtname, 0);
385 deadline = getNtpTime(env, jdeadline);//
386 minSeparation = getNtpTime(env, jminSeparation);//
387 // call ORTE function
388 s = ORTESubscriptionCreate(d,
397 (void*)callback_cont,
398 (uint32_t) j_multicastIP);
402 printf(":!c: s = NULL [subscription not created] \n");
412 (*env)->ReleaseStringUTFChars(env, jtopic, topic);
413 (*env)->ReleaseStringUTFChars(env, jtname, typename);
414 // returns handle of new created Subscription
415 if(flag_ok == 0) return 0;