1 /* Java Wrappers to the MSG API. */
3 /* Copyright (c) 2007-2015. The SimGrid Team.
4 * All rights reserved. */
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. */
9 #include <simgrid/msg.h>
10 #include <simgrid/simix.h>
11 #include <surf/surfxml_parse.h>
13 #include <src/simix/smx_private.h>
15 #include "jmsg_process.h"
19 #include "jmsg_host.h"
20 #include "jmsg_storage.h"
21 #include "jmsg_task.h"
22 #include "jxbt_utilities.h"
26 #include "JavaContext.hpp"
28 /* Shut up some errors in eclipse online compiler. I wish such a pimple wouldn't be needed */
35 /* end of eclipse-mandated pimple */
40 int JAVA_STORAGE_LEVEL;
42 static int create_jprocess(int argc, char *argv[]);
44 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
46 JavaVM *__java_vm = NULL;
48 JavaVM *get_java_VM(void)
53 JNIEnv *get_current_thread_env(void)
57 __java_vm->AttachCurrentThread((void **) &env, NULL);
61 void jmsg_throw_status(JNIEnv *env, msg_error_t status) {
64 jxbt_throw_time_out_failure(env,NULL);
66 case MSG_TRANSFER_FAILURE:
67 jxbt_throw_transfer_failure(env,NULL);
69 case MSG_HOST_FAILURE:
70 jxbt_throw_host_failure(env,NULL);
72 case MSG_TASK_CANCELED:
73 jxbt_throw_task_cancelled(env,NULL);
76 jxbt_throw_native(env,xbt_strdup("undefined message failed (please see jmsg_throw_status function in jmsg.c)"));
81 /***************************************************************************************
82 * Unsortable functions *
83 ***************************************************************************************/
85 JNIEXPORT jdouble JNICALL
86 Java_org_simgrid_msg_Msg_getClock(JNIEnv * env, jclass cls)
88 return (jdouble) MSG_get_clock();
91 static void __JAVA_host_priv_free(void *host)
95 static void __JAVA_storage_priv_free(void *storage)
99 JNIEXPORT void JNICALL
100 Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
108 XBT_LOG_CONNECT(jmsg);
109 XBT_LOG_CONNECT(jtrace);
111 env->GetJavaVM(&__java_vm);
113 simgrid::simix::factory_initializer = simgrid::java::java_factory;
114 jthrowable exc = env->ExceptionOccurred();
116 env->ExceptionClear();
119 setlocale(LC_NUMERIC,"C");
122 argc = (int) env->GetArrayLength(jargs);
125 argv = xbt_new(char *, argc + 1);
126 argv[0] = xbt_strdup("java");
128 for (index = 0; index < argc - 1; index++) {
129 jval = (jstring) env->GetObjectArrayElement(jargs, index);
130 tmp = env->GetStringUTFChars(jval, 0);
131 argv[index + 1] = xbt_strdup(tmp);
132 env->ReleaseStringUTFChars(jval, tmp);
136 MSG_init(&argc, argv);
138 JAVA_HOST_LEVEL = xbt_lib_add_level(host_lib, (void_f_pvoid_t) __JAVA_host_priv_free);
139 JAVA_STORAGE_LEVEL = xbt_lib_add_level(storage_lib, (void_f_pvoid_t) __JAVA_storage_priv_free);
141 for (index = 0; index < argc; index++)
147 JNIEXPORT void JNICALL
148 JNICALL Java_org_simgrid_msg_Msg_run(JNIEnv * env, jclass cls)
151 xbt_dynar_t hosts, storages;
152 jobject jhost, jstorage;
155 XBT_DEBUG("Ready to run MSG_MAIN");
157 XBT_DEBUG("Done running MSG_MAIN");
158 jxbt_check_res("MSG_main()", rv, MSG_OK,
159 xbt_strdup("unexpected error : MSG_main() failed .. please report this bug "));
161 XBT_INFO("MSG_main finished; Cleaning up the simulation...");
162 /* Cleanup java hosts */
163 hosts = MSG_hosts_as_dynar();
164 for (unsigned long index = 0; index < xbt_dynar_length(hosts) - 1; index++) {
165 jhost = (jobject) xbt_lib_get_level(xbt_dynar_get_as(hosts,index,msg_host_t), JAVA_HOST_LEVEL);
167 jhost_unref(env, jhost);
170 xbt_dynar_free(&hosts);
172 /* Cleanup java storages */
173 storages = MSG_storages_as_dynar();
174 if(!xbt_dynar_is_empty(storages)){
175 for (unsigned long index = 0; index < xbt_dynar_length(storages) - 1; index++) {
176 jstorage = (jobject) xbt_lib_get_level(xbt_dynar_get_as(storages,index,msg_storage_t), JAVA_STORAGE_LEVEL);
178 jstorage_unref(env, jstorage);
181 xbt_dynar_free(&storages);
185 JNIEXPORT void JNICALL
186 Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
187 jstring jplatformFile)
190 const char *platformFile =
191 env->GetStringUTFChars(jplatformFile, 0);
193 MSG_create_environment(platformFile);
195 env->ReleaseStringUTFChars(jplatformFile, platformFile);
198 JNIEXPORT jobject JNICALL
199 Java_org_simgrid_msg_Msg_environmentGetRoutingRoot(JNIEnv * env, jclass cls)
201 msg_as_t as = MSG_environment_get_routing_root();
202 jobject jas = jas_new_instance(env);
204 jxbt_throw_jni(env, "java As instantiation failed");
207 jas = jas_ref(env, jas);
209 jxbt_throw_jni(env, "new global ref allocation failed");
212 jas_bind(jas, as, env);
214 return (jobject) jas;
217 JNIEXPORT void JNICALL
218 Java_org_simgrid_msg_Msg_debug(JNIEnv * env, jclass cls, jstring js)
220 const char *s = env->GetStringUTFChars(js, 0);
222 env->ReleaseStringUTFChars(js, s);
224 JNIEXPORT void JNICALL
225 Java_org_simgrid_msg_Msg_verb(JNIEnv * env, jclass cls, jstring js)
227 const char *s = env->GetStringUTFChars(js, 0);
229 env->ReleaseStringUTFChars(js, s);
231 JNIEXPORT void JNICALL
232 Java_org_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
234 const char *s = env->GetStringUTFChars(js, 0);
236 env->ReleaseStringUTFChars(js, s);
238 JNIEXPORT void JNICALL
239 Java_org_simgrid_msg_Msg_warn(JNIEnv * env, jclass cls, jstring js)
241 const char *s = env->GetStringUTFChars(js, 0);
243 env->ReleaseStringUTFChars(js, s);
245 JNIEXPORT void JNICALL
246 Java_org_simgrid_msg_Msg_error(JNIEnv * env, jclass cls, jstring js)
248 const char *s = env->GetStringUTFChars(js, 0);
250 env->ReleaseStringUTFChars(js, s);
252 JNIEXPORT void JNICALL
253 Java_org_simgrid_msg_Msg_critical(JNIEnv * env, jclass cls, jstring js)
255 const char *s = env->GetStringUTFChars(js, 0);
256 XBT_CRITICAL("%s", s);
257 env->ReleaseStringUTFChars(js, s);
259 JNIEXPORT void JNICALL
260 Java_org_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
261 jstring jdeploymentFile)
264 const char *deploymentFile =
265 env->GetStringUTFChars(jdeploymentFile, 0);
267 SIMIX_function_register_default(create_jprocess);
268 MSG_launch_application(deploymentFile);
271 * Function called when there is the need to create the java Process object
272 * (when we are using deployement files).
273 * it HAS to be executed on the process context, else really bad things will happen.
275 static int create_jprocess(int argc, char *argv[]) {
276 JNIEnv *env = get_current_thread_env();
277 //Change the "." in class name for "/".
278 xbt_str_subst(argv[0],'.','/',0);
279 jclass class_Process = env->FindClass(argv[0]);
280 xbt_str_subst(argv[0],'/','.',0);
281 //Retrieve the methodID for the constructor
282 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]);
283 jmethodID constructor_Process = env->GetMethodID(class_Process, "<init>", "(Lorg/simgrid/msg/Host;Ljava/lang/String;[Ljava/lang/String;)V");
284 xbt_assert((constructor_Process != NULL), "Constructor not found for class %s. Is there a (Host, String ,String[]) constructor in your class ?", argv[0]);
286 //Retrieve the name of the process.
287 jstring jname = env->NewStringUTF(argv[0]);
288 //Build the arguments
289 jobjectArray args = (jobjectArray)env->NewObjectArray(argc - 1,
290 env->FindClass("java/lang/String"),
291 env->NewStringUTF(""));
293 for (i = 1; i < argc; i++)
294 env->SetObjectArrayElement(args,i - 1,
295 env->NewStringUTF(argv[i]));
296 //Retrieve the host for the process.
297 jstring jhostName = env->NewStringUTF(MSG_host_get_name(MSG_host_self()));
298 jobject jhost = Java_org_simgrid_msg_Host_getByName(env, NULL, jhostName);
299 //creates the process
300 jobject jprocess = env->NewObject(class_Process, constructor_Process, jhost, jname, args);
301 xbt_assert((jprocess != NULL), "Process allocation failed.");
302 jprocess = env->NewGlobalRef(jprocess);
303 //bind the process to the context
304 msg_process_t process = MSG_process_self();
305 simgrid::java::JavaContext* context =
306 (simgrid::java::JavaContext*) MSG_process_get_smx_ctx(process);
307 context->jprocess = jprocess;
308 /* sets the PID and the PPID of the process */
309 env->SetIntField(jprocess, jprocess_field_Process_pid,(jint) MSG_process_get_PID(process));
310 env->SetIntField(jprocess, jprocess_field_Process_ppid, (jint) MSG_process_get_PPID(process));
311 jprocess_bind(jprocess, process, env);