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 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."));
65 JNIEXPORT jobject JNICALL
66 Java_org_simgrid_msg_Host_getByName(JNIEnv * env, jclass cls,
68 m_host_t host; /* native host */
69 jobject jhost; /* global reference to the java host instance returned */
71 /* get the C string from the java string */
72 const char *name = (*env)->GetStringUTFChars(env, jname, 0);
74 jxbt_throw_null(env,bprintf("No host can have a null name"));
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);
82 if (!host) { /* invalid name */
83 jxbt_throw_host_not_found(env, name);
84 (*env)->ReleaseStringUTFChars(env, jname, name);
87 (*env)->ReleaseStringUTFChars(env, jname, name);
89 if (!MSG_host_get_data(host)) { /* native host not associated yet with java host */
91 /* Instantiate a new java host */
92 jhost = jhost_new_instance(env);
95 jxbt_throw_jni(env, "java host instantiation failed");
99 /* get a global reference to the newly created host */
100 jhost = jhost_ref(env, jhost);
103 jxbt_throw_jni(env, "new global ref allocation failed");
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);
111 /* the native host data field is set with the global reference to the
112 * java host returned by this function
114 MSG_host_set_data(host, (void *) jhost);
117 /* return the global reference to the java host instance */
118 return (jobject) MSG_host_get_data(host);
121 JNIEXPORT jobject JNICALL
122 Java_org_simgrid_msg_Host_currentHost(JNIEnv * env, jclass cls) {
125 m_host_t host = MSG_host_self();
127 if (!MSG_host_get_data(host)) {
128 /* the native host not yet associated with the java host instance */
130 /* instanciate a new java host instance */
131 jhost = jhost_new_instance(env);
134 jxbt_throw_jni(env, "java host instantiation failed");
138 /* get a global reference to the newly created host */
139 jhost = jhost_ref(env, jhost);
142 jxbt_throw_jni(env, "global ref allocation failed");
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);
153 jhost = (jobject) MSG_host_get_data(host);
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;
166 JNIEXPORT jdouble JNICALL
167 Java_org_simgrid_msg_Host_getSpeed(JNIEnv * env,
169 m_host_t host = jhost_get_native(env, jhost);
172 jxbt_throw_notbound(env, "host", jhost);
176 return (jdouble) MSG_get_host_speed(host);
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);
183 jxbt_throw_notbound(env, "host", jhost);
187 return (jint) MSG_get_host_msgload(host);
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);
194 jxbt_throw_notbound(env, "host", jhost);
198 return (jboolean) MSG_host_is_avail(host);
201 JNIEXPORT jobjectArray JNICALL
202 Java_org_simgrid_msg_Host_all(JNIEnv * env, jclass cls_arg)
210 xbt_dynar_t table = MSG_hosts_as_dynar();
211 int count = xbt_dynar_length(table);
213 jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
219 jtable = (*env)->NewObjectArray(env, (jsize) count, cls, NULL);
222 jxbt_throw_jni(env, "Hosts table allocation failed");
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));
231 jname = (*env)->NewStringUTF(env, MSG_host_get_name(host));
234 Java_org_simgrid_msg_Host_getByName(env, cls_arg, jname);
235 /* FIXME: leak of jname ? */
238 (*env)->SetObjectArrayElement(env, jtable, index, jhost);
240 xbt_dynar_free(&table);