Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use simgrid::Host instead of xbt_dictelt_t for root main object
[simgrid.git] / src / bindings / java / jmsg_storage.cpp
1 /* Functions related to the java storage API.                            */
2
3 /* Copyright (c) 2012-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 <surf/surf_routing.h>
11
12 #include "simgrid/msg.h"
13 #include "jmsg.h"
14 #include "jmsg_storage.h"
15 #include "jxbt_utilities.h"
16
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
18
19 static jmethodID jstorage_method_Storage_constructor;
20 static jfieldID jstorage_field_Storage_bind;
21 static jfieldID jstorage_field_Storage_name;
22
23 jobject jstorage_new_instance(JNIEnv * env) {
24   jclass cls = jxbt_get_class(env, "org/simgrid/msg/Storage");
25   return env->NewObject(cls, jstorage_method_Storage_constructor);
26 }
27
28 msg_storage_t jstorage_get_native(JNIEnv * env, jobject jstorage) {
29   return (msg_storage_t) (uintptr_t) env->GetLongField(jstorage, jstorage_field_Storage_bind);
30 }
31
32 JNIEXPORT void JNICALL
33 Java_org_simgrid_msg_Storage_nativeInit(JNIEnv *env, jclass cls) {
34   jclass class_Storage = env->FindClass("org/simgrid/msg/Storage");
35   jstorage_method_Storage_constructor = env->GetMethodID(class_Storage, "<init>", "()V");
36   jstorage_field_Storage_bind = jxbt_get_jfield(env,class_Storage, "bind", "J");
37   jstorage_field_Storage_name = jxbt_get_jfield(env, class_Storage, "name", "Ljava/lang/String;");
38   if (!class_Storage || !jstorage_field_Storage_name || !jstorage_method_Storage_constructor || !jstorage_field_Storage_bind) {
39     jxbt_throw_native(env,bprintf("Can't find some fields in Java class. You should report this bug."));
40   }
41 }
42
43 void jstorage_bind(jobject jstorage, msg_storage_t storage, JNIEnv * env) {
44   env->SetLongField(jstorage, jstorage_field_Storage_bind, (jlong) (uintptr_t) (storage));
45 }
46
47 jobject jstorage_ref(JNIEnv * env, jobject jstorage) {
48   return env->NewGlobalRef(jstorage);
49 }
50
51 void jstorage_unref(JNIEnv * env, jobject jstorage) {
52   env->DeleteGlobalRef(jstorage);
53 }
54
55 const char *jstorage_get_name(jobject jstorage, JNIEnv * env) {
56   msg_storage_t storage = jstorage_get_native(env, jstorage);
57   return MSG_storage_get_name(storage);
58 }
59
60 JNIEXPORT jobject JNICALL
61 Java_org_simgrid_msg_Storage_getByName(JNIEnv * env, jclass cls,
62                                          jstring jname) {
63   msg_storage_t storage;
64   jobject jstorage;
65
66   /* get the C string from the java string */
67   if (jname == NULL) {
68         jxbt_throw_null(env,bprintf("No host can have a null name"));
69         return NULL;
70   }
71   const char *name = env->GetStringUTFChars(jname, 0);
72   storage = MSG_storage_get_by_name(name);
73
74   if (!storage) {                  /* invalid name */
75     jxbt_throw_storage_not_found(env, name);
76     env->ReleaseStringUTFChars(jname, name);
77     return NULL;
78   }
79   env->ReleaseStringUTFChars(jname, name);
80
81   if (!xbt_lib_get_level(storage, JAVA_STORAGE_LEVEL)) {       /* native host not associated yet with java host */
82
83     /* Instantiate a new java storage */
84     jstorage = jstorage_new_instance(env);
85
86     if (!jstorage) {
87       jxbt_throw_jni(env, "java storage instantiation failed");
88       return NULL;
89     }
90
91     /* get a global reference to the newly created storage */
92     jstorage = jstorage_ref(env, jstorage);
93
94     if (!jstorage) {
95       jxbt_throw_jni(env, "new global ref allocation failed");
96       return NULL;
97     }
98     /* Sets the java storage name */
99     env->SetObjectField(jstorage, jstorage_field_Storage_name, jname);
100     /* bind the java storage and the native storage */
101     jstorage_bind(jstorage, storage, env);
102
103     /* the native storage data field is set with the global reference to the
104      * java storage returned by this function
105      */
106     xbt_lib_set(storage_lib, storage->key, JAVA_STORAGE_LEVEL, (void *) jstorage);
107   }
108
109   /* return the global reference to the java storage instance */
110   return (jobject) xbt_lib_get_level(storage, JAVA_STORAGE_LEVEL);
111 }
112
113
114 JNIEXPORT jlong JNICALL
115 Java_org_simgrid_msg_Storage_getSize(JNIEnv * env,jobject jstorage) {
116   msg_storage_t storage = jstorage_get_native(env, jstorage);
117
118   if (!storage) {
119     jxbt_throw_notbound(env, "storage", jstorage);
120     return -1;
121   }
122
123   return (jlong) MSG_storage_get_size(storage);
124 }
125
126 JNIEXPORT jlong JNICALL
127 Java_org_simgrid_msg_Storage_getFreeSize(JNIEnv * env,jobject jstorage) {
128   msg_storage_t storage = jstorage_get_native(env, jstorage);
129
130   if (!storage) {
131     jxbt_throw_notbound(env, "storage", jstorage);
132     return -1;
133   }
134
135   return (jlong) MSG_storage_get_free_size(storage);
136 }
137
138 JNIEXPORT jlong JNICALL
139 Java_org_simgrid_msg_Storage_getUsedSize(JNIEnv * env,jobject jstorage) {
140   msg_storage_t storage = jstorage_get_native(env, jstorage);
141
142   if (!storage) {
143     jxbt_throw_notbound(env, "storage", jstorage);
144     return -1;
145   }
146
147   return (jlong) MSG_storage_get_used_size(storage);
148 }
149
150 JNIEXPORT jobject JNICALL
151 Java_org_simgrid_msg_Storage_getProperty(JNIEnv *env, jobject jstorage, jobject jname) {
152   msg_storage_t storage = jstorage_get_native(env, jstorage);
153
154   if (!storage) {
155     jxbt_throw_notbound(env, "storage", jstorage);
156     return NULL;
157   }
158   const char *name = env->GetStringUTFChars((jstring) jname, 0);
159
160   const char *property = MSG_storage_get_property_value(storage, name);
161   if (!property) {
162     return NULL;
163   }
164   jobject jproperty = env->NewStringUTF(property);
165
166   env->ReleaseStringUTFChars((jstring) jname, name);
167
168   return jproperty;
169 }
170
171 JNIEXPORT void JNICALL
172 Java_org_simgrid_msg_Storage_setProperty(JNIEnv *env, jobject jstorage, jobject jname, jobject jvalue) {
173         msg_storage_t storage = jstorage_get_native(env, jstorage);
174
175   if (!storage) {
176     jxbt_throw_notbound(env, "storage", jstorage);
177     return;
178   }
179   const char *name = env->GetStringUTFChars((jstring) jname, 0);
180   const char *value_java = env->GetStringUTFChars((jstring) jvalue, 0);
181   char *value = xbt_strdup(value_java);
182
183   MSG_storage_set_property_value(storage, name, value, xbt_free_f);
184
185   env->ReleaseStringUTFChars((jstring) jvalue, value);
186   env->ReleaseStringUTFChars((jstring) jname, name);
187
188 }
189
190 JNIEXPORT jobject JNICALL
191 Java_org_simgrid_msg_Storage_getHost(JNIEnv * env,jobject jstorage) {
192   msg_storage_t storage = jstorage_get_native(env, jstorage);
193
194   if (!storage) {
195     jxbt_throw_notbound(env, "storage", jstorage);
196     return NULL;
197   }
198   const char *host_name = MSG_storage_get_host(storage);
199   if (!host_name) {
200     return NULL;
201   }
202   jobject jhost_name = env->NewStringUTF(host_name);
203
204   return jhost_name;
205 }
206
207 JNIEXPORT jobjectArray JNICALL
208 Java_org_simgrid_msg_Storage_all(JNIEnv * env, jclass cls_arg)
209 {
210   int index;
211   jobjectArray jtable;
212   jobject jstorage;
213   jstring jname;
214   msg_storage_t storage;
215
216   xbt_dynar_t table =  MSG_storages_as_dynar();
217   int count = xbt_dynar_length(table);
218
219   jclass cls = jxbt_get_class(env, "org/simgrid/msg/Storage");
220
221   if (!cls) {
222     return NULL;
223   }
224
225   jtable = env->NewObjectArray((jsize) count, cls, NULL);
226
227   if (!jtable) {
228     jxbt_throw_jni(env, "Storages table allocation failed");
229     return NULL;
230   }
231
232   for (index = 0; index < count; index++) {
233     storage = xbt_dynar_get_as(table,index,msg_storage_t);
234     jstorage = (jobject) (xbt_lib_get_level(storage, JAVA_STORAGE_LEVEL));
235
236     if (!jstorage) {
237       jname = env->NewStringUTF(MSG_storage_get_name(storage));
238       jstorage = Java_org_simgrid_msg_Storage_getByName(env, cls_arg, jname);
239     }
240
241     env->SetObjectArrayElement(jtable, index, jstorage);
242   }
243   xbt_dynar_free(&table);
244   return jtable;
245 }