Logo AND Algorithmique Numérique Distribuée

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