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 "jmsg_application_handler.h"
21 #include "jxbt_utilities.h"
25 /* Shut up some errors in eclipse online compiler. I wish such a pimple wouldn't be needed */
32 /* end of eclipse-mandated pimple */
34 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
36 static JavaVM *__java_vm = NULL;
39 JavaVM *get_java_VM(void)
44 JNIEnv *get_current_thread_env(void)
48 (*__java_vm)->AttachCurrentThread(__java_vm, (void **) &env, NULL);
51 /***************************************************************************************
52 * Unsortable functions *
53 ***************************************************************************************/
55 JNIEXPORT jdouble JNICALL
56 Java_org_simgrid_msg_Msg_getClock(JNIEnv * env, jclass cls)
58 return (jdouble) MSG_get_clock();
61 JNIEXPORT void JNICALL
62 Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
70 smx_factory_initializer_to_use = SIMIX_ctx_java_factory_init;
72 setlocale(LC_NUMERIC,"C");
75 argc = (int) (*env)->GetArrayLength(env, jargs);
78 argv = xbt_new(char *, argc + 1);
79 argv[0] = strdup("java");
81 for (index = 0; index < argc - 1; index++) {
82 jval = (jstring) (*env)->GetObjectArrayElement(env, jargs, index);
83 tmp = (*env)->GetStringUTFChars(env, jval, 0);
84 argv[index + 1] = strdup(tmp);
85 (*env)->ReleaseStringUTFChars(env, jval, tmp);
89 MSG_global_init(&argc, argv);
91 for (index = 0; index < argc; index++)
96 (*env)->GetJavaVM(env, &__java_vm);
99 JNIEXPORT void JNICALL
100 JNICALL Java_org_simgrid_msg_Msg_run(JNIEnv * env, jclass cls)
108 XBT_INFO("Ready to run MSG_MAIN");
110 XBT_INFO("Done running MSG_MAIN");
111 jxbt_check_res("MSG_main()", rv, MSG_OK,
113 ("unexpected error : MSG_main() failed .. please report this bug "));
115 XBT_INFO("MSG_main finished");
117 XBT_INFO("Clean java world");
118 /* Cleanup java hosts */
119 hosts = MSG_hosts_as_dynar();
120 for (index = 0; index < xbt_dynar_length(hosts) - 1; index++) {
121 jhost = (jobject) MSG_host_get_data(xbt_dynar_get_as(hosts,index,m_host_t));
123 jhost_unref(env, jhost);
126 xbt_dynar_free(&hosts);
127 XBT_INFO("Clean native world");
129 JNIEXPORT void JNICALL
130 JNICALL Java_org_simgrid_msg_Msg_clean(JNIEnv * env, jclass cls)
132 /* cleanup native stuff. Calling it is ... useless since leaking memory at the end of the simulation is a non-issue */
133 MSG_error_t rv = MSG_OK != MSG_clean();
134 jxbt_check_res("MSG_clean()", rv, MSG_OK,
136 ("unexpected error : MSG_clean() failed .. please report this bug "));
139 JNIEXPORT void JNICALL
140 Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
141 jstring jplatformFile)
144 const char *platformFile =
145 (*env)->GetStringUTFChars(env, jplatformFile, 0);
147 MSG_create_environment(platformFile);
149 (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
152 JNIEXPORT void JNICALL
153 Java_org_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
155 const char *s = (*env)->GetStringUTFChars(env, js, 0);
157 (*env)->ReleaseStringUTFChars(env, js, s);
160 JNIEXPORT void JNICALL
161 Java_org_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
162 jstring jdeploymentFile)
165 const char *deploymentFile =
166 (*env)->GetStringUTFChars(env, jdeploymentFile, 0);
168 surf_parse_reset_callbacks();
170 surfxml_add_callback(STag_surfxml_process_cb_list,
171 japplication_handler_on_begin_process);
173 surfxml_add_callback(ETag_surfxml_argument_cb_list,
174 japplication_handler_on_process_arg);
176 surfxml_add_callback(STag_surfxml_prop_cb_list,
177 japplication_handler_on_property);
179 surfxml_add_callback(ETag_surfxml_process_cb_list,
180 japplication_handler_on_end_process);
182 surf_parse_open(deploymentFile);
184 japplication_handler_on_start_document();
187 jxbt_throw_jni(env, "surf_parse() failed");
191 japplication_handler_on_end_document();
193 (*env)->ReleaseStringUTFChars(env, jdeploymentFile, deploymentFile);