Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8124ac4a15bb681640ec6d12a5635e59692d174b
[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/NetZone.hpp"
9 #include "simgrid/s4u/host.hpp"
10
11 #include "jmsg_as.h"
12 #include "jmsg_host.h"
13 #include "jxbt_utilities.h"
14 #include "jmsg.h"
15
16 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
17
18 SG_BEGIN_DECL()
19
20 static jmethodID jas_method_As_constructor;
21 static jfieldID jas_field_As_bind;
22
23 jobject jnetzone_new_instance(JNIEnv* env)
24 {
25   jclass cls = jxbt_get_class(env, "org/simgrid/msg/As");
26   return env->NewObject(cls, jas_method_As_constructor);
27 }
28
29 jobject jnetzone_ref(JNIEnv* env, jobject jas)
30 {
31   return env->NewGlobalRef(jas);
32 }
33
34 void jnetzone_unref(JNIEnv* env, jobject jas)
35 {
36   env->DeleteGlobalRef(jas);
37 }
38
39 void jnetzone_bind(jobject jas, simgrid::s4u::NetZone* netzone, JNIEnv* env)
40 {
41   env->SetLongField(jas, jas_field_As_bind, (jlong)(uintptr_t)(netzone));
42 }
43
44 simgrid::s4u::NetZone* jnetzone_get_native(JNIEnv* env, jobject jas)
45 {
46   return (simgrid::s4u::NetZone*)(uintptr_t)env->GetLongField(jas, jas_field_As_bind);
47 }
48
49 JNIEXPORT void JNICALL Java_org_simgrid_msg_As_nativeInit(JNIEnv* env, jclass cls)
50 {
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");
56 }
57
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());
61 }
62
63 JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_As_getSons(JNIEnv * env, jobject jas) {
64   int index = 0;
65   jobjectArray jtable;
66   jobject tmp_jas;
67   simgrid::s4u::NetZone* tmp_as;
68   simgrid::s4u::NetZone* self_as = jnetzone_get_native(env, jas);
69
70   xbt_dict_t dict = self_as->children();
71   int count = xbt_dict_length(dict);
72   jclass cls = env->FindClass("org/simgrid/msg/As");
73
74   if (!cls)
75     return nullptr;
76
77   jtable = env->NewObjectArray(static_cast<jsize>(count), cls, nullptr);
78
79   if (!jtable) {
80     jxbt_throw_jni(env, "Hosts table allocation failed");
81     return nullptr;
82   }
83
84   xbt_dict_cursor_t cursor=nullptr;
85   char *key;
86
87   xbt_dict_foreach(dict,cursor,key,tmp_as) {
88     tmp_jas = jnetzone_new_instance(env);
89     if (!tmp_jas) {
90       jxbt_throw_jni(env, "java As instantiation failed");
91       return nullptr;
92     }
93     tmp_jas = jnetzone_ref(env, tmp_jas);
94     if (!tmp_jas) {
95       jxbt_throw_jni(env, "new global ref allocation failed");
96       return nullptr;
97     }
98     jnetzone_bind(tmp_jas, tmp_as, env);
99
100     env->SetObjectArrayElement(jtable, index, tmp_jas);
101     index++;
102   }
103   return jtable;
104 }
105
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);
108
109   if (!as) {
110     jxbt_throw_notbound(env, "as", jas);
111     return nullptr;
112   }
113   const char *name = env->GetStringUTFChars(static_cast<jstring>(jname), 0);
114
115   const char *property = MSG_environment_as_get_property_value(as, name);
116   if (!property) {
117     return nullptr;
118   }
119
120   jobject jproperty = env->NewStringUTF(property);
121
122   env->ReleaseStringUTFChars(static_cast<jstring>(jname), name);
123
124   return jproperty;
125 }
126
127 JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_As_getHosts(JNIEnv * env, jobject jas)
128 {
129   jobjectArray jtable;
130   jobject jhost;
131   jstring jname;
132   msg_host_t host;
133   simgrid::s4u::NetZone* as = jnetzone_get_native(env, jas);
134
135   xbt_dynar_t table = as->hosts();
136   int count = xbt_dynar_length(table);
137
138   jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
139
140   if (!cls)
141     return nullptr;
142
143   jtable = env->NewObjectArray(static_cast<jsize>(count), cls, nullptr);
144
145   if (!jtable) {
146     jxbt_throw_jni(env, "Hosts table allocation failed");
147     return nullptr;
148   }
149
150   for (int index = 0; index < count; index++) {
151     host = xbt_dynar_get_as(table,index,msg_host_t);
152
153     jhost = static_cast<jobject>(host->extension(JAVA_HOST_LEVEL));
154     if (!jhost) {
155       jname = env->NewStringUTF(host->cname());
156
157       jhost = Java_org_simgrid_msg_Host_getByName(env, cls, jname);
158
159       env->ReleaseStringUTFChars(static_cast<jstring>(jname), host->cname());
160     }
161
162     env->SetObjectArrayElement(jtable, index, jhost);
163   }
164   xbt_dynar_free(&table);
165   return jtable;
166 }
167
168 SG_END_DECL()