Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4a9a8f6de07f8b7f0970aba668f79476d45c73ac
[simgrid.git] / src / bindings / ruby / rb_msg.c
1 #include "rb_msg.h"
2 #include "msg/msg.h"
3 #include "msg/datatypes.h"
4 #include "xbt/sysdep.h"        
5 #include "xbt/log.h"
6 #include "xbt/asserts.h"
7 #include "surf/surfxml_parse.h"
8
9
10 #include "rb_msg_task.c"
11 #include "rb_msg_host.c"
12 #include "rb_msg_process.c"
13 #include "rb_application_handler.c"
14
15
16
17 //Init Msg_Init From Ruby
18
19
20 static void msg_init(VALUE Class,VALUE args)
21 {
22   
23   char **argv=NULL;  
24   const char *tmp;
25   int argc,type,i;
26   VALUE *ptr ;
27   
28   
29   
30   // Testing The Args Type
31   type =  TYPE(args);
32   
33   if (type != T_ARRAY )
34   {
35     rb_raise(rb_eRuntimeError,"Argh!! Bad Arguments to msg_init");
36     return;
37   }
38     
39   ptr= RARRAY(args)->ptr;
40   argc= RARRAY(args)->len;
41   
42 //   Create C Array to Hold Data_Get_Struct 
43   argv = xbt_new0(char *, argc);  // argc or argc +1
44   
45   argv[0] = strdup("ruby");
46   
47   
48   for (i=0;i<argc;i++)
49   {
50    VALUE value = ptr[i];
51    type = TYPE(value);
52 //   if (type == T_STRING)
53    tmp = RSTRING(value)->ptr;
54    argv[i+1] = strdup(tmp);
55     
56   }
57   
58   // Calling C Msg_Init Method
59   MSG_global_init(&argc,argv);
60   MSG_set_channel_number(10); // Okey !! Okey !! This Must Be Fixed Dynamiclly , But Later ;)
61   SIMIX_context_select_factory("ruby"); 
62   
63   // Free Stuffs 
64   for (i=0;i<argc;i++)
65    free(argv[i]) ;
66   
67   free (argv);
68  
69   printf("Msg Init...Done\n");
70   return;
71 }
72
73
74
75
76 //Init Msg_Run From Ruby
77 static void msg_run(VALUE class)
78 {
79   
80  xbt_fifo_item_t item = NULL;
81  m_host_t host = NULL;
82  VALUE rbHost;
83  
84  // Let's Run
85  if (MSG_OK != MSG_main()){
86    
87    rb_raise(rb_eRuntimeError,"MSG_main() failed");
88    
89  }
90   DEBUG
91     ("MSG_main finished. Bail out before cleanup since there is a bug in this part.");
92      /* Cleanup Ruby hosts */
93    DEBUG("Clean Ruby World");
94    xbt_fifo_foreach(msg_global->host, item, host, m_host_t) {
95    
96      //rbHost = (VALUE)host->data;// ??!!
97     
98       }
99     
100    if (MSG_OK != MSG_clean()){
101     
102      rb_raise(rb_eRuntimeError,"MSG_clean() failed");
103    }
104     
105     return;
106     
107 }
108
109
110
111
112 // Create Environment
113 static void msg_createEnvironment(VALUE class,VALUE plateformFile)
114 {
115  
116   
117   int type = TYPE(plateformFile);
118   
119   if ( type != T_STRING )
120     rb_raise(rb_eRuntimeError,"Bad Argument's Type");
121    
122  const char * platform =  RSTRING(plateformFile)->ptr;
123  
124  MSG_create_environment(platform);
125  
126  return; 
127   
128 }
129
130
131 // deploy Application
132
133 static void msg_deployApplication(VALUE class,VALUE deploymentFile )
134 {
135   
136     int type = TYPE(deploymentFile);
137   
138     if ( type != T_STRING )
139      rb_raise(rb_eRuntimeError,"Bad Argument's Type");
140   
141     const char *dep_file = RSTRING(deploymentFile)->ptr;
142     
143     
144     surf_parse_reset_parser();
145     
146     surfxml_add_callback(STag_surfxml_process_cb_list,
147                          application_handler_on_begin_process);
148     
149     surfxml_add_callback(STag_surfxml_argument_cb_list,
150                          application_handler_on_process_arg);
151
152     surfxml_add_callback(STag_surfxml_prop_cb_list,
153                          application_handler_on_property);
154                          
155     surfxml_add_callback(STag_surfxml_process_cb_list,
156                          application_handler_on_end_process);
157                          
158     surf_parse_open(dep_file);
159                          
160     application_handler_on_start_document();
161     
162     if(surf_parse())
163       rb_raise(rb_eRuntimeError,"surf_parse() failed");
164     
165     surf_parse_close();
166     
167     application_handler_on_end_document();
168     
169   
170 }
171
172
173
174 static void msg_registerFunction(VALUE class,VALUE function_name,VALUE code)
175 {
176   
177  char * fct_name = RSTRING(function_name)->ptr;
178  xbt_main_func_t fct_code 
179   
180 }
181 // INFO
182 static void msg_info(VALUE class,VALUE msg)
183 {
184  const char *s = RSTRING(msg)->ptr;
185  INFO("%s",s);
186 }
187
188
189 // Get Clock
190 static VALUE msg_get_clock(VALUE class)
191 {
192  
193   return DBL2NUM(MSG_get_clock());
194   
195 }
196
197 //pajeOutput
198 static void msg_paje_out(VALUE class,VALUE pajeFile)
199 {
200   const char *pfile = RSTRING(pajeFile)->ptr;
201   MSG_paje_output(pfile);
202   
203 }
204
205 /*****************************************************************************************************************
206
207 Wrapping MSG module and its Class ( Task,Host) & Methods ( Process's method...ect)
208 To Ruby 
209
210  the part after "Init_" is the name of the C extension specified in extconf.rb , not the name of C source file
211  
212 *****************************************************************************************************************/
213
214 void Init_msg()
215 {
216   
217   // Modules
218    rb_msg = rb_define_module("MSG");
219     
220    //Associated Environment Methods!
221    rb_define_method(rb_msg,"init",msg_init,1);
222    rb_define_method(rb_msg,"run",msg_run,0);
223    rb_define_method(rb_msg,"createEnvironment",msg_createEnvironment,1);
224    rb_define_method(rb_msg,"deployApplication",msg_deployApplication,1);
225    rb_define_method(rb_msg,"info",msg_info,1);
226    rb_define_method(rb_msg,"getClock",msg_get_clock,0);
227    rb_define_method(rb_msg,"pajeOutput",msg_paje_out,1);
228    
229    // Associated Process Methods
230 //    rb_define_method(rb_msg,"processCreate",processCreate,2);
231    rb_define_method(rb_msg,"processSuspend",processSuspend,1);
232    rb_define_method(rb_msg,"processResume",processResume,1);
233    rb_define_method(rb_msg,"processIsSuspend",processIsSuspend,1);
234    rb_define_method(rb_msg,"processKill",processKill,1);
235    rb_define_method(rb_msg,"processGetHost",processGetHost,1);
236      
237    //Classes
238    rb_task = rb_define_class_under(rb_msg,"Task",rb_cObject);
239    rb_host = rb_define_class_under(rb_msg,"Host",rb_cObject);
240    
241    //Task Methods
242    rb_define_module_function(rb_task,"new",task_new,3);
243    rb_define_module_function(rb_task,"compSize",task_comp,1);
244    rb_define_module_function(rb_task,"name",task_name,1);
245    rb_define_module_function(rb_task,"execute",task_execute,1);
246    rb_define_module_function(rb_task,"send",task_send,2);
247    rb_define_module_function(rb_task,"receive",task_receive,1);
248    rb_define_module_function(rb_task,"receive2",task_receive2,2);
249    rb_define_module_function(rb_task,"sender",task_sender,1);
250    rb_define_module_function(rb_task,"source",task_source,1);
251    rb_define_module_function(rb_task,"listen",task_listen,2);
252    rb_define_module_function(rb_task,"listenFromHost",task_listen_host,3);
253     
254    //Host Methods
255    rb_define_module_function(rb_host,"getByName",host_get_by_name,1);
256    rb_define_module_function(rb_host,"name",host_name,1);
257    rb_define_module_function(rb_host,"speed",host_speed,1);
258    rb_define_module_function(rb_host,"number",host_number,0);
259    rb_define_module_function(rb_host,"setData",host_set_data,2);
260    rb_define_module_function(rb_host,"getData",host_get_data,1);
261 //    rb_define_module_function(rb_host,"hasData",host_has_data,1);
262    rb_define_module_function(rb_host,"isAvail",host_is_avail,1);
263    
264 }