Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b2fe858b0a816e8b018862884633cb36a958e75a
[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 "simgrid/msg.h"
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(jmsg);
18
19 static jmethodID jas_method_As_constructor;
20 static jfieldID jas_field_As_bind;
21
22 jobject jas_new_instance(JNIEnv * env) {
23   jclass cls = jxbt_get_class(env, "org/simgrid/msg/As");
24   return env->NewObject(cls, jas_method_As_constructor);
25 }
26
27 jobject jas_ref(JNIEnv * env, jobject jas) {
28   return env->NewGlobalRef(jas);
29 }
30
31 void jas_unref(JNIEnv * env, jobject jas) {
32   env->DeleteGlobalRef(jas);
33 }
34
35 void jas_bind(jobject jas, msg_as_t as, JNIEnv * env) {
36   env->SetLongField(jas, jas_field_As_bind, (jlong) (uintptr_t) (as));
37 }
38
39 msg_as_t jas_get_native(JNIEnv * env, jobject jas) {
40   return (msg_as_t) (uintptr_t) env->GetLongField(jas, jas_field_As_bind);
41 }
42
43 JNIEXPORT void JNICALL Java_org_simgrid_msg_As_nativeInit(JNIEnv *env, jclass cls) {
44   jclass class_As = env->FindClass("org/simgrid/msg/As");
45   jas_method_As_constructor = env->GetMethodID(class_As, "<init>", "()V");
46   jas_field_As_bind = jxbt_get_jfield(env,class_As, "bind", "J");
47   if (!class_As || !jas_method_As_constructor || !jas_field_As_bind) {
48     jxbt_throw_native(env,bprintf("Can't find some fields in Java class. You should report this bug."));
49   }
50 }
51
52 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_As_getName(JNIEnv * env, jobject jas) {
53   msg_as_t as = jas_get_native(env, jas);
54   return env->NewStringUTF(MSG_environment_as_get_name(as));
55 }
56
57 JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_As_getSons(JNIEnv * env, jobject jas) {
58   int index = 0;
59   jobjectArray jtable;
60   jobject tmp_jas;
61   msg_as_t tmp_as;
62   msg_as_t self_as = jas_get_native(env, jas);
63   
64   xbt_dict_t dict = MSG_environment_as_get_routing_sons(self_as);
65   int count = xbt_dict_length(dict);
66   jclass cls = env->FindClass("org/simgrid/msg/As");
67
68   if (!cls) {
69     return NULL;
70   }
71
72   jtable = env->NewObjectArray((jsize) count, cls, NULL);
73
74   if (!jtable) {
75     jxbt_throw_jni(env, "Hosts table allocation failed");
76     return NULL;
77   }
78
79   xbt_dict_cursor_t cursor=NULL;
80   char *key;
81
82   xbt_dict_foreach(dict,cursor,key,tmp_as) {
83     tmp_jas = jas_new_instance(env);
84     if (!tmp_jas) {
85       jxbt_throw_jni(env, "java As instantiation failed");
86       return NULL;
87     }
88     tmp_jas = jas_ref(env, tmp_jas);
89     if (!tmp_jas) {
90       jxbt_throw_jni(env, "new global ref allocation failed");
91       return NULL;
92     }
93     jas_bind(tmp_jas, tmp_as, env);
94
95     env->SetObjectArrayElement(jtable, index, tmp_jas);
96     index++;
97   }
98   return jtable;
99 }
100
101 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_As_getProperty(JNIEnv *env, jobject jas, jobject jname) {
102   msg_as_t as = jas_get_native(env, jas);
103
104   if (!as) {
105     jxbt_throw_notbound(env, "as", jas);
106     return NULL;
107   }
108   const char *name = env->GetStringUTFChars((jstring) jname, 0);
109
110   const char *property = MSG_environment_as_get_property_value(as, name);
111   if (!property) {
112     return NULL;
113   }
114
115   jobject jproperty = env->NewStringUTF(property);
116
117   env->ReleaseStringUTFChars((jstring) jname, name);
118
119   return jproperty;
120 }
121
122 JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_As_getHosts(JNIEnv * env, jobject jas)
123 {
124   int index;
125   jobjectArray jtable;
126   jobject jhost;
127   jstring jname;
128   msg_host_t host;
129   msg_as_t as = jas_get_native(env, jas);
130
131   xbt_dynar_t table =  MSG_environment_as_get_hosts(as);
132   int count = xbt_dynar_length(table);
133
134   jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
135
136   if (!cls) {
137     return NULL;
138   }
139
140   jtable = env->NewObjectArray((jsize) count, cls, NULL);
141
142   if (!jtable) {
143     jxbt_throw_jni(env, "Hosts table allocation failed");
144     return NULL;
145   }
146
147   for (index = 0; index < count; index++) {
148     host = xbt_dynar_get_as(table,index,msg_host_t);
149
150     jhost = (jobject) host->extension(JAVA_HOST_LEVEL);
151     if (!jhost) {
152       jname = env->NewStringUTF(MSG_host_get_name(host));
153
154       jhost = Java_org_simgrid_msg_Host_getByName(env, cls, jname);
155
156       /* FIXME: leak of jname ? */
157     }
158
159     env->SetObjectArrayElement(jtable, index, jhost);
160   }
161   xbt_dynar_free(&table);
162   return jtable;
163 }