Logo AND Algorithmique Numérique Distribuée

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