X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/0424e24e1f270b3c65b20e994079679b18be3eb8..b8df87e176f27b25534f27d7e240defa32ca35bc:/src/bindings/java/jmsg_as.cpp diff --git a/src/bindings/java/jmsg_as.cpp b/src/bindings/java/jmsg_as.cpp index 0a3f1c329d..5d9b460b58 100644 --- a/src/bindings/java/jmsg_as.cpp +++ b/src/bindings/java/jmsg_as.cpp @@ -1,48 +1,46 @@ -/* Functions related to the java host instances. */ +/* Java bindings of the NetZones. */ -/* Copyright (c) 2007-2015. The SimGrid Team. - * All rights reserved. */ +/* Copyright (c) 2007-2019. The SimGrid Team. All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ -#include -#include -#include - +#include "simgrid/kernel/routing/NetZoneImpl.hpp" +#include "simgrid/s4u/Host.hpp" #include "simgrid/s4u/NetZone.hpp" -#include -#include "simgrid/msg.h" -#include "jmsg_as.h" +#include "jmsg.hpp" +#include "jmsg_as.hpp" #include "jmsg_host.h" -#include "jxbt_utilities.h" -#include "jmsg.h" +#include "jxbt_utilities.hpp" -XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg); +XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(java); static jmethodID jas_method_As_constructor; static jfieldID jas_field_As_bind; -jobject jas_new_instance(JNIEnv * env) { +jobject jnetzone_new_instance(JNIEnv* env) +{ jclass cls = jxbt_get_class(env, "org/simgrid/msg/As"); return env->NewObject(cls, jas_method_As_constructor); } -jobject jas_ref(JNIEnv * env, jobject jas) { +jobject jnetzone_ref(JNIEnv* env, jobject jas) +{ return env->NewGlobalRef(jas); } -void jas_unref(JNIEnv * env, jobject jas) { +void jnetzone_unref(JNIEnv* env, jobject jas) +{ env->DeleteGlobalRef(jas); } -void jas_bind(jobject jas, simgrid::s4u::NetZone* netzone, JNIEnv* env) +void jnetzone_bind(jobject jas, simgrid::s4u::NetZone* netzone, JNIEnv* env) { env->SetLongField(jas, jas_field_As_bind, (jlong)(uintptr_t)(netzone)); } -simgrid::s4u::NetZone* jas_get_native(JNIEnv* env, jobject jas) +simgrid::s4u::NetZone* jnetzone_get_native(JNIEnv* env, jobject jas) { return (simgrid::s4u::NetZone*)(uintptr_t)env->GetLongField(jas, jas_field_As_bind); } @@ -57,46 +55,39 @@ JNIEXPORT void JNICALL Java_org_simgrid_msg_As_nativeInit(JNIEnv* env, jclass cl } JNIEXPORT jobject JNICALL Java_org_simgrid_msg_As_getName(JNIEnv * env, jobject jas) { - simgrid::s4u::NetZone* as = jas_get_native(env, jas); - return env->NewStringUTF(as->name()); + simgrid::s4u::NetZone* as = jnetzone_get_native(env, jas); + return env->NewStringUTF(as->get_cname()); } JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_As_getSons(JNIEnv * env, jobject jas) { int index = 0; jobjectArray jtable; - jobject tmp_jas; - simgrid::s4u::NetZone* tmp_as; - simgrid::s4u::NetZone* self_as = jas_get_native(env, jas); + sg_netzone_t self_as = jnetzone_get_native(env, jas); - xbt_dict_t dict = self_as->children(); - int count = xbt_dict_length(dict); jclass cls = env->FindClass("org/simgrid/msg/As"); - if (!cls) + if (not cls) return nullptr; - jtable = env->NewObjectArray(static_cast(count), cls, nullptr); + jtable = env->NewObjectArray(static_cast(self_as->get_children().size()), cls, nullptr); - if (!jtable) { + if (not jtable) { jxbt_throw_jni(env, "Hosts table allocation failed"); return nullptr; } - xbt_dict_cursor_t cursor=nullptr; - char *key; - - xbt_dict_foreach(dict,cursor,key,tmp_as) { - tmp_jas = jas_new_instance(env); - if (!tmp_jas) { + for (auto const& tmp_as : self_as->get_children()) { + jobject tmp_jas = jnetzone_new_instance(env); + if (not tmp_jas) { jxbt_throw_jni(env, "java As instantiation failed"); return nullptr; } - tmp_jas = jas_ref(env, tmp_jas); - if (!tmp_jas) { + tmp_jas = jnetzone_ref(env, tmp_jas); + if (not tmp_jas) { jxbt_throw_jni(env, "new global ref allocation failed"); return nullptr; } - jas_bind(tmp_jas, tmp_as, env); + jnetzone_bind(tmp_jas, tmp_as, env); env->SetObjectArrayElement(jtable, index, tmp_jas); index++; @@ -105,16 +96,16 @@ JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_As_getSons(JNIEnv * env, job } JNIEXPORT jobject JNICALL Java_org_simgrid_msg_As_getProperty(JNIEnv *env, jobject jas, jobject jname) { - simgrid::s4u::NetZone* as = jas_get_native(env, jas); + simgrid::s4u::NetZone* as = jnetzone_get_native(env, jas); - if (!as) { + if (not as) { jxbt_throw_notbound(env, "as", jas); return nullptr; } const char *name = env->GetStringUTFChars(static_cast(jname), 0); - const char *property = MSG_environment_as_get_property_value(as, name); - if (!property) { + const char* property = MSG_zone_get_property_value(as, name); + if (not property) { return nullptr; } @@ -130,38 +121,34 @@ JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_As_getHosts(JNIEnv * env, jo jobjectArray jtable; jobject jhost; jstring jname; - msg_host_t host; - simgrid::s4u::NetZone* as = jas_get_native(env, jas); - - xbt_dynar_t table = as->hosts(); - int count = xbt_dynar_length(table); + simgrid::s4u::NetZone* as = jnetzone_get_native(env, jas); jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host"); - - if (!cls) + if (not cls) return nullptr; - jtable = env->NewObjectArray(static_cast(count), cls, nullptr); + std::vector table = as->get_all_hosts(); + + jtable = env->NewObjectArray(static_cast(table.size()), cls, nullptr); - if (!jtable) { + if (not jtable) { jxbt_throw_jni(env, "Hosts table allocation failed"); return nullptr; } - for (int index = 0; index < count; index++) { - host = xbt_dynar_get_as(table,index,msg_host_t); - + int index = 0; + for (auto const& host : table) { jhost = static_cast(host->extension(JAVA_HOST_LEVEL)); - if (!jhost) { - jname = env->NewStringUTF(host->cname()); + if (not jhost) { + jname = env->NewStringUTF(host->get_cname()); jhost = Java_org_simgrid_msg_Host_getByName(env, cls, jname); - env->ReleaseStringUTFChars(static_cast(jname), host->cname()); + env->ReleaseStringUTFChars(static_cast(jname), host->get_cname()); } env->SetObjectArrayElement(jtable, index, jhost); + index++; } - xbt_dynar_free(&table); return jtable; }