Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
apply some coding standard in function naming (agier has to be in
[simgrid.git] / src / bindings / java / jmsg_host.c
1 /* Functions related to the java host instances.                            */
2
3 /* Copyright (c) 2007-2012. 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 "msg/msg.h"
11 #include "jmsg.h"
12 #include "jmsg_host.h"
13 #include "jxbt_utilities.h"
14
15 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
16
17 static jmethodID jhost_method_Host_constructor;
18 static jfieldID jhost_field_Host_bind;
19 static jfieldID jhost_field_Host_name;
20
21
22 jobject jhost_new_instance(JNIEnv * env) {
23   jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
24   return (*env)->NewObject(env, cls, jhost_method_Host_constructor);
25 }
26
27 jobject jhost_ref(JNIEnv * env, jobject jhost) {
28   return (*env)->NewGlobalRef(env, jhost);
29 }
30
31 void jhost_unref(JNIEnv * env, jobject jhost) {
32   (*env)->DeleteGlobalRef(env, jhost);
33 }
34
35 void jhost_bind(jobject jhost, msg_host_t host, JNIEnv * env) {
36   (*env)->SetLongField(env, jhost, jhost_field_Host_bind, (jlong) (long) (host));
37 }
38
39 msg_host_t jhost_get_native(JNIEnv * env, jobject jhost) {
40   return (msg_host_t) (long) (*env)->GetLongField(env, jhost, jhost_field_Host_bind);
41 }
42
43 const char *jhost_get_name(jobject jhost, JNIEnv * env) {
44   msg_host_t host = jhost_get_native(env, jhost);
45   return MSG_host_get_name(host);
46 }
47
48 jboolean jhost_is_valid(jobject jhost, JNIEnv * env) {
49   if ((*env)->GetLongField(env, jhost, jhost_field_Host_bind)) {
50     return JNI_TRUE;
51   } else {
52     return JNI_FALSE;
53   }
54 }
55
56 JNIEXPORT void JNICALL
57 Java_org_simgrid_msg_Host_nativeInit(JNIEnv *env, jclass cls) {
58   jclass class_Host = (*env)->FindClass(env, "org/simgrid/msg/Host");
59   jhost_method_Host_constructor = (*env)->GetMethodID(env, 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 JNIEXPORT jobject JNICALL
67 Java_org_simgrid_msg_Host_getByName(JNIEnv * env, jclass cls,
68                                          jstring jname) {
69   msg_host_t host;                /* native host                                          */
70   jobject jhost;                /* global reference to the java host instance returned  */
71
72   /* get the C string from the java string */
73   const char *name = (*env)->GetStringUTFChars(env, jname, 0);
74   if (name == NULL) {
75         jxbt_throw_null(env,bprintf("No host can have a null name"));
76         return NULL;
77   }
78   /* get the host by name       (the hosts are created during the grid resolution) */
79   host = MSG_get_host_by_name(name);
80
81   if (!host) {                  /* invalid name */
82     jxbt_throw_host_not_found(env, name);
83     (*env)->ReleaseStringUTFChars(env, jname, name);
84     return NULL;
85   }
86   (*env)->ReleaseStringUTFChars(env, jname, name);
87
88   if (!MSG_host_get_data(host)) {       /* native host not associated yet with java host */
89
90     /* Instantiate a new java host */
91     jhost = jhost_new_instance(env);
92
93     if (!jhost) {
94       jxbt_throw_jni(env, "java host instantiation failed");
95       return NULL;
96     }
97
98     /* get a global reference to the newly created host */
99     jhost = jhost_ref(env, jhost);
100
101     if (!jhost) {
102       jxbt_throw_jni(env, "new global ref allocation failed");
103       return NULL;
104     }
105     /* Sets the java host name */
106     (*env)->SetObjectField(env, jhost, jhost_field_Host_name, jname);
107     /* bind the java host and the native host */
108     jhost_bind(jhost, host, env);
109
110     /* the native host data field is set with the global reference to the
111      * java host returned by this function
112      */
113     MSG_host_set_data(host, (void *) jhost);
114   }
115
116   /* return the global reference to the java host instance */
117   return (jobject) MSG_host_get_data(host);
118 }
119
120 JNIEXPORT jobject JNICALL
121 Java_org_simgrid_msg_Host_currentHost(JNIEnv * env, jclass cls) {
122   jobject jhost;
123
124   msg_host_t host = MSG_host_self();
125
126   if (!MSG_host_get_data(host)) {
127     /* the native host not yet associated with the java host instance */
128
129     /* instanciate a new java host instance */
130     jhost = jhost_new_instance(env);
131
132     if (!jhost) {
133       jxbt_throw_jni(env, "java host instantiation failed");
134       return NULL;
135     }
136
137     /* get a global reference to the newly created host */
138     jhost = jhost_ref(env, jhost);
139
140     if (!jhost) {
141       jxbt_throw_jni(env, "global ref allocation failed");
142       return NULL;
143     }
144     /* Sets the host name */
145     const char *name = MSG_host_get_name(host);
146     jobject jname = (*env)->NewStringUTF(env,name);
147     (*env)->SetObjectField(env, jhost, jhost_field_Host_name, jname);
148     /* Bind & store it */
149     jhost_bind(jhost, host, env);
150     MSG_host_set_data(host, (void *) jhost);
151   } else {
152     jhost = (jobject) MSG_host_get_data(host);
153   }
154
155   return jhost;
156 }
157 JNIEXPORT jint JNICALL
158 Java_org_simgrid_msg_Host_getCount(JNIEnv * env, jclass cls) {
159   xbt_dynar_t hosts =  MSG_hosts_as_dynar();
160   int nb_host = xbt_dynar_length(hosts);
161   xbt_dynar_free(&hosts);
162   return (jint) nb_host;
163 }
164
165 JNIEXPORT jdouble JNICALL
166 Java_org_simgrid_msg_Host_getSpeed(JNIEnv * env,
167                                         jobject jhost) {
168   msg_host_t host = jhost_get_native(env, jhost);
169
170   if (!host) {
171     jxbt_throw_notbound(env, "host", jhost);
172     return -1;
173   }
174
175   return (jdouble) MSG_get_host_speed(host);
176 }
177
178 JNIEXPORT jdouble JNICALL
179 Java_org_simgrid_msg_Host_getCoreNumber(JNIEnv * env,
180                                         jobject jhost) {
181   msg_host_t host = jhost_get_native(env, jhost);
182
183   if (!host) {
184     jxbt_throw_notbound(env, "host", jhost);
185     return -1;
186   }
187
188   return (jdouble) MSG_host_get_core_number(host);
189 }
190
191 JNIEXPORT jint JNICALL
192 Java_org_simgrid_msg_Host_getLoad(JNIEnv * env, jobject jhost) {
193   msg_host_t host = jhost_get_native(env, jhost);
194
195   if (!host) {
196     jxbt_throw_notbound(env, "host", jhost);
197     return -1;
198   }
199
200   return (jint) MSG_get_host_msgload(host);
201 }
202 JNIEXPORT jobject JNICALL
203 Java_org_simgrid_msg_Host_getProperty(JNIEnv *env, jobject jhost, jobject jname) {
204   msg_host_t host = jhost_get_native(env, jhost);
205
206   if (!host) {
207     jxbt_throw_notbound(env, "host", jhost);
208     return NULL;
209   }
210   const char *name = (*env)->GetStringUTFChars(env, jname, 0);
211
212   const char *property = MSG_host_get_property_value(host, name);
213   if (!property) {
214     return NULL;
215   }
216
217   jobject jproperty = (*env)->NewStringUTF(env, property);
218
219   (*env)->ReleaseStringUTFChars(env, jname, name);
220
221   return jproperty;
222 }
223
224 JNIEXPORT void JNICALL
225 Java_org_simgrid_msg_Host_setProperty(JNIEnv *env, jobject jhost, jobject jname, jobject jvalue) {
226   msg_host_t host = jhost_get_native(env, jhost);
227
228   if (!host) {
229     jxbt_throw_notbound(env, "host", jhost);
230     return;
231   }
232   const char *name = (*env)->GetStringUTFChars(env, jname, 0);
233   const char *value_java = (*env)->GetStringUTFChars(env, jvalue, 0);
234   char *value = strdup(value_java);
235
236   MSG_host_set_property_value(host,name,value,xbt_free);
237
238   (*env)->ReleaseStringUTFChars(env, jvalue, value);
239   (*env)->ReleaseStringUTFChars(env, jname, name);
240
241 }
242 JNIEXPORT jboolean JNICALL
243 Java_org_simgrid_msg_Host_isAvail(JNIEnv * env, jobject jhost) {
244   msg_host_t host = jhost_get_native(env, jhost);
245
246   if (!host) {
247     jxbt_throw_notbound(env, "host", jhost);
248     return 0;
249   }
250
251   return (jboolean) MSG_host_is_avail(host);
252 }
253
254 JNIEXPORT jobjectArray JNICALL
255 Java_org_simgrid_msg_Host_all(JNIEnv * env, jclass cls_arg)
256 {
257   int index;
258   jobjectArray jtable;
259   jobject jhost;
260   jstring jname;
261   msg_host_t host;
262
263   xbt_dynar_t table =  MSG_hosts_as_dynar();
264   int count = xbt_dynar_length(table);
265
266   jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
267
268   if (!cls) {
269     return NULL;
270   }
271
272   jtable = (*env)->NewObjectArray(env, (jsize) count, cls, NULL);
273
274   if (!jtable) {
275     jxbt_throw_jni(env, "Hosts table allocation failed");
276     return NULL;
277   }
278
279   for (index = 0; index < count; index++) {
280     host = xbt_dynar_get_as(table,index,msg_host_t);
281     jhost = (jobject) (MSG_host_get_data(host));
282
283     if (!jhost) {
284       jname = (*env)->NewStringUTF(env, MSG_host_get_name(host));
285
286       jhost =
287                 Java_org_simgrid_msg_Host_getByName(env, cls_arg, jname);
288       /* FIXME: leak of jname ? */
289     }
290
291     (*env)->SetObjectArrayElement(env, jtable, index, jhost);
292   }
293   xbt_dynar_free(&table);
294   return jtable;
295 }
296
297 JNIEXPORT void JNICALL 
298 Java_org_simgrid_msg_Host_setAsyncMailbox(JNIEnv * env, jclass cls_arg, jobject jname){
299
300   const char *name = (*env)->GetStringUTFChars(env, jname, 0);
301   MSG_mailbox_set_async(name);
302   (*env)->ReleaseStringUTFChars(env, jname, name);
303
304 }