]> rtime.felk.cvut.cz Git - orte.git/blob - orte/libjorte/JORTEPublicationCreate.c
JORTE: add 64-bit support
[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  jint      jbufflen,
73  jobject   j_instance,
74  jobject   j_persistence,
75  jint      j_strength)
76 {
77   ORTEPublication  *p = 0;
78   ORTEDomain       *d;
79   const char       *topic;
80   const char       *typeName;
81   char             *buffer;
82   NtpTime           persistence;
83   int               strength;
84   int               flag_ok = 0;
85
86   // check domain handle
87   d = (ORTEDomain *) dom_handle;
88   if(d == 0)
89   {
90     printf(":!c: publication create failed! [bad domain handle] \n");
91     return 0;
92   }
93   // get topic
94   topic = (*env)->GetStringUTFChars(env, j_topic, 0);
95   if(topic == 0)
96   {
97     // OutOfMemoryError already thrown
98     #ifdef TEST_STAGE
99       printf(":!c: topic = NULL \n");
100     #endif
101     return 0;
102   }
103   // get typeName
104   typeName = (*env)->GetStringUTFChars(env, j_type_name, 0);
105   if(typeName == 0)
106   {
107     // OutOfMemoryError already thrown
108     #ifdef TEST_STAGE
109       printf(":!c: typeName = NULL \n");
110     #endif
111     // free memory
112     (*env)->ReleaseStringUTFChars(env, j_topic, topic);
113     return 0;
114   }
115
116   do
117   {
118     // get persistence
119     persistence = getNtpTime(env, j_persistence);
120     // get strenght
121     strength = (int) j_strength;
122     // create buffer
123     buffer = (char*) malloc((int32_t) jbufflen);
124     if(buffer == 0)
125     {
126       printf(":!c: buffer create failed! \n");
127       break;
128     }
129     // call ORTE function
130     p = ORTEPublicationCreate(d,
131                               topic,
132                               typeName,
133                               (void *) buffer,
134                               &persistence,
135                               strength,
136                               sendCallBack, // BUDE NULL!!
137                               j_instance,
138                               NULL);
139     if(p == 0)
140     {
141       printf(":!c: publication create failed! \n");
142       break;
143     }
144     // set flag
145     flag_ok = 1;
146   }  while(0);
147
148   // free memory in every case
149   (*env)->ReleaseStringUTFChars(env, j_topic, topic);
150   (*env)->ReleaseStringUTFChars(env, j_type_name, typeName);
151   //
152   if (flag_ok == 0)
153   {
154     // free memory
155     if(buffer != 0) free(buffer);
156     return 0;
157   }
158   return ((jlong) p);
159
160 }