Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
redefinition of task and host ruby classes to adopte an object-oriented approach
[simgrid.git] / src / bindings / ruby / simgrid_ruby.c
1 /* SimGrid Ruby bindings                                                    */
2
3 /* Copyright (c) 2010, the SimGrid team. All right reserved.                */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "xbt.h"
9 #include "bindings/ruby_bindings.h"
10
11 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ruby);
12
13 // MSG Module
14 VALUE rb_msg;
15 // MSG Classes
16 VALUE rb_task;
17 VALUE rb_host;
18
19 //Init Msg  From Ruby
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     rb_raise(rb_eRuntimeError,"Bad arguments to msg_init (expecting an array)");
30     return;
31   }
32   ptr= RARRAY(args)->ptr;
33   argc= RARRAY(args)->len;
34   //  Create C Array to Hold Data_Get_Struct
35   argc++;
36   argv = xbt_new0(char *, argc);
37   argv[0] = strdup("ruby");
38   for (i=0;i<argc-1;i++) {
39     VALUE value = ptr[i];
40     type = TYPE(value);
41     //  if (type == T_STRING)
42     tmp = RSTRING(value)->ptr;
43     argv[i+1] = strdup(tmp);
44   }
45   // Calling C Msg_Init Method
46   MSG_global_init(&argc,argv);
47
48   // Cleanups
49   for (i=0;i<argc;i++)
50     free(argv[i]) ;
51   free (argv);
52 }
53 //Init Msg_Run From Ruby
54 static void msg_run(VALUE class) {
55   DEBUG0("Start Running...");
56   m_host_t *hosts;
57   int cpt,host_count;
58   VALUE rbHost;
59   // Let's Run
60   //printf("msg_run3\n");
61   if (MSG_OK != MSG_main()){
62     rb_raise(rb_eRuntimeError,"MSG_main() failed");
63   }
64
65   DEBUG0
66   ("MSG_main finished. Bail out before cleanup since there is a bug in this part.");
67   /* Cleanup Ruby hosts */
68   DEBUG0("Clean Ruby World");
69   hosts = MSG_get_host_table();
70   host_count = MSG_get_host_number();
71   for (cpt=0;cpt<host_count;cpt++) {
72     rbHost = (VALUE)((hosts[cpt])->data);// ??!!
73   }
74
75   if (MSG_OK != MSG_clean()){
76     rb_raise(rb_eRuntimeError,"MSG_clean() failed");
77   }
78   return;
79 }
80
81 static void msg_createEnvironment(VALUE class,VALUE plateformFile) {
82
83   int type = TYPE(plateformFile);
84   if ( type != T_STRING )
85     rb_raise(rb_eRuntimeError,"Bad Argument's Type");
86   const char * platform =  RSTRING(plateformFile)->ptr;
87   MSG_create_environment(platform);
88   DEBUG1("Create Environment (%s)...Done",platform);
89 }
90
91 //deploy Application
92 static void msg_deployApplication(VALUE class,VALUE deploymentFile ) {
93
94   int type = TYPE(deploymentFile);
95   if ( type != T_STRING )
96     rb_raise(rb_eRuntimeError,"Bad Argument's Type for deployApplication ");
97   const char *dep_file = RSTRING(deploymentFile)->ptr;
98   surf_parse_reset_parser();
99   surfxml_add_callback(STag_surfxml_process_cb_list,
100       rb_application_handler_on_begin_process);
101   surfxml_add_callback(ETag_surfxml_argument_cb_list,
102       rb_application_handler_on_process_arg);
103
104   surfxml_add_callback(STag_surfxml_prop_cb_list,
105       rb_application_handler_on_property);
106
107   surfxml_add_callback(ETag_surfxml_process_cb_list,
108       rb_application_handler_on_end_process);
109
110   surf_parse_open(dep_file);
111   rb_application_handler_on_start_document();
112   if(surf_parse())
113     rb_raise(rb_eRuntimeError,"surf_parse() failed");
114   surf_parse_close();
115   
116   rb_application_handler_on_end_document();
117
118   DEBUG1("Deploy Application(%s)...Done",dep_file);
119 }
120
121 // INFO
122 static void msg_info(VALUE class,VALUE msg) {
123   const char *s = RSTRING(msg)->ptr;
124   INFO1("%s",s);
125 }
126 static void msg_debug(VALUE class,VALUE msg) {
127   const char *s = RSTRING(msg)->ptr;
128   DEBUG1("%s",s);
129 }
130
131 // Get Clock FIXME: return the double instead of float
132 static VALUE msg_get_clock(VALUE class) {
133
134   return rb_float_new(MSG_get_clock());
135
136 }
137
138 // Ruby intropspection : Instanciate a ruby Class From its Name
139 // Used by ProcessFactory::createProcess
140
141 static VALUE msg_new_ruby_instance(VALUE class,VALUE className) {
142   ruby_init();
143   ruby_init_loadpath();
144   char * p_className = RSTRING(className)->ptr;
145
146   return rb_funcall3(rb_const_get(rb_cObject, rb_intern(p_className)),rb_intern("new"),0, 0);
147 }
148
149 //This Time With Args
150 static VALUE msg_new_ruby_instance_with_args(VALUE class,VALUE className,VALUE args) {
151   ruby_init();
152   ruby_init_loadpath();
153   char * p_className = RSTRING(className)->ptr;
154   return rb_funcall(rb_const_get(rb_cObject, rb_intern(p_className)),rb_intern("new"), 1, args);
155 }
156
157
158 extern const char*xbt_ctx_factory_to_use; /*Hack: let msg load directly the right factory */
159
160 typedef VALUE(*rb_meth)(ANYARGS);
161 void Init_simgrid_ruby() {
162   xbt_ctx_factory_to_use = "ruby";
163
164   // Modules
165   rb_msg = rb_define_module("MSG");
166   //Associated Environment Methods!
167   rb_define_module_function(rb_msg,"init",(rb_meth)msg_init,1);
168   rb_define_module_function(rb_msg,"run",(rb_meth)msg_run,0);
169   rb_define_module_function(rb_msg,"createEnvironment",(rb_meth)msg_createEnvironment,1);
170   rb_define_module_function(rb_msg,"deployApplication",(rb_meth)msg_deployApplication,1);
171   rb_define_module_function(rb_msg,"info",(rb_meth)msg_info,1);
172   rb_define_module_function(rb_msg,"debug",(rb_meth)msg_debug,1);
173   rb_define_module_function(rb_msg,"getClock",(rb_meth)msg_get_clock,0);
174   rb_define_module_function(rb_msg,"rubyNewInstance",(rb_meth)msg_new_ruby_instance,1);
175   rb_define_module_function(rb_msg,"rubyNewInstanceArgs",(rb_meth)msg_new_ruby_instance_with_args,2);
176
177   // Associated Process Methods
178   rb_define_method(rb_msg,"processSuspend",(rb_meth)rb_process_suspend,1);
179   rb_define_method(rb_msg,"processResume",(rb_meth)rb_process_resume,1);
180   rb_define_method(rb_msg,"processIsSuspend",(rb_meth)rb_process_isSuspended,1);
181   rb_define_method(rb_msg,"processKill",(rb_meth)rb_process_kill_up,1);
182   rb_define_method(rb_msg,"processGetHost",(rb_meth)rb_process_getHost,1);
183   rb_define_method(rb_msg,"processExit",(rb_meth)rb_process_exit,1);
184
185   //Classes
186   rb_task = rb_define_class_under(rb_msg,"RbTask",rb_cObject);
187   rb_host = rb_define_class_under(rb_msg,"RbHost",rb_cObject);
188
189   //Task Methods FIXME: Convert to methods
190   rb_define_module_function(rb_task,"new",(rb_meth)rb_task_new,3);
191   rb_define_module_function(rb_task,"compSize",(rb_meth)rb_task_comp,1);
192   rb_define_module_function(rb_task,"name",(rb_meth)rb_task_name,1);
193   rb_define_module_function(rb_task,"execute",(rb_meth)rb_task_execute,1);
194   rb_define_module_function(rb_task,"send",(rb_meth)rb_task_send,2);
195   rb_define_module_function(rb_task,"receive",(rb_meth)rb_task_receive,1);
196   rb_define_module_function(rb_task,"sender",(rb_meth)rb_task_sender,1);
197   rb_define_module_function(rb_task,"source",(rb_meth)rb_task_source,1);
198   rb_define_module_function(rb_task,"listen",(rb_meth)rb_task_listen,2);
199   rb_define_module_function(rb_task,"listenFromHost",(rb_meth)rb_task_listen_host,3);
200
201   //Host Methods
202   rb_define_module_function(rb_host,"getByName",(rb_meth)rb_host_get_by_name,1);
203   rb_define_module_function(rb_host,"name",(rb_meth)rb_host_name,1);
204   rb_define_module_function(rb_host,"speed",(rb_meth)rb_host_speed,1);
205   rb_define_module_function(rb_host,"number",(rb_meth)rb_host_number,0);
206   rb_define_module_function(rb_host,"setData",(rb_meth)rb_host_set_data,2);
207   rb_define_module_function(rb_host,"getData",(rb_meth)rb_host_get_data,1);
208   rb_define_module_function(rb_host,"isAvail",(rb_meth)rb_host_is_avail,1);
209 }