Logo AND Algorithmique Numérique Distribuée

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