Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8cd7da183734595fdc5543ecf6225a37f1069172
[simgrid.git] / simgrid-java / src / 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 JNIEXPORT jint JNICALL
178 Java_org_simgrid_msg_Host_getLoad(JNIEnv * env, jobject jhost) {
179   msg_host_t host = jhost_get_native(env, jhost);
180
181   if (!host) {
182     jxbt_throw_notbound(env, "host", jhost);
183     return -1;
184   }
185
186   return (jint) MSG_get_host_msgload(host);
187 }
188 JNIEXPORT jobject JNICALL
189 Java_org_simgrid_msg_Host_getProperty(JNIEnv *env, jobject jhost, jobject jname) {
190   msg_host_t host = jhost_get_native(env, jhost);
191
192   if (!host) {
193     jxbt_throw_notbound(env, "host", jhost);
194     return NULL;
195   }
196   const char *name = (*env)->GetStringUTFChars(env, jname, 0);
197
198   const char *property = MSG_host_get_property_value(host, name);
199   if (!property) {
200     return NULL;
201   }
202
203   jobject jproperty = (*env)->NewStringUTF(env, property);
204
205   (*env)->ReleaseStringUTFChars(env, jname, name);
206
207   return jproperty;
208 }
209 JNIEXPORT void JNICALL
210 Java_org_simgrid_msg_Host_setProperty(JNIEnv *env, jobject jhost, jobject jname, jobject jvalue) {
211   msg_host_t host = jhost_get_native(env, jhost);
212
213   if (!host) {
214     jxbt_throw_notbound(env, "host", jhost);
215     return;
216   }
217   const char *name = (*env)->GetStringUTFChars(env, jname, 0);
218   const char *value_java = (*env)->GetStringUTFChars(env, jvalue, 0);
219   char *value = strdup(value_java);
220
221   MSG_host_set_property_value(host,name,value,xbt_free);
222
223   (*env)->ReleaseStringUTFChars(env, jvalue, value);
224   (*env)->ReleaseStringUTFChars(env, jname, name);
225
226 }
227 JNIEXPORT jboolean JNICALL
228 Java_org_simgrid_msg_Host_isAvail(JNIEnv * env, jobject jhost) {
229   msg_host_t host = jhost_get_native(env, jhost);
230
231   if (!host) {
232     jxbt_throw_notbound(env, "host", jhost);
233     return 0;
234   }
235
236   return (jboolean) MSG_host_is_avail(host);
237 }
238
239 JNIEXPORT jobjectArray JNICALL
240 Java_org_simgrid_msg_Host_all(JNIEnv * env, jclass cls_arg)
241 {
242   int index;
243   jobjectArray jtable;
244   jobject jhost;
245   jstring jname;
246   msg_host_t host;
247
248   xbt_dynar_t table =  MSG_hosts_as_dynar();
249   int count = xbt_dynar_length(table);
250
251   jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
252
253   if (!cls) {
254     return NULL;
255   }
256
257   jtable = (*env)->NewObjectArray(env, (jsize) count, cls, NULL);
258
259   if (!jtable) {
260     jxbt_throw_jni(env, "Hosts table allocation failed");
261     return NULL;
262   }
263
264   for (index = 0; index < count; index++) {
265     host = xbt_dynar_get_as(table,index,msg_host_t);
266     jhost = (jobject) (MSG_host_get_data(host));
267
268     if (!jhost) {
269       jname = (*env)->NewStringUTF(env, MSG_host_get_name(host));
270
271       jhost =
272                 Java_org_simgrid_msg_Host_getByName(env, cls_arg, jname);
273       /* FIXME: leak of jname ? */
274     }
275
276     (*env)->SetObjectArrayElement(env, jtable, index, jhost);
277   }
278   xbt_dynar_free(&table);
279   return jtable;
280 }