Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f3cf6c49a07ad82866bca3345d484ee4be421876
[simgrid.git] / src / bindings / java / jmsg.cpp
1 /* Java Wrappers to the MSG API.                                            */
2
3 /* Copyright (c) 2007-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 <simgrid/msg.h>
10 #include <simgrid/simix.h>
11 #include <surf/surfxml_parse.h>
12 #include <locale.h>
13 #include <src/simix/smx_private.h>
14
15 #include "jmsg_process.h"
16
17 #include "jmsg_as.h"
18
19 #include "jmsg_host.h"
20 #include "jmsg_storage.h"
21 #include "jmsg_task.h"
22 #include "jxbt_utilities.h"
23
24 #include "jmsg.h"
25
26 #include "JavaContext.hpp"
27
28 /* Shut up some errors in eclipse online compiler. I wish such a pimple wouldn't be needed */
29 #ifndef JNIEXPORT
30 #define JNIEXPORT
31 #endif
32 #ifndef JNICALL
33 #define JNICALL
34 #endif
35 /* end of eclipse-mandated pimple */
36
37 SG_BEGIN_DECL()
38
39 int JAVA_HOST_LEVEL;
40 int JAVA_STORAGE_LEVEL;
41
42 static int create_jprocess(int argc, char *argv[]);
43
44 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
45
46 JavaVM *__java_vm = NULL;
47
48 JavaVM *get_java_VM(void)
49 {
50   return __java_vm;
51 }
52
53 JNIEnv *get_current_thread_env(void)
54 {
55   JNIEnv *env;
56
57   __java_vm->AttachCurrentThread((void **) &env, NULL);
58   return env;
59 }
60
61 void jmsg_throw_status(JNIEnv *env, msg_error_t status) {
62   switch (status) {
63     case MSG_TIMEOUT:
64         jxbt_throw_time_out_failure(env,NULL);
65     break;
66     case MSG_TRANSFER_FAILURE:
67         jxbt_throw_transfer_failure(env,NULL);
68     break;
69     case MSG_HOST_FAILURE:
70         jxbt_throw_host_failure(env,NULL);
71     break;
72     case MSG_TASK_CANCELED:
73         jxbt_throw_task_cancelled(env,NULL);
74     break;
75     default:
76         jxbt_throw_native(env,xbt_strdup("undefined message failed (please see jmsg_throw_status function in jmsg.c)"));
77   }
78 }
79
80
81 /***************************************************************************************
82  * Unsortable functions                                                        *
83  ***************************************************************************************/
84
85 JNIEXPORT jdouble JNICALL
86 Java_org_simgrid_msg_Msg_getClock(JNIEnv * env, jclass cls)
87 {
88   return (jdouble) MSG_get_clock();
89 }
90
91 static void __JAVA_host_priv_free(void *host)
92 {
93 }
94
95 static void __JAVA_storage_priv_free(void *storage)
96 {
97 }
98
99 JNIEXPORT void JNICALL
100 Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
101 {
102   char **argv = NULL;
103   int index;
104   int argc = 0;
105   jstring jval;
106   const char *tmp;
107
108   XBT_LOG_CONNECT(jmsg);
109   XBT_LOG_CONNECT(jtrace);
110
111   env->GetJavaVM(&__java_vm);
112
113   simgrid::simix::factory_initializer = simgrid::java::java_factory;
114   jthrowable exc = env->ExceptionOccurred();
115   if (exc) {
116     env->ExceptionClear();
117   }
118
119   setlocale(LC_NUMERIC,"C");
120
121   if (jargs)
122     argc = (int) env->GetArrayLength(jargs);
123
124   argc++;
125   argv = xbt_new(char *, argc + 1);
126   argv[0] = xbt_strdup("java");
127
128   for (index = 0; index < argc - 1; index++) {
129     jval = (jstring) env->GetObjectArrayElement(jargs, index);
130     tmp = env->GetStringUTFChars(jval, 0);
131     argv[index + 1] = xbt_strdup(tmp);
132     env->ReleaseStringUTFChars(jval, tmp);
133   }
134   argv[argc] = NULL;
135
136   MSG_init(&argc, argv);
137
138   JAVA_HOST_LEVEL = xbt_lib_add_level(host_lib, (void_f_pvoid_t) __JAVA_host_priv_free);
139   JAVA_STORAGE_LEVEL = xbt_lib_add_level(storage_lib, (void_f_pvoid_t) __JAVA_storage_priv_free);
140
141   for (index = 0; index < argc; index++)
142     free(argv[index]);
143
144   free(argv);
145 }
146
147 JNIEXPORT void JNICALL
148     JNICALL Java_org_simgrid_msg_Msg_run(JNIEnv * env, jclass cls)
149 {
150   msg_error_t rv;
151   xbt_dynar_t hosts, storages;
152   jobject jhost, jstorage;
153
154   /* Run everything */
155   XBT_DEBUG("Ready to run MSG_MAIN");
156   rv = MSG_main();
157   XBT_DEBUG("Done running MSG_MAIN");
158   jxbt_check_res("MSG_main()", rv, MSG_OK,
159                  xbt_strdup("unexpected error : MSG_main() failed .. please report this bug "));
160
161   XBT_INFO("MSG_main finished; Cleaning up the simulation...");
162   /* Cleanup java hosts */
163   hosts = MSG_hosts_as_dynar();
164   for (unsigned long index = 0; index < xbt_dynar_length(hosts) - 1; index++) {
165     jhost = (jobject) xbt_lib_get_level(xbt_dynar_get_as(hosts,index,msg_host_t), JAVA_HOST_LEVEL);
166     if (jhost)
167       jhost_unref(env, jhost);
168
169   }
170   xbt_dynar_free(&hosts);
171
172   /* Cleanup java storages */
173   storages = MSG_storages_as_dynar();
174   if(!xbt_dynar_is_empty(storages)){
175     for (unsigned long index = 0; index < xbt_dynar_length(storages) - 1; index++) {
176       jstorage = (jobject) xbt_lib_get_level(xbt_dynar_get_as(storages,index,msg_storage_t), JAVA_STORAGE_LEVEL);
177       if (jstorage)
178         jstorage_unref(env, jstorage);
179     }
180   }
181   xbt_dynar_free(&storages);
182
183 }
184
185 JNIEXPORT void JNICALL
186 Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
187                                        jstring jplatformFile)
188 {
189
190   const char *platformFile =
191       env->GetStringUTFChars(jplatformFile, 0);
192
193   MSG_create_environment(platformFile);
194
195   env->ReleaseStringUTFChars(jplatformFile, platformFile);
196 }
197
198 JNIEXPORT jobject JNICALL
199 Java_org_simgrid_msg_Msg_environmentGetRoutingRoot(JNIEnv * env, jclass cls)
200 {
201   msg_as_t as = MSG_environment_get_routing_root();
202   jobject jas = jas_new_instance(env);
203   if (!jas) {
204     jxbt_throw_jni(env, "java As instantiation failed");
205     return NULL;
206   }
207   jas = jas_ref(env, jas);
208   if (!jas) {
209     jxbt_throw_jni(env, "new global ref allocation failed");
210     return NULL;
211   }
212   jas_bind(jas, as, env);
213
214   return (jobject) jas;
215 }
216
217 JNIEXPORT void JNICALL
218 Java_org_simgrid_msg_Msg_debug(JNIEnv * env, jclass cls, jstring js)
219 {
220   const char *s = env->GetStringUTFChars(js, 0);
221   XBT_DEBUG("%s", s);
222   env->ReleaseStringUTFChars(js, s);
223 }
224 JNIEXPORT void JNICALL
225 Java_org_simgrid_msg_Msg_verb(JNIEnv * env, jclass cls, jstring js)
226 {
227   const char *s = env->GetStringUTFChars(js, 0);
228   XBT_VERB("%s", s);
229   env->ReleaseStringUTFChars(js, s);
230 }
231 JNIEXPORT void JNICALL
232 Java_org_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
233 {
234   const char *s = env->GetStringUTFChars(js, 0);
235   XBT_INFO("%s", s);
236   env->ReleaseStringUTFChars(js, s);
237 }
238 JNIEXPORT void JNICALL
239 Java_org_simgrid_msg_Msg_warn(JNIEnv * env, jclass cls, jstring js)
240 {
241   const char *s = env->GetStringUTFChars(js, 0);
242   XBT_WARN("%s", s);
243   env->ReleaseStringUTFChars(js, s);
244 }
245 JNIEXPORT void JNICALL
246 Java_org_simgrid_msg_Msg_error(JNIEnv * env, jclass cls, jstring js)
247 {
248   const char *s = env->GetStringUTFChars(js, 0);
249   XBT_ERROR("%s", s);
250   env->ReleaseStringUTFChars(js, s);
251 }
252 JNIEXPORT void JNICALL
253 Java_org_simgrid_msg_Msg_critical(JNIEnv * env, jclass cls, jstring js)
254 {
255   const char *s = env->GetStringUTFChars(js, 0);
256   XBT_CRITICAL("%s", s);
257   env->ReleaseStringUTFChars(js, s);
258 }
259 JNIEXPORT void JNICALL
260 Java_org_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
261                                        jstring jdeploymentFile)
262 {
263
264   const char *deploymentFile =
265       env->GetStringUTFChars(jdeploymentFile, 0);
266
267   SIMIX_function_register_default(create_jprocess);
268   MSG_launch_application(deploymentFile);
269 }
270 /**
271  * Function called when there is the need to create the java Process object
272  * (when we are using deployement files).
273  * it HAS to be executed on the process context, else really bad things will happen.
274  */
275 static int create_jprocess(int argc, char *argv[]) {
276   JNIEnv *env = get_current_thread_env();
277   //Change the "." in class name for "/".
278   xbt_str_subst(argv[0],'.','/',0);
279   jclass class_Process = env->FindClass(argv[0]);
280   xbt_str_subst(argv[0],'/','.',0);
281   //Retrieve the methodID for the constructor
282   xbt_assert((class_Process != NULL), "Class not found (%s). The deployment file must use the fully qualified class name, including the package. The case is important.", argv[0]);
283   jmethodID constructor_Process = env->GetMethodID(class_Process, "<init>", "(Lorg/simgrid/msg/Host;Ljava/lang/String;[Ljava/lang/String;)V");
284   xbt_assert((constructor_Process != NULL), "Constructor not found for class %s. Is there a (Host, String ,String[]) constructor in your class ?", argv[0]);
285
286   //Retrieve the name of the process.
287   jstring jname = env->NewStringUTF(argv[0]);
288   //Build the arguments
289   jobjectArray args = (jobjectArray)env->NewObjectArray(argc - 1,
290     env->FindClass("java/lang/String"),
291     env->NewStringUTF(""));
292   int i;
293   for (i = 1; i < argc; i++)
294       env->SetObjectArrayElement(args,i - 1,
295         env->NewStringUTF(argv[i]));
296   //Retrieve the host for the process.
297   jstring jhostName = env->NewStringUTF(MSG_host_get_name(MSG_host_self()));
298   jobject jhost = Java_org_simgrid_msg_Host_getByName(env, NULL, jhostName);
299   //creates the process
300   jobject jprocess = env->NewObject(class_Process, constructor_Process, jhost, jname, args);
301   xbt_assert((jprocess != NULL), "Process allocation failed.");
302   jprocess = env->NewGlobalRef(jprocess);
303   //bind the process to the context
304   msg_process_t process = MSG_process_self();
305   simgrid::java::JavaContext* context =
306     (simgrid::java::JavaContext*) MSG_process_get_smx_ctx(process);
307   context->jprocess = jprocess;
308   /* sets the PID and the PPID of the process */
309   env->SetIntField(jprocess, jprocess_field_Process_pid,(jint) MSG_process_get_PID(process));
310   env->SetIntField(jprocess, jprocess_field_Process_ppid, (jint) MSG_process_get_PPID(process));
311   jprocess_bind(jprocess, process, env);
312
313   return 0;
314 }
315
316 SG_END_DECL()