Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add/update copyright notices.
[simgrid.git] / src / bindings / java / jmsg_host.c
1 /* Functions related to the java host instances.                            */
2
3 /* Copyright (c) 2007-2014. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include "xbt/str.h"
10 #include "msg/msg.h"
11 #include "jmsg.h"
12 #include "jmsg_host.h"
13 #include "jxbt_utilities.h"
14
15 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
16
17 static jmethodID jhost_method_Host_constructor;
18 static jfieldID jhost_field_Host_bind;
19 static jfieldID jhost_field_Host_name;
20
21
22 jobject jhost_new_instance(JNIEnv * env) {
23   jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
24   return (*env)->NewObject(env, cls, jhost_method_Host_constructor);
25 }
26
27 jobject jhost_ref(JNIEnv * env, jobject jhost) {
28   return (*env)->NewGlobalRef(env, jhost);
29 }
30
31 void jhost_unref(JNIEnv * env, jobject jhost) {
32   (*env)->DeleteGlobalRef(env, jhost);
33 }
34
35 void jhost_bind(jobject jhost, msg_host_t host, JNIEnv * env) {
36   (*env)->SetLongField(env, jhost, jhost_field_Host_bind, (jlong) (long) (host));
37 }
38
39 msg_host_t jhost_get_native(JNIEnv * env, jobject jhost) {
40   return (msg_host_t) (long) (*env)->GetLongField(env, jhost, jhost_field_Host_bind);
41 }
42
43 const char *jhost_get_name(jobject jhost, JNIEnv * env) {
44   msg_host_t host = jhost_get_native(env, jhost);
45   return MSG_host_get_name(host);
46 }
47
48 jboolean jhost_is_valid(jobject jhost, JNIEnv * env) {
49   if ((*env)->GetLongField(env, jhost, jhost_field_Host_bind)) {
50     return JNI_TRUE;
51   } else {
52     return JNI_FALSE;
53   }
54 }
55
56 JNIEXPORT void JNICALL
57 Java_org_simgrid_msg_Host_nativeInit(JNIEnv *env, jclass cls) {
58   jclass class_Host = (*env)->FindClass(env, "org/simgrid/msg/Host");
59   jhost_method_Host_constructor = (*env)->GetMethodID(env, class_Host, "<init>", "()V");
60   jhost_field_Host_bind = jxbt_get_jfield(env,class_Host, "bind", "J");
61   jhost_field_Host_name = jxbt_get_jfield(env, class_Host, "name", "Ljava/lang/String;");
62   if (!class_Host || !jhost_field_Host_name || !jhost_method_Host_constructor || !jhost_field_Host_bind) {
63     jxbt_throw_native(env,bprintf("Can't find some fields in Java class. You should report this bug."));
64   }
65 }
66 JNIEXPORT jobject JNICALL
67 Java_org_simgrid_msg_Host_getByName(JNIEnv * env, jclass cls,
68                                          jstring jname) {
69   msg_host_t host;                /* native host                                          */
70   jobject jhost;                /* global reference to the java host instance returned  */
71
72   /* get the C string from the java string */
73   const char *name = (*env)->GetStringUTFChars(env, jname, 0);
74   if (name == NULL) {
75         jxbt_throw_null(env,bprintf("No host can have a null name"));
76         return NULL;
77   }
78   /* get the host by name       (the hosts are created during the grid resolution) */
79   host = MSG_get_host_by_name(name);
80
81   if (!host) {                  /* invalid name */
82     jxbt_throw_host_not_found(env, name);
83     (*env)->ReleaseStringUTFChars(env, jname, name);
84     return NULL;
85   }
86   (*env)->ReleaseStringUTFChars(env, jname, name);
87
88   if (!MSG_host_get_data(host)) {       /* native host not associated yet with java host */
89
90     /* Instantiate a new java host */
91     jhost = jhost_new_instance(env);
92
93     if (!jhost) {
94       jxbt_throw_jni(env, "java host instantiation failed");
95       return NULL;
96     }
97
98     /* get a global reference to the newly created host */
99     jhost = jhost_ref(env, jhost);
100
101     if (!jhost) {
102       jxbt_throw_jni(env, "new global ref allocation failed");
103       return NULL;
104     }
105     /* Sets the java host name */
106     (*env)->SetObjectField(env, jhost, jhost_field_Host_name, jname);
107     /* bind the java host and the native host */
108     jhost_bind(jhost, host, env);
109
110     /* the native host data field is set with the global reference to the
111      * java host returned by this function
112      */
113     MSG_host_set_data(host, (void *) jhost);
114   }
115
116   /* return the global reference to the java host instance */
117   return (jobject) MSG_host_get_data(host);
118 }
119
120 JNIEXPORT jobject JNICALL
121 Java_org_simgrid_msg_Host_currentHost(JNIEnv * env, jclass cls) {
122   jobject jhost;
123
124   msg_host_t host = MSG_host_self();
125
126   if (!MSG_host_get_data(host)) {
127     /* the native host not yet associated with the java host instance */
128
129     /* instanciate a new java host instance */
130     jhost = jhost_new_instance(env);
131
132     if (!jhost) {
133       jxbt_throw_jni(env, "java host instantiation failed");
134       return NULL;
135     }
136
137     /* get a global reference to the newly created host */
138     jhost = jhost_ref(env, jhost);
139
140     if (!jhost) {
141       jxbt_throw_jni(env, "global ref allocation failed");
142       return NULL;
143     }
144     /* Sets the host name */
145     const char *name = MSG_host_get_name(host);
146     jobject jname = (*env)->NewStringUTF(env,name);
147     (*env)->SetObjectField(env, jhost, jhost_field_Host_name, jname);
148     /* Bind & store it */
149     jhost_bind(jhost, host, env);
150     MSG_host_set_data(host, (void *) jhost);
151   } else {
152     jhost = (jobject) MSG_host_get_data(host);
153   }
154
155   return jhost;
156 }
157
158 JNIEXPORT void JNICALL
159 Java_org_simgrid_msg_Host_on(JNIEnv *env, jobject jhost) {
160   msg_host_t host = jhost_get_native(env, jhost);
161   MSG_host_on(host);
162 }
163
164 JNIEXPORT void JNICALL
165 Java_org_simgrid_msg_Host_off(JNIEnv *env, jobject jhost) {
166   msg_host_t host = jhost_get_native(env, jhost);
167   MSG_host_off(host);
168 }
169
170 JNIEXPORT jint JNICALL
171 Java_org_simgrid_msg_Host_getCount(JNIEnv * env, jclass cls) {
172   xbt_dynar_t hosts =  MSG_hosts_as_dynar();
173   int nb_host = xbt_dynar_length(hosts);
174   xbt_dynar_free(&hosts);
175   return (jint) nb_host;
176 }
177
178 JNIEXPORT jdouble JNICALL
179 Java_org_simgrid_msg_Host_getSpeed(JNIEnv * env,
180                                         jobject jhost) {
181   msg_host_t host = jhost_get_native(env, jhost);
182
183   if (!host) {
184     jxbt_throw_notbound(env, "host", jhost);
185     return -1;
186   }
187
188   return (jdouble) MSG_get_host_speed(host);
189 }
190
191 JNIEXPORT jdouble JNICALL
192 Java_org_simgrid_msg_Host_getCoreNumber(JNIEnv * env,
193                                         jobject jhost) {
194   msg_host_t host = jhost_get_native(env, jhost);
195
196   if (!host) {
197     jxbt_throw_notbound(env, "host", jhost);
198     return -1;
199   }
200
201   return (jdouble) MSG_host_get_core_number(host);
202 }
203
204 JNIEXPORT jint JNICALL
205 Java_org_simgrid_msg_Host_getLoad(JNIEnv * env, jobject jhost) {
206   msg_host_t host = jhost_get_native(env, jhost);
207
208   if (!host) {
209     jxbt_throw_notbound(env, "host", jhost);
210     return -1;
211   }
212
213   return (jint) MSG_get_host_msgload(host);
214 }
215 JNIEXPORT jobject JNICALL
216 Java_org_simgrid_msg_Host_getProperty(JNIEnv *env, jobject jhost, jobject jname) {
217   msg_host_t host = jhost_get_native(env, jhost);
218
219   if (!host) {
220     jxbt_throw_notbound(env, "host", jhost);
221     return NULL;
222   }
223   const char *name = (*env)->GetStringUTFChars(env, jname, 0);
224
225   const char *property = MSG_host_get_property_value(host, name);
226   if (!property) {
227     return NULL;
228   }
229
230   jobject jproperty = (*env)->NewStringUTF(env, property);
231
232   (*env)->ReleaseStringUTFChars(env, jname, name);
233
234   return jproperty;
235 }
236
237 JNIEXPORT void JNICALL
238 Java_org_simgrid_msg_Host_setProperty(JNIEnv *env, jobject jhost, jobject jname, jobject jvalue) {
239   msg_host_t host = jhost_get_native(env, jhost);
240
241   if (!host) {
242     jxbt_throw_notbound(env, "host", jhost);
243     return;
244   }
245   const char *name = (*env)->GetStringUTFChars(env, jname, 0);
246   const char *value_java = (*env)->GetStringUTFChars(env, jvalue, 0);
247   char *value = strdup(value_java);
248
249   MSG_host_set_property_value(host,name,value,xbt_free);
250
251   (*env)->ReleaseStringUTFChars(env, jvalue, value);
252   (*env)->ReleaseStringUTFChars(env, jname, name);
253
254 }
255 JNIEXPORT jboolean JNICALL
256 Java_org_simgrid_msg_Host_isAvail(JNIEnv * env, jobject jhost) {
257   msg_host_t host = jhost_get_native(env, jhost);
258
259   if (!host) {
260     jxbt_throw_notbound(env, "host", jhost);
261     return 0;
262   }
263
264   return (jboolean) MSG_host_is_avail(host);
265 }
266
267 JNIEXPORT jobjectArray JNICALL
268 Java_org_simgrid_msg_Host_all(JNIEnv * env, jclass cls_arg)
269 {
270   int index;
271   jobjectArray jtable;
272   jobject jhost;
273   jstring jname;
274   msg_host_t host;
275
276   xbt_dynar_t table =  MSG_hosts_as_dynar();
277   int count = xbt_dynar_length(table);
278
279   jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
280
281   if (!cls) {
282     return NULL;
283   }
284
285   jtable = (*env)->NewObjectArray(env, (jsize) count, cls, NULL);
286
287   if (!jtable) {
288     jxbt_throw_jni(env, "Hosts table allocation failed");
289     return NULL;
290   }
291
292   for (index = 0; index < count; index++) {
293     host = xbt_dynar_get_as(table,index,msg_host_t);
294     jhost = (jobject) (MSG_host_get_data(host));
295
296     if (!jhost) {
297       jname = (*env)->NewStringUTF(env, MSG_host_get_name(host));
298
299       jhost =
300                 Java_org_simgrid_msg_Host_getByName(env, cls_arg, jname);
301       /* FIXME: leak of jname ? */
302     }
303
304     (*env)->SetObjectArrayElement(env, jtable, index, jhost);
305   }
306   xbt_dynar_free(&table);
307   return jtable;
308 }
309
310 JNIEXPORT void JNICALL 
311 Java_org_simgrid_msg_Host_setAsyncMailbox(JNIEnv * env, jclass cls_arg, jobject jname){
312
313   const char *name = (*env)->GetStringUTFChars(env, jname, 0);
314   MSG_mailbox_set_async(name);
315   (*env)->ReleaseStringUTFChars(env, jname, name);
316
317 }