]> rtime.felk.cvut.cz Git - orte.git/blob - orte/libjorte/JORTEPublicationCreate.c
JORTE: switch to direct ByteBuffer
[orte.git] / orte / libjorte / JORTEPublicationCreate.c
1 /* JORTEPublicationCreate.c */
2
3 /**
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')
8   *
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
13   * @version 0.1
14   *
15   *
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.
20   *
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.
25   *
26   */
27
28 #include <stdlib.h>
29 // origin orte headers
30 #include "orte.h"
31 #include "orte_all.h"
32 // pregenerated header
33 #include "jorte/org_ocera_orte_Publication.h"
34 #include "jorte/jorte_protos_api.h"
35 #include "jorte/4all.h"
36
37 // ### DOCASNE ##################################################################
38
39 // ### DOCASNE ##################################################################
40 int counter = 0;
41
42 void
43 sendCallBack(const ORTESendInfo *info,void *vinstance, void *sendCallBackParam)
44 {
45   char *instance=(char*)vinstance;
46
47   printf(":c: zacatek sendCallBack()..\n");
48
49   switch (info->status) {
50     case NEED_DATA:
51       printf(":c:PUB: Sampling publication, count %d\n", counter++);
52       printf(":c:PUB: !! DOCASNE - ZRUSIT!! - callback() u Publishera \n");
53           //sprintf(instance,"Hello Universe! (%d)",counter++);
54       break;
55     case CQL:  //criticalQueueLevel
56       break;
57   }
58 }
59 // ### DOCASNE ##################################################################
60
61 // ### DOCASNE ##################################################################
62
63
64 // native method
65 JNIEXPORT jlong JNICALL
66 Java_org_ocera_orte_Publication_jORTEPublicationCreate
67 (JNIEnv   *env ,
68  jobject   obj,
69  jlong     dom_handle,
70  jstring   j_topic,
71  jstring   j_type_name,
72  jobject   j_instance,
73  jobject   j_persistence,
74  jint      j_strength)
75 {
76   ORTEPublication  *p = 0;
77   ORTEDomain       *d;
78   const char       *topic;
79   const char       *typeName;
80   void             *buffer;
81   NtpTime           persistence;
82   int               strength;
83   int               flag_ok = 0;
84
85   // check domain handle
86   d = (ORTEDomain *) dom_handle;
87   if(d == 0)
88   {
89     printf(":!c: publication create failed! [bad domain handle] \n");
90     return 0;
91   }
92   // get topic
93   topic = (*env)->GetStringUTFChars(env, j_topic, 0);
94   if(topic == 0)
95   {
96     // OutOfMemoryError already thrown
97     #ifdef TEST_STAGE
98       printf(":!c: topic = NULL \n");
99     #endif
100     return 0;
101   }
102   // get typeName
103   typeName = (*env)->GetStringUTFChars(env, j_type_name, 0);
104   if(typeName == 0)
105   {
106     // OutOfMemoryError already thrown
107     #ifdef TEST_STAGE
108       printf(":!c: typeName = NULL \n");
109     #endif
110     // free memory
111     (*env)->ReleaseStringUTFChars(env, j_topic, topic);
112     return 0;
113   }
114
115   do
116   {
117     // get persistence
118     persistence = getNtpTime(env, j_persistence);
119     // get strenght
120     strength = (int) j_strength;
121     // get direct ByteBuffer pointer from Java
122     buffer = (*env)->GetDirectBufferAddress(env, j_instance);
123     if(buffer == 0)
124     {
125       printf(":!c: buffer create failed! \n");
126       break;
127     }
128     // call ORTE function
129     p = ORTEPublicationCreate(d,
130                               topic,
131                               typeName,
132                               buffer,
133                               &persistence,
134                               strength,
135                               sendCallBack, // BUDE NULL!!
136                               NULL,
137                               NULL);
138     if(p == 0)
139     {
140       printf(":!c: publication create failed! \n");
141       break;
142     }
143     // set flag
144     flag_ok = 1;
145   }  while(0);
146
147   // free memory in every case
148   (*env)->ReleaseStringUTFChars(env, j_topic, topic);
149   (*env)->ReleaseStringUTFChars(env, j_type_name, typeName);
150   //
151   if (flag_ok == 0)
152   {
153     return 0;
154   }
155   return ((jlong) p);
156
157 }