1 /* SimGrid Ruby bindings */
3 /* Copyright (c) 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 "bindings/ruby_bindings.h"
11 #include "simix/smx_context_private.h"
13 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ruby);
22 static void msg_init(VALUE Class, VALUE args)
28 // Testing The Args Type
30 if (type != T_ARRAY) {
31 rb_raise(rb_eRuntimeError,
32 "Bad arguments to msg_init (expecting an array)");
35 ptr = RARRAY(args)->ptr;
36 argc = RARRAY(args)->len;
37 // Create C array to hold data_get_struct
39 argv = xbt_new0(char *, argc);
40 argv[0] = strdup("ruby");
41 for (i = 0; i < argc - 1; i++) {
44 // if (type == T_STRING)
45 tmp = RSTRING(value)->ptr;
46 argv[i + 1] = strdup(tmp);
48 // Calling C Msg_Init Method
49 MSG_global_init(&argc, argv);
52 for (i = 0; i < argc; i++)
57 //Init Msg_Run From Ruby
58 static void msg_run(VALUE class)
60 DEBUG0("Start Running...");
65 //printf("msg_run3\n");
66 if (MSG_OK != MSG_main()) {
67 rb_raise(rb_eRuntimeError, "MSG_main() failed");
71 ("MSG_main finished. Bail out before cleanup since there is a bug in this part.");
72 /* Cleanup Ruby hosts */
73 DEBUG0("Clean Ruby World ");
74 hosts = MSG_get_host_table();
75 host_count = MSG_get_host_number();
76 for (cpt = 0; cpt < host_count; cpt++) {
77 rbHost = (VALUE) ((hosts[cpt])->data);
82 static void msg_clean(VALUE class)
84 if (MSG_OK != MSG_clean())
85 rb_raise(rb_eRuntimeError, "MSG_clean() failed");
89 static void msg_createEnvironment(VALUE class, VALUE plateformFile)
92 int type = TYPE(plateformFile);
94 rb_raise(rb_eRuntimeError, "Bad Argument's Type");
95 const char *platform = RSTRING(plateformFile)->ptr;
96 MSG_create_environment(platform);
97 DEBUG1("Create Environment (%s)...Done", platform);
101 static void msg_deployApplication(VALUE class, VALUE deploymentFile)
104 int type = TYPE(deploymentFile);
105 if (type != T_STRING)
106 rb_raise(rb_eRuntimeError,
107 "Bad Argument's Type for deployApplication ");
108 const char *dep_file = RSTRING(deploymentFile)->ptr;
109 surf_parse_reset_parser();
110 surfxml_add_callback(STag_surfxml_process_cb_list,
111 rb_application_handler_on_begin_process);
112 surfxml_add_callback(ETag_surfxml_argument_cb_list,
113 rb_application_handler_on_process_arg);
115 surfxml_add_callback(STag_surfxml_prop_cb_list,
116 rb_application_handler_on_property);
118 surfxml_add_callback(ETag_surfxml_process_cb_list,
119 rb_application_handler_on_end_process);
121 surf_parse_open(dep_file);
122 rb_application_handler_on_start_document();
124 rb_raise(rb_eRuntimeError, "surf_parse() failed");
127 rb_application_handler_on_end_document();
129 DEBUG1("Deploy Application(%s)...Done", dep_file);
133 static void msg_info(VALUE class, VALUE msg)
135 const char *s = RSTRING(msg)->ptr;
139 static void msg_debug(VALUE class, VALUE msg)
141 const char *s = RSTRING(msg)->ptr;
146 static VALUE msg_get_clock(VALUE class)
148 return rb_float_new(MSG_get_clock());
152 typedef VALUE(*rb_meth) (ANYARGS);
153 void Init_libsimgrid()
155 factory_initializer_to_use = SIMIX_ctx_ruby_factory_init;
158 rb_msg = rb_define_module("MSG");
159 //Associated Environment Methods
160 rb_define_module_function(rb_msg, "init", (rb_meth) msg_init, 1);
161 rb_define_module_function(rb_msg, "run", (rb_meth) msg_run, 0);
162 rb_define_module_function(rb_msg, "createEnvironment",
163 (rb_meth) msg_createEnvironment, 1);
164 rb_define_module_function(rb_msg, "deployApplication",
165 (rb_meth) msg_deployApplication, 1);
166 rb_define_module_function(rb_msg, "info", (rb_meth) msg_info, 1);
167 rb_define_module_function(rb_msg, "debug", (rb_meth) msg_debug, 1);
168 rb_define_module_function(rb_msg, "getClock", (rb_meth) msg_get_clock,
170 rb_define_module_function(rb_msg, "exit", (rb_meth) msg_clean, 0);
172 //Associated Process Methods
173 rb_define_method(rb_msg, "processSuspend", (rb_meth) rb_process_suspend,
175 rb_define_method(rb_msg, "processResume", (rb_meth) rb_process_resume,
177 rb_define_method(rb_msg, "processIsSuspend",
178 (rb_meth) rb_process_isSuspended, 1);
179 rb_define_method(rb_msg, "processKill", (rb_meth) rb_process_kill_up, 1);
180 rb_define_method(rb_msg, "processKillDown",
181 (rb_meth) rb_process_kill_down, 1);
182 rb_define_method(rb_msg, "processGetHost", (rb_meth) rb_process_getHost,
184 rb_define_method(rb_msg, "processExit", (rb_meth) rb_process_exit, 1);
187 rb_task = rb_define_class_under(rb_msg, "RbTask", rb_cObject);
188 rb_host = rb_define_class_under(rb_msg, "RbHost", rb_cObject);
191 rb_define_module_function(rb_task, "new", (rb_meth) rb_task_new, 3);
192 rb_define_module_function(rb_task, "compSize", (rb_meth) rb_task_comp,
194 rb_define_module_function(rb_task, "name", (rb_meth) rb_task_name, 1);
195 rb_define_module_function(rb_task, "execute", (rb_meth) rb_task_execute,
197 rb_define_module_function(rb_task, "send", (rb_meth) rb_task_send, 2);
198 rb_define_module_function(rb_task, "receive", (rb_meth) rb_task_receive,
200 rb_define_module_function(rb_task, "sender", (rb_meth) rb_task_sender,
202 rb_define_module_function(rb_task, "source", (rb_meth) rb_task_source,
204 rb_define_module_function(rb_task, "listen", (rb_meth) rb_task_listen,
206 rb_define_module_function(rb_task, "listenFromHost",
207 (rb_meth) rb_task_listen_host, 3);
208 rb_define_module_function(rb_task, "setPriority",
209 (rb_meth) rb_task_set_priority, 2);
210 rb_define_module_function(rb_task, "cancel", (rb_meth) rb_task_cancel,
212 rb_define_module_function(rb_task, "hasData", (rb_meth) rb_task_has_data,
214 rb_define_module_function(rb_task, "setData", (rb_meth) rb_task_set_data,
216 rb_define_module_function(rb_task, "data", (rb_meth) rb_task_get_data,
220 rb_define_module_function(rb_host, "getByName",
221 (rb_meth) rb_host_get_by_name, 1);
222 rb_define_module_function(rb_host, "name", (rb_meth) rb_host_name, 1);
223 rb_define_module_function(rb_host, "speed", (rb_meth) rb_host_speed, 1);
224 rb_define_module_function(rb_host, "number", (rb_meth) rb_host_number,
226 rb_define_module_function(rb_host, "isAvail", (rb_meth) rb_host_is_avail,
228 rb_define_module_function(rb_host, "getHostProcess",
229 (rb_meth) rb_host_process, 1);
230 rb_define_module_function(rb_host, "all",
231 (rb_meth) rb_host_get_all_hosts, 0);