Logo AND Algorithmique Numérique Distribuée

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