1 /* Functions related to the java host instances. */
3 /* Copyright (c) 2007, 2009, 2010. The SimGrid Team.
4 * All rights reserved. */
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. */
11 #include "jmsg_host.h"
12 #include "jxbt_utilities.h"
14 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
16 static jmethodID jhost_method_Host_constructor;
17 static jfieldID jhost_field_Host_bind;
18 static jfieldID jhost_field_Host_name;
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);
26 jobject jhost_ref(JNIEnv * env, jobject jhost) {
27 return (*env)->NewGlobalRef(env, jhost);
30 void jhost_unref(JNIEnv * env, jobject jhost) {
31 (*env)->DeleteGlobalRef(env, jhost);
34 void jhost_bind(jobject jhost, m_host_t host, JNIEnv * env) {
35 (*env)->SetLongField(env, jhost, jhost_field_Host_bind, (jlong) (long) (host));
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);
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);
47 jboolean jhost_is_valid(jobject jhost, JNIEnv * env) {
48 if ((*env)->GetLongField(env, jhost, jhost_field_Host_bind)) {
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."));
66 JNIEXPORT jobject JNICALL
67 Java_org_simgrid_msg_Host_getByName(JNIEnv * env, jclass cls,
69 m_host_t host; /* native host */
70 jobject jhost; /* global reference to the java host instance returned */
72 /* get the C string from the java string */
73 const char *name = (*env)->GetStringUTFChars(env, jname, 0);
75 jxbt_throw_null(env,bprintf("No host can have a null name"));
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);
83 if (!host) { /* invalid name */
84 jxbt_throw_host_not_found(env, name);
85 (*env)->ReleaseStringUTFChars(env, jname, name);
88 (*env)->ReleaseStringUTFChars(env, jname, name);
90 if (!MSG_host_get_data(host)) { /* native host not associated yet with java host */
92 /* Instantiate a new java host */
93 jhost = jhost_new_instance(env);
96 jxbt_throw_jni(env, "java host instantiation failed");
100 /* get a global reference to the newly created host */
101 jhost = jhost_ref(env, jhost);
104 jxbt_throw_jni(env, "new global ref allocation failed");
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);
112 /* the native host data field is set with the global reference to the
113 * java host returned by this function
115 MSG_host_set_data(host, (void *) jhost);
118 /* return the global reference to the java host instance */
119 return (jobject) MSG_host_get_data(host);
122 JNIEXPORT jobject JNICALL
123 Java_org_simgrid_msg_Host_currentHost(JNIEnv * env, jclass cls) {
126 m_host_t host = MSG_host_self();
128 if (!MSG_host_get_data(host)) {
129 /* the native host not yet associated with the java host instance */
131 /* instanciate a new java host instance */
132 jhost = jhost_new_instance(env);
135 jxbt_throw_jni(env, "java host instantiation failed");
139 /* get a global reference to the newly created host */
140 jhost = jhost_ref(env, jhost);
143 jxbt_throw_jni(env, "global ref allocation failed");
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);
154 jhost = (jobject) MSG_host_get_data(host);
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;
167 JNIEXPORT jdouble JNICALL
168 Java_org_simgrid_msg_Host_getSpeed(JNIEnv * env,
170 m_host_t host = jhost_get_native(env, jhost);
173 jxbt_throw_notbound(env, "host", jhost);
177 return (jdouble) MSG_get_host_speed(host);
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);
184 jxbt_throw_notbound(env, "host", jhost);
188 return (jint) MSG_get_host_msgload(host);
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);
195 jxbt_throw_notbound(env, "host", jhost);
199 return (jboolean) MSG_host_is_avail(host);
202 JNIEXPORT jobjectArray JNICALL
203 Java_org_simgrid_msg_Host_all(JNIEnv * env, jclass cls_arg)
211 xbt_dynar_t table = MSG_hosts_as_dynar();
212 int count = xbt_dynar_length(table);
214 jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
220 jtable = (*env)->NewObjectArray(env, (jsize) count, cls, NULL);
223 jxbt_throw_jni(env, "Hosts table allocation failed");
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));
232 jname = (*env)->NewStringUTF(env, MSG_host_get_name(host));
235 Java_org_simgrid_msg_Host_getByName(env, cls_arg, jname);
236 /* FIXME: leak of jname ? */
239 (*env)->SetObjectArrayElement(env, jtable, index, jhost);
241 xbt_dynar_free(&table);