Logo AND Algorithmique Numérique Distribuée

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