Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
7208bdec768d3c454f4b0be7ed152fbdd57b660f
[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_PTR(args);
35   argc = RARRAY_LEN(args);
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_PTR(value);
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_PTR(plateformFile);
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_PTR(deploymentFile);
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_PTR(msg);
135   INFO1("%s", s);
136 }
137
138 static void msg_debug(VALUE class, VALUE msg)
139 {
140   const char *s = RSTRING_PTR(msg);
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 typedef VALUE(*rb_meth) (ANYARGS);
152 void Init_libsimgrid()
153 {
154   factory_initializer_to_use = SIMIX_ctx_ruby_factory_init;
155
156   // Modules
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,
168                             0);
169   rb_define_module_function(rb_msg, "exit", (rb_meth) msg_clean, 0);
170
171   //Associated Process Methods
172   rb_define_method(rb_msg, "processSuspend", (rb_meth) rb_process_suspend,
173                    1);
174   rb_define_method(rb_msg, "processResume", (rb_meth) rb_process_resume,
175                    1);
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,
182                    1);
183   rb_define_method(rb_msg, "processExit", (rb_meth) rb_process_exit, 1);
184
185   //Classes
186   rb_task = rb_define_class_under(rb_msg, "RbTask", rb_cObject);
187   rb_host = rb_define_class_under(rb_msg, "RbHost", rb_cObject);
188
189   //Task Methods 
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,
192                             1);
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,
195                             1);
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,
198                             1);
199   rb_define_module_function(rb_task, "sender", (rb_meth) rb_task_sender,
200                             1);
201   rb_define_module_function(rb_task, "source", (rb_meth) rb_task_source,
202                             1);
203   rb_define_module_function(rb_task, "listen", (rb_meth) rb_task_listen,
204                             2);
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,
210                             1);
211   rb_define_module_function(rb_task, "hasData", (rb_meth) rb_task_has_data,
212                             1);
213   rb_define_module_function(rb_task, "setData", (rb_meth) rb_task_set_data,
214                             2);
215   rb_define_module_function(rb_task, "data", (rb_meth) rb_task_get_data,
216                             1);
217
218   //Host Methods
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,
224                             0);
225   rb_define_module_function(rb_host, "isAvail", (rb_meth) rb_host_is_avail,
226                             1);
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);
231 }