Logo AND Algorithmique Numérique Distribuée

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