Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use MSG_host_by_name() instead of MSG_get_host_by_name()
[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 "simgrid/msg.h"
11 #include "jmsg.h"
12 #include "jmsg_host.h"
13 #include "jxbt_utilities.h"
14 #include "jmsg_storage.h"
15
16
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
18
19 static jmethodID jhost_method_Host_constructor;
20 static jfieldID jhost_field_Host_bind;
21 static jfieldID jhost_field_Host_name;
22
23 jobject jhost_new_instance(JNIEnv * env) {
24   jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
25   return (*env)->NewObject(env, cls, jhost_method_Host_constructor);
26 }
27
28 jobject jhost_ref(JNIEnv * env, jobject jhost) {
29   return (*env)->NewGlobalRef(env, jhost);
30 }
31
32 void jhost_unref(JNIEnv * env, jobject jhost) {
33   (*env)->DeleteGlobalRef(env, jhost);
34 }
35
36 void jhost_bind(jobject jhost, msg_host_t host, JNIEnv * env) {
37   (*env)->SetLongField(env, jhost, jhost_field_Host_bind, (jlong) (long) (host));
38 }
39
40 msg_host_t jhost_get_native(JNIEnv * env, jobject jhost) {
41   return (msg_host_t) (long) (*env)->GetLongField(env, jhost, jhost_field_Host_bind);
42 }
43
44 const char *jhost_get_name(jobject jhost, JNIEnv * env) {
45   msg_host_t host = jhost_get_native(env, jhost);
46   return MSG_host_get_name(host);
47 }
48
49 jboolean jhost_is_valid(jobject jhost, JNIEnv * env) {
50   if ((*env)->GetLongField(env, jhost, jhost_field_Host_bind)) {
51     return JNI_TRUE;
52   } else {
53     return JNI_FALSE;
54   }
55 }
56
57 JNIEXPORT void JNICALL
58 Java_org_simgrid_msg_Host_nativeInit(JNIEnv *env, jclass cls) {
59   jclass class_Host = (*env)->FindClass(env, "org/simgrid/msg/Host");
60   jhost_method_Host_constructor = (*env)->GetMethodID(env, class_Host, "<init>", "()V");
61   jhost_field_Host_bind = jxbt_get_jfield(env,class_Host, "bind", "J");
62   jhost_field_Host_name = jxbt_get_jfield(env, class_Host, "name", "Ljava/lang/String;");
63   if (!class_Host || !jhost_field_Host_name || !jhost_method_Host_constructor || !jhost_field_Host_bind) {
64     jxbt_throw_native(env,bprintf("Can't find some fields in Java class. You should report this bug."));
65   }
66 }
67 JNIEXPORT jobject JNICALL
68 Java_org_simgrid_msg_Host_getByName(JNIEnv * env, jclass cls,
69                                          jstring jname) {
70   msg_host_t host;                /* native host                                          */
71   jobject jhost;                /* global reference to the java host instance returned  */
72
73   /* get the C string from the java string */
74   if (jname == NULL) {
75         jxbt_throw_null(env,bprintf("No host can have a null name"));
76         return NULL;
77   }
78   const char *name = (*env)->GetStringUTFChars(env, jname, 0);
79   /* get the host by name       (the hosts are created during the grid resolution) */
80   host = MSG_host_by_name(name);
81
82   if (!host) {                  /* invalid name */
83     jxbt_throw_host_not_found(env, name);
84     (*env)->ReleaseStringUTFChars(env, jname, name);
85     return NULL;
86   }
87   (*env)->ReleaseStringUTFChars(env, jname, name);
88
89   if (!xbt_lib_get_level(host, JAVA_HOST_LEVEL)) {       /* native host not associated yet with java host */
90
91     /* Instantiate a new java host */
92     jhost = jhost_new_instance(env);
93
94     if (!jhost) {
95       jxbt_throw_jni(env, "java host instantiation failed");
96       return NULL;
97     }
98
99     /* get a global reference to the newly created host */
100     jhost = jhost_ref(env, jhost);
101
102     if (!jhost) {
103       jxbt_throw_jni(env, "new global ref allocation failed");
104       return NULL;
105     }
106     /* Sets the java host name */
107     (*env)->SetObjectField(env, jhost, jhost_field_Host_name, jname);
108     /* bind the java host and the native host */
109     jhost_bind(jhost, host, env);
110
111     /* the native host data field is set with the global reference to the
112      * java host returned by this function
113      */
114     xbt_lib_set(host_lib, host->key, JAVA_HOST_LEVEL, (void *) jhost);
115   }
116
117   /* return the global reference to the java host instance */
118   return (jobject) xbt_lib_get_level(host, JAVA_HOST_LEVEL);
119 }
120
121 JNIEXPORT jobject JNICALL
122 Java_org_simgrid_msg_Host_currentHost(JNIEnv * env, jclass cls) {
123   jobject jhost;
124
125   msg_host_t host = MSG_host_self();
126
127   if (!xbt_lib_get_level(host, JAVA_HOST_LEVEL)) {
128     /* the native host not yet associated with the java host instance */
129
130     /* instanciate a new java host instance */
131     jhost = jhost_new_instance(env);
132
133     if (!jhost) {
134       jxbt_throw_jni(env, "java host instantiation failed");
135       return NULL;
136     }
137
138     /* get a global reference to the newly created host */
139     jhost = jhost_ref(env, jhost);
140
141     if (!jhost) {
142       jxbt_throw_jni(env, "global ref allocation failed");
143       return NULL;
144     }
145     /* Sets the host name */
146     const char *name = MSG_host_get_name(host);
147     jobject jname = (*env)->NewStringUTF(env,name);
148     (*env)->SetObjectField(env, jhost, jhost_field_Host_name, jname);
149     /* Bind & store it */
150     jhost_bind(jhost, host, env);
151     xbt_lib_set(host_lib, host->key, JAVA_HOST_LEVEL, (void *) jhost);
152   } else {
153     jhost = (jobject) xbt_lib_get_level(host, JAVA_HOST_LEVEL);
154   }
155
156   return jhost;
157 }
158
159 JNIEXPORT void JNICALL
160 Java_org_simgrid_msg_Host_on(JNIEnv *env, jobject jhost) {
161   msg_host_t host = jhost_get_native(env, jhost);
162   MSG_host_on(host);
163 }
164
165 JNIEXPORT void JNICALL
166 Java_org_simgrid_msg_Host_off(JNIEnv *env, jobject jhost) {
167   msg_host_t host = jhost_get_native(env, jhost);
168   MSG_host_off(host); 
169 }
170
171 JNIEXPORT jint JNICALL
172 Java_org_simgrid_msg_Host_getCount(JNIEnv * env, jclass cls) {
173   xbt_dynar_t hosts =  MSG_hosts_as_dynar();
174   int nb_host = xbt_dynar_length(hosts);
175   xbt_dynar_free(&hosts);
176   return (jint) nb_host;
177 }
178
179 JNIEXPORT jdouble JNICALL
180 Java_org_simgrid_msg_Host_getSpeed(JNIEnv * env,
181                                         jobject jhost) {
182   msg_host_t host = jhost_get_native(env, jhost);
183
184   if (!host) {
185     jxbt_throw_notbound(env, "host", jhost);
186     return -1;
187   }
188
189   return (jdouble) MSG_get_host_speed(host);
190 }
191
192 JNIEXPORT jdouble JNICALL
193 Java_org_simgrid_msg_Host_getCoreNumber(JNIEnv * env,
194                                         jobject jhost) {
195   msg_host_t host = jhost_get_native(env, jhost);
196
197   if (!host) {
198     jxbt_throw_notbound(env, "host", jhost);
199     return -1;
200   }
201
202   return (jdouble) MSG_host_get_core_number(host);
203 }
204
205 JNIEXPORT jint JNICALL
206 Java_org_simgrid_msg_Host_getLoad(JNIEnv * env, jobject jhost) {
207   msg_host_t host = jhost_get_native(env, jhost);
208
209   if (!host) {
210     jxbt_throw_notbound(env, "host", jhost);
211     return -1;
212   }
213
214   return (jint) MSG_get_host_msgload(host);
215 }
216 JNIEXPORT jobject JNICALL
217 Java_org_simgrid_msg_Host_getProperty(JNIEnv *env, jobject jhost, jobject jname) {
218   msg_host_t host = jhost_get_native(env, jhost);
219
220   if (!host) {
221     jxbt_throw_notbound(env, "host", jhost);
222     return NULL;
223   }
224   const char *name = (*env)->GetStringUTFChars(env, jname, 0);
225
226   const char *property = MSG_host_get_property_value(host, name);
227   if (!property) {
228     return NULL;
229   }
230
231   jobject jproperty = (*env)->NewStringUTF(env, property);
232
233   (*env)->ReleaseStringUTFChars(env, jname, name);
234
235   return jproperty;
236 }
237
238 JNIEXPORT void JNICALL
239 Java_org_simgrid_msg_Host_setProperty(JNIEnv *env, jobject jhost, jobject jname, jobject jvalue) {
240   msg_host_t host = jhost_get_native(env, jhost);
241
242   if (!host) {
243     jxbt_throw_notbound(env, "host", jhost);
244     return;
245   }
246   const char *name = (*env)->GetStringUTFChars(env, jname, 0);
247   const char *value_java = (*env)->GetStringUTFChars(env, jvalue, 0);
248   char *value = xbt_strdup(value_java);
249
250   MSG_host_set_property_value(host, name, value, xbt_free_f);
251
252   (*env)->ReleaseStringUTFChars(env, jvalue, value);
253   (*env)->ReleaseStringUTFChars(env, jname, name);
254
255 }
256 JNIEXPORT jboolean JNICALL
257 Java_org_simgrid_msg_Host_isOn(JNIEnv * env, jobject jhost) {
258   msg_host_t host = jhost_get_native(env, jhost);
259
260   if (!host) {
261     jxbt_throw_notbound(env, "host", jhost);
262     return 0;
263   }
264
265   return (jboolean) MSG_host_is_on(host);
266 }
267
268 JNIEXPORT jobjectArray JNICALL
269 Java_org_simgrid_msg_Host_getMountedStorage(JNIEnv * env, jobject jhost){
270
271   msg_host_t host = jhost_get_native(env, jhost);
272   jobject jstorage;
273   jstring jname;
274
275   if (!host) {
276     jxbt_throw_notbound(env, "host", jhost);
277     return 0;
278   }
279
280   int index = 0;
281         jobjectArray jtable;
282         xbt_dict_t dict =  MSG_host_get_mounted_storage_list(host);
283         int count = xbt_dict_length(dict);
284         jclass cls = (*env)->FindClass(env, "org/simgrid/msg/Storage");
285
286         jtable = (*env)->NewObjectArray(env, (jsize) count, cls, NULL);
287
288         if (!jtable) {
289          jxbt_throw_jni(env, "Storages table allocation failed");
290          return NULL;
291         }
292
293         xbt_dict_cursor_t cursor=NULL;
294         const char *mount_name, *storage_name;
295
296         xbt_dict_foreach(dict,cursor,mount_name,storage_name) {
297                 jname = (*env)->NewStringUTF(env, storage_name);
298           jstorage = Java_org_simgrid_msg_Storage_getByName(env,cls,jname);
299           (*env)->SetObjectArrayElement(env, jtable, index, jstorage);
300     index++;
301         }
302         xbt_dict_free(&dict);
303         return jtable;
304 }
305
306 JNIEXPORT jobjectArray JNICALL
307 Java_org_simgrid_msg_Host_getAttachedStorage(JNIEnv * env, jobject jhost){
308   msg_host_t host = jhost_get_native(env, jhost);
309
310   if (!host) {
311     jxbt_throw_notbound(env, "host", jhost);
312     return 0;
313   }
314   jobjectArray jtable;
315
316   xbt_dynar_t dyn = MSG_host_get_attached_storage_list(host);
317   int count = xbt_dynar_length(dyn);
318   jclass cls = jxbt_get_class(env, "java/lang/String");
319   jtable = (*env)->NewObjectArray(env, (jsize) count, cls, NULL);
320   int index;
321   char *storage_name;
322   jstring jstorage_name;
323   for (index = 0; index < count; index++) {
324         storage_name = xbt_dynar_get_as(dyn,index,char*);
325         jstorage_name = (*env)->NewStringUTF(env,storage_name);
326         (*env)->SetObjectArrayElement(env, jtable, index, jstorage_name);
327   }
328
329   return jtable;
330 }
331
332 JNIEXPORT jobjectArray JNICALL
333 Java_org_simgrid_msg_Host_getStorageContent(JNIEnv * env, jobject jhost){
334   msg_host_t host = jhost_get_native(env, jhost);
335
336   if (!host) {
337     jxbt_throw_notbound(env, "host", jhost);
338     return 0;
339   }
340   return (jobjectArray)MSG_host_get_storage_content(host);
341 }
342
343
344 JNIEXPORT jobjectArray JNICALL
345 Java_org_simgrid_msg_Host_all(JNIEnv * env, jclass cls_arg)
346 {
347   int index;
348   jobjectArray jtable;
349   jobject jhost;
350   jstring jname;
351   msg_host_t host;
352
353   xbt_dynar_t table =  MSG_hosts_as_dynar();
354   int count = xbt_dynar_length(table);
355
356   jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
357
358   if (!cls) {
359     return NULL;
360   }
361
362   jtable = (*env)->NewObjectArray(env, (jsize) count, cls, NULL);
363
364   if (!jtable) {
365     jxbt_throw_jni(env, "Hosts table allocation failed");
366     return NULL;
367   }
368
369   for (index = 0; index < count; index++) {
370     host = xbt_dynar_get_as(table,index,msg_host_t);
371     jhost = (jobject) (xbt_lib_get_level(host, JAVA_HOST_LEVEL));
372
373     if (!jhost) {
374       jname = (*env)->NewStringUTF(env, MSG_host_get_name(host));
375
376       jhost =
377                 Java_org_simgrid_msg_Host_getByName(env, cls_arg, jname);
378       /* FIXME: leak of jname ? */
379     }
380
381     (*env)->SetObjectArrayElement(env, jtable, index, jhost);
382   }
383   xbt_dynar_free(&table);
384   return jtable;
385 }
386
387 JNIEXPORT void JNICALL 
388 Java_org_simgrid_msg_Host_setAsyncMailbox(JNIEnv * env, jclass cls_arg, jobject jname){
389
390   const char *name = (*env)->GetStringUTFChars(env, jname, 0);
391   MSG_mailbox_set_async(name);
392   (*env)->ReleaseStringUTFChars(env, jname, name);
393
394 }