Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'mc'
[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     default:
71         jxbt_throw_native(env,xbt_strdup("communication failed"));
72   }
73 }
74
75
76 /***************************************************************************************
77  * Unsortable functions                                                        *
78  ***************************************************************************************/
79
80 JNIEXPORT jdouble JNICALL
81 Java_org_simgrid_msg_Msg_getClock(JNIEnv * env, jclass cls)
82 {
83   return (jdouble) MSG_get_clock();
84 }
85
86 static void __JAVA_host_priv_free(void *host)
87 {
88 }
89
90 static void __JAVA_storage_priv_free(void *storage)
91 {
92 }
93
94 JNIEXPORT void JNICALL
95 Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
96 {
97   char **argv = NULL;
98   int index;
99   int argc = 0;
100   jstring jval;
101   const char *tmp;
102
103   XBT_LOG_CONNECT(jmsg);
104 #ifdef HAVE_TRACING
105   XBT_LOG_CONNECT(jtrace);
106 #endif
107
108   (*env)->GetJavaVM(env, &__java_vm);
109
110   if ((*env)->FindClass(env, "java/dyn/Coroutine"))
111     smx_factory_initializer_to_use = SIMIX_ctx_cojava_factory_init;
112   else
113     smx_factory_initializer_to_use = SIMIX_ctx_java_factory_init;
114   jthrowable exc = (*env)->ExceptionOccurred(env);
115   if (exc) {
116     (*env)->ExceptionClear(env);
117   }
118
119   setlocale(LC_NUMERIC,"C");
120
121   if (jargs)
122     argc = (int) (*env)->GetArrayLength(env, jargs);
123
124   argc++;
125   argv = xbt_new(char *, argc + 1);
126   argv[0] = strdup("java");
127
128   for (index = 0; index < argc - 1; index++) {
129     jval = (jstring) (*env)->GetObjectArrayElement(env, jargs, index);
130     tmp = (*env)->GetStringUTFChars(env, jval, 0);
131     argv[index + 1] = strdup(tmp);
132     (*env)->ReleaseStringUTFChars(env, 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   if (smx_factory_initializer_to_use == SIMIX_ctx_cojava_factory_init)
147     XBT_INFO("Using Coroutines. Your simulation is on steroid.");
148   else if (smx_factory_initializer_to_use == SIMIX_ctx_java_factory_init)
149     XBT_INFO("Using regular java threads. Coroutines could speed your simulation up.");
150   else
151     xbt_die("Unknown context factory. Please report bug.");
152 }
153
154 JNIEXPORT void JNICALL
155     JNICALL Java_org_simgrid_msg_Msg_run(JNIEnv * env, jclass cls)
156 {
157   msg_error_t rv;
158   int index;
159   xbt_dynar_t hosts, storages;
160   jobject jhost, jstorage;
161
162
163   /* Run everything */
164   XBT_DEBUG("Ready to run MSG_MAIN");
165   rv = MSG_main();
166   XBT_DEBUG("Done running MSG_MAIN");
167   jxbt_check_res("MSG_main()", rv, MSG_OK,
168                  xbt_strdup("unexpected error : MSG_main() failed .. please report this bug "));
169
170   XBT_INFO("MSG_main finished; Cleaning up the simulation...");
171   /* Cleanup java hosts */
172   hosts = MSG_hosts_as_dynar();
173   for (index = 0; index < xbt_dynar_length(hosts) - 1; index++) {
174     jhost = (jobject) xbt_lib_get_level(xbt_dynar_get_as(hosts,index,msg_host_t), JAVA_HOST_LEVEL);
175     if (jhost)
176       jhost_unref(env, jhost);
177
178   }
179   xbt_dynar_free(&hosts);
180
181   /* Cleanup java storages */
182   storages = MSG_storages_as_dynar();
183   if(!xbt_dynar_is_empty(storages)){
184     for (index = 0; index < xbt_dynar_length(storages) - 1; index++) {
185       jstorage = (jobject) xbt_lib_get_level(xbt_dynar_get_as(storages,index,msg_storage_t), JAVA_STORAGE_LEVEL);
186       if (jstorage)
187         jstorage_unref(env, jstorage);
188     }
189   }
190   xbt_dynar_free(&storages);
191
192 }
193
194 JNIEXPORT void JNICALL
195 Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
196                                        jstring jplatformFile)
197 {
198
199   const char *platformFile =
200       (*env)->GetStringUTFChars(env, jplatformFile, 0);
201
202   MSG_create_environment(platformFile);
203
204   (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
205 }
206
207 JNIEXPORT jobject JNICALL
208 Java_org_simgrid_msg_Msg_environmentGetRoutingRoot(JNIEnv * env, jclass cls)
209 {
210   msg_as_t as = MSG_environment_get_routing_root();
211   jobject jas = jas_new_instance(env);
212   if (!jas) {
213     jxbt_throw_jni(env, "java As instantiation failed");
214     return NULL;
215   }
216   jas = jas_ref(env, jas);
217   if (!jas) {
218     jxbt_throw_jni(env, "new global ref allocation failed");
219     return NULL;
220   }
221   jas_bind(jas, as, env);
222
223   return (jobject) jas;
224 }
225
226 JNIEXPORT void JNICALL
227 Java_org_simgrid_msg_Msg_debug(JNIEnv * env, jclass cls, jstring js)
228 {
229   const char *s = (*env)->GetStringUTFChars(env, js, 0);
230   XBT_DEBUG("%s", s);
231   (*env)->ReleaseStringUTFChars(env, js, s);
232 }
233 JNIEXPORT void JNICALL
234 Java_org_simgrid_msg_Msg_verb(JNIEnv * env, jclass cls, jstring js)
235 {
236   const char *s = (*env)->GetStringUTFChars(env, js, 0);
237   XBT_VERB("%s", s);
238   (*env)->ReleaseStringUTFChars(env, js, s);
239 }
240 JNIEXPORT void JNICALL
241 Java_org_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
242 {
243   const char *s = (*env)->GetStringUTFChars(env, js, 0);
244   XBT_INFO("%s", s);
245   (*env)->ReleaseStringUTFChars(env, js, s);
246 }
247 JNIEXPORT void JNICALL
248 Java_org_simgrid_msg_Msg_warn(JNIEnv * env, jclass cls, jstring js)
249 {
250   const char *s = (*env)->GetStringUTFChars(env, js, 0);
251   XBT_WARN("%s", s);
252   (*env)->ReleaseStringUTFChars(env, js, s);
253 }
254 JNIEXPORT void JNICALL
255 Java_org_simgrid_msg_Msg_error(JNIEnv * env, jclass cls, jstring js)
256 {
257   const char *s = (*env)->GetStringUTFChars(env, js, 0);
258   XBT_ERROR("%s", s);
259   (*env)->ReleaseStringUTFChars(env, js, s);
260 }
261 JNIEXPORT void JNICALL
262 Java_org_simgrid_msg_Msg_critical(JNIEnv * env, jclass cls, jstring js)
263 {
264   const char *s = (*env)->GetStringUTFChars(env, js, 0);
265   XBT_CRITICAL("%s", s);
266   (*env)->ReleaseStringUTFChars(env, js, s);
267 }
268 JNIEXPORT void JNICALL
269 Java_org_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
270                                        jstring jdeploymentFile)
271 {
272
273   const char *deploymentFile =
274       (*env)->GetStringUTFChars(env, jdeploymentFile, 0);
275
276   SIMIX_function_register_default(create_jprocess);
277   MSG_launch_application(deploymentFile);
278 }
279 /**
280  * Function called when there is the need to create the java Process object
281  * (when we are using deployement files).
282  * it HAS to be executed on the process context, else really bad things will happen.
283  */
284 static int create_jprocess(int argc, char *argv[]) {
285   JNIEnv *env = get_current_thread_env();
286   //Change the "." in class name for "/".
287   xbt_str_subst(argv[0],'.','/',0);
288   jclass class_Process = (*env)->FindClass(env, argv[0]);
289   xbt_str_subst(argv[0],'/','.',0);
290   //Retrieve the methodID for the constructor
291   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]);
292   jmethodID constructor_Process = (*env)->GetMethodID(env, class_Process, "<init>", "(Lorg/simgrid/msg/Host;Ljava/lang/String;[Ljava/lang/String;)V");
293   xbt_assert((constructor_Process != NULL), "Constructor not found for class %s. Is there a (Host, String ,String[]) constructor in your class ?", argv[0]);
294
295   //Retrieve the name of the process.
296   jstring jname = (*env)->NewStringUTF(env, argv[0]);
297   //Build the arguments
298   jobjectArray args = (jobjectArray)((*env)->NewObjectArray(env,argc - 1,
299   (*env)->FindClass(env,"java/lang/String"),
300   (*env)->NewStringUTF(env,"")));
301   int i;
302   for (i = 1; i < argc; i++)
303       (*env)->SetObjectArrayElement(env,args,i - 1,(*env)->NewStringUTF(env, argv[i]));
304   //Retrieve the host for the process.
305   jstring jhostName = (*env)->NewStringUTF(env, MSG_host_get_name(MSG_host_self()));
306   jobject jhost = Java_org_simgrid_msg_Host_getByName(env, NULL, jhostName);
307   //creates the process
308   jobject jprocess = (*env)->NewObject(env, class_Process, constructor_Process, jhost, jname, args);
309   xbt_assert((jprocess != NULL), "Process allocation failed.");
310   jprocess = (*env)->NewGlobalRef(env, jprocess);
311   //bind the process to the context
312   msg_process_t process = MSG_process_self();
313   smx_ctx_java_t context = (smx_ctx_java_t)MSG_process_get_smx_ctx(process);
314   context->jprocess = jprocess;
315 /* sets the PID and the PPID of the process */
316 (*env)->SetIntField(env, jprocess, jprocess_field_Process_pid,(jint) MSG_process_get_PID(process));
317 (*env)->SetIntField(env, jprocess, jprocess_field_Process_ppid, (jint) MSG_process_get_PPID(process));
318   jprocess_bind(jprocess, process, env);
319
320   return 0;
321 }
322