Logo AND Algorithmique Numérique Distribuée

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