Logo AND Algorithmique Numérique Distribuée

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