Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[BINDINGS] NULL -> nullptr substitution
[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 == nullptr) {
73     jxbt_throw_null(env,bprintf("No host can have a null name"));
74     return nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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 nullptr;
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, nullptr);
261
262   if (!jtable) {
263    jxbt_throw_jni(env, "Storages table allocation failed");
264    return nullptr;
265   }
266
267   xbt_dict_cursor_t cursor=nullptr;
268   const char* mount_name;
269   const char* storage_name;
270
271   xbt_dict_foreach(dict,cursor,mount_name,storage_name) {
272     jname = env->NewStringUTF(storage_name);
273     jstorage = Java_org_simgrid_msg_Storage_getByName(env,cls,jname);
274     env->SetObjectArrayElement(jtable, index, jstorage);
275     index++;
276   }
277   xbt_dict_free(&dict);
278   return jtable;
279 }
280
281 JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_Host_getAttachedStorage(JNIEnv * env, jobject jhost)
282 {
283   msg_host_t host = jhost_get_native(env, jhost);
284
285   if (!host) {
286     jxbt_throw_notbound(env, "host", jhost);
287     return 0;
288   }
289   jobjectArray jtable;
290
291   xbt_dynar_t dyn = MSG_host_get_attached_storage_list(host);
292   int count = xbt_dynar_length(dyn);
293   jclass cls = jxbt_get_class(env, "java/lang/String");
294   jtable = env->NewObjectArray((jsize) count, cls, nullptr);
295   int index;
296   char *storage_name;
297   jstring jstorage_name;
298   for (index = 0; index < count; index++) {
299     storage_name = xbt_dynar_get_as(dyn,index,char*);
300     jstorage_name = env->NewStringUTF(storage_name);
301     env->SetObjectArrayElement(jtable, index, jstorage_name);
302   }
303
304   return jtable;
305 }
306
307 JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_Host_getStorageContent(JNIEnv * env, jobject jhost)
308 {
309   msg_host_t host = jhost_get_native(env, jhost);
310
311   if (!host) {
312     jxbt_throw_notbound(env, "host", jhost);
313     return 0;
314   }
315   return (jobjectArray)MSG_host_get_storage_content(host);
316 }
317
318 JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_Host_all(JNIEnv * env, jclass cls_arg)
319 {
320   int index;
321   jobjectArray jtable;
322   jobject jhost;
323   jstring jname;
324   msg_host_t host;
325
326   xbt_dynar_t table =  MSG_hosts_as_dynar();
327   int count = xbt_dynar_length(table);
328
329   jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
330
331   if (!cls) {
332     return nullptr;
333   }
334
335   jtable = env->NewObjectArray((jsize) count, cls, nullptr);
336
337   if (!jtable) {
338     jxbt_throw_jni(env, "Hosts table allocation failed");
339     return nullptr;
340   }
341
342   for (index = 0; index < count; index++) {
343     host = xbt_dynar_get_as(table,index,msg_host_t);
344     jhost = (jobject) host->extension(JAVA_HOST_LEVEL);
345
346     if (!jhost) {
347       jname = env->NewStringUTF(MSG_host_get_name(host));
348
349       jhost = Java_org_simgrid_msg_Host_getByName(env, cls_arg, jname);
350       /* FIXME: leak of jname ? */
351     }
352
353     env->SetObjectArrayElement(jtable, index, jhost);
354   }
355   xbt_dynar_free(&table);
356   return jtable;
357 }
358
359 JNIEXPORT void JNICALL Java_org_simgrid_msg_Host_setAsyncMailbox(JNIEnv * env, jclass cls_arg, jobject jname)
360 {
361   const char *name = env->GetStringUTFChars((jstring) jname, 0);
362   MSG_mailbox_set_async(name);
363   env->ReleaseStringUTFChars((jstring) jname, name);
364 }
365
366 #include "simgrid/plugins/energy.h"
367 JNIEXPORT jdouble JNICALL Java_org_simgrid_msg_Host_getConsumedEnergy (JNIEnv *env, jobject jhost)
368 {
369   msg_host_t host = jhost_get_native(env, jhost);
370
371   if (!host) {
372     jxbt_throw_notbound(env, "host", jhost);
373     return 0;
374   }
375
376   return MSG_host_get_consumed_energy(host);
377 }