Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
23c9d5c8b6f1685b8e1472f52520ab2bf62f56fe
[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,"Bad arguments to msg_init (expecting an array)");
31     return;
32   }
33   ptr= RARRAY(args)->ptr;
34   argc= RARRAY(args)->len;
35   //  Create C Array to Hold Data_Get_Struct
36   argc++;
37   argv = xbt_new0(char *, argc);
38   argv[0] = strdup("ruby");
39   for (i=0;i<argc-1;i++) {
40     VALUE value = ptr[i];
41     type = TYPE(value);
42     //  if (type == T_STRING)
43     tmp = RSTRING(value)->ptr;
44     argv[i+1] = strdup(tmp);
45   }
46   // Calling C Msg_Init Method
47   MSG_global_init(&argc,argv);
48
49   // Cleanups
50   for (i=0;i<argc;i++)
51     free(argv[i]) ;
52   free (argv);
53 }
54 //Init Msg_Run From Ruby
55 static void msg_run(VALUE class) {
56   DEBUG0("Start Running...");
57   m_host_t *hosts;
58   int cpt,host_count;
59   VALUE rbHost;
60   // Let's Run
61   //printf("msg_run3\n");
62   if (MSG_OK != MSG_main()){
63     rb_raise(rb_eRuntimeError,"MSG_main() failed");
64   }
65
66   DEBUG0
67   ("MSG_main finished. Bail out before cleanup since there is a bug in this part.");
68   /* Cleanup Ruby hosts */
69   DEBUG0("Clean Ruby World  ");
70   hosts = MSG_get_host_table();
71   host_count = MSG_get_host_number();
72   for (cpt=0;cpt<host_count;cpt++) {
73     rbHost = (VALUE)((hosts[cpt])->data);
74   }
75   return;
76 }
77
78 static void msg_clean(VALUE class)
79 {
80    if (MSG_OK != MSG_clean())
81     rb_raise(rb_eRuntimeError,"MSG_clean() failed");
82   
83 }
84 static void msg_createEnvironment(VALUE class,VALUE plateformFile) {
85
86   int type = TYPE(plateformFile);
87   if ( type != T_STRING )
88     rb_raise(rb_eRuntimeError,"Bad Argument's Type");
89   const char * platform =  RSTRING(plateformFile)->ptr;
90   MSG_create_environment(platform);
91   DEBUG1("Create Environment (%s)...Done",platform);
92 }
93
94 //deploy Application
95 static void msg_deployApplication(VALUE class,VALUE deploymentFile ) {
96
97   int type = TYPE(deploymentFile);
98   if ( type != T_STRING )
99     rb_raise(rb_eRuntimeError,"Bad Argument's Type for deployApplication ");
100   const char *dep_file = RSTRING(deploymentFile)->ptr;
101   surf_parse_reset_parser();
102   surfxml_add_callback(STag_surfxml_process_cb_list,
103       rb_application_handler_on_begin_process);
104   surfxml_add_callback(ETag_surfxml_argument_cb_list,
105       rb_application_handler_on_process_arg);
106
107   surfxml_add_callback(STag_surfxml_prop_cb_list,
108       rb_application_handler_on_property);
109
110   surfxml_add_callback(ETag_surfxml_process_cb_list,
111       rb_application_handler_on_end_process);
112
113   surf_parse_open(dep_file);
114   rb_application_handler_on_start_document();
115   if(surf_parse())
116     rb_raise(rb_eRuntimeError,"surf_parse() failed");
117   surf_parse_close();
118   
119   rb_application_handler_on_end_document();
120
121   DEBUG1("Deploy Application(%s)...Done",dep_file);
122 }
123
124 // INFO
125 static void msg_info(VALUE class,VALUE msg) {
126   const char *s = RSTRING(msg)->ptr;
127   INFO1("%s",s);
128 }
129 static void msg_debug(VALUE class,VALUE msg) {
130   const char *s = RSTRING(msg)->ptr;
131   DEBUG1("%s",s);
132 }
133
134 // get Clock
135 static VALUE msg_get_clock(VALUE class) {
136   return rb_float_new(MSG_get_clock());
137
138 }
139
140 extern const char*xbt_ctx_factory_to_use; /*Hack: let msg load directly the right factory */
141
142 typedef VALUE(*rb_meth)(ANYARGS);
143 void Init_libsimgrid() {
144   xbt_ctx_factory_to_use = "ruby";
145
146   // Modules
147   rb_msg = rb_define_module("MSG");
148   //Associated Environment Methods
149   rb_define_module_function(rb_msg,"init",(rb_meth)msg_init,1);
150   rb_define_module_function(rb_msg,"run",(rb_meth)msg_run,0);
151   rb_define_module_function(rb_msg,"createEnvironment",(rb_meth)msg_createEnvironment,1);
152   rb_define_module_function(rb_msg,"deployApplication",(rb_meth)msg_deployApplication,1);
153   rb_define_module_function(rb_msg,"info",(rb_meth)msg_info,1);
154   rb_define_module_function(rb_msg,"debug",(rb_meth)msg_debug,1);
155   rb_define_module_function(rb_msg,"getClock",(rb_meth)msg_get_clock,0);
156   rb_define_module_function(rb_msg,"exit",(rb_meth)msg_clean,0);
157
158   //Associated Process Methods
159   rb_define_method(rb_msg,"processSuspend",(rb_meth)rb_process_suspend,1);
160   rb_define_method(rb_msg,"processResume",(rb_meth)rb_process_resume,1);
161   rb_define_method(rb_msg,"processIsSuspend",(rb_meth)rb_process_isSuspended,1);
162   rb_define_method(rb_msg,"processKill",(rb_meth)rb_process_kill_up,1);
163   rb_define_method(rb_msg,"processKillDown",(rb_meth)rb_process_kill_down,1);
164   rb_define_method(rb_msg,"processGetHost",(rb_meth)rb_process_getHost,1);
165   rb_define_method(rb_msg,"processExit",(rb_meth)rb_process_exit,1);
166
167   //Classes
168   rb_task = rb_define_class_under(rb_msg,"RbTask",rb_cObject);
169   rb_host = rb_define_class_under(rb_msg,"RbHost",rb_cObject);
170
171   //Task Methods 
172   rb_define_module_function(rb_task,"new",(rb_meth)rb_task_new,3);
173   rb_define_module_function(rb_task,"compSize",(rb_meth)rb_task_comp,1);
174   rb_define_module_function(rb_task,"name",(rb_meth)rb_task_name,1);
175   rb_define_module_function(rb_task,"execute",(rb_meth)rb_task_execute,1);
176   rb_define_module_function(rb_task,"send",(rb_meth)rb_task_send,2);
177   rb_define_module_function(rb_task,"receive",(rb_meth)rb_task_receive,1);
178   rb_define_module_function(rb_task,"sender",(rb_meth)rb_task_sender,1);
179   rb_define_module_function(rb_task,"source",(rb_meth)rb_task_source,1);
180   rb_define_module_function(rb_task,"listen",(rb_meth)rb_task_listen,2);
181   rb_define_module_function(rb_task,"listenFromHost",(rb_meth)rb_task_listen_host,3);
182   rb_define_module_function(rb_task,"setPriority",(rb_meth)rb_task_set_priority,2);
183   rb_define_module_function(rb_task,"cancel",(rb_meth)rb_task_cancel,1);
184   rb_define_module_function(rb_task,"hasData",(rb_meth)rb_task_has_data,1);
185   rb_define_module_function(rb_task,"setData",(rb_meth)rb_task_set_data,2);
186   rb_define_module_function(rb_task,"data",(rb_meth)rb_task_get_data,1);
187
188   //Host Methods
189   rb_define_module_function(rb_host,"getByName",(rb_meth)rb_host_get_by_name,1);
190   rb_define_module_function(rb_host,"name",(rb_meth)rb_host_name,1);
191   rb_define_module_function(rb_host,"speed",(rb_meth)rb_host_speed,1);
192   rb_define_module_function(rb_host,"number",(rb_meth)rb_host_number,0);
193   rb_define_module_function(rb_host,"isAvail",(rb_meth)rb_host_is_avail,1);
194   rb_define_module_function(rb_host,"getHostProcess",(rb_meth)rb_host_process,1);
195   rb_define_module_function(rb_host,"all",(rb_meth)rb_host_get_all_hosts,0);
196 }