2 * Copyright 2010. The SimGrid Team. All right reserved.
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.
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 */
11 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ruby);
13 // Used to instanciate the Process
16 static VALUE function_name;
17 static VALUE host_name;
20 static VALUE rb_process_instance(VALUE fct_name,VALUE arguments,VALUE properties) {
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)),rb_intern("new"),3,fct_name,arguments,properties);
27 // FIXME: don't mess with MSG internals here, use MSG_process_create_with_arguments()
28 static void rb_process_create_with_args(VALUE fct_name,VALUE arguments,VALUE properties,VALUE ht_name) {
30 VALUE ruby_process = rb_process_instance(fct_name,arguments,properties);
31 m_process_t process; // Native Process to Create
32 const char * name ; // Name of C Native Processs
35 rb_raise(rb_eRuntimeError,"Internal error: Process name cannot be NULL");
37 // Allocate the data for the simulation
38 process = xbt_new0(s_m_process_t,1);
39 process->simdata = xbt_new0(s_simdata_process_t,1);
40 // Do we Really Need to Create Ruby Process Instance , >> process is already a Ruby Process !! So..Keep on ;)
41 // Bind The Ruby Process instance to The Native Process
42 rb_process_bind(ruby_process,process);
43 name = RSTRING(fct_name)->ptr;
44 process->name = xbt_strdup(name);
46 m_host_t host = MSG_get_host_by_name(RSTRING(ht_name)->ptr);
47 process->simdata->m_host = host;
49 //Data_Get_Struct(host,s_m_host_t,process->simdata->m_host);
51 if(!(process->simdata->m_host)) { // Not Binded
52 free(process->simdata);
55 rb_raise(rb_eRuntimeError,"Host not bound while creating native process");
57 process->simdata->PID = msg_global->PID++; // msg_global ??
59 DEBUG7("fill in process %s/%s (pid=%d) %p (sd=%p , host=%p, host->sd=%p)",
60 process->name , process->simdata->m_host->name,process->simdata->PID,
61 process,process->simdata, process->simdata->m_host,
62 process->simdata->m_host->simdata);
65 /* FIXME: that's mainly for debugging. We could only allocate this if XBT_LOG_ISENABLED(ruby,debug) is true since I guess this leaks */
66 char **argv=xbt_new(char*,2);
67 argv[0] = bprintf("%s@%s",process->name,process->simdata->m_host->simdata->smx_host->name);
69 process->simdata->s_process =
70 SIMIX_process_create(process->name,
71 (xbt_main_func_t)ruby_process,
73 process->simdata->m_host->simdata->smx_host->name,
76 DEBUG1("context created (s_process=%p)",process->simdata->s_process);
78 if (SIMIX_process_self()) { // SomeOne Created Me !!
79 process->simdata->PPID = MSG_process_get_PID(SIMIX_process_self()->data);
81 process->simdata->PPID = -1;
83 process->simdata->last_errno = MSG_OK;
84 // let's Add the Process to the list of the Simulation's Processes
85 xbt_fifo_unshift(msg_global->process_list,process);
91 void rb_application_handler_on_start_document(void) {
93 args = rb_ary_new(); // Max lenght = 16 ?!
97 void rb_application_handler_on_end_document(void) {
98 // FIXME: probably leaking
99 //application_handler_class = Qnil;
102 function_name = Qnil;
107 void rb_application_handler_on_begin_process(void) {
109 host_name = rb_str_new2(A_surfxml_process_host);
110 function_name = rb_str_new2(A_surfxml_process_function);
114 void rb_application_handler_on_process_arg(void) {
116 VALUE arg = rb_str_new2(A_surfxml_argument_value);
117 rb_ary_push(args,arg);
120 void rb_application_handler_on_property(void) {
121 // FIXME: properties are never added to the ruby process
122 VALUE id = rb_str_new2(A_surfxml_prop_id);
123 VALUE val = rb_str_new2(A_surfxml_prop_value);
124 int i_id = NUM2INT (id);
125 rb_ary_store(prop,i_id,val);
130 void rb_application_handler_on_end_process(void) {
132 rb_process_create_with_args(function_name,args,prop,host_name);