Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
5ac81ec155fbeb4b6947edcdd900e18b184c7398
[simgrid.git] / src / bindings / ruby / simgrid_ruby.c
1 /* SimGrid Ruby bindings                                                    */
2
3 /* Copyright (c) 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 "xbt.h"
10 #include "bindings/ruby_bindings.h"
11
12 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ruby);
13
14 // MSG Module
15 VALUE rb_msg;
16 // MSG Classes
17 VALUE rb_task;
18 VALUE rb_host;
19
20 //Init Msg  From Ruby
21 static void msg_init(VALUE Class, VALUE args)
22 {
23   char **argv = NULL;
24   const char *tmp;
25   int argc, type, i;
26   VALUE *ptr;
27   // Testing The Args Type
28   type = TYPE(args);
29   if (type != T_ARRAY) {
30     rb_raise(rb_eRuntimeError,
31              "Bad arguments to msg_init (expecting an array)");
32     return;
33   }
34   ptr = RARRAY(args)->ptr;
35   argc = RARRAY(args)->len;
36   //  Create C array to hold data_get_struct
37   argc++;
38   argv = xbt_new0(char *, argc);
39   argv[0] = strdup("ruby");
40   for (i = 0; i < argc - 1; i++) {
41     VALUE value = ptr[i];
42     type = TYPE(value);
43     //  if (type == T_STRING)
44     tmp = RSTRING(value)->ptr;
45     argv[i + 1] = strdup(tmp);
46   }
47   // Calling C Msg_Init Method
48   MSG_global_init(&argc, argv);
49
50   // Cleanups
51   for (i = 0; i < argc; i++)
52     free(argv[i]);
53   free(argv);
54 }
55
56 //Init Msg_Run From Ruby
57 static void msg_run(VALUE class)
58 {
59   DEBUG0("Start Running...");
60   m_host_t *hosts;
61   int cpt, host_count;
62   VALUE rbHost;
63   // Let's Run
64   //printf("msg_run3\n");
65   if (MSG_OK != MSG_main()) {
66     rb_raise(rb_eRuntimeError, "MSG_main() failed");
67   }
68
69   DEBUG0
70       ("MSG_main finished. Bail out before cleanup since there is a bug in this part.");
71   /* Cleanup Ruby hosts */
72   DEBUG0("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);
77   }
78   return;
79 }
80
81 static void msg_clean(VALUE class)
82 {
83   if (MSG_OK != MSG_clean())
84     rb_raise(rb_eRuntimeError, "MSG_clean() failed");
85
86 }
87
88 static void msg_createEnvironment(VALUE class, VALUE plateformFile)
89 {
90
91   int type = TYPE(plateformFile);
92   if (type != T_STRING)
93     rb_raise(rb_eRuntimeError, "Bad Argument's Type");
94   const char *platform = RSTRING(plateformFile)->ptr;
95   MSG_create_environment(platform);
96   DEBUG1("Create Environment (%s)...Done", platform);
97 }
98
99 //deploy Application
100 static void msg_deployApplication(VALUE class, VALUE deploymentFile)
101 {
102
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(deploymentFile)->ptr;
108   surf_parse_reset_parser();
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);
113
114   surfxml_add_callback(STag_surfxml_prop_cb_list,
115                        rb_application_handler_on_property);
116
117   surfxml_add_callback(ETag_surfxml_process_cb_list,
118                        rb_application_handler_on_end_process);
119
120   surf_parse_open(dep_file);
121   rb_application_handler_on_start_document();
122   if (surf_parse())
123     rb_raise(rb_eRuntimeError, "surf_parse() failed");
124   surf_parse_close();
125
126   rb_application_handler_on_end_document();
127
128   DEBUG1("Deploy Application(%s)...Done", dep_file);
129 }
130
131 // INFO
132 static void msg_info(VALUE class, VALUE msg)
133 {
134   const char *s = RSTRING(msg)->ptr;
135   INFO1("%s", s);
136 }
137
138 static void msg_debug(VALUE class, VALUE msg)
139 {
140   const char *s = RSTRING(msg)->ptr;
141   DEBUG1("%s", s);
142 }
143
144 // get Clock
145 static VALUE msg_get_clock(VALUE class)
146 {
147   return rb_float_new(MSG_get_clock());
148
149 }
150
151 extern const char *xbt_ctx_factory_to_use;      /*Hack: let msg load directly the right factory */
152
153 typedef VALUE(*rb_meth) (ANYARGS);
154 void Init_libsimgrid()
155 {
156   xbt_ctx_factory_to_use = "ruby";
157
158   // Modules
159   rb_msg = rb_define_module("MSG");
160   //Associated Environment Methods
161   rb_define_module_function(rb_msg, "init", (rb_meth) msg_init, 1);
162   rb_define_module_function(rb_msg, "run", (rb_meth) msg_run, 0);
163   rb_define_module_function(rb_msg, "createEnvironment",
164                             (rb_meth) msg_createEnvironment, 1);
165   rb_define_module_function(rb_msg, "deployApplication",
166                             (rb_meth) msg_deployApplication, 1);
167   rb_define_module_function(rb_msg, "info", (rb_meth) msg_info, 1);
168   rb_define_module_function(rb_msg, "debug", (rb_meth) msg_debug, 1);
169   rb_define_module_function(rb_msg, "getClock", (rb_meth) msg_get_clock,
170                             0);
171   rb_define_module_function(rb_msg, "exit", (rb_meth) msg_clean, 0);
172
173   //Associated Process Methods
174   rb_define_method(rb_msg, "processSuspend", (rb_meth) rb_process_suspend,
175                    1);
176   rb_define_method(rb_msg, "processResume", (rb_meth) rb_process_resume,
177                    1);
178   rb_define_method(rb_msg, "processIsSuspend",
179                    (rb_meth) rb_process_isSuspended, 1);
180   rb_define_method(rb_msg, "processKill", (rb_meth) rb_process_kill_up, 1);
181   rb_define_method(rb_msg, "processKillDown",
182                    (rb_meth) rb_process_kill_down, 1);
183   rb_define_method(rb_msg, "processGetHost", (rb_meth) rb_process_getHost,
184                    1);
185   rb_define_method(rb_msg, "processExit", (rb_meth) rb_process_exit, 1);
186
187   //Classes
188   rb_task = rb_define_class_under(rb_msg, "RbTask", rb_cObject);
189   rb_host = rb_define_class_under(rb_msg, "RbHost", rb_cObject);
190
191   //Task Methods 
192   rb_define_module_function(rb_task, "new", (rb_meth) rb_task_new, 3);
193   rb_define_module_function(rb_task, "compSize", (rb_meth) rb_task_comp,
194                             1);
195   rb_define_module_function(rb_task, "name", (rb_meth) rb_task_name, 1);
196   rb_define_module_function(rb_task, "execute", (rb_meth) rb_task_execute,
197                             1);
198   rb_define_module_function(rb_task, "send", (rb_meth) rb_task_send, 2);
199   rb_define_module_function(rb_task, "receive", (rb_meth) rb_task_receive,
200                             1);
201   rb_define_module_function(rb_task, "sender", (rb_meth) rb_task_sender,
202                             1);
203   rb_define_module_function(rb_task, "source", (rb_meth) rb_task_source,
204                             1);
205   rb_define_module_function(rb_task, "listen", (rb_meth) rb_task_listen,
206                             2);
207   rb_define_module_function(rb_task, "listenFromHost",
208                             (rb_meth) rb_task_listen_host, 3);
209   rb_define_module_function(rb_task, "setPriority",
210                             (rb_meth) rb_task_set_priority, 2);
211   rb_define_module_function(rb_task, "cancel", (rb_meth) rb_task_cancel,
212                             1);
213   rb_define_module_function(rb_task, "hasData", (rb_meth) rb_task_has_data,
214                             1);
215   rb_define_module_function(rb_task, "setData", (rb_meth) rb_task_set_data,
216                             2);
217   rb_define_module_function(rb_task, "data", (rb_meth) rb_task_get_data,
218                             1);
219
220   //Host Methods
221   rb_define_module_function(rb_host, "getByName",
222                             (rb_meth) rb_host_get_by_name, 1);
223   rb_define_module_function(rb_host, "name", (rb_meth) rb_host_name, 1);
224   rb_define_module_function(rb_host, "speed", (rb_meth) rb_host_speed, 1);
225   rb_define_module_function(rb_host, "number", (rb_meth) rb_host_number,
226                             0);
227   rb_define_module_function(rb_host, "isAvail", (rb_meth) rb_host_is_avail,
228                             1);
229   rb_define_module_function(rb_host, "getHostProcess",
230                             (rb_meth) rb_host_process, 1);
231   rb_define_module_function(rb_host, "all",
232                             (rb_meth) rb_host_get_all_hosts, 0);
233 }