4 * Copyright 2010 Martin Quinson, Mehdi Fekari
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.
14 #include "msg/datatypes.h"
15 #include "xbt/sysdep.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"
25 //Init Msg_Init From Ruby
26 static void msg_init(VALUE Class,VALUE args)
32 // Testing The Args Type
36 rb_raise(rb_eRuntimeError,"Argh!!! Bad Arguments to msg_init");
39 ptr= RARRAY(args)->ptr;
40 argc= RARRAY(args)->len;
41 // Create C Array to Hold Data_Get_Struct
43 argv = xbt_new0(char *, argc);
44 argv[0] = strdup("ruby");
45 for (i=0;i<argc-1;i++)
49 // if (type == T_STRING)
50 tmp = RSTRING(value)->ptr;
51 argv[i+1] = strdup(tmp);
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");
64 INFO0("Msg Init...Done");
69 //Init Msg_Run From Ruby
70 static void msg_run(VALUE class)
74 INFO0("Start Running...");
76 xbt_fifo_item_t item = NULL;
80 //printf("msg_run3\n");
81 if (MSG_OK != MSG_main()){
82 rb_raise(rb_eRuntimeError,"MSG_main() failed");
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;// ??!!
93 INFO0("Start Cleaning...");
96 if (MSG_OK != MSG_clean()){
97 rb_raise(rb_eRuntimeError,"MSG_clean() failed");
103 static void msg_createEnvironment(VALUE class,VALUE plateformFile)
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");
117 static void msg_deployApplication(VALUE class,VALUE deploymentFile )
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);
128 surfxml_add_callback(ETag_surfxml_argument_cb_list,
129 application_handler_on_process_arg);
131 surfxml_add_callback(STag_surfxml_prop_cb_list,
132 application_handler_on_property);
134 surfxml_add_callback(ETag_surfxml_process_cb_list,
135 application_handler_on_end_process);
137 surf_parse_open(dep_file);
138 application_handler_on_start_document();
140 rb_raise(rb_eRuntimeError,"surf_parse() failed");
142 application_handler_on_end_document();
144 INFO0("Deploy Application...Done");
149 static void msg_info(VALUE class,VALUE msg)
151 const char *s = RSTRING(msg)->ptr;
156 static void msg_get_clock(VALUE class)
159 printf("Simulation time %f\n",MSG_get_clock());
164 static void msg_paje_out(VALUE class,VALUE pajeFile)
166 const char *pfile = RSTRING(pajeFile)->ptr;
167 MSG_paje_output(pfile);
171 // Ruby intropspection : Instanciate a ruby Class From its Name
172 // Used by ProcessFactory::createProcess
174 static VALUE msg_new_ruby_instance(VALUE class,VALUE className)
177 ruby_init_loadpath();
178 char * p_className = RSTRING(className)->ptr;
180 return rb_funcall3(rb_const_get(rb_cObject, rb_intern(p_className)),rb_intern("new"),0, 0);
184 //This Time With Args
185 static VALUE msg_new_ruby_instance_with_args(VALUE class,VALUE className,VALUE args)
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);
192 /*****************************************************************************************************************
194 Wrapping MSG module and its Class ( Task,Host) & Methods ( Process's method...ect)
197 the part after "Init_" is the name of the C extension specified in extconf.rb , not the name of C source file
199 *****************************************************************************************************************/
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);
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);
225 rb_task = rb_define_class_under(rb_msg,"Task",rb_cObject);
226 rb_host = rb_define_class_under(rb_msg,"Host",rb_cObject);
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);
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);