Logo AND Algorithmique Numérique Distribuée

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