Logo AND Algorithmique Numérique Distribuée

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