Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f77f534bc9c3b375035e9b6e65d655863ce47e5b
[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 /*Hack: let msg load directly the right factory */
152 typedef void (*SIMIX_ctx_factory_initializer_t)(smx_context_factory_t *);
153 extern SIMIX_ctx_factory_initializer_t factory_initializer_to_use;
154
155
156 typedef VALUE(*rb_meth) (ANYARGS);
157 void Init_libsimgrid()
158 {
159   factory_initializer_to_use = SIMIX_ctx_ruby_factory_init;
160
161   // Modules
162   rb_msg = rb_define_module("MSG");
163   //Associated Environment Methods
164   rb_define_module_function(rb_msg, "init", (rb_meth) msg_init, 1);
165   rb_define_module_function(rb_msg, "run", (rb_meth) msg_run, 0);
166   rb_define_module_function(rb_msg, "createEnvironment",
167                             (rb_meth) msg_createEnvironment, 1);
168   rb_define_module_function(rb_msg, "deployApplication",
169                             (rb_meth) msg_deployApplication, 1);
170   rb_define_module_function(rb_msg, "info", (rb_meth) msg_info, 1);
171   rb_define_module_function(rb_msg, "debug", (rb_meth) msg_debug, 1);
172   rb_define_module_function(rb_msg, "getClock", (rb_meth) msg_get_clock,
173                             0);
174   rb_define_module_function(rb_msg, "exit", (rb_meth) msg_clean, 0);
175
176   //Associated Process Methods
177   rb_define_method(rb_msg, "processSuspend", (rb_meth) rb_process_suspend,
178                    1);
179   rb_define_method(rb_msg, "processResume", (rb_meth) rb_process_resume,
180                    1);
181   rb_define_method(rb_msg, "processIsSuspend",
182                    (rb_meth) rb_process_isSuspended, 1);
183   rb_define_method(rb_msg, "processKill", (rb_meth) rb_process_kill_up, 1);
184   rb_define_method(rb_msg, "processKillDown",
185                    (rb_meth) rb_process_kill_down, 1);
186   rb_define_method(rb_msg, "processGetHost", (rb_meth) rb_process_getHost,
187                    1);
188   rb_define_method(rb_msg, "processExit", (rb_meth) rb_process_exit, 1);
189
190   //Classes
191   rb_task = rb_define_class_under(rb_msg, "RbTask", rb_cObject);
192   rb_host = rb_define_class_under(rb_msg, "RbHost", rb_cObject);
193
194   //Task Methods 
195   rb_define_module_function(rb_task, "new", (rb_meth) rb_task_new, 3);
196   rb_define_module_function(rb_task, "compSize", (rb_meth) rb_task_comp,
197                             1);
198   rb_define_module_function(rb_task, "name", (rb_meth) rb_task_name, 1);
199   rb_define_module_function(rb_task, "execute", (rb_meth) rb_task_execute,
200                             1);
201   rb_define_module_function(rb_task, "send", (rb_meth) rb_task_send, 2);
202   rb_define_module_function(rb_task, "receive", (rb_meth) rb_task_receive,
203                             1);
204   rb_define_module_function(rb_task, "sender", (rb_meth) rb_task_sender,
205                             1);
206   rb_define_module_function(rb_task, "source", (rb_meth) rb_task_source,
207                             1);
208   rb_define_module_function(rb_task, "listen", (rb_meth) rb_task_listen,
209                             2);
210   rb_define_module_function(rb_task, "listenFromHost",
211                             (rb_meth) rb_task_listen_host, 3);
212   rb_define_module_function(rb_task, "setPriority",
213                             (rb_meth) rb_task_set_priority, 2);
214   rb_define_module_function(rb_task, "cancel", (rb_meth) rb_task_cancel,
215                             1);
216   rb_define_module_function(rb_task, "hasData", (rb_meth) rb_task_has_data,
217                             1);
218   rb_define_module_function(rb_task, "setData", (rb_meth) rb_task_set_data,
219                             2);
220   rb_define_module_function(rb_task, "data", (rb_meth) rb_task_get_data,
221                             1);
222
223   //Host Methods
224   rb_define_module_function(rb_host, "getByName",
225                             (rb_meth) rb_host_get_by_name, 1);
226   rb_define_module_function(rb_host, "name", (rb_meth) rb_host_name, 1);
227   rb_define_module_function(rb_host, "speed", (rb_meth) rb_host_speed, 1);
228   rb_define_module_function(rb_host, "number", (rb_meth) rb_host_number,
229                             0);
230   rb_define_module_function(rb_host, "isAvail", (rb_meth) rb_host_is_avail,
231                             1);
232   rb_define_module_function(rb_host, "getHostProcess",
233                             (rb_meth) rb_host_process, 1);
234   rb_define_module_function(rb_host, "all",
235                             (rb_meth) rb_host_get_all_hosts, 0);
236 }