Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
The surf_file_to_parse have to be set to NULL after closing file
[simgrid.git] / src / bindings / ruby / rb_application_handler.c
1 /* Copyright (c) 2010. The SimGrid Team.
2  * All rights 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,
20                                  VALUE properties)
21 {
22   ruby_init();
23   ruby_init_loadpath();
24   char *p_className = RSTRING(fct_name)->ptr;   // name of process is the one of the class
25   return rb_funcall(rb_const_get(rb_cObject, rb_intern(p_className)),
26                     rb_intern("new"), 3, fct_name, arguments, properties);
27 }
28
29 // FIXME: don't mess with MSG internals here, use MSG_process_create_with_arguments()
30 static void rb_process_create_with_args(VALUE fct_name, VALUE arguments,
31                                         VALUE properties, VALUE ht_name)
32 {
33
34   VALUE ruby_process =
35       rb_process_instance(fct_name, arguments, properties);
36   m_process_t process;          // Native Process to Create
37   const char *name;             // Name of C Native Processs
38
39
40   if (!fct_name)
41     rb_raise(rb_eRuntimeError,
42              "Internal error: Process name cannot be NULL");
43   name = RSTRING(fct_name)->ptr;
44   DEBUG1("Create native process %s", name);
45
46   // Allocate the data for the simulation
47   process = xbt_new0(s_m_process_t, 1);
48   process->simdata = xbt_new0(s_simdata_process_t, 1);
49   // Bind The Ruby Process instance to The Native Process
50   rb_process_bind(ruby_process, process);
51   process->name = xbt_strdup(name);
52   // Host
53   m_host_t host = MSG_get_host_by_name(RSTRING(ht_name)->ptr);
54   process->simdata->m_host = host;
55
56   if (!(process->simdata->m_host)) {    // Not Binded
57     free(process->simdata);
58     free(process->data);
59     free(process);
60     rb_raise(rb_eRuntimeError,
61              "Host not bound while creating native process");
62   }
63
64   process->simdata->PID = msg_global->PID++;
65
66   DEBUG7
67       ("fill in process %s/%s (pid=%d) %p (sd=%p , host=%p, host->sd=%p)",
68        process->name, process->simdata->m_host->name,
69        process->simdata->PID, process, process->simdata,
70        process->simdata->m_host, 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] =
75       bprintf("%s@%s", process->name,
76               process->simdata->m_host->simdata->smx_host->name);
77   argv[1] = NULL;
78   process->simdata->s_process =
79       SIMIX_process_create(process->name,
80                            (xbt_main_func_t) ruby_process,
81                            (void *) process,
82                            process->simdata->m_host->simdata->
83                            smx_host->name, 1, argv, NULL);
84
85   DEBUG1("context created (s_process=%p)", process->simdata->s_process);
86
87   if (SIMIX_process_self()) {   // SomeOne Created Me !!
88     process->simdata->PPID =
89         MSG_process_get_PID(SIMIX_process_self()->data);
90   } else {
91     process->simdata->PPID = -1;
92   }
93   process->simdata->last_errno = MSG_OK;
94   // let's Add the Process to the list of the Simulation's Processes
95   xbt_fifo_unshift(msg_global->process_list, process);
96 }
97
98
99 void rb_application_handler_on_start_document(void)
100 {
101
102
103   args = rb_ary_new();          // Max length = 16 !!
104   prop = rb_ary_new();
105
106 }
107
108 void rb_application_handler_on_end_document(void)
109 {
110
111   args = Qnil;
112   prop = Qnil;
113   function_name = Qnil;
114   host_name = Qnil;
115 }
116
117 void rb_application_handler_on_begin_process(void)
118 {
119
120   host_name = rb_str_new2(A_surfxml_process_host);
121   function_name = rb_str_new2(A_surfxml_process_function);
122
123   args = rb_ary_new();          // Max length = 16 ?!
124   prop = rb_ary_new();
125
126 }
127
128 void rb_application_handler_on_process_arg(void)
129 {
130
131   VALUE arg = rb_str_new2(A_surfxml_argument_value);
132   rb_ary_push(args, arg);
133 }
134
135 void rb_application_handler_on_property(void)
136 {
137
138   VALUE id = rb_str_new2(A_surfxml_prop_id);
139   VALUE val = rb_str_new2(A_surfxml_prop_value);
140   int i_id = NUM2INT(id);
141   rb_ary_store(prop, i_id, val);
142
143 }
144
145 void rb_application_handler_on_end_process(void)
146 {
147
148   rb_process_create_with_args(function_name, args, prop, host_name);
149
150 }