Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
aaf5b1081973898ddb5c2ed38019fd6240c5215c
[simgrid.git] / src / jmsg.c
1 /* Java Wrappers to the MSG API.                                            */
2
3 /* Copyright (c) 2007, 2008, 2009, 2010. 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
16 #include "jmsg_process.h"
17
18 #include "jmsg_host.h"
19 #include "jmsg_task.h"
20 #include "jxbt_utilities.h"
21
22 #include "jmsg.h"
23
24 /* Shut up some errors in eclipse online compiler. I wish such a pimple wouldn't be needed */
25 #ifndef JNIEXPORT
26 #define JNIEXPORT
27 #endif
28 #ifndef JNICALL
29 #define JNICALL
30 #endif
31 /* end of eclipse-mandated pimple */
32
33 static int create_jprocess(int argc, char *argv[]);
34
35 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
36
37 JavaVM *__java_vm = NULL;
38
39 JavaVM *get_java_VM(void)
40 {
41   return __java_vm;
42 }
43
44 JNIEnv *get_current_thread_env(void)
45 {
46   JNIEnv *env;
47
48   (*__java_vm)->AttachCurrentThread(__java_vm, (void **) &env, NULL);
49   return env;
50 }
51
52 void jmsg_throw_status(JNIEnv *env, MSG_error_t status) {
53         switch (status) {
54                 case MSG_TIMEOUT:
55                         jxbt_throw_time_out_failure(env,NULL);
56                 break;
57                 case MSG_TRANSFER_FAILURE:
58                         jxbt_throw_transfer_failure(env,NULL);
59                 break;
60                 case MSG_HOST_FAILURE:
61                         jxbt_throw_host_failure(env,NULL);
62                 break;
63                 default:
64                         jxbt_throw_native(env,bprintf("communication failed"));
65         }
66 }
67
68
69 /***************************************************************************************
70  * Unsortable functions                                                        *
71  ***************************************************************************************/
72
73 JNIEXPORT jdouble JNICALL
74 Java_org_simgrid_msg_Msg_getClock(JNIEnv * env, jclass cls)
75 {
76   return (jdouble) MSG_get_clock();
77 }
78
79 JNIEXPORT void JNICALL
80 Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
81 {
82   char **argv = NULL;
83   int index;
84   int argc = 0;
85   jstring jval;
86   const char *tmp;
87
88   (*env)->GetJavaVM(env, &__java_vm);
89
90   smx_factory_initializer_to_use = SIMIX_ctx_java_factory_init;
91
92   setlocale(LC_NUMERIC,"C");
93
94   if (jargs)
95     argc = (int) (*env)->GetArrayLength(env, jargs);
96
97   argc++;
98   argv = xbt_new(char *, argc + 1);
99   argv[0] = strdup("java");
100
101   for (index = 0; index < argc - 1; index++) {
102     jval = (jstring) (*env)->GetObjectArrayElement(env, jargs, index);
103     tmp = (*env)->GetStringUTFChars(env, jval, 0);
104     argv[index + 1] = strdup(tmp);
105     (*env)->ReleaseStringUTFChars(env, jval, tmp);
106   }
107   argv[argc] = NULL;
108
109   MSG_global_init(&argc, argv);
110
111   for (index = 0; index < argc; index++)
112     free(argv[index]);
113
114   free(argv);
115 }
116
117 JNIEXPORT void JNICALL
118     JNICALL Java_org_simgrid_msg_Msg_run(JNIEnv * env, jclass cls)
119 {
120   MSG_error_t rv;
121   int index;
122   xbt_dynar_t hosts;
123   jobject jhost;
124
125   /* Run everything */
126   XBT_INFO("Ready to run MSG_MAIN");
127   rv = MSG_main();
128   XBT_INFO("Done running MSG_MAIN");
129   jxbt_check_res("MSG_main()", rv, MSG_OK,
130                  bprintf
131                  ("unexpected error : MSG_main() failed .. please report this bug "));
132
133   XBT_INFO("MSG_main finished");
134
135   XBT_INFO("Clean java world");
136   /* Cleanup java hosts */
137   hosts = MSG_hosts_as_dynar();
138   for (index = 0; index < xbt_dynar_length(hosts) - 1; index++) {
139     jhost = (jobject) MSG_host_get_data(xbt_dynar_get_as(hosts,index,m_host_t));
140     if (jhost)
141       jhost_unref(env, jhost);
142
143   }
144   xbt_dynar_free(&hosts);
145   XBT_INFO("Clean native world");
146 }
147 JNIEXPORT void JNICALL
148     JNICALL Java_org_simgrid_msg_Msg_clean(JNIEnv * env, jclass cls)
149 {
150   /* cleanup native stuff. Calling it is ... useless since leaking memory at the end of the simulation is a non-issue */
151   MSG_error_t rv = MSG_OK != MSG_clean();
152   jxbt_check_res("MSG_clean()", rv, MSG_OK,
153                  bprintf
154                  ("unexpected error : MSG_clean() failed .. please report this bug "));
155 }
156
157 JNIEXPORT void JNICALL
158 Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
159                                        jstring jplatformFile)
160 {
161
162   const char *platformFile =
163       (*env)->GetStringUTFChars(env, jplatformFile, 0);
164
165   MSG_create_environment(platformFile);
166
167   (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
168 }
169 JNIEXPORT void JNICALL
170 Java_org_simgrid_msg_Msg_debug(JNIEnv * env, jclass cls, jstring js)
171 {
172   const char *s = (*env)->GetStringUTFChars(env, js, 0);
173   XBT_DEBUG("%s", s);
174   (*env)->ReleaseStringUTFChars(env, js, s);
175 }
176 JNIEXPORT void JNICALL
177 Java_org_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
178 {
179   const char *s = (*env)->GetStringUTFChars(env, js, 0);
180   XBT_INFO("%s", s);
181   (*env)->ReleaseStringUTFChars(env, js, s);
182 }
183
184 JNIEXPORT void JNICALL
185 Java_org_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
186                                        jstring jdeploymentFile)
187 {
188
189   const char *deploymentFile =
190       (*env)->GetStringUTFChars(env, jdeploymentFile, 0);
191
192   SIMIX_function_register_default(create_jprocess);
193   MSG_launch_application(deploymentFile);
194 }
195 /**
196  * Function called when there is the need to create the java Process object
197  * (when we are using deployement files).
198  * it HAS to be executed on the process context, else really bad things will happen.
199  */
200 static int create_jprocess(int argc, char *argv[]) {
201         JNIEnv *env = get_current_thread_env();
202         //Change the "." in class name for "/".
203         xbt_str_subst(argv[0],'.','/',0);
204         jclass class_Process = (*env)->FindClass(env, argv[0]);
205         xbt_str_subst(argv[0],'/','.',0);
206         //Retrieve the methodID for the constructor
207         xbt_assert((class_Process != NULL), "Class not found.");
208         jmethodID constructor_Process = (*env)->GetMethodID(env, class_Process, "<init>", "(Lorg/simgrid/msg/Host;Ljava/lang/String;[Ljava/lang/String;)V");
209         xbt_assert((constructor_Process != NULL), "Constructor not found for class %s. Is there a (Host, String ,String[]) constructor in your class ?", argv[0]);
210
211         //Retrieve the name of the process.
212         jstring jname = (*env)->NewStringUTF(env, argv[0]);
213         //Build the arguments
214         jobjectArray args = (jobjectArray)((*env)->NewObjectArray(env,argc - 1,
215         (*env)->FindClass(env,"java/lang/String"),
216         (*env)->NewStringUTF(env,"")));
217         int i;
218         for (i = 1; i < argc; i++)
219                 (*env)->SetObjectArrayElement(env,args,i - 1,(*env)->NewStringUTF(env, argv[i]));
220         //Retrieve the host for the process.
221         jstring jhostName = (*env)->NewStringUTF(env, MSG_host_get_name(MSG_host_self()));
222         jobject jhost = Java_org_simgrid_msg_Host_getByName(env, NULL, jhostName);
223         //creates the process
224         jobject jprocess = (*env)->NewObject(env, class_Process, constructor_Process, jhost, jname, args);
225         xbt_assert((jprocess != NULL), "Process allocation failed.");
226         jprocess = (*env)->NewGlobalRef(env, jprocess);
227         //bind the process to the context
228         m_process_t process = MSG_process_self();
229         smx_ctx_java_t context = (smx_ctx_java_t)MSG_process_get_smx_ctx(process);
230         context->jprocess = jprocess;
231   /* sets the PID and the PPID of the process */
232   (*env)->SetIntField(env, jprocess, jprocess_field_Process_pid,(jint) MSG_process_get_PID(process));
233   (*env)->SetIntField(env, jprocess, jprocess_field_Process_ppid, (jint) MSG_process_get_PPID(process));
234
235         jprocess_bind(jprocess, process, env);
236
237         return 0;
238 }
239