1 /* Functions related to the java host instances. */
3 /* Copyright (c) 2007-2015. 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 <xbt/dynar.h>
13 #include "simgrid/s4u/NetZone.hpp"
14 #include <simgrid/s4u/host.hpp>
16 #include "simgrid/msg.h"
18 #include "jmsg_host.h"
19 #include "jxbt_utilities.h"
22 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
24 static jmethodID jas_method_As_constructor;
25 static jfieldID jas_field_As_bind;
27 jobject jas_new_instance(JNIEnv * env) {
28 jclass cls = jxbt_get_class(env, "org/simgrid/msg/As");
29 return env->NewObject(cls, jas_method_As_constructor);
32 jobject jas_ref(JNIEnv * env, jobject jas) {
33 return env->NewGlobalRef(jas);
36 void jas_unref(JNIEnv * env, jobject jas) {
37 env->DeleteGlobalRef(jas);
40 void jas_bind(jobject jas, simgrid::s4u::NetZone* netzone, JNIEnv* env)
42 env->SetLongField(jas, jas_field_As_bind, (jlong)(uintptr_t)(netzone));
45 simgrid::s4u::NetZone* jas_get_native(JNIEnv* env, jobject jas)
47 return (simgrid::s4u::NetZone*)(uintptr_t)env->GetLongField(jas, jas_field_As_bind);
50 JNIEXPORT void JNICALL Java_org_simgrid_msg_As_nativeInit(JNIEnv *env, jclass cls) {
51 jclass class_As = env->FindClass("org/simgrid/msg/As");
52 jas_method_As_constructor = env->GetMethodID(class_As, "<init>", "()V");
53 jas_field_As_bind = jxbt_get_jfield(env,class_As, "bind", "J");
54 if (!class_As || !jas_method_As_constructor || !jas_field_As_bind) {
55 jxbt_throw_native(env,bprintf("Can't find some fields in Java class. You should report this bug."));
59 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_As_getName(JNIEnv * env, jobject jas) {
60 simgrid::s4u::NetZone* as = jas_get_native(env, jas);
61 return env->NewStringUTF(as->name());
64 JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_As_getSons(JNIEnv * env, jobject jas) {
68 simgrid::s4u::NetZone* tmp_as;
69 simgrid::s4u::NetZone* self_as = jas_get_native(env, jas);
71 xbt_dict_t dict = self_as->children();
72 int count = xbt_dict_length(dict);
73 jclass cls = env->FindClass("org/simgrid/msg/As");
78 jtable = env->NewObjectArray(static_cast<jsize>(count), cls, nullptr);
81 jxbt_throw_jni(env, "Hosts table allocation failed");
85 xbt_dict_cursor_t cursor=nullptr;
88 xbt_dict_foreach(dict,cursor,key,tmp_as) {
89 tmp_jas = jas_new_instance(env);
91 jxbt_throw_jni(env, "java As instantiation failed");
94 tmp_jas = jas_ref(env, tmp_jas);
96 jxbt_throw_jni(env, "new global ref allocation failed");
99 jas_bind(tmp_jas, tmp_as, env);
101 env->SetObjectArrayElement(jtable, index, tmp_jas);
107 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_As_getProperty(JNIEnv *env, jobject jas, jobject jname) {
108 simgrid::s4u::NetZone* as = jas_get_native(env, jas);
111 jxbt_throw_notbound(env, "as", jas);
114 const char *name = env->GetStringUTFChars(static_cast<jstring>(jname), 0);
116 const char *property = MSG_environment_as_get_property_value(as, name);
121 jobject jproperty = env->NewStringUTF(property);
123 env->ReleaseStringUTFChars(static_cast<jstring>(jname), name);
128 JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_As_getHosts(JNIEnv * env, jobject jas)
134 simgrid::s4u::NetZone* as = jas_get_native(env, jas);
136 xbt_dynar_t table = as->hosts();
137 int count = xbt_dynar_length(table);
139 jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
144 jtable = env->NewObjectArray(static_cast<jsize>(count), cls, nullptr);
147 jxbt_throw_jni(env, "Hosts table allocation failed");
151 for (int index = 0; index < count; index++) {
152 host = xbt_dynar_get_as(table,index,msg_host_t);
154 jhost = static_cast<jobject>(host->extension(JAVA_HOST_LEVEL));
156 jname = env->NewStringUTF(host->cname());
158 jhost = Java_org_simgrid_msg_Host_getByName(env, cls, jname);
160 env->ReleaseStringUTFChars(static_cast<jstring>(jname), host->cname());
163 env->SetObjectArrayElement(jtable, index, jhost);
165 xbt_dynar_free(&table);