Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
6a86db2392d898c62e04ca9822c11f51171157bf
[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
10 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ruby);
11
12 // Used to instanciate the Process 
13 static VALUE args;
14 static VALUE prop;
15 static VALUE function_name;
16 static VALUE host_name;
17
18 static VALUE rb_process_instance(VALUE fct_name, VALUE arguments,
19                                  VALUE properties)
20 {
21   ruby_init();
22   ruby_init_loadpath();
23   char *p_className = RSTRING(fct_name)->ptr;   // name of process is the one of the class
24   return rb_funcall(rb_const_get(rb_cObject, rb_intern(p_className)),
25                     rb_intern("new"), 3, fct_name, arguments, properties);
26 }
27
28 // FIXME: don't mess with MSG internals here, use MSG_process_create_with_arguments()
29 static void rb_process_create_with_args(VALUE fct_name, VALUE arguments,
30                                         VALUE properties, VALUE ht_name)
31 {
32
33   VALUE ruby_process =
34       rb_process_instance(fct_name, arguments, properties);
35   m_process_t process = NULL;          // Native Process to Create
36   const char *name;             // Name of C Native Processs
37
38
39   if (!fct_name)
40     rb_raise(rb_eRuntimeError,
41              "Internal error: Process name cannot be NULL");
42   name = RSTRING(fct_name)->ptr;
43   DEBUG1("Create native process %s", name);
44
45   char **argv = xbt_new(char *, 2);
46   argv[0] = bprintf("%s@%s", name, RSTRING(ht_name)->ptr);
47   argv[1] = NULL;
48
49   // Allocate the data for the simulation
50   process = MSG_process_create_with_arguments(name,
51       (xbt_main_func_t) ruby_process,
52       process,
53       MSG_get_host_by_name(RSTRING(ht_name)->ptr),
54       1, argv);
55
56   // Bind The Ruby Process instance to The Native Process
57   rb_process_bind(ruby_process, process);
58 }
59
60
61 void rb_application_handler_on_start_document(void)
62 {
63
64
65   args = rb_ary_new();          // Max length = 16 !!
66   prop = rb_ary_new();
67
68 }
69
70 void rb_application_handler_on_end_document(void)
71 {
72
73   args = Qnil;
74   prop = Qnil;
75   function_name = Qnil;
76   host_name = Qnil;
77 }
78
79 void rb_application_handler_on_begin_process(void)
80 {
81
82   host_name = rb_str_new2(A_surfxml_process_host);
83   function_name = rb_str_new2(A_surfxml_process_function);
84
85   args = rb_ary_new();          // Max length = 16 ?!
86   prop = rb_ary_new();
87
88 }
89
90 void rb_application_handler_on_process_arg(void)
91 {
92
93   VALUE arg = rb_str_new2(A_surfxml_argument_value);
94   rb_ary_push(args, arg);
95 }
96
97 void rb_application_handler_on_property(void)
98 {
99
100   VALUE id = rb_str_new2(A_surfxml_prop_id);
101   VALUE val = rb_str_new2(A_surfxml_prop_value);
102   int i_id = NUM2INT(id);
103   rb_ary_store(prop, i_id, val);
104
105 }
106
107 void rb_application_handler_on_end_process(void)
108 {
109
110   rb_process_create_with_args(function_name, args, prop, host_name);
111
112 }