Logo AND Algorithmique Numérique Distribuée

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