Logo AND Algorithmique Numérique Distribuée

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