Logo AND Algorithmique Numérique Distribuée

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