Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@7180 48e7efb5...
[simgrid.git] / src / bindings / ruby / rb_application_handler.c
1 /*
2  * Copyright 2010. The SimGrid Team. All right reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package.
6  */
7 #include "bindings/ruby_bindings.h"
8 #include "surf/surfxml_parse.h"
9 #include "msg/private.h" /* s_simdata_process_t */
10
11 // XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ruby,bindings,"Ruby Bindings");
12
13 // Used to instanciate the Process
14
15 static  VALUE args;
16 static  VALUE prop;
17 static  VALUE function_name;
18 static  VALUE host_name; 
19
20
21
22 static VALUE rb_process_instance(VALUE fct_name,VALUE arguments,VALUE properties) {
23   ruby_init();
24   ruby_init_loadpath();
25   char * p_className = RSTRING(fct_name)->ptr; // name of process is the one of the class
26   return rb_funcall(rb_const_get(rb_cObject, rb_intern(p_className)),rb_intern("new"),3,fct_name,arguments,properties);
27 }
28
29 static void rb_process_create_with_args(VALUE fct_name,VALUE arguments,VALUE properties,VALUE ht_name) {
30   
31   VALUE ruby_process = rb_process_instance(fct_name,arguments,properties);
32   m_process_t process; // Native Process to Create
33   const char * name ; // Name of C Native Processs
34
35   if(!fct_name)
36     rb_raise(rb_eRuntimeError,"Internal error: Process name cannot be NULL");
37
38   // Allocate the data for the simulation
39   process = xbt_new0(s_m_process_t,1);
40   process->simdata = xbt_new0(s_simdata_process_t,1);
41   // Do we Really Need to Create Ruby Process Instance , >> process is already a Ruby Process !! So..Keep on ;)
42   // Bind The Ruby Process instance to The Native Process
43   rb_process_bind(ruby_process,process);
44   name = RSTRING(fct_name)->ptr;
45   process->name = xbt_strdup(name);
46   // Host
47   m_host_t host = MSG_get_host_by_name(RSTRING(ht_name)->ptr);
48   process->simdata->m_host = host;
49   
50   //Data_Get_Struct(host,s_m_host_t,process->simdata->m_host);
51
52   if(!(process->simdata->m_host)) { // Not Binded
53     free(process->simdata);
54     free(process->data);
55     free(process);
56     rb_raise(rb_eRuntimeError,"Host not bound while creating native process");
57   }
58   process->simdata->PID = msg_global->PID++; //  msg_global ??
59
60 /*
61   DEBUG7("fill in process %s/%s (pid=%d) %p (sd=%p , host=%p, host->sd=%p)",
62       process->name , process->simdata->m_host->name,process->simdata->PID,
63       process,process->simdata, process->simdata->m_host,
64       process->simdata->m_host->simdata);
65 */
66
67 printf("fill in process %s/%s (pid=%d) %p (sd=%p , host=%p, host->sd=%p)\n",
68       process->name , process->simdata->m_host->name,process->simdata->PID,
69       process,process->simdata, process->simdata->m_host,
70       process->simdata->m_host->simdata);
71
72   /* FIXME: that's mainly for debugging. We could only allocate this if XBT_LOG_ISENABLED(ruby,debug) is true since I guess this leaks */
73   char **argv=xbt_new(char*,2);
74   argv[0] = bprintf("%s@%s",process->name,process->simdata->m_host->simdata->smx_host->name);
75   argv[1] = NULL;
76   process->simdata->s_process =
77       SIMIX_process_create(process->name,
78           (xbt_main_func_t)ruby_process,
79           (void *) process,
80           process->simdata->m_host->simdata->smx_host->name,
81           1,argv,NULL);
82
83  // DEBUG1("context created (s_process=%p)",process->simdata->s_process);
84  printf("context created (s_process=%p)\n",process->simdata->s_process);
85
86   if (SIMIX_process_self()) { // SomeOne Created Me !!
87     process->simdata->PPID = MSG_process_get_PID(SIMIX_process_self()->data);
88   } else {
89     process->simdata->PPID = -1;
90   }
91   process->simdata->last_errno = MSG_OK;
92   // let's Add the Process to the list of the Simulation's Processes
93   xbt_fifo_unshift(msg_global->process_list,process);
94 }
95
96
97
98
99 void rb_application_handler_on_start_document(void) {
100   
101    args = rb_ary_new();  // Max lenght = 16 ?!
102    prop = rb_ary_new();
103 }
104
105 void rb_application_handler_on_end_document(void) {
106   //application_handler_class = Qnil;
107   args = Qnil;
108   prop = Qnil;
109   function_name = Qnil;
110   host_name = Qnil;
111   
112 }
113
114 void rb_application_handler_on_begin_process(void) {
115   
116   host_name = rb_str_new2(A_surfxml_process_host);;
117   function_name = rb_str_new2(A_surfxml_process_function);
118   
119 }
120
121 void rb_application_handler_on_process_arg(void) {
122   
123   VALUE arg = rb_str_new2(A_surfxml_argument_value);
124   rb_ary_push(args,arg);
125 }
126
127 void rb_application_handler_on_property(void) {
128   VALUE id = rb_str_new2(A_surfxml_prop_id);
129   VALUE val =  rb_str_new2(A_surfxml_prop_value);
130   int i_id = NUM2INT (id);
131   rb_ary_store(prop,i_id,val);
132
133 }
134
135
136 void rb_application_handler_on_end_process(void) {
137
138   rb_process_create_with_args(function_name,args,prop,host_name);
139     
140 }
141
142