Logo AND Algorithmique Numérique Distribuée

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