Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
better synchronization , cleanin' up ruby process ,it works
[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 FIXME: don't mess with MSG internals that way */
10
11 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ruby);
12
13 // Used to instanciate the Process
14 static  VALUE args;
15 static  VALUE prop;
16 static  VALUE function_name;
17 static  VALUE host_name; 
18
19 static VALUE rb_process_instance(VALUE fct_name,VALUE arguments,VALUE properties) {
20   ruby_init();
21   ruby_init_loadpath();
22   char * p_className = RSTRING(fct_name)->ptr; // name of process is the one of the class
23   return rb_funcall(rb_const_get(rb_cObject, rb_intern(p_className)),rb_intern("new"),3,fct_name,arguments,properties);
24 }
25
26 // FIXME: don't mess with MSG internals here, use MSG_process_create_with_arguments()
27 static void rb_process_create_with_args(VALUE fct_name,VALUE arguments,VALUE properties,VALUE ht_name) {
28   
29   VALUE ruby_process = rb_process_instance(fct_name,arguments,properties);
30   m_process_t process; // Native Process to Create
31   const char * name ; // Name of C Native Processs
32
33
34   if(!fct_name)
35     rb_raise(rb_eRuntimeError,"Internal error: Process name cannot be NULL");
36   name = RSTRING(fct_name)->ptr;
37   DEBUG1("Create native process %s",name);
38
39   // Allocate the data for the simulation
40   process = xbt_new0(s_m_process_t,1);
41   process->simdata = xbt_new0(s_simdata_process_t,1);
42   // Bind The Ruby Process instance to The Native Process
43   rb_process_bind(ruby_process,process);
44   process->name = xbt_strdup(name);
45   // Host
46   m_host_t host = MSG_get_host_by_name(RSTRING(ht_name)->ptr);
47   process->simdata->m_host = host;
48   
49   if(!(process->simdata->m_host)) { // Not Binded
50     free(process->simdata);
51     free(process->data);
52     free(process);
53     rb_raise(rb_eRuntimeError,"Host not bound while creating native process");
54   }
55
56   process->simdata->PID = msg_global->PID++; 
57
58   DEBUG7("fill in process %s/%s (pid=%d) %p (sd=%p , host=%p, host->sd=%p)",
59       process->name , process->simdata->m_host->name,process->simdata->PID,
60       process,process->simdata, process->simdata->m_host,
61       process->simdata->m_host->simdata);
62
63   /* FIXME: that's mainly for debugging. We could only allocate this if XBT_LOG_ISENABLED(ruby,debug) is true since I guess this leaks */
64   char **argv=xbt_new(char*,2);
65   argv[0] = bprintf("%s@%s",process->name,process->simdata->m_host->simdata->smx_host->name);
66   argv[1] = NULL;
67   process->simdata->s_process =
68       SIMIX_process_create(process->name,
69           (xbt_main_func_t)ruby_process,
70           (void *) process,
71           process->simdata->m_host->simdata->smx_host->name,
72           1,argv,NULL);
73
74   DEBUG1("context created (s_process=%p)",process->simdata->s_process);
75
76   if (SIMIX_process_self()) { // SomeOne Created Me !!
77     process->simdata->PPID = MSG_process_get_PID(SIMIX_process_self()->data);
78   } else {
79     process->simdata->PPID = -1;
80   }
81   process->simdata->last_errno = MSG_OK;
82   // let's Add the Process to the list of the Simulation's Processes
83   xbt_fifo_unshift(msg_global->process_list,process);
84 }
85
86
87 void rb_application_handler_on_start_document(void) {
88   
89
90    args = rb_ary_new();  // Max lenght = 16 !!
91    prop = rb_ary_new();
92
93 }
94
95 void rb_application_handler_on_end_document(void) {
96
97   args = Qnil;
98   prop = Qnil;
99   function_name = Qnil;
100   host_name = Qnil;  
101 }
102
103 void rb_application_handler_on_begin_process(void) {
104   
105   host_name = rb_str_new2(A_surfxml_process_host);
106   function_name = rb_str_new2(A_surfxml_process_function);
107
108   args = rb_ary_new();  // Max lenght = 16 ?!
109   prop = rb_ary_new();
110
111 }
112
113 void rb_application_handler_on_process_arg(void) {
114   
115   VALUE arg = rb_str_new2(A_surfxml_argument_value);
116   rb_ary_push(args,arg);
117 }
118
119 void rb_application_handler_on_property(void) {
120
121   VALUE id = rb_str_new2(A_surfxml_prop_id);
122   VALUE val =  rb_str_new2(A_surfxml_prop_value);
123   int i_id = NUM2INT (id);
124   rb_ary_store(prop,i_id,val);
125
126 }
127
128 void rb_application_handler_on_end_process(void) {
129
130   rb_process_create_with_args(function_name,args,prop,host_name);
131     
132 }