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 jobject jhost_new_instance(JNIEnv * env) {
18 jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
19 jmethodID constructor = jxbt_get_jmethod(env, cls, "<init>", "()V");
24 return (*env)->NewObject(env, cls, constructor);
27 jobject jhost_ref(JNIEnv * env, jobject jhost) {
28 return (*env)->NewGlobalRef(env, jhost);
31 void jhost_unref(JNIEnv * env, jobject jhost) {
32 (*env)->DeleteGlobalRef(env, jhost);
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");
41 (*env)->SetLongField(env, jhost, id, (jlong) (long) (host));
44 m_host_t jhost_get_native(JNIEnv * env, jobject jhost) {
45 jfieldID id = jxbt_get_sfield(env, "org/simgrid/msg/Host", "bind", "J");
50 return (m_host_t) (long) (*env)->GetLongField(env, jhost, id);
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);
58 jboolean jhost_is_valid(jobject jhost, JNIEnv * env) {
59 jfieldID id = jxbt_get_sfield(env, "org/simgrid/msg/Host", "bind", "J");
64 if ((*env)->GetLongField(env, jhost, id)) {
71 JNIEXPORT jobject JNICALL
72 Java_org_simgrid_msg_Host_getByName(JNIEnv * env, jclass cls,
74 m_host_t host; /* native host */
75 jobject jhost; /* global reference to the java host instance returned */
77 /* get the C string from the java string */
78 const char *name = (*env)->GetStringUTFChars(env, jname, 0);
80 jxbt_throw_null(env,bprintf("No host can have a null name"));
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);
88 if (!host) { /* invalid name */
89 jxbt_throw_host_not_found(env, name);
90 (*env)->ReleaseStringUTFChars(env, jname, name);
93 (*env)->ReleaseStringUTFChars(env, jname, name);
95 if (!MSG_host_get_data(host)) { /* native host not associated yet with java host */
97 /* Instantiate a new java host */
98 jhost = jhost_new_instance(env);
101 jxbt_throw_jni(env, "java host instantiation failed");
105 /* get a global reference to the newly created host */
106 jhost = jhost_ref(env, jhost);
109 jxbt_throw_jni(env, "new global ref allocation failed");
113 /* bind the java host and the native host */
114 jhost_bind(jhost, host, env);
116 /* the native host data field is set with the global reference to the
117 * java host returned by this function
119 MSG_host_set_data(host, (void *) jhost);
122 /* return the global reference to the java host instance */
123 return (jobject) MSG_host_get_data(host);
126 JNIEXPORT jobject JNICALL
127 Java_org_simgrid_msg_Host_currentHost(JNIEnv * env, jclass cls) {
130 m_host_t host = MSG_host_self();
132 if (!MSG_host_get_data(host)) {
133 /* the native host not yet associated with the java host instance */
135 /* instanciate a new java host instance */
136 jhost = jhost_new_instance(env);
139 jxbt_throw_jni(env, "java host instantiation failed");
143 /* get a global reference to the newly created host */
144 jhost = jhost_ref(env, jhost);
147 jxbt_throw_jni(env, "global ref allocation failed");
151 /* Bind & store it */
152 jhost_bind(jhost, host, env);
153 MSG_host_set_data(host, (void *) jhost);
155 jhost = (jobject) MSG_host_get_data(host);
161 JNIEXPORT jstring JNICALL
162 Java_org_simgrid_msg_Host_getName(JNIEnv * env,
164 m_host_t host = jhost_get_native(env, jhost);
168 jxbt_throw_notbound(env, "host", jhost);
172 name = MSG_host_get_name(host);
174 xbt_die("This host has no name...");
176 return (*env)->NewStringUTF(env, name);
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;
187 JNIEXPORT jdouble JNICALL
188 Java_org_simgrid_msg_Host_getSpeed(JNIEnv * env,
190 m_host_t host = jhost_get_native(env, jhost);
193 jxbt_throw_notbound(env, "host", jhost);
197 return (jdouble) MSG_get_host_speed(host);
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);
204 jxbt_throw_notbound(env, "host", jhost);
208 return (jint) MSG_get_host_msgload(host);
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);
215 jxbt_throw_notbound(env, "host", jhost);
219 return (jboolean) MSG_host_is_avail(host);