1 /* Java bindings of the NetZones. */
3 /* Copyright (c) 2007-2017. The SimGrid Team. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
8 #include "simgrid/s4u/NetZone.hpp"
9 #include "simgrid/s4u/host.hpp"
12 #include "jmsg_host.h"
13 #include "jxbt_utilities.h"
16 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
20 static jmethodID jas_method_As_constructor;
21 static jfieldID jas_field_As_bind;
23 jobject jnetzone_new_instance(JNIEnv* env)
25 jclass cls = jxbt_get_class(env, "org/simgrid/msg/As");
26 return env->NewObject(cls, jas_method_As_constructor);
29 jobject jnetzone_ref(JNIEnv* env, jobject jas)
31 return env->NewGlobalRef(jas);
34 void jnetzone_unref(JNIEnv* env, jobject jas)
36 env->DeleteGlobalRef(jas);
39 void jnetzone_bind(jobject jas, simgrid::s4u::NetZone* netzone, JNIEnv* env)
41 env->SetLongField(jas, jas_field_As_bind, (jlong)(uintptr_t)(netzone));
44 simgrid::s4u::NetZone* jnetzone_get_native(JNIEnv* env, jobject jas)
46 return (simgrid::s4u::NetZone*)(uintptr_t)env->GetLongField(jas, jas_field_As_bind);
49 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 xbt_assert(class_As && jas_method_As_constructor && jas_field_As_bind,
55 "Native initialization of msg/AS failed. Please report that bug");
58 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_As_getName(JNIEnv * env, jobject jas) {
59 simgrid::s4u::NetZone* as = jnetzone_get_native(env, jas);
60 return env->NewStringUTF(as->name());
63 JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_As_getSons(JNIEnv * env, jobject jas) {
67 simgrid::s4u::NetZone* tmp_as;
68 simgrid::s4u::NetZone* self_as = jnetzone_get_native(env, jas);
70 xbt_dict_t dict = self_as->children();
71 int count = xbt_dict_length(dict);
72 jclass cls = env->FindClass("org/simgrid/msg/As");
77 jtable = env->NewObjectArray(static_cast<jsize>(count), cls, nullptr);
80 jxbt_throw_jni(env, "Hosts table allocation failed");
84 xbt_dict_cursor_t cursor=nullptr;
87 xbt_dict_foreach(dict,cursor,key,tmp_as) {
88 tmp_jas = jnetzone_new_instance(env);
90 jxbt_throw_jni(env, "java As instantiation failed");
93 tmp_jas = jnetzone_ref(env, tmp_jas);
95 jxbt_throw_jni(env, "new global ref allocation failed");
98 jnetzone_bind(tmp_jas, tmp_as, env);
100 env->SetObjectArrayElement(jtable, index, tmp_jas);
106 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_As_getProperty(JNIEnv *env, jobject jas, jobject jname) {
107 simgrid::s4u::NetZone* as = jnetzone_get_native(env, jas);
110 jxbt_throw_notbound(env, "as", jas);
113 const char *name = env->GetStringUTFChars(static_cast<jstring>(jname), 0);
115 const char *property = MSG_environment_as_get_property_value(as, name);
120 jobject jproperty = env->NewStringUTF(property);
122 env->ReleaseStringUTFChars(static_cast<jstring>(jname), name);
127 JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_As_getHosts(JNIEnv * env, jobject jas)
133 simgrid::s4u::NetZone* as = jnetzone_get_native(env, jas);
135 xbt_dynar_t table = as->hosts();
136 int count = xbt_dynar_length(table);
138 jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
143 jtable = env->NewObjectArray(static_cast<jsize>(count), cls, nullptr);
146 jxbt_throw_jni(env, "Hosts table allocation failed");
150 for (int index = 0; index < count; index++) {
151 host = xbt_dynar_get_as(table,index,msg_host_t);
153 jhost = static_cast<jobject>(host->extension(JAVA_HOST_LEVEL));
155 jname = env->NewStringUTF(host->cname());
157 jhost = Java_org_simgrid_msg_Host_getByName(env, cls, jname);
159 env->ReleaseStringUTFChars(static_cast<jstring>(jname), host->cname());
162 env->SetObjectArrayElement(jtable, index, jhost);
164 xbt_dynar_free(&table);