Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
55cc4a1bb0952d91579d8bf80b3bb75e867edced
[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
58 Java_org_simgrid_msg_Host_nativeInit(JNIEnv *env, jclass cls) {
59   jclass class_Host = env->FindClass("org/simgrid/msg/Host");
60   jhost_method_Host_constructor = env->GetMethodID(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(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(jname, name);
85     return NULL;
86   }
87   env->ReleaseStringUTFChars(jname, name);
88
89   if (!host->extension(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(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     host->extension_set(JAVA_HOST_LEVEL, (void *)jhost);
115   }
116
117   /* return the global reference to the java host instance */
118   return (jobject) host->extension(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 (!host->extension(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(name);
148     env->SetObjectField(jhost, jhost_field_Host_name, jname);
149     /* Bind & store it */
150     jhost_bind(jhost, host, env);
151     host->extension_set(JAVA_HOST_LEVEL, (void *) jhost);
152   } else {
153     jhost = (jobject) host->extension(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 jobject JNICALL
206 Java_org_simgrid_msg_Host_getProperty(JNIEnv *env, jobject jhost, jobject jname) {
207   msg_host_t host = jhost_get_native(env, jhost);
208
209   if (!host) {
210     jxbt_throw_notbound(env, "host", jhost);
211     return NULL;
212   }
213   const char *name = env->GetStringUTFChars((jstring) jname, 0);
214
215   const char *property = MSG_host_get_property_value(host, name);
216   if (!property) {
217     return NULL;
218   }
219
220   jobject jproperty = env->NewStringUTF(property);
221
222   env->ReleaseStringUTFChars((jstring) jname, name);
223
224   return jproperty;
225 }
226
227 JNIEXPORT void JNICALL
228 Java_org_simgrid_msg_Host_setProperty(JNIEnv *env, jobject jhost, jobject jname, jobject jvalue) {
229   msg_host_t host = jhost_get_native(env, jhost);
230
231   if (!host) {
232     jxbt_throw_notbound(env, "host", jhost);
233     return;
234   }
235   const char *name = env->GetStringUTFChars((jstring) jname, 0);
236   const char *value_java = env->GetStringUTFChars((jstring) jvalue, 0);
237   char *value = xbt_strdup(value_java);
238
239   MSG_host_set_property_value(host, name, value, xbt_free_f);
240
241   env->ReleaseStringUTFChars((jstring) jvalue, value);
242   env->ReleaseStringUTFChars((jstring) jname, name);
243
244 }
245 JNIEXPORT jboolean JNICALL
246 Java_org_simgrid_msg_Host_isOn(JNIEnv * env, jobject jhost) {
247   msg_host_t host = jhost_get_native(env, jhost);
248
249   if (!host) {
250     jxbt_throw_notbound(env, "host", jhost);
251     return 0;
252   }
253
254   return (jboolean) MSG_host_is_on(host);
255 }
256
257 JNIEXPORT jobjectArray JNICALL
258 Java_org_simgrid_msg_Host_getMountedStorage(JNIEnv * env, jobject jhost){
259
260   msg_host_t host = jhost_get_native(env, jhost);
261   jobject jstorage;
262   jstring jname;
263
264   if (!host) {
265     jxbt_throw_notbound(env, "host", jhost);
266     return 0;
267   }
268
269   int index = 0;
270         jobjectArray jtable;
271         xbt_dict_t dict =  MSG_host_get_mounted_storage_list(host);
272         int count = xbt_dict_length(dict);
273         jclass cls = env->FindClass("org/simgrid/msg/Storage");
274
275         jtable = env->NewObjectArray((jsize) count, cls, NULL);
276
277         if (!jtable) {
278          jxbt_throw_jni(env, "Storages table allocation failed");
279          return NULL;
280         }
281
282         xbt_dict_cursor_t cursor=NULL;
283         const char *mount_name, *storage_name;
284
285         xbt_dict_foreach(dict,cursor,mount_name,storage_name) {
286                 jname = env->NewStringUTF(storage_name);
287           jstorage = Java_org_simgrid_msg_Storage_getByName(env,cls,jname);
288           env->SetObjectArrayElement(jtable, index, jstorage);
289     index++;
290         }
291         xbt_dict_free(&dict);
292         return jtable;
293 }
294
295 JNIEXPORT jobjectArray JNICALL
296 Java_org_simgrid_msg_Host_getAttachedStorage(JNIEnv * env, jobject jhost){
297   msg_host_t host = jhost_get_native(env, jhost);
298
299   if (!host) {
300     jxbt_throw_notbound(env, "host", jhost);
301     return 0;
302   }
303   jobjectArray jtable;
304
305   xbt_dynar_t dyn = MSG_host_get_attached_storage_list(host);
306   int count = xbt_dynar_length(dyn);
307   jclass cls = jxbt_get_class(env, "java/lang/String");
308   jtable = env->NewObjectArray((jsize) count, cls, NULL);
309   int index;
310   char *storage_name;
311   jstring jstorage_name;
312   for (index = 0; index < count; index++) {
313     storage_name = xbt_dynar_get_as(dyn,index,char*);
314     jstorage_name = env->NewStringUTF(storage_name);
315     env->SetObjectArrayElement(jtable, index, jstorage_name);
316   }
317
318   return jtable;
319 }
320
321 JNIEXPORT jobjectArray JNICALL
322 Java_org_simgrid_msg_Host_getStorageContent(JNIEnv * env, jobject jhost){
323   msg_host_t host = jhost_get_native(env, jhost);
324
325   if (!host) {
326     jxbt_throw_notbound(env, "host", jhost);
327     return 0;
328   }
329   return (jobjectArray)MSG_host_get_storage_content(host);
330 }
331
332
333 JNIEXPORT jobjectArray JNICALL
334 Java_org_simgrid_msg_Host_all(JNIEnv * env, jclass cls_arg)
335 {
336   int index;
337   jobjectArray jtable;
338   jobject jhost;
339   jstring jname;
340   msg_host_t host;
341
342   xbt_dynar_t table =  MSG_hosts_as_dynar();
343   int count = xbt_dynar_length(table);
344
345   jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
346
347   if (!cls) {
348     return NULL;
349   }
350
351   jtable = env->NewObjectArray((jsize) count, cls, NULL);
352
353   if (!jtable) {
354     jxbt_throw_jni(env, "Hosts table allocation failed");
355     return NULL;
356   }
357
358   for (index = 0; index < count; index++) {
359     host = xbt_dynar_get_as(table,index,msg_host_t);
360     jhost = (jobject) host->extension(JAVA_HOST_LEVEL);
361
362     if (!jhost) {
363       jname = env->NewStringUTF(MSG_host_get_name(host));
364
365       jhost =
366                 Java_org_simgrid_msg_Host_getByName(env, cls_arg, jname);
367       /* FIXME: leak of jname ? */
368     }
369
370     env->SetObjectArrayElement(jtable, index, jhost);
371   }
372   xbt_dynar_free(&table);
373   return jtable;
374 }
375
376 JNIEXPORT void JNICALL 
377 Java_org_simgrid_msg_Host_setAsyncMailbox(JNIEnv * env, jclass cls_arg, jobject jname){
378
379   const char *name = env->GetStringUTFChars((jstring) jname, 0);
380   MSG_mailbox_set_async(name);
381   env->ReleaseStringUTFChars((jstring) jname, name);
382
383 }
384
385 #include "simgrid/plugins/energy.h"
386 JNIEXPORT jdouble JNICALL Java_org_simgrid_msg_Host_getConsumedEnergy (JNIEnv *env, jobject jhost) {
387   msg_host_t host = jhost_get_native(env, jhost);
388
389   if (!host) {
390     jxbt_throw_notbound(env, "host", jhost);
391     return 0;
392   }
393
394   return MSG_host_get_consumed_energy(host);
395
396 }
397