Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
49ac55fdd076da2a0bc78ccae1a0d022db16bb01
[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 "jmsg_application_handler.h"
21 #include "jxbt_utilities.h"
22
23 #include "jmsg.h"
24
25 /* Shut up some errors in eclipse online compiler. I wish such a pimple wouldn't be needed */
26 #ifndef JNIEXPORT
27 #define JNIEXPORT
28 #endif
29 #ifndef JNICALL
30 #define JNICALL
31 #endif
32 /* end of eclipse-mandated pimple */
33
34 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
35
36 static JavaVM *__java_vm = NULL;
37
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
50   return env;
51 }
52 /***************************************************************************************
53  * Unsortable functions                                                        *
54  ***************************************************************************************/
55
56 JNIEXPORT jdouble JNICALL
57 Java_org_simgrid_msg_Msg_getClock(JNIEnv * env, jclass cls)
58 {
59   return (jdouble) MSG_get_clock();
60 }
61
62 JNIEXPORT void JNICALL
63 Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
64 {
65   char **argv = NULL;
66   int index;
67   int argc = 0;
68   jstring jval;
69   const char *tmp;
70
71   smx_factory_initializer_to_use = SIMIX_ctx_java_factory_init;
72
73   setlocale(LC_NUMERIC,"C");
74
75   if (jargs)
76     argc = (int) (*env)->GetArrayLength(env, jargs);
77
78   argc++;
79   argv = xbt_new(char *, argc + 1);
80   argv[0] = strdup("java");
81
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);
87   }
88   argv[argc] = NULL;
89
90   MSG_global_init(&argc, argv);
91
92   for (index = 0; index < argc; index++)
93     free(argv[index]);
94
95   free(argv);
96
97   (*env)->GetJavaVM(env, &__java_vm);
98 }
99
100 JNIEXPORT void JNICALL
101     JNICALL Java_org_simgrid_msg_Msg_run(JNIEnv * env, jclass cls)
102 {
103   MSG_error_t rv;
104   int index;
105   xbt_dynar_t hosts;
106   jobject jhost;
107
108   /* Run everything */
109   XBT_INFO("Ready to run MSG_MAIN");
110   rv = MSG_main();
111   XBT_INFO("Done running MSG_MAIN");
112   jxbt_check_res("MSG_main()", rv, MSG_OK,
113                  bprintf
114                  ("unexpected error : MSG_main() failed .. please report this bug "));
115
116   XBT_INFO("MSG_main finished");
117
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));
123     if (jhost)
124       jhost_unref(env, jhost);
125
126   }
127   xbt_dynar_free(&hosts);
128   XBT_INFO("Clean native world");
129 }
130 JNIEXPORT void JNICALL
131     JNICALL Java_org_simgrid_msg_Msg_clean(JNIEnv * env, jclass cls)
132 {
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,
136                  bprintf
137                  ("unexpected error : MSG_clean() failed .. please report this bug "));
138 }
139
140 JNIEXPORT void JNICALL
141 Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
142                                        jstring jplatformFile)
143 {
144
145   const char *platformFile =
146       (*env)->GetStringUTFChars(env, jplatformFile, 0);
147
148   MSG_create_environment(platformFile);
149
150   (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
151 }
152
153 JNIEXPORT void JNICALL
154 Java_org_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
155 {
156   const char *s = (*env)->GetStringUTFChars(env, js, 0);
157   XBT_INFO("%s", s);
158   (*env)->ReleaseStringUTFChars(env, js, s);
159 }
160
161 JNIEXPORT void JNICALL
162 Java_org_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
163                                        jstring jdeploymentFile)
164 {
165
166   const char *deploymentFile =
167       (*env)->GetStringUTFChars(env, jdeploymentFile, 0);
168
169   surf_parse_reset_callbacks();
170
171   surfxml_add_callback(STag_surfxml_process_cb_list,
172                        japplication_handler_on_begin_process);
173
174   surfxml_add_callback(ETag_surfxml_argument_cb_list,
175                        japplication_handler_on_process_arg);
176
177   surfxml_add_callback(STag_surfxml_prop_cb_list,
178                        japplication_handler_on_property);
179
180   surfxml_add_callback(ETag_surfxml_process_cb_list,
181                        japplication_handler_on_end_process);
182
183   surf_parse_open(deploymentFile);
184
185   japplication_handler_on_start_document();
186
187   if (surf_parse())
188     jxbt_throw_jni(env, "surf_parse() failed");
189
190   surf_parse_close();
191
192   japplication_handler_on_end_document();
193
194   (*env)->ReleaseStringUTFChars(env, jdeploymentFile, deploymentFile);
195 }