1 /* Java Wrappers to the MSG API. */
3 /* Copyright (c) 2007, 2008, 2009, 2010. 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. */
10 #include <simgrid/simix.h>
11 #include <surf/surfxml_parse.h>
14 #include "smx_context_java.h"
16 #include "jmsg_process.h"
18 #include "jmsg_host.h"
19 #include "jmsg_task.h"
20 #include "jxbt_utilities.h"
24 /* Shut up some errors in eclipse online compiler. I wish such a pimple wouldn't be needed */
31 /* end of eclipse-mandated pimple */
33 static int create_jprocess(int argc, char *argv[]);
35 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
37 static JavaVM *__java_vm = NULL;
40 JavaVM *get_java_VM(void)
45 JNIEnv *get_current_thread_env(void)
49 (*__java_vm)->AttachCurrentThread(__java_vm, (void **) &env, NULL);
52 /***************************************************************************************
53 * Unsortable functions *
54 ***************************************************************************************/
56 JNIEXPORT jdouble JNICALL
57 Java_org_simgrid_msg_Msg_getClock(JNIEnv * env, jclass cls)
59 return (jdouble) MSG_get_clock();
62 JNIEXPORT void JNICALL
63 Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
71 smx_factory_initializer_to_use = SIMIX_ctx_java_factory_init;
73 setlocale(LC_NUMERIC,"C");
76 argc = (int) (*env)->GetArrayLength(env, jargs);
79 argv = xbt_new(char *, argc + 1);
80 argv[0] = strdup("java");
82 for (index = 0; index < argc - 1; index++) {
83 jval = (jstring) (*env)->GetObjectArrayElement(env, jargs, index);
84 tmp = (*env)->GetStringUTFChars(env, jval, 0);
85 argv[index + 1] = strdup(tmp);
86 (*env)->ReleaseStringUTFChars(env, jval, tmp);
90 MSG_global_init(&argc, argv);
92 for (index = 0; index < argc; index++)
97 (*env)->GetJavaVM(env, &__java_vm);
100 JNIEXPORT void JNICALL
101 JNICALL Java_org_simgrid_msg_Msg_run(JNIEnv * env, jclass cls)
109 XBT_INFO("Ready to run MSG_MAIN");
111 XBT_INFO("Done running MSG_MAIN");
112 jxbt_check_res("MSG_main()", rv, MSG_OK,
114 ("unexpected error : MSG_main() failed .. please report this bug "));
116 XBT_INFO("MSG_main finished");
118 XBT_INFO("Clean java world");
119 /* Cleanup java hosts */
120 hosts = MSG_hosts_as_dynar();
121 for (index = 0; index < xbt_dynar_length(hosts) - 1; index++) {
122 jhost = (jobject) MSG_host_get_data(xbt_dynar_get_as(hosts,index,m_host_t));
124 jhost_unref(env, jhost);
127 xbt_dynar_free(&hosts);
128 XBT_INFO("Clean native world");
130 JNIEXPORT void JNICALL
131 JNICALL Java_org_simgrid_msg_Msg_clean(JNIEnv * env, jclass cls)
133 /* cleanup native stuff. Calling it is ... useless since leaking memory at the end of the simulation is a non-issue */
134 MSG_error_t rv = MSG_OK != MSG_clean();
135 jxbt_check_res("MSG_clean()", rv, MSG_OK,
137 ("unexpected error : MSG_clean() failed .. please report this bug "));
140 JNIEXPORT void JNICALL
141 Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
142 jstring jplatformFile)
145 const char *platformFile =
146 (*env)->GetStringUTFChars(env, jplatformFile, 0);
148 MSG_create_environment(platformFile);
150 (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
153 JNIEXPORT void JNICALL
154 Java_org_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
156 const char *s = (*env)->GetStringUTFChars(env, js, 0);
158 (*env)->ReleaseStringUTFChars(env, js, s);
161 JNIEXPORT void JNICALL
162 Java_org_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
163 jstring jdeploymentFile)
166 const char *deploymentFile =
167 (*env)->GetStringUTFChars(env, jdeploymentFile, 0);
169 SIMIX_function_register_default(create_jprocess);
170 MSG_launch_application(deploymentFile);
173 * Function called when there is the need to create the java Process object
174 * (when we are using deployement files).
175 * it HAS to be executed on the process context, else really bad things will happen.
177 static int create_jprocess(int argc, char *argv[]) {
178 JNIEnv *env = get_current_thread_env();
179 //Change the "." in class name for "/".
180 xbt_str_subst(argv[0],'.','/',0);
181 jclass class_Process = (*env)->FindClass(env, argv[0]);
182 xbt_str_subst(argv[0],'/','.',0);
183 //Retrieve the methodID for the constructor
184 xbt_assert((class_Process != NULL), "Class not found.");
185 jmethodID constructor_Process = (*env)->GetMethodID(env, class_Process, "<init>", "(Lorg/simgrid/msg/Host;Ljava/lang/String;[Ljava/lang/String;)V");
186 xbt_assert((constructor_Process != NULL), "Constructor not found for class %s. Is there a (Host, String ,String[]) constructor in your class ?", argv[0]);
188 //Retrieve the name of the process.
189 jstring jname = (*env)->NewStringUTF(env, argv[0]);
190 //Build the arguments
191 jobjectArray args = (jobjectArray)((*env)->NewObjectArray(env,argc - 1,
192 (*env)->FindClass(env,"java/lang/String"),
193 (*env)->NewStringUTF(env,"")));
195 for (i = 1; i < argc; i++)
196 (*env)->SetObjectArrayElement(env,args,i - 1,(*env)->NewStringUTF(env, argv[i]));
197 //Retrieve the host for the process.
198 jstring jhostName = (*env)->NewStringUTF(env, MSG_host_get_name(MSG_host_self()));
199 jobject jhost = Java_org_simgrid_msg_Host_getByName(env, NULL, jhostName);
200 //creates the process
201 jobject jprocess = (*env)->NewObject(env, class_Process, constructor_Process, jhost, jname, args);
202 xbt_assert((jprocess != NULL), "Process allocation failed.");
203 jprocess = (*env)->NewGlobalRef(env, jprocess);
204 //bind the process to the context
205 m_process_t process = MSG_process_self();
206 smx_ctx_java_t context = (smx_ctx_java_t)MSG_process_get_smx_ctx(process);
207 context->jprocess = jprocess;
208 /* sets the PID and the PPID of the process */
209 (*env)->SetIntField(env, jprocess, jprocess_field_Process_pid,(jint) MSG_process_get_PID(process));
210 (*env)->SetIntField(env, jprocess, jprocess_field_Process_ppid, (jint) MSG_process_get_PPID(process));
212 jprocess_bind(jprocess, process, env);