Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
rename all MSG_zone_* functions (backward compatible)
[simgrid.git] / src / bindings / java / jmsg_as.cpp
1 /* Java bindings of the NetZones.                                           */
2
3 /* Copyright (c) 2007-2017. The SimGrid Team. All rights reserved.          */
4
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. */
7
8 #include "simgrid/s4u/Host.hpp"
9 #include "simgrid/s4u/NetZone.hpp"
10 #include "src/kernel/routing/NetZoneImpl.hpp"
11
12 #include "jmsg_as.h"
13 #include "jmsg_host.h"
14 #include "jxbt_utilities.h"
15 #include "jmsg.h"
16
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(java);
18
19 SG_BEGIN_DECL()
20
21 static jmethodID jas_method_As_constructor;
22 static jfieldID jas_field_As_bind;
23
24 jobject jnetzone_new_instance(JNIEnv* env)
25 {
26   jclass cls = jxbt_get_class(env, "org/simgrid/msg/As");
27   return env->NewObject(cls, jas_method_As_constructor);
28 }
29
30 jobject jnetzone_ref(JNIEnv* env, jobject jas)
31 {
32   return env->NewGlobalRef(jas);
33 }
34
35 void jnetzone_unref(JNIEnv* env, jobject jas)
36 {
37   env->DeleteGlobalRef(jas);
38 }
39
40 void jnetzone_bind(jobject jas, simgrid::s4u::NetZone* netzone, JNIEnv* env)
41 {
42   env->SetLongField(jas, jas_field_As_bind, (jlong)(uintptr_t)(netzone));
43 }
44
45 simgrid::s4u::NetZone* jnetzone_get_native(JNIEnv* env, jobject jas)
46 {
47   return (simgrid::s4u::NetZone*)(uintptr_t)env->GetLongField(jas, jas_field_As_bind);
48 }
49
50 JNIEXPORT void JNICALL Java_org_simgrid_msg_As_nativeInit(JNIEnv* env, jclass cls)
51 {
52   jclass class_As = env->FindClass("org/simgrid/msg/As");
53   jas_method_As_constructor = env->GetMethodID(class_As, "<init>", "()V");
54   jas_field_As_bind = jxbt_get_jfield(env,class_As, "bind", "J");
55   xbt_assert(class_As && jas_method_As_constructor && jas_field_As_bind,
56              "Native initialization of msg/AS failed. Please report that bug");
57 }
58
59 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_As_getName(JNIEnv * env, jobject jas) {
60   simgrid::s4u::NetZone* as = jnetzone_get_native(env, jas);
61   return env->NewStringUTF(as->name());
62 }
63
64 JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_As_getSons(JNIEnv * env, jobject jas) {
65   int index = 0;
66   jobjectArray jtable;
67   sg_netzone_t self_as = jnetzone_get_native(env, jas);
68
69   jclass cls = env->FindClass("org/simgrid/msg/As");
70
71   if (!cls)
72     return nullptr;
73
74   jtable = env->NewObjectArray(static_cast<jsize>(self_as->children()->size()), cls, nullptr);
75
76   if (!jtable) {
77     jxbt_throw_jni(env, "Hosts table allocation failed");
78     return nullptr;
79   }
80
81   for (auto tmp_as : *self_as->children()) {
82     jobject tmp_jas = jnetzone_new_instance(env);
83     if (!tmp_jas) {
84       jxbt_throw_jni(env, "java As instantiation failed");
85       return nullptr;
86     }
87     tmp_jas = jnetzone_ref(env, tmp_jas);
88     if (!tmp_jas) {
89       jxbt_throw_jni(env, "new global ref allocation failed");
90       return nullptr;
91     }
92     jnetzone_bind(tmp_jas, tmp_as, env);
93
94     env->SetObjectArrayElement(jtable, index, tmp_jas);
95     index++;
96   }
97   return jtable;
98 }
99
100 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_As_getProperty(JNIEnv *env, jobject jas, jobject jname) {
101   simgrid::s4u::NetZone* as = jnetzone_get_native(env, jas);
102
103   if (!as) {
104     jxbt_throw_notbound(env, "as", jas);
105     return nullptr;
106   }
107   const char *name = env->GetStringUTFChars(static_cast<jstring>(jname), 0);
108
109   const char* property = MSG_zone_get_property_value(as, name);
110   if (!property) {
111     return nullptr;
112   }
113
114   jobject jproperty = env->NewStringUTF(property);
115
116   env->ReleaseStringUTFChars(static_cast<jstring>(jname), name);
117
118   return jproperty;
119 }
120
121 JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_As_getHosts(JNIEnv * env, jobject jas)
122 {
123   jobjectArray jtable;
124   jobject jhost;
125   jstring jname;
126   simgrid::s4u::NetZone* as = jnetzone_get_native(env, jas);
127
128   jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
129   std::vector<sg_host_t>* table = as->hosts();
130   if (!cls)
131     return nullptr;
132
133   jtable = env->NewObjectArray(static_cast<jsize>(table->size()), cls, nullptr);
134
135   if (!jtable) {
136     jxbt_throw_jni(env, "Hosts table allocation failed");
137     return nullptr;
138   }
139
140   int index = 0;
141   for (auto host : *table) {
142     jhost = static_cast<jobject>(host->extension(JAVA_HOST_LEVEL));
143     if (!jhost) {
144       jname = env->NewStringUTF(host->cname());
145
146       jhost = Java_org_simgrid_msg_Host_getByName(env, cls, jname);
147
148       env->ReleaseStringUTFChars(static_cast<jstring>(jname), host->cname());
149     }
150
151     env->SetObjectArrayElement(jtable, index, jhost);
152     index++;
153   }
154   return jtable;
155 }
156
157 SG_END_DECL()