Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Do not request status if not requested by caller.
[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 #include "simix/smx_context_private.h"
12
13 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ruby);
14
15 // MSG Module
16 VALUE rb_msg;
17 // MSG Classes
18 VALUE rb_task;
19 VALUE rb_host;
20
21 //Init Msg  From Ruby
22 static void msg_init(VALUE Class, VALUE args)
23 {
24   char **argv = NULL;
25   const char *tmp;
26   int argc, type, i;
27   VALUE *ptr;
28   // Testing The Args Type
29   type = TYPE(args);
30   if (type != T_ARRAY) {
31     rb_raise(rb_eRuntimeError,
32              "Bad arguments to msg_init (expecting an array)");
33     return;
34   }
35   ptr = RARRAY(args)->ptr;
36   argc = RARRAY(args)->len;
37   //  Create C array to hold data_get_struct
38   argc++;
39   argv = xbt_new0(char *, argc);
40   argv[0] = strdup("ruby");
41   for (i = 0; i < argc - 1; i++) {
42     VALUE value = ptr[i];
43     type = TYPE(value);
44     //  if (type == T_STRING)
45     tmp = RSTRING(value)->ptr;
46     argv[i + 1] = strdup(tmp);
47   }
48   // Calling C Msg_Init Method
49   MSG_global_init(&argc, argv);
50
51   // Cleanups
52   for (i = 0; i < argc; i++)
53     free(argv[i]);
54   free(argv);
55 }
56
57 //Init Msg_Run From Ruby
58 static void msg_run(VALUE class)
59 {
60   DEBUG0("Start Running...");
61   m_host_t *hosts;
62   int cpt, host_count;
63   VALUE rbHost;
64   // Let's Run
65   //printf("msg_run3\n");
66   if (MSG_OK != MSG_main()) {
67     rb_raise(rb_eRuntimeError, "MSG_main() failed");
68   }
69
70   DEBUG0
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);
78   }
79   return;
80 }
81
82 static void msg_clean(VALUE class)
83 {
84   if (MSG_OK != MSG_clean())
85     rb_raise(rb_eRuntimeError, "MSG_clean() failed");
86
87 }
88
89 static void msg_createEnvironment(VALUE class, VALUE plateformFile)
90 {
91
92   int type = TYPE(plateformFile);
93   if (type != T_STRING)
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);
98 }
99
100 //deploy Application
101 static void msg_deployApplication(VALUE class, VALUE deploymentFile)
102 {
103
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);
114
115   surfxml_add_callback(STag_surfxml_prop_cb_list,
116                        rb_application_handler_on_property);
117
118   surfxml_add_callback(ETag_surfxml_process_cb_list,
119                        rb_application_handler_on_end_process);
120
121   surf_parse_open(dep_file);
122   rb_application_handler_on_start_document();
123   if (surf_parse())
124     rb_raise(rb_eRuntimeError, "surf_parse() failed");
125   surf_parse_close();
126
127   rb_application_handler_on_end_document();
128
129   DEBUG1("Deploy Application(%s)...Done", dep_file);
130 }
131
132 // INFO
133 static void msg_info(VALUE class, VALUE msg)
134 {
135   const char *s = RSTRING(msg)->ptr;
136   INFO1("%s", s);
137 }
138
139 static void msg_debug(VALUE class, VALUE msg)
140 {
141   const char *s = RSTRING(msg)->ptr;
142   DEBUG1("%s", s);
143 }
144
145 // get Clock
146 static VALUE msg_get_clock(VALUE class)
147 {
148   return rb_float_new(MSG_get_clock());
149
150 }
151
152 typedef VALUE(*rb_meth) (ANYARGS);
153 void Init_libsimgrid()
154 {
155   factory_initializer_to_use = SIMIX_ctx_ruby_factory_init;
156
157   // Modules
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,
169                             0);
170   rb_define_module_function(rb_msg, "exit", (rb_meth) msg_clean, 0);
171
172   //Associated Process Methods
173   rb_define_method(rb_msg, "processSuspend", (rb_meth) rb_process_suspend,
174                    1);
175   rb_define_method(rb_msg, "processResume", (rb_meth) rb_process_resume,
176                    1);
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,
183                    1);
184   rb_define_method(rb_msg, "processExit", (rb_meth) rb_process_exit, 1);
185
186   //Classes
187   rb_task = rb_define_class_under(rb_msg, "RbTask", rb_cObject);
188   rb_host = rb_define_class_under(rb_msg, "RbHost", rb_cObject);
189
190   //Task Methods 
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,
193                             1);
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,
196                             1);
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,
199                             1);
200   rb_define_module_function(rb_task, "sender", (rb_meth) rb_task_sender,
201                             1);
202   rb_define_module_function(rb_task, "source", (rb_meth) rb_task_source,
203                             1);
204   rb_define_module_function(rb_task, "listen", (rb_meth) rb_task_listen,
205                             2);
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,
211                             1);
212   rb_define_module_function(rb_task, "hasData", (rb_meth) rb_task_has_data,
213                             1);
214   rb_define_module_function(rb_task, "setData", (rb_meth) rb_task_set_data,
215                             2);
216   rb_define_module_function(rb_task, "data", (rb_meth) rb_task_get_data,
217                             1);
218
219   //Host Methods
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,
225                             0);
226   rb_define_module_function(rb_host, "isAvail", (rb_meth) rb_host_is_avail,
227                             1);
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);
232 }