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"
12 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ruby);
21 static void msg_init(VALUE Class, VALUE args)
27 // Testing The Args Type
29 if (type != T_ARRAY) {
30 rb_raise(rb_eRuntimeError,
31 "Bad arguments to msg_init (expecting an array)");
34 ptr = RARRAY_PTR(args);
35 argc = RARRAY_LEN(args);
36 // Create C array to hold data_get_struct
38 argv = xbt_new0(char *, argc);
39 argv[0] = strdup("ruby");
40 for (i = 0; i < argc - 1; i++) {
43 // if (type == T_STRING)
44 tmp = RSTRING_PTR(value);
45 argv[i + 1] = strdup(tmp);
47 // Calling C Msg_Init Method
48 MSG_global_init(&argc, argv);
51 for (i = 0; i < argc; i++)
56 //Init Msg_Run From Ruby
57 static void msg_run(VALUE class)
59 XBT_DEBUG("Start Running...");
64 //printf("msg_run3\n");
65 if (MSG_OK != MSG_main()) {
66 rb_raise(rb_eRuntimeError, "MSG_main() failed");
70 ("MSG_main finished. Bail out before cleanup since there is a bug in this part.");
71 /* Cleanup Ruby hosts */
72 XBT_DEBUG("Clean Ruby World ");
73 hosts = MSG_get_host_table();
74 host_count = MSG_get_host_number();
75 for (cpt = 0; cpt < host_count; cpt++) {
76 rbHost = (VALUE) ((hosts[cpt])->data);
81 static void msg_clean(VALUE class)
83 if (MSG_OK != MSG_clean())
84 rb_raise(rb_eRuntimeError, "MSG_clean() failed");
88 static void msg_createEnvironment(VALUE class, VALUE plateformFile)
91 int type = TYPE(plateformFile);
93 rb_raise(rb_eRuntimeError, "Bad Argument's Type");
94 const char *platform = RSTRING_PTR(plateformFile);
95 MSG_create_environment(platform);
96 XBT_DEBUG("Create Environment (%s)...Done", platform);
100 static void msg_deployApplication(VALUE class, VALUE deploymentFile)
103 int type = TYPE(deploymentFile);
104 if (type != T_STRING)
105 rb_raise(rb_eRuntimeError,
106 "Bad Argument's Type for deployApplication ");
107 const char *dep_file = RSTRING_PTR(deploymentFile);
108 surf_parse_reset_callbacks();
109 surfxml_add_callback(STag_surfxml_process_cb_list,
110 rb_application_handler_on_begin_process);
111 surfxml_add_callback(ETag_surfxml_argument_cb_list,
112 rb_application_handler_on_process_arg);
114 surfxml_add_callback(STag_surfxml_prop_cb_list,
115 rb_application_handler_on_property);
117 surfxml_add_callback(ETag_surfxml_process_cb_list,
118 rb_application_handler_on_end_process);
120 surf_parse_open(dep_file);
121 rb_application_handler_on_start_document();
123 rb_raise(rb_eRuntimeError, "surf_parse() failed");
126 rb_application_handler_on_end_document();
128 XBT_DEBUG("Deploy Application(%s)...Done", dep_file);
132 static void msg_info(VALUE class, VALUE msg)
134 const char *s = RSTRING_PTR(msg);
138 static void msg_debug(VALUE class, VALUE msg)
140 const char *s = RSTRING_PTR(msg);
145 static VALUE msg_get_clock(VALUE class)
147 return rb_float_new(MSG_get_clock());
151 typedef VALUE(*rb_meth) (ANYARGS);
152 void Init_libsimgrid()
154 smx_factory_initializer_to_use = SIMIX_ctx_ruby_factory_init;
157 rb_msg = rb_define_module("MSG");
158 //Associated Environment Methods
159 rb_define_module_function(rb_msg, "init", (rb_meth) msg_init, 1);
160 rb_define_module_function(rb_msg, "run", (rb_meth) msg_run, 0);
161 rb_define_module_function(rb_msg, "createEnvironment",
162 (rb_meth) msg_createEnvironment, 1);
163 rb_define_module_function(rb_msg, "deployApplication",
164 (rb_meth) msg_deployApplication, 1);
165 rb_define_module_function(rb_msg, "info", (rb_meth) msg_info, 1);
166 rb_define_module_function(rb_msg, "debug", (rb_meth) msg_debug, 1);
167 rb_define_module_function(rb_msg, "getClock", (rb_meth) msg_get_clock,
169 rb_define_module_function(rb_msg, "exit", (rb_meth) msg_clean, 0);
171 //Associated Process Methods
172 rb_define_method(rb_msg, "processSuspend", (rb_meth) rb_process_suspend,
174 rb_define_method(rb_msg, "processResume", (rb_meth) rb_process_resume,
176 rb_define_method(rb_msg, "processIsSuspend",
177 (rb_meth) rb_process_isSuspended, 1);
178 rb_define_method(rb_msg, "processKill", (rb_meth) rb_process_kill_up, 1);
179 rb_define_method(rb_msg, "processKillDown",
180 (rb_meth) rb_process_kill_down, 1);
181 rb_define_method(rb_msg, "processGetHost", (rb_meth) rb_process_getHost,
183 rb_define_method(rb_msg, "processExit", (rb_meth) rb_process_exit, 1);
186 rb_task = rb_define_class_under(rb_msg, "RbTask", rb_cObject);
187 rb_host = rb_define_class_under(rb_msg, "RbHost", rb_cObject);
190 rb_define_module_function(rb_task, "new", (rb_meth) rb_task_new, 3);
191 rb_define_module_function(rb_task, "compSize", (rb_meth) rb_task_comp,
193 rb_define_module_function(rb_task, "name", (rb_meth) rb_task_name, 1);
194 rb_define_module_function(rb_task, "execute", (rb_meth) rb_task_execute,
196 rb_define_module_function(rb_task, "send", (rb_meth) rb_task_send, 2);
197 rb_define_module_function(rb_task, "receive", (rb_meth) rb_task_receive,
199 rb_define_module_function(rb_task, "sender", (rb_meth) rb_task_sender,
201 rb_define_module_function(rb_task, "source", (rb_meth) rb_task_source,
203 rb_define_module_function(rb_task, "listen", (rb_meth) rb_task_listen,
205 rb_define_module_function(rb_task, "listenFromHost",
206 (rb_meth) rb_task_listen_host, 3);
207 rb_define_module_function(rb_task, "setPriority",
208 (rb_meth) rb_task_set_priority, 2);
209 rb_define_module_function(rb_task, "cancel", (rb_meth) rb_task_cancel,
211 rb_define_module_function(rb_task, "hasData", (rb_meth) rb_task_has_data,
213 rb_define_module_function(rb_task, "setData", (rb_meth) rb_task_set_data,
215 rb_define_module_function(rb_task, "data", (rb_meth) rb_task_get_data,
219 rb_define_module_function(rb_host, "getByName",
220 (rb_meth) rb_host_get_by_name, 1);
221 rb_define_module_function(rb_host, "name", (rb_meth) rb_host_name, 1);
222 rb_define_module_function(rb_host, "speed", (rb_meth) rb_host_speed, 1);
223 rb_define_module_function(rb_host, "number", (rb_meth) rb_host_number,
225 rb_define_module_function(rb_host, "isAvail", (rb_meth) rb_host_is_avail,
227 rb_define_module_function(rb_host, "getHostProcess",
228 (rb_meth) rb_host_process, 1);
229 rb_define_module_function(rb_host, "all",
230 (rb_meth) rb_host_get_all_hosts, 0);