Logo AND Algorithmique Numérique Distribuée

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