Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Massive cleanups in ruby. Not yet working (segfault on task reception)
[simgrid.git] / src / bindings / ruby / rb_msg.c
1 /* 
2  * Copyright 2010. The SimGrid Team. All right reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license GNU LGPL) which comes with this package.
6  */ 
7
8 #include "rb_msg.h"
9 #include "msg/msg.h"
10 #include "msg/datatypes.h"
11 #include "xbt/sysdep.h"        
12 #include "xbt/log.h"
13 #include "xbt/asserts.h"
14 #include "surf/surfxml_parse.h"
15 #include "rb_msg_task.c"
16 #include "rb_msg_host.c"
17 #include "rb_msg_process.c"
18 #include "rb_application_handler.c"
19
20 #define MY_DEBUG
21 //Init Msg_Init From Ruby
22 static void msg_init(VALUE Class,VALUE args)
23
24   char **argv=NULL;    
25   const char *tmp;
26   int argc,type,i;
27   VALUE *ptr ;
28   // Testing The Args Type
29   type =  TYPE(args);
30   if (type != T_ARRAY )
31   {
32     rb_raise(rb_eRuntimeError,"Argh!!! Bad Arguments to msg_init");
33     return;
34   }
35   ptr= RARRAY(args)->ptr;
36   argc= RARRAY(args)->len;
37 //  Create C Array to Hold Data_Get_Struct 
38   argc++; 
39   argv = xbt_new0(char *, argc);  
40   argv[0] = strdup("ruby");
41   for (i=0;i<argc-1;i++)
42   {
43    VALUE value = ptr[i];
44    type = TYPE(value);
45 //  if (type == T_STRING)
46    tmp = RSTRING(value)->ptr;
47    argv[i+1] = strdup(tmp); 
48   }
49   // Calling C Msg_Init Method
50   MSG_global_init(&argc,argv);
51   MSG_set_channel_number(10); // Okey !! Okey !! This Must Be Fixed Dynamiclly , But Later ;)
52   SIMIX_context_select_factory("ruby");
53        
54   // Free Stuffs 
55   for (i=0;i<argc;i++)
56    free(argv[i]) ;
57   
58   free (argv);
59   #ifdef MY_DEBUG
60   INFO0("Msg Init...Done");
61   #endif
62   return;
63 }
64
65 //Init Msg_Run From Ruby
66 static void msg_run(VALUE class)
67 {
68   
69  #ifdef MY_DEBUG
70  INFO0("Start Running...");
71  #endif
72  xbt_fifo_item_t item = NULL;
73  m_host_t host = NULL;
74  VALUE rbHost;  
75  // Let's Run
76  //printf("msg_run3\n");
77  if (MSG_OK != MSG_main()){
78    rb_raise(rb_eRuntimeError,"MSG_main() failed");
79  }
80  
81   DEBUG
82     ("MSG_main finished. Bail out before cleanup since there is a bug in this part.");
83      /* Cleanup Ruby hosts */
84    DEBUG("Clean Ruby World");
85    xbt_fifo_foreach(msg_global->host, item, host, m_host_t) {
86      //rbHost = (VALUE)host->data;// ??!!
87       }
88  #ifdef MY_DEBUG
89  INFO0("Start Cleaning...");
90  #endif
91    
92    if (MSG_OK != MSG_clean()){
93      rb_raise(rb_eRuntimeError,"MSG_clean() failed");
94    }
95     return;
96
97
98 //Create Environment
99 static void msg_createEnvironment(VALUE class,VALUE plateformFile)
100 {
101  
102    int type = TYPE(plateformFile);
103    if ( type != T_STRING )
104       rb_raise(rb_eRuntimeError,"Bad Argument's Type");  
105    const char * platform =  RSTRING(plateformFile)->ptr;
106    MSG_create_environment(platform);
107    printf("Create Environment...Done\n");
108
109  return; 
110 }
111  
112 //deploy Application
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     #ifdef MY_DEBUG
140     INFO0("Deploy Application...Done");
141     #endif
142 }
143  
144 // INFO
145 static void msg_info(VALUE class,VALUE msg)
146 {
147  const char *s = RSTRING(msg)->ptr;
148  INFO1("%s",s);
149 }
150
151 // Get Clock
152 static void msg_get_clock(VALUE class)
153 {
154  
155   printf("Simulation time %f\n",MSG_get_clock());
156   
157 }   
158
159 //pajeOutput
160 static void msg_paje_out(VALUE class,VALUE pajeFile)
161 {
162   const char *pfile = RSTRING(pajeFile)->ptr;
163   MSG_paje_output(pfile);
164   
165 }
166
167 // Ruby intropspection : Instanciate a ruby Class From its Name 
168 // Used by ProcessFactory::createProcess
169
170 static VALUE msg_new_ruby_instance(VALUE class,VALUE className)
171 {
172   ruby_init();
173   ruby_init_loadpath();
174   char * p_className = RSTRING(className)->ptr;
175   
176   return rb_funcall3(rb_const_get(rb_cObject, rb_intern(p_className)),rb_intern("new"),0, 0);
177   
178 }
179  
180 //This Time With Args
181 static VALUE msg_new_ruby_instance_with_args(VALUE class,VALUE className,VALUE args)
182 {
183   ruby_init();
184   ruby_init_loadpath();
185   char * p_className = RSTRING(className)->ptr;
186   return rb_funcall(rb_const_get(rb_cObject, rb_intern(p_className)),rb_intern("new"), 1, args); 
187 }
188 /*****************************************************************************************************************
189
190 Wrapping MSG module and its Class ( Task,Host) & Methods ( Process's method...ect)
191 To Ruby 
192
193 the part after "Init_" is the name of the C extension specified in extconf.rb , not the name of C source file
194  
195 *****************************************************************************************************************/
196 void Init_msg()
197 {
198    // Modules
199    rb_msg = rb_define_module("MSG");
200    //Associated Environment Methods!
201    rb_define_method(rb_msg,"init",msg_init,1);
202    rb_define_method(rb_msg,"run",msg_run,0);
203    rb_define_method(rb_msg,"createEnvironment",msg_createEnvironment,1);
204    rb_define_method(rb_msg,"deployApplication",msg_deployApplication,1);
205    rb_define_method(rb_msg,"info",msg_info,1);
206    rb_define_method(rb_msg,"getClock",msg_get_clock,0);
207    rb_define_method(rb_msg,"pajeOutput",msg_paje_out,1);
208    rb_define_method(rb_msg,"rubyNewInstance",msg_new_ruby_instance,1);
209    rb_define_method(rb_msg,"rubyNewInstanceArgs",msg_new_ruby_instance_with_args,2);
210      
211    // Associated Process Methods
212    rb_define_method(rb_msg,"processCreate",processCreate,2);
213    rb_define_method(rb_msg,"processSuspend",processSuspend,1);
214    rb_define_method(rb_msg,"processResume",processResume,1);
215    rb_define_method(rb_msg,"processIsSuspend",processIsSuspend,1);
216    rb_define_method(rb_msg,"processKill",processKill,1);
217    rb_define_method(rb_msg,"processGetHost",processGetHost,1);
218    rb_define_method(rb_msg,"processExit",processExit,1);
219    
220    //Classes       
221    rb_task = rb_define_class_under(rb_msg,"Task",rb_cObject);
222    rb_host = rb_define_class_under(rb_msg,"Host",rb_cObject);
223     
224    //Task Methods    
225    rb_define_module_function(rb_task,"new",task_new,3);
226    rb_define_module_function(rb_task,"compSize",task_comp,1);
227    rb_define_module_function(rb_task,"name",task_name,1);
228    rb_define_module_function(rb_task,"execute",task_execute,1);
229    rb_define_module_function(rb_task,"send",task_send,2); 
230    rb_define_module_function(rb_task,"receive",task_receive,1);
231    rb_define_module_function(rb_task,"receive2",task_receive2,2);
232    rb_define_module_function(rb_task,"sender",task_sender,1);
233    rb_define_module_function(rb_task,"source",task_source,1);
234    rb_define_module_function(rb_task,"listen",task_listen,2);
235    rb_define_module_function(rb_task,"listenFromHost",task_listen_host,3);
236    rb_define_module_function(rb_task,"put",task_put,2);
237    rb_define_module_function(rb_task,"get",task_get,0);
238    
239    //Host Methods  
240    rb_define_module_function(rb_host,"getByName",host_get_by_name,1);
241    rb_define_module_function(rb_host,"name",host_name,1);
242    rb_define_module_function(rb_host,"speed",host_speed,1);
243    rb_define_module_function(rb_host,"number",host_number,0); 
244    rb_define_module_function(rb_host,"setData",host_set_data,2);
245    rb_define_module_function(rb_host,"getData",host_get_data,1);
246    //rb_define_module_function(rb_host,"hasData",host_has_data,1);
247    rb_define_module_function(rb_host,"isAvail",host_is_avail,1);
248    
249 }