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