Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Deleted simulatedSleep, use waitFor instead. Moved everything from MsgNative to their...
[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 jobject jhost_new_instance(JNIEnv * env) {
17
18   jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
19   jmethodID constructor = jxbt_get_jmethod(env, cls, "<init>", "()V");
20
21   if (!constructor)
22     return NULL;
23
24   return (*env)->NewObject(env, cls, 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, m_host_t host, JNIEnv * env) {
36   jfieldID id = jxbt_get_sfield(env, "org/simgrid/msg/Host", "bind", "J");
37
38   if (!id)
39     return;
40
41   (*env)->SetLongField(env, jhost, id, (jlong) (long) (host));
42 }
43
44 m_host_t jhost_get_native(JNIEnv * env, jobject jhost) {
45   jfieldID id = jxbt_get_sfield(env, "org/simgrid/msg/Host", "bind", "J");
46
47   if (!id)
48     return NULL;
49
50   return (m_host_t) (long) (*env)->GetLongField(env, jhost, id);
51 }
52
53 const char *jhost_get_name(jobject jhost, JNIEnv * env) {
54   m_host_t host = jhost_get_native(env, jhost);
55   return MSG_host_get_name(host);
56 }
57
58 jboolean jhost_is_valid(jobject jhost, JNIEnv * env) {
59   jfieldID id = jxbt_get_sfield(env, "org/simgrid/msg/Host", "bind", "J");
60
61   if (!id)
62     return 0;
63
64   if ((*env)->GetLongField(env, jhost, id)) {
65     return JNI_TRUE;
66   } else {
67     return JNI_FALSE;
68   }
69 }
70
71 JNIEXPORT jobject JNICALL
72 Java_org_simgrid_msg_Host_getByName(JNIEnv * env, jclass cls,
73                                          jstring jname) {
74   m_host_t host;                /* native host                                          */
75   jobject jhost;                /* global reference to the java host instance returned  */
76
77   /* get the C string from the java string */
78   const char *name = (*env)->GetStringUTFChars(env, jname, 0);
79   if (name == NULL) {
80         jxbt_throw_null(env,bprintf("No host can have a null name"));
81         return NULL;
82   }
83   XBT_DEBUG("Looking for host '%s'",name);
84   /* get the host by name       (the hosts are created during the grid resolution) */
85   host = MSG_get_host_by_name(name);
86   XBT_DEBUG("MSG gave %p as native host", host);
87
88   if (!host) {                  /* invalid name */
89     jxbt_throw_host_not_found(env, name);
90     (*env)->ReleaseStringUTFChars(env, jname, name);
91     return NULL;
92   }
93   (*env)->ReleaseStringUTFChars(env, jname, name);
94
95   if (!MSG_host_get_data(host)) {       /* native host not associated yet with java host */
96
97     /* Instantiate a new java host */
98     jhost = jhost_new_instance(env);
99
100     if (!jhost) {
101       jxbt_throw_jni(env, "java host instantiation failed");
102       return NULL;
103     }
104
105     /* get a global reference to the newly created host */
106     jhost = jhost_ref(env, jhost);
107
108     if (!jhost) {
109       jxbt_throw_jni(env, "new global ref allocation failed");
110       return NULL;
111     }
112
113     /* bind the java host and the native host */
114     jhost_bind(jhost, host, env);
115
116     /* the native host data field is set with the global reference to the
117      * java host returned by this function
118      */
119     MSG_host_set_data(host, (void *) jhost);
120   }
121
122   /* return the global reference to the java host instance */
123   return (jobject) MSG_host_get_data(host);
124 }
125
126 JNIEXPORT jobject JNICALL
127 Java_org_simgrid_msg_Host_currentHost(JNIEnv * env, jclass cls) {
128   jobject jhost;
129
130   m_host_t host = MSG_host_self();
131
132   if (!MSG_host_get_data(host)) {
133     /* the native host not yet associated with the java host instance */
134
135     /* instanciate a new java host instance */
136     jhost = jhost_new_instance(env);
137
138     if (!jhost) {
139       jxbt_throw_jni(env, "java host instantiation failed");
140       return NULL;
141     }
142
143     /* get a global reference to the newly created host */
144     jhost = jhost_ref(env, jhost);
145
146     if (!jhost) {
147       jxbt_throw_jni(env, "global ref allocation failed");
148       return NULL;
149     }
150
151     /* Bind & store it */
152     jhost_bind(jhost, host, env);
153     MSG_host_set_data(host, (void *) jhost);
154   } else {
155     jhost = (jobject) MSG_host_get_data(host);
156   }
157
158   return jhost;
159 }
160
161 JNIEXPORT jstring JNICALL
162 Java_org_simgrid_msg_Host_getName(JNIEnv * env,
163                                   jobject jhost) {
164   m_host_t host = jhost_get_native(env, jhost);
165   const char* name;
166
167   if (!host) {
168     jxbt_throw_notbound(env, "host", jhost);
169     return NULL;
170   }
171
172   name = MSG_host_get_name(host);
173   if (!name)
174           xbt_die("This host has no name...");
175
176   return (*env)->NewStringUTF(env, name);
177 }
178
179 JNIEXPORT jint JNICALL
180 Java_org_simgrid_msg_Host_getCount(JNIEnv * env, jclass cls) {
181   xbt_dynar_t hosts =  MSG_hosts_as_dynar();
182   int nb_host = xbt_dynar_length(hosts);
183   xbt_dynar_free(&hosts);
184   return (jint) nb_host;
185 }
186
187 JNIEXPORT jdouble JNICALL
188 Java_org_simgrid_msg_Host_getSpeed(JNIEnv * env,
189                                         jobject jhost) {
190   m_host_t host = jhost_get_native(env, jhost);
191
192   if (!host) {
193     jxbt_throw_notbound(env, "host", jhost);
194     return -1;
195   }
196
197   return (jdouble) MSG_get_host_speed(host);
198 }
199 JNIEXPORT jint JNICALL
200 Java_org_simgrid_msg_Host_getLoad(JNIEnv * env, jobject jhost) {
201   m_host_t host = jhost_get_native(env, jhost);
202
203   if (!host) {
204     jxbt_throw_notbound(env, "host", jhost);
205     return -1;
206   }
207
208   return (jint) MSG_get_host_msgload(host);
209 }
210 JNIEXPORT jboolean JNICALL
211 Java_org_simgrid_msg_Host_isAvail(JNIEnv * env, jobject jhost) {
212   m_host_t host = jhost_get_native(env, jhost);
213
214   if (!host) {
215     jxbt_throw_notbound(env, "host", jhost);
216     return 0;
217   }
218
219   return (jboolean) MSG_host_is_avail(host);
220 }
221
222 JNIEXPORT jobjectArray JNICALL
223 Java_org_simgrid_msg_Host_all(JNIEnv * env, jclass cls_arg)
224 {
225   int index;
226   jobjectArray jtable;
227   jobject jhost;
228   jstring jname;
229   m_host_t host;
230
231   xbt_dynar_t table =  MSG_hosts_as_dynar();
232   int count = xbt_dynar_length(table);
233
234   jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
235
236   if (!cls) {
237     return NULL;
238   }
239
240   jtable = (*env)->NewObjectArray(env, (jsize) count, cls, NULL);
241
242   if (!jtable) {
243     jxbt_throw_jni(env, "Hosts table allocation failed");
244     return NULL;
245   }
246
247   for (index = 0; index < count; index++) {
248     host = xbt_dynar_get_as(table,index,m_host_t);
249     jhost = (jobject) (MSG_host_get_data(host));
250
251     if (!jhost) {
252       jname = (*env)->NewStringUTF(env, MSG_host_get_name(host));
253
254       jhost =
255                 Java_org_simgrid_msg_Host_getByName(env, cls_arg, jname);
256       /* FIXME: leak of jname ? */
257     }
258
259     (*env)->SetObjectArrayElement(env, jtable, index, jhost);
260   }
261   xbt_dynar_free(&table);
262   return jtable;
263 }