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!
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 obj = callback_cont->obj;
95 rinfo = callback_cont->rinfo;
96 obj_msg = callback_cont->msg;
101 printf(":c: env = %p, obj_msg = %p \n", env, obj_msg);
109 cls = findClass(env, "org.ocera.orte.types.RecvInfo");
113 printf(":!c: cls = NULL \n");
117 // call object constructor
118 mid = (*env)->GetMethodID(env, cls, "<init>", "()V");
122 printf(":!c: constructor failed! \n");
127 rinfo = (*env)->NewObject(env, cls, mid);
131 printf(":!c: rinfo = NULL \n");
135 // create global reference
136 callback_cont->rinfo = (*env)->NewGlobalRef(env, rinfo);
137 if (callback_cont->rinfo == 0)
140 printf(":!c: callback_cont->rinfo = NULL \n");
145 ////////////////////////////////////////////////////
146 // set RecvInfo instance
147 if(setRecvInfo(env,info,callback_cont->rinfo) == 0)
150 printf(":!c: setRecvInfo() failed! \n");
154 ////////////////////////////////////////////////////
155 // control print - only in TEST_STAGE
157 printf(":c: rinfo created :] \n");
158 printf(":c:----- ORTERecvInfo members ------ \n");
159 printf(":c: recvStatus: %d \n", info->status);
160 printf(":c: senderGuid: hid = %d, aid = %d, oid = %d \n",
161 info->senderGUID.hid,info->senderGUID.aid,info->senderGUID.oid);
162 printf(":c: topic: %s \n",info->topic);
163 printf(":c: type: %s \n",info->type);
164 printf(":c: localTimeRecv: sec = %d, fract = %d \n",
165 info->localTimeReceived.seconds,info->localTimeReceived.fraction);
166 printf(":c: remoteTimePub: sec = %d, fract = %d \n",
167 info->remoteTimePublished.seconds,info->remoteTimePublished.fraction);
168 printf(":c: seqNr: high = %d, low = %d \n",info->sn.high,info->sn.low);
169 printf(":c:---------------------------------- \n");
171 ////////////////////////////////////////////////////
172 // update MessageData instance
174 cls_msg = (*env)->GetObjectClass(env, obj_msg);
178 printf(":!c: cls_msg = NULL \n");
182 /////////////////////////////////////////////////////
184 mid = (*env)->GetMethodID(env,
191 printf(":!c: mid = NULL \n");
196 (*env)->CallVoidMethod(env,
200 /* *************************** *
201 * call JAVA CallBack method *
202 * *************************** */
204 printf(":c: call JAVA CallBack method \n");
209 cls = (*env)->GetObjectClass(env,callback_cont->obj);
213 printf(":!c: cls = NULL \n");
218 mid = (*env)->GetMethodID(env,
221 "(Lorg/ocera/orte/types/RecvInfo;Lorg/ocera/orte/types/MessageData;)V");
225 printf(":!c: cls = NULL \n");
232 printf(":c: volam callback metodu.. halo jsi tam?? \n");
234 // call object's method
235 (*env)->CallVoidMethod(env,
236 callback_cont->obj, /*obj*/
238 callback_cont->rinfo,
242 // detach current thread
243 if((*jvm)->DetachCurrentThread(jvm) != 0)
244 printf(":c!: DetachCurrentThread failed! \n");
247 printf(":c: ------------ thats all from recvCallBack ------------ \n\n");
252 /* ****************************************************************** *
254 * ****************************************************************** */
255 JNIEXPORT jlong JNICALL
256 Java_org_ocera_orte_Subscription_jORTESubscriptionCreate
259 jlong dhandle, // appDomain handle
260 jint jsmode, // subs mode
261 jint jstype, // subs type
262 jstring jtopic, // subs topic
263 jstring jtname, // subs typeName
264 jobject jinstance, // direct ByteBuffer
265 jobject obj_msg, // messageData instance
267 jobject jminSeparation,
268 jobject obj_callback,
276 ORTESubscription *s = 0;
278 SubscriptionMode smode;
279 SubscriptionType stype;
281 NtpTime minSeparation;
283 JORTECallbackContext_t *callback_cont;
284 // standart variables
285 const char *topic = 0;
286 const char *typename = 0;
290 // don't forget use free() funct.!!
291 callback_cont = (JORTECallbackContext_t*)malloc(sizeof(JORTECallbackContext_t));
296 // get direct ByteBuffer pointer from Java
297 buffer = (*env)->GetDirectBufferAddress(env, jinstance);
298 // check obj_callback
299 if (obj_callback == 0)
302 printf(":!c: obj_callback = NULL \n");
307 jint b = (*env)->GetJavaVM(env,&jvm);
311 printf(":!c: getJavaVM() failed! \n");
318 printf(":c: getJavaVM succesfull.. \n");
321 callback_cont->jvm = jvm;
322 // create global references
323 callback_cont->obj = (*env)->NewGlobalRef(env, obj_callback);
325 if (callback_cont->obj == 0)
328 printf(":c: global reference not created! \n");
332 // create global references
333 callback_cont->msg = (*env)->NewGlobalRef(env, obj_msg);
335 if (callback_cont->msg == 0)
338 printf(":c: global reference not created! \n");
342 // init RecvInfo pointer
343 callback_cont->rinfo = 0;
345 cls = (*env)->GetObjectClass(env, obj);
349 printf(":!c: cls = NULL \n");
353 // fieldID - callbackContextHandle
354 fid = (*env)->GetFieldID(env,
356 "callbackContextHandle",
361 printf(":!c: fid = NULL \n");
365 (*env)->SetLongField(env,
368 (jlong) callback_cont);
370 printf(":c: ORTESubscriptionCreate() calling..\n");
373 d = (ORTEDomain *) dhandle;
377 printf(":!c: d = NULL [bad domain handle] \n");
382 smode = (SubscriptionMode) jsmode;
383 stype = (SubscriptionType) jstype;
384 topic = (*env)->GetStringUTFChars(env, jtopic, 0);
385 typename = (*env)->GetStringUTFChars(env, jtname, 0);
386 deadline = getNtpTime(env, jdeadline);//
387 minSeparation = getNtpTime(env, jminSeparation);//
388 // call ORTE function
389 s = ORTESubscriptionCreate(d,
398 (void*)callback_cont,
399 (uint32_t) j_multicastIP);
403 printf(":!c: s = NULL [subscription not created] \n");
413 (*env)->ReleaseStringUTFChars(env, jtopic, topic);
414 (*env)->ReleaseStringUTFChars(env, jtname, typename);
415 // returns handle of new created Subscription
416 if(flag_ok == 0) return 0;