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!
58 jmethodID mid_callback = 0;
65 JORTECallbackContext_t *callback_cont = (JORTECallbackContext_t*)recvCallBackParam;
68 printf("\n\n:c: --------------- recvCallBack called.. --------------- \n");
73 // set local variables from struct
74 if(callback_cont->jvm == 0)
77 printf(":!c: jvm = NULL \n");
81 jvm = callback_cont->jvm;
83 (*jvm)->AttachCurrentThread(jvm, (void **)&env, NULL);
87 printf(":!c: env = NULL \n");
92 if(callback_cont->obj == 0)
95 printf(":!c: obj = NULL \n");
100 obj = callback_cont->obj;
101 rinfo = callback_cont->rinfo;
102 obj_msg = callback_cont->msg;
107 printf(":c: env = %p, obj_msg = %p \n", env, obj_msg);
115 cls = findClass(env, "org.ocera.orte.types.RecvInfo");
119 printf(":!c: cls = NULL \n");
123 // call object constructor
124 mid = (*env)->GetMethodID(env, cls, "<init>", "()V");
128 printf(":!c: constructor failed! \n");
133 rinfo = (*env)->NewObject(env, cls, mid);
137 printf(":!c: rinfo = NULL \n");
141 // create global reference
142 callback_cont->rinfo = (*env)->NewGlobalRef(env, rinfo);
143 if (callback_cont->rinfo == 0)
146 printf(":!c: callback_cont->rinfo = NULL \n");
151 ////////////////////////////////////////////////////
152 // set RecvInfo instance
153 if(setRecvInfo(env,info,callback_cont->rinfo) == 0)
156 printf(":!c: setRecvInfo() failed! \n");
160 ////////////////////////////////////////////////////
161 // control print - only in TEST_STAGE
163 printf(":c: rinfo created :] \n");
164 printf(":c:----- ORTERecvInfo members ------ \n");
165 printf(":c: recvStatus: %d \n", info->status);
166 printf(":c: senderGuid: hid = %d, aid = %d, oid = %d \n",
167 info->senderGUID.hid,info->senderGUID.aid,info->senderGUID.oid);
168 printf(":c: topic: %s \n",info->topic);
169 printf(":c: type: %s \n",info->type);
170 printf(":c: localTimeRecv: sec = %d, fract = %d \n",
171 info->localTimeReceived.seconds,info->localTimeReceived.fraction);
172 printf(":c: remoteTimePub: sec = %d, fract = %d \n",
173 info->remoteTimePublished.seconds,info->remoteTimePublished.fraction);
174 printf(":c: seqNr: high = %d, low = %d \n",info->sn.high,info->sn.low);
175 printf(":c:---------------------------------- \n");
177 ////////////////////////////////////////////////////
178 // update MessageData instance
180 cls_msg = (*env)->GetObjectClass(env, obj_msg);
184 printf(":!c: cls_msg = NULL \n");
188 /////////////////////////////////////////////////////
190 mid = (*env)->GetMethodID(env,
197 printf(":!c: mid = NULL \n");
202 (*env)->CallVoidMethod(env,
206 /* *************************** *
207 * call JAVA CallBack method *
208 * *************************** */
210 printf(":c: call JAVA CallBack method \n");
215 cls = (*env)->GetObjectClass(env,callback_cont->obj);
219 printf(":!c: cls = NULL \n");
224 mid = (*env)->GetMethodID(env,
227 "(Lorg/ocera/orte/types/RecvInfo;Lorg/ocera/orte/types/MessageData;)V");
231 printf(":!c: cls = NULL \n");
238 printf(":c: volam callback metodu.. halo jsi tam?? \n");
240 // call object's method
241 (*env)->CallVoidMethod(env,
242 callback_cont->obj, /*obj*/
244 callback_cont->rinfo,
250 // detach current thread
251 if((*jvm)->DetachCurrentThread(jvm) != 0)
252 printf(":c!: DetachCurrentThread failed! \n");
255 printf(":c: ------------ thats all from recvCallBack ------------ \n\n");
260 /* ****************************************************************** *
262 * ****************************************************************** */
263 JNIEXPORT jlong JNICALL
264 Java_org_ocera_orte_Subscription_jORTESubscriptionCreate
267 jlong dhandle, // appDomain handle
268 jint jsmode, // subs mode
269 jint jstype, // subs type
270 jstring jtopic, // subs topic
271 jstring jtname, // subs typeName
272 jobject jinstance, // direct ByteBuffer
273 jobject obj_msg, // messageData instance
275 jobject jminSeparation,
276 jobject obj_callback,
284 ORTESubscription *s = 0;
286 SubscriptionMode smode;
287 SubscriptionType stype;
289 NtpTime minSeparation;
291 JORTECallbackContext_t *callback_cont;
292 // standart variables
293 const char *topic = 0;
294 const char *typename = 0;
298 // don't forget use free() funct.!!
299 callback_cont = (JORTECallbackContext_t*)malloc(sizeof(JORTECallbackContext_t));
304 // get direct ByteBuffer pointer from Java
305 buffer = (*env)->GetDirectBufferAddress(env, jinstance);
306 // check obj_callback
307 if (obj_callback == 0)
310 printf(":!c: obj_callback = NULL \n");
315 jint b = (*env)->GetJavaVM(env,&jvm);
319 printf(":!c: getJavaVM() failed! \n");
326 printf(":c: getJavaVM succesfull.. \n");
329 callback_cont->jvm = jvm;
330 // create global references
331 callback_cont->obj = (*env)->NewGlobalRef(env, obj_callback);
333 if (callback_cont->obj == 0)
336 printf(":c: global reference not created! \n");
340 // create global references
341 callback_cont->msg = (*env)->NewGlobalRef(env, obj_msg);
343 if (callback_cont->msg == 0)
346 printf(":c: global reference not created! \n");
350 // init RecvInfo pointer
351 callback_cont->rinfo = 0;
353 cls = (*env)->GetObjectClass(env, obj);
357 printf(":!c: cls = NULL \n");
361 // fieldID - callbackContextHandle
362 fid = (*env)->GetFieldID(env,
364 "callbackContextHandle",
369 printf(":!c: fid = NULL \n");
373 (*env)->SetLongField(env,
376 (jlong) callback_cont);
378 printf(":c: ORTESubscriptionCreate() calling..\n");
381 d = (ORTEDomain *) dhandle;
385 printf(":!c: d = NULL [bad domain handle] \n");
390 smode = (SubscriptionMode) jsmode;
391 stype = (SubscriptionType) jstype;
392 topic = (*env)->GetStringUTFChars(env, jtopic, 0);
393 typename = (*env)->GetStringUTFChars(env, jtname, 0);
394 deadline = getNtpTime(env, jdeadline);//
395 minSeparation = getNtpTime(env, jminSeparation);//
396 // call ORTE function
397 s = ORTESubscriptionCreate(d,
406 (void*)callback_cont,
407 (uint32_t) j_multicastIP);
411 printf(":!c: s = NULL [subscription not created] \n");
421 (*env)->ReleaseStringUTFChars(env, jtopic, topic);
422 (*env)->ReleaseStringUTFChars(env, jtname, typename);
423 // returns handle of new created Subscription
424 if(flag_ok == 0) return 0;