Logo AND Algorithmique Numérique Distribuée

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