Logo AND Algorithmique Numérique Distribuée

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