Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c01a13256e27062464be4b24a69af7d98cb06582
[simgrid.git] / src / jmsg_host.c
1 /* Functions related to the java host instances.                            */
2
3 /* Copyright (c) 2007, 2009, 2010. 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, m_host_t host, JNIEnv * env) {
35   (*env)->SetLongField(env, jhost, jhost_field_Host_bind, (jlong) (long) (host));
36 }
37
38 m_host_t jhost_get_native(JNIEnv * env, jobject jhost) {
39   return (m_host_t) (long) (*env)->GetLongField(env, jhost, jhost_field_Host_bind);
40 }
41
42 const char *jhost_get_name(jobject jhost, JNIEnv * env) {
43   m_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         //FIXME: Don't use jxbt_get_sfield directly, it is slower.
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   m_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   XBT_DEBUG("Looking for host '%s'",name);
79   /* get the host by name       (the hosts are created during the grid resolution) */
80   host = MSG_get_host_by_name(name);
81   XBT_DEBUG("MSG gave %p as native host", host);
82
83   if (!host) {                  /* invalid name */
84     jxbt_throw_host_not_found(env, name);
85     (*env)->ReleaseStringUTFChars(env, jname, name);
86     return NULL;
87   }
88   (*env)->ReleaseStringUTFChars(env, jname, name);
89
90   if (!MSG_host_get_data(host)) {       /* native host not associated yet with java host */
91
92     /* Instantiate a new java host */
93     jhost = jhost_new_instance(env);
94
95     if (!jhost) {
96       jxbt_throw_jni(env, "java host instantiation failed");
97       return NULL;
98     }
99
100     /* get a global reference to the newly created host */
101     jhost = jhost_ref(env, jhost);
102
103     if (!jhost) {
104       jxbt_throw_jni(env, "new global ref allocation failed");
105       return NULL;
106     }
107     /* Sets the java host name */
108     (*env)->SetObjectField(env, jhost, jhost_field_Host_name, jname);
109     /* bind the java host and the native host */
110     jhost_bind(jhost, host, env);
111
112     /* the native host data field is set with the global reference to the
113      * java host returned by this function
114      */
115     MSG_host_set_data(host, (void *) jhost);
116   }
117
118   /* return the global reference to the java host instance */
119   return (jobject) MSG_host_get_data(host);
120 }
121
122 JNIEXPORT jobject JNICALL
123 Java_org_simgrid_msg_Host_currentHost(JNIEnv * env, jclass cls) {
124   jobject jhost;
125
126   m_host_t host = MSG_host_self();
127
128   if (!MSG_host_get_data(host)) {
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 NULL;
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 NULL;
145     }
146     /* Sets the host name */
147     const char *name = MSG_host_get_name(host);
148     jobject jname = (*env)->NewStringUTF(env,name);
149     (*env)->SetObjectField(env, jhost, jhost_field_Host_name, jname);
150     /* Bind & store it */
151     jhost_bind(jhost, host, env);
152     MSG_host_set_data(host, (void *) jhost);
153   } else {
154     jhost = (jobject) MSG_host_get_data(host);
155   }
156
157   return jhost;
158 }
159 JNIEXPORT jint JNICALL
160 Java_org_simgrid_msg_Host_getCount(JNIEnv * env, jclass cls) {
161   xbt_dynar_t hosts =  MSG_hosts_as_dynar();
162   int nb_host = xbt_dynar_length(hosts);
163   xbt_dynar_free(&hosts);
164   return (jint) nb_host;
165 }
166
167 JNIEXPORT jdouble JNICALL
168 Java_org_simgrid_msg_Host_getSpeed(JNIEnv * env,
169                                         jobject jhost) {
170   m_host_t host = jhost_get_native(env, jhost);
171
172   if (!host) {
173     jxbt_throw_notbound(env, "host", jhost);
174     return -1;
175   }
176
177   return (jdouble) MSG_get_host_speed(host);
178 }
179 JNIEXPORT jint JNICALL
180 Java_org_simgrid_msg_Host_getLoad(JNIEnv * env, jobject jhost) {
181   m_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 (jint) MSG_get_host_msgload(host);
189 }
190 JNIEXPORT jboolean JNICALL
191 Java_org_simgrid_msg_Host_isAvail(JNIEnv * env, jobject jhost) {
192   m_host_t host = jhost_get_native(env, jhost);
193
194   if (!host) {
195     jxbt_throw_notbound(env, "host", jhost);
196     return 0;
197   }
198
199   return (jboolean) MSG_host_is_avail(host);
200 }
201
202 JNIEXPORT jobjectArray JNICALL
203 Java_org_simgrid_msg_Host_all(JNIEnv * env, jclass cls_arg)
204 {
205   int index;
206   jobjectArray jtable;
207   jobject jhost;
208   jstring jname;
209   m_host_t host;
210
211   xbt_dynar_t table =  MSG_hosts_as_dynar();
212   int count = xbt_dynar_length(table);
213
214   jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
215
216   if (!cls) {
217     return NULL;
218   }
219
220   jtable = (*env)->NewObjectArray(env, (jsize) count, cls, NULL);
221
222   if (!jtable) {
223     jxbt_throw_jni(env, "Hosts table allocation failed");
224     return NULL;
225   }
226
227   for (index = 0; index < count; index++) {
228     host = xbt_dynar_get_as(table,index,m_host_t);
229     jhost = (jobject) (MSG_host_get_data(host));
230
231     if (!jhost) {
232       jname = (*env)->NewStringUTF(env, MSG_host_get_name(host));
233
234       jhost =
235                 Java_org_simgrid_msg_Host_getByName(env, cls_arg, jname);
236       /* FIXME: leak of jname ? */
237     }
238
239     (*env)->SetObjectArrayElement(env, jtable, index, jhost);
240   }
241   xbt_dynar_free(&table);
242   return jtable;
243 }