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);
26 static jmethodID jas_method_As_constructor;
27 static jfieldID jas_field_As_bind;
29 jobject jas_new_instance(JNIEnv * env) {
30 jclass cls = jxbt_get_class(env, "org/simgrid/msg/As");
31 return env->NewObject(cls, jas_method_As_constructor);
34 jobject jas_ref(JNIEnv * env, jobject jas) {
35 return env->NewGlobalRef(jas);
38 void jas_unref(JNIEnv * env, jobject jas) {
39 env->DeleteGlobalRef(jas);
42 void jas_bind(jobject jas, simgrid::s4u::NetZone* netzone, JNIEnv* env)
44 env->SetLongField(jas, jas_field_As_bind, (jlong)(uintptr_t)(netzone));
47 simgrid::s4u::NetZone* jas_get_native(JNIEnv* env, jobject jas)
49 return (simgrid::s4u::NetZone*)(uintptr_t)env->GetLongField(jas, jas_field_As_bind);
52 JNIEXPORT void JNICALL Java_org_simgrid_msg_As_nativeInit(JNIEnv* env, jclass cls)
54 jclass class_As = env->FindClass("org/simgrid/msg/As");
55 jas_method_As_constructor = env->GetMethodID(class_As, "<init>", "()V");
56 jas_field_As_bind = jxbt_get_jfield(env,class_As, "bind", "J");
57 xbt_assert(class_As && jas_method_As_constructor && jas_field_As_bind,
58 "Native initialization of msg/AS failed. Please report that bug");
61 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_As_getName(JNIEnv * env, jobject jas) {
62 simgrid::s4u::NetZone* as = jas_get_native(env, jas);
63 return env->NewStringUTF(as->name());
66 JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_As_getSons(JNIEnv * env, jobject jas) {
70 simgrid::s4u::NetZone* tmp_as;
71 simgrid::s4u::NetZone* self_as = jas_get_native(env, jas);
73 xbt_dict_t dict = self_as->children();
74 int count = xbt_dict_length(dict);
75 jclass cls = env->FindClass("org/simgrid/msg/As");
80 jtable = env->NewObjectArray(static_cast<jsize>(count), cls, nullptr);
83 jxbt_throw_jni(env, "Hosts table allocation failed");
87 xbt_dict_cursor_t cursor=nullptr;
90 xbt_dict_foreach(dict,cursor,key,tmp_as) {
91 tmp_jas = jas_new_instance(env);
93 jxbt_throw_jni(env, "java As instantiation failed");
96 tmp_jas = jas_ref(env, tmp_jas);
98 jxbt_throw_jni(env, "new global ref allocation failed");
101 jas_bind(tmp_jas, tmp_as, env);
103 env->SetObjectArrayElement(jtable, index, tmp_jas);
109 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_As_getProperty(JNIEnv *env, jobject jas, jobject jname) {
110 simgrid::s4u::NetZone* as = jas_get_native(env, jas);
113 jxbt_throw_notbound(env, "as", jas);
116 const char *name = env->GetStringUTFChars(static_cast<jstring>(jname), 0);
118 const char *property = MSG_environment_as_get_property_value(as, name);
123 jobject jproperty = env->NewStringUTF(property);
125 env->ReleaseStringUTFChars(static_cast<jstring>(jname), name);
130 JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_As_getHosts(JNIEnv * env, jobject jas)
136 simgrid::s4u::NetZone* as = jas_get_native(env, jas);
138 xbt_dynar_t table = as->hosts();
139 int count = xbt_dynar_length(table);
141 jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
146 jtable = env->NewObjectArray(static_cast<jsize>(count), cls, nullptr);
149 jxbt_throw_jni(env, "Hosts table allocation failed");
153 for (int index = 0; index < count; index++) {
154 host = xbt_dynar_get_as(table,index,msg_host_t);
156 jhost = static_cast<jobject>(host->extension(JAVA_HOST_LEVEL));
158 jname = env->NewStringUTF(host->cname());
160 jhost = Java_org_simgrid_msg_Host_getByName(env, cls, jname);
162 env->ReleaseStringUTFChars(static_cast<jstring>(jname), host->cname());
165 env->SetObjectArrayElement(jtable, index, jhost);
167 xbt_dynar_free(&table);