Logo AND Algorithmique Numérique Distribuée

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