Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[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   int index;
152   xbt_dynar_t hosts, storages;
153   jobject jhost, jstorage;
154
155
156   /* Run everything */
157   XBT_DEBUG("Ready to run MSG_MAIN");
158   rv = MSG_main();
159   XBT_DEBUG("Done running MSG_MAIN");
160   jxbt_check_res("MSG_main()", rv, MSG_OK,
161                  xbt_strdup("unexpected error : MSG_main() failed .. please report this bug "));
162
163   XBT_INFO("MSG_main finished; Cleaning up the simulation...");
164   /* Cleanup java hosts */
165   hosts = MSG_hosts_as_dynar();
166   for (index = 0; index < xbt_dynar_length(hosts) - 1; index++) {
167     jhost = (jobject) xbt_lib_get_level(xbt_dynar_get_as(hosts,index,msg_host_t), JAVA_HOST_LEVEL);
168     if (jhost)
169       jhost_unref(env, jhost);
170
171   }
172   xbt_dynar_free(&hosts);
173
174   /* Cleanup java storages */
175   storages = MSG_storages_as_dynar();
176   if(!xbt_dynar_is_empty(storages)){
177     for (index = 0; index < xbt_dynar_length(storages) - 1; index++) {
178       jstorage = (jobject) xbt_lib_get_level(xbt_dynar_get_as(storages,index,msg_storage_t), JAVA_STORAGE_LEVEL);
179       if (jstorage)
180         jstorage_unref(env, jstorage);
181     }
182   }
183   xbt_dynar_free(&storages);
184
185 }
186
187 JNIEXPORT void JNICALL
188 Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
189                                        jstring jplatformFile)
190 {
191
192   const char *platformFile =
193       env->GetStringUTFChars(jplatformFile, 0);
194
195   MSG_create_environment(platformFile);
196
197   env->ReleaseStringUTFChars(jplatformFile, platformFile);
198 }
199
200 JNIEXPORT jobject JNICALL
201 Java_org_simgrid_msg_Msg_environmentGetRoutingRoot(JNIEnv * env, jclass cls)
202 {
203   msg_as_t as = MSG_environment_get_routing_root();
204   jobject jas = jas_new_instance(env);
205   if (!jas) {
206     jxbt_throw_jni(env, "java As instantiation failed");
207     return NULL;
208   }
209   jas = jas_ref(env, jas);
210   if (!jas) {
211     jxbt_throw_jni(env, "new global ref allocation failed");
212     return NULL;
213   }
214   jas_bind(jas, as, env);
215
216   return (jobject) jas;
217 }
218
219 JNIEXPORT void JNICALL
220 Java_org_simgrid_msg_Msg_debug(JNIEnv * env, jclass cls, jstring js)
221 {
222   const char *s = env->GetStringUTFChars(js, 0);
223   XBT_DEBUG("%s", s);
224   env->ReleaseStringUTFChars(js, s);
225 }
226 JNIEXPORT void JNICALL
227 Java_org_simgrid_msg_Msg_verb(JNIEnv * env, jclass cls, jstring js)
228 {
229   const char *s = env->GetStringUTFChars(js, 0);
230   XBT_VERB("%s", s);
231   env->ReleaseStringUTFChars(js, s);
232 }
233 JNIEXPORT void JNICALL
234 Java_org_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
235 {
236   const char *s = env->GetStringUTFChars(js, 0);
237   XBT_INFO("%s", s);
238   env->ReleaseStringUTFChars(js, s);
239 }
240 JNIEXPORT void JNICALL
241 Java_org_simgrid_msg_Msg_warn(JNIEnv * env, jclass cls, jstring js)
242 {
243   const char *s = env->GetStringUTFChars(js, 0);
244   XBT_WARN("%s", s);
245   env->ReleaseStringUTFChars(js, s);
246 }
247 JNIEXPORT void JNICALL
248 Java_org_simgrid_msg_Msg_error(JNIEnv * env, jclass cls, jstring js)
249 {
250   const char *s = env->GetStringUTFChars(js, 0);
251   XBT_ERROR("%s", s);
252   env->ReleaseStringUTFChars(js, s);
253 }
254 JNIEXPORT void JNICALL
255 Java_org_simgrid_msg_Msg_critical(JNIEnv * env, jclass cls, jstring js)
256 {
257   const char *s = env->GetStringUTFChars(js, 0);
258   XBT_CRITICAL("%s", s);
259   env->ReleaseStringUTFChars(js, s);
260 }
261 JNIEXPORT void JNICALL
262 Java_org_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
263                                        jstring jdeploymentFile)
264 {
265
266   const char *deploymentFile =
267       env->GetStringUTFChars(jdeploymentFile, 0);
268
269   SIMIX_function_register_default(create_jprocess);
270   MSG_launch_application(deploymentFile);
271 }
272 /**
273  * Function called when there is the need to create the java Process object
274  * (when we are using deployement files).
275  * it HAS to be executed on the process context, else really bad things will happen.
276  */
277 static int create_jprocess(int argc, char *argv[]) {
278   JNIEnv *env = get_current_thread_env();
279   //Change the "." in class name for "/".
280   xbt_str_subst(argv[0],'.','/',0);
281   jclass class_Process = env->FindClass(argv[0]);
282   xbt_str_subst(argv[0],'/','.',0);
283   //Retrieve the methodID for the constructor
284   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]);
285   jmethodID constructor_Process = env->GetMethodID(class_Process, "<init>", "(Lorg/simgrid/msg/Host;Ljava/lang/String;[Ljava/lang/String;)V");
286   xbt_assert((constructor_Process != NULL), "Constructor not found for class %s. Is there a (Host, String ,String[]) constructor in your class ?", argv[0]);
287
288   //Retrieve the name of the process.
289   jstring jname = env->NewStringUTF(argv[0]);
290   //Build the arguments
291   jobjectArray args = (jobjectArray)env->NewObjectArray(argc - 1,
292     env->FindClass("java/lang/String"),
293     env->NewStringUTF(""));
294   int i;
295   for (i = 1; i < argc; i++)
296       env->SetObjectArrayElement(args,i - 1,
297         env->NewStringUTF(argv[i]));
298   //Retrieve the host for the process.
299   jstring jhostName = env->NewStringUTF(MSG_host_get_name(MSG_host_self()));
300   jobject jhost = Java_org_simgrid_msg_Host_getByName(env, NULL, jhostName);
301   //creates the process
302   jobject jprocess = env->NewObject(class_Process, constructor_Process, jhost, jname, args);
303   xbt_assert((jprocess != NULL), "Process allocation failed.");
304   jprocess = env->NewGlobalRef(jprocess);
305   //bind the process to the context
306   msg_process_t process = MSG_process_self();
307   simgrid::java::JavaContext* context =
308     (simgrid::java::JavaContext*) MSG_process_get_smx_ctx(process);
309   context->jprocess = jprocess;
310   /* sets the PID and the PPID of the process */
311   env->SetIntField(jprocess, jprocess_field_Process_pid,(jint) MSG_process_get_PID(process));
312   env->SetIntField(jprocess, jprocess_field_Process_ppid, (jint) MSG_process_get_PPID(process));
313   jprocess_bind(jprocess, process, env);
314
315   return 0;
316 }
317
318 SG_END_DECL()