Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c776b2b1d10e143cc022c37d442ce1e36f207a96
[simgrid.git] / 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 "jmsg.h"
11 #include "jmsg_host.h"
12 #include "jxbt_utilities.h"
13
14 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
15
16 static jmethodID jhost_method_Host_constructor;
17 static jfieldID jhost_field_Host_bind;
18 static jfieldID jhost_field_Host_name;
19
20
21 jobject jhost_new_instance(JNIEnv * env) {
22   jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
23   return (*env)->NewObject(env, cls, jhost_method_Host_constructor);
24 }
25
26 jobject jhost_ref(JNIEnv * env, jobject jhost) {
27   return (*env)->NewGlobalRef(env, jhost);
28 }
29
30 void jhost_unref(JNIEnv * env, jobject jhost) {
31   (*env)->DeleteGlobalRef(env, jhost);
32 }
33
34 void jhost_bind(jobject jhost, msg_host_t host, JNIEnv * env) {
35   (*env)->SetLongField(env, jhost, jhost_field_Host_bind, (jlong) (long) (host));
36 }
37
38 msg_host_t jhost_get_native(JNIEnv * env, jobject jhost) {
39   return (msg_host_t) (long) (*env)->GetLongField(env, jhost, jhost_field_Host_bind);
40 }
41
42 const char *jhost_get_name(jobject jhost, JNIEnv * env) {
43   msg_host_t host = jhost_get_native(env, jhost);
44   return MSG_host_get_name(host);
45 }
46
47 jboolean jhost_is_valid(jobject jhost, JNIEnv * env) {
48   if ((*env)->GetLongField(env, jhost, jhost_field_Host_bind)) {
49     return JNI_TRUE;
50   } else {
51     return JNI_FALSE;
52   }
53 }
54
55 JNIEXPORT void JNICALL
56 Java_org_simgrid_msg_Host_nativeInit(JNIEnv *env, jclass cls) {
57   jclass class_Host = (*env)->FindClass(env, "org/simgrid/msg/Host");
58   jhost_method_Host_constructor = (*env)->GetMethodID(env, class_Host, "<init>", "()V");
59   jhost_field_Host_bind = jxbt_get_jfield(env,class_Host, "bind", "J");
60   jhost_field_Host_name = jxbt_get_jfield(env, class_Host, "name", "Ljava/lang/String;");
61   if (!class_Host || !jhost_field_Host_name || !jhost_method_Host_constructor || !jhost_field_Host_bind) {
62     jxbt_throw_native(env,bprintf("Can't find some fields in Java class. You should report this bug."));
63   }
64 }
65 JNIEXPORT jobject JNICALL
66 Java_org_simgrid_msg_Host_getByName(JNIEnv * env, jclass cls,
67                                          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   const char *name = (*env)->GetStringUTFChars(env, jname, 0);
73   if (name == NULL) {
74         jxbt_throw_null(env,bprintf("No host can have a null name"));
75         return NULL;
76   }
77   /* get the host by name       (the hosts are created during the grid resolution) */
78   host = MSG_get_host_by_name(name);
79
80   if (!host) {                  /* invalid name */
81     jxbt_throw_host_not_found(env, name);
82     (*env)->ReleaseStringUTFChars(env, jname, name);
83     return NULL;
84   }
85   (*env)->ReleaseStringUTFChars(env, jname, name);
86
87   if (!MSG_host_get_data(host)) {       /* native host not associated yet with java host */
88
89     /* Instantiate a new java host */
90     jhost = jhost_new_instance(env);
91
92     if (!jhost) {
93       jxbt_throw_jni(env, "java host instantiation failed");
94       return NULL;
95     }
96
97     /* get a global reference to the newly created host */
98     jhost = jhost_ref(env, jhost);
99
100     if (!jhost) {
101       jxbt_throw_jni(env, "new global ref allocation failed");
102       return NULL;
103     }
104     /* Sets the java host name */
105     (*env)->SetObjectField(env, jhost, jhost_field_Host_name, jname);
106     /* bind the java host and the native host */
107     jhost_bind(jhost, host, env);
108
109     /* the native host data field is set with the global reference to the
110      * java host returned by this function
111      */
112     MSG_host_set_data(host, (void *) jhost);
113   }
114
115   /* return the global reference to the java host instance */
116   return (jobject) MSG_host_get_data(host);
117 }
118
119 JNIEXPORT jobject JNICALL
120 Java_org_simgrid_msg_Host_currentHost(JNIEnv * env, jclass cls) {
121   jobject jhost;
122
123   msg_host_t host = MSG_host_self();
124
125   if (!MSG_host_get_data(host)) {
126     /* the native host not yet associated with the java host instance */
127
128     /* instanciate a new java host instance */
129     jhost = jhost_new_instance(env);
130
131     if (!jhost) {
132       jxbt_throw_jni(env, "java host instantiation failed");
133       return NULL;
134     }
135
136     /* get a global reference to the newly created host */
137     jhost = jhost_ref(env, jhost);
138
139     if (!jhost) {
140       jxbt_throw_jni(env, "global ref allocation failed");
141       return NULL;
142     }
143     /* Sets the host name */
144     const char *name = MSG_host_get_name(host);
145     jobject jname = (*env)->NewStringUTF(env,name);
146     (*env)->SetObjectField(env, jhost, jhost_field_Host_name, jname);
147     /* Bind & store it */
148     jhost_bind(jhost, host, env);
149     MSG_host_set_data(host, (void *) jhost);
150   } else {
151     jhost = (jobject) MSG_host_get_data(host);
152   }
153
154   return jhost;
155 }
156 JNIEXPORT jint JNICALL
157 Java_org_simgrid_msg_Host_getCount(JNIEnv * env, jclass cls) {
158   xbt_dynar_t hosts =  MSG_hosts_as_dynar();
159   int nb_host = xbt_dynar_length(hosts);
160   xbt_dynar_free(&hosts);
161   return (jint) nb_host;
162 }
163
164 JNIEXPORT jdouble JNICALL
165 Java_org_simgrid_msg_Host_getSpeed(JNIEnv * env,
166                                         jobject jhost) {
167   msg_host_t host = jhost_get_native(env, jhost);
168
169   if (!host) {
170     jxbt_throw_notbound(env, "host", jhost);
171     return -1;
172   }
173
174   return (jdouble) MSG_get_host_speed(host);
175 }
176 JNIEXPORT jint JNICALL
177 Java_org_simgrid_msg_Host_getLoad(JNIEnv * env, jobject jhost) {
178   msg_host_t host = jhost_get_native(env, jhost);
179
180   if (!host) {
181     jxbt_throw_notbound(env, "host", jhost);
182     return -1;
183   }
184
185   return (jint) MSG_get_host_msgload(host);
186 }
187 JNIEXPORT jboolean JNICALL
188 Java_org_simgrid_msg_Host_isAvail(JNIEnv * env, jobject jhost) {
189   msg_host_t host = jhost_get_native(env, jhost);
190
191   if (!host) {
192     jxbt_throw_notbound(env, "host", jhost);
193     return 0;
194   }
195
196   return (jboolean) MSG_host_is_avail(host);
197 }
198
199 JNIEXPORT jobjectArray JNICALL
200 Java_org_simgrid_msg_Host_all(JNIEnv * env, jclass cls_arg)
201 {
202   int index;
203   jobjectArray jtable;
204   jobject jhost;
205   jstring jname;
206   msg_host_t host;
207
208   xbt_dynar_t table =  MSG_hosts_as_dynar();
209   int count = xbt_dynar_length(table);
210
211   jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
212
213   if (!cls) {
214     return NULL;
215   }
216
217   jtable = (*env)->NewObjectArray(env, (jsize) count, cls, NULL);
218
219   if (!jtable) {
220     jxbt_throw_jni(env, "Hosts table allocation failed");
221     return NULL;
222   }
223
224   for (index = 0; index < count; index++) {
225     host = xbt_dynar_get_as(table,index,msg_host_t);
226     jhost = (jobject) (MSG_host_get_data(host));
227
228     if (!jhost) {
229       jname = (*env)->NewStringUTF(env, MSG_host_get_name(host));
230
231       jhost =
232                 Java_org_simgrid_msg_Host_getByName(env, cls_arg, jname);
233       /* FIXME: leak of jname ? */
234     }
235
236     (*env)->SetObjectArrayElement(env, jtable, index, jhost);
237   }
238   xbt_dynar_free(&table);
239   return jtable;
240 }