require 'ProcessFactory'
-$debug = true
+$DEBUG = true
class ApplicationHandler
# Initialize
def initialize()
-
+ #Nothing todo
end
-
-# onStartDocument
+ # onStartDocument
def onStartDocument()
@processFactory = ProcessFactory.new
-
- if ($debug)
+ if ($DEBUG)
puts "onStartDocument"
end
-
end
# onBeginProcess
- def onBeginProcess (hostName ,function)
+ def onBeginProcess(hostName,function)
@processFactory.setProcessIdentity(hostName,function)
- if ($debug)
+ if ($DEBUG)
puts "onBeginProcess"
end
end
# onProperty
-
- def onProperty(id,value)
+ def onProperty(id,value)
@processFactory.setProperty(id,value)
- if ($debug)
+ if ($DEBUG)
puts "onProperty"
end
end
# RegisterProcessArg
-
- def onProcessArg(arg)
+ def onProcessArg(arg)
@processFactory.registerProcessArg(arg)
- if ($debug)
+ if ($DEBUG)
puts "onProcessArg"
end
end
# OnEndProcess
-
- def onEndProcess()
+ def onEndProcess()
@processFactory.createProcess()
- if ($debug)
+ if ($DEBUG)
puts "onEndProcess"
end
end
-
# onEndDocument
-
- def onEndDocument()
+ def onEndDocument()
# Euh...Actually Nothin' to Do !!
- if($debug)
+ if($DEBUG)
puts "onEndDocument"
end
end
-
-# End Class
-
-end
+ # End Class
+ end
class Master < RbProcess
-
-
-
- def initialize(*args)
-
+ def initialize()
super()
- channel = 0
- puts "Hey ..from Master"#info("Hey")
+ p RbHost.number()
+
- slaves = Array.new()
-
+ end
+
+
+ def run(args) # args is an array Containin' arguments for function master
+ puts "Hey From Ruby...I'm The Master"
size = args.size
-
- puts "Args = " + size
+ puts "Number of Args for Master = " + size.to_s
for i in 0..size-1
- puts "argv :" + args[1]
+ puts args[i]
end
- raise "Master needs 3 arguments" if size < 3
-
- numberOfTask = args[0] #convert to int
- taskComputeSize = args[1] #convert to double
- taskCommunicationSize = args[2] #convert to double
- slaveCount = args[3] #convert to int
-
-
-# todo = Array.new(numberOfTask)
+ raise "Master needs 3 arguments" if size < 3
+ numberOfTask = Integer(args[0])
+ taskComputeSize = Float(args[1])
+ taskCommunicationSize = Float(args[2])
+ slaveCount = Integer(args[3])
#Creating & Sending Task
- for i in 0..numberOfTask
+ for i in 0..numberOfTask
task = RbTask.new("Task_" + i.to_s, taskComputeSize , taskCommunicationSize );
s_alias = "slave " + (i%slaveCount).to_s
- puts "Master Sending "+ RbTask.name(task) + " to " + s_alias
+ puts "Master Sending "+ RbTask.name(task) + " to " + s_alias + " with Comput Size " + RbTask.compSize(task).to_s
RbTask.send(task,s_alias)
puts "Master Done Sending " +RbTask.name(task) + " to " + s_alias
-
end
# Sending Finalize Tasks
puts "Master Sending Finalize to " + s_alias
RbTask.send(RbTask.new("finalize",0,0),s_alias)
end
-
puts "Master : Everything's Done"
-
- end
-
+ end
end
-
-
require 'msg'
require 'RubyProcess'
-
require 'Master'
require 'Slave'
-Msg.init()
-Msg.createEnvironment("../../../examples/msg/msg_platform.xml")
-Msg.deployApplication("MasterSlave_deploy.xml")
-Msg.run()
-Msg.exit()
+include MSG
+
+# raise "Bad Number Of Arguments" if ARGV.length != 2
+
+# info("Bye")
+
+init(ARGV)
+raise "Bad Number Of Arguments " if (ARGV.length < 2)
+# p Host.number()
+createEnvironment(ARGV[0])
+# p Host.number()
+deployApplication(ARGV[1])
+# p Host.number()
+
+
+# run()
+# exit()
# Attributes
attr_accessor :args, :proprieties, :hostName, :function
-
-
# Initlialize
def initialize()
# setProcessIdentity
- def setProcessIdentity( hostName , function)
+ def setProcessIdentity(hostName,function)
@hostName = hostName
@function = function
proprieties.clear
end
-
end
# RegisterProcess
end
# CreateProcess
-
def createProcess()
-
-
- process = RbProcess.new()
-
-
+ process = rubyNewInstance(@function)
+# process = rubyNewInstanceArgs(@function,@args)
process.name = @function
-
-
- process.id = process.nextId() # This increment Automaticly The Static ProcessNextId for The Class RbProcess
-
-
-
-
- host = rbHost.getByName(@hostName)
-
- Process.createProcess(process,host)
-
+ process.id = process.nextId() # This increment Automaticly The Static ProcessNextId for The Class RbProces
+# host = RbHost.getByName(@hostName)
+# Process.createProcess(process,host)
size = @args.size
-
for i in 0..size-1
-
process.pargs.push(@args[i])
-
end
-
process.proprieties = @proprieties
-
@proprieties = Hash.new
end
-
# SetProperty
-
- def setProperty( id , value )
+ def setProperty(id,value)
@proprieties[id] = value
require 'Semaphore'
include MSG
-$debug = true # This is a Global Variable Useful for Debugging
+$DEBUG = true # This is a Global Variable Useful for Debugging
-class RbProcess < Thread
-
-
-
-
+class RbProcess < Thread
@@nextProcessId = 0
# Attributes
attr_accessor :bind, :id, :proprieties, :name,
:pargs, :schedBegin, :schedEnd
-
-
-# Initialize
-
-
- # Used in ApplicationHandler to Initialize it
-
-
-
+# Initialize : USED in ApplicationHandler to Initialize it
def initialize(*args)
-
argc = args.size
-
-
-
-# Default Init
+# Default Init***************************** No Args
if argc == 0 #>>> new()
super() {
-
@id = 0
@bind = 0
@name = ""
@pargs = Array.new()
-
init_var()
-
-
-
- if $debug
- puts "Init Default Initialzer..."
- end
-
-# Thread.pass #yield ?!!
+ if $DEBUG
+ puts "Init Default Initialzer..."
+ end
+# Thread.pass
# sleep # Sleep Forever ... To Keep Thread Alive ?!!
}
end
- # Initi with 2 arguments
+ # Init with 1 Argument ***********************>>Name( Not Used )
+ if argc == 1
+ super(){
+ @name = args[0]
+ @pargs = Array.new()
+ init_var()
+ if $DEBUG
+ puts "Init with Name..."
+ end
+ }
+ end
- if argc == 2 # >>>(HostName,Name) Or (Host , Name)
+
+
+ # Init with 2 arguments **********************>>>(HostName,Name) Or (Host , Name)
+
+ if argc == 2
super(){
-
-
type = args[0].type()
-
if ( type.to_s == "String")
host = Host.getByName(args[0])
end
-
if ( type.to_s == "MSG::Host")
host = args[0]
end
-
-
- if $debug
+ if $DEBUG
puts host
end
-
-
raise "Process Name Cannot Be Null" if args[1].empty?
-
@name = args[1] # First Arg
-
- if $debug
+ if $DEBUG
puts @name
end
-
-
@pargs = Array.new() # No Args[] Passed in Arguments
-
- @@nextProcessId += 1
- @id = @@nextProcessId
-
- init_var()
-
- createProcess(self,host) #TODO >> MSG::Process
- if $debug
+ @@nextProcessId += 1
+ @id = @@nextProcessId
+ init_var()
+ createProcess(self,host)
+ if $DEBUG
puts "Initilize with 2 args"
end
-# sleep # Keep The Thread Runin'
+# sleep
}
end
+
+
-# Init with 3 arguments
+ # Init with 3 arguments **********************(hostName,Name,args[]) or # (Host,Name,args[])
- if argc == 3 #(hostName,Name,args[]) or # (Host,Name,args[])
+ if argc == 3
super(){
-
type = args[0].type()
-
if( type.to_s == "String")
-# host = Host.getByName(args[0])
- host ="Host.getByName(args[0])"
+ host =Host.getByName(args[0])
end
-
if ( type.to_s == "MSG::Host" )
host = args[0]
end
-
- if $debug
+ if $DEBUG
puts host
end
-
raise "Process Name Cannot Be Null" if args[0].empty?
-
@name = args[1]
-
type = args[2].type()
-
raise "Third Argument Should be an Array" if type != "Array"
-
@pargs = args[3]
-
-
@@nextProcessId +=1
@id = @@nextProcessId
-
init_var()
-
- createProcess(self,host) #TODO RubyMsg
+ createProcess(self,host)
- if $debug
+ if $DEBUG
puts "Initilize with 3 args"
end
def init_var()
-
@proprieties = Hash.new()
# Process Synchronization Tools
@schedBegin = Semaphore.new(0)
return @@nextProcessId
end
-
-
-
-
-
-
- if $debug
+
+ if $DEBUG
#Process List
def processList()
end
-
- #get Own Name
+ #get Own Name
def getName()
end
-
-
- # Stop
+ # Stop
def unschedule()
end
-
- #C Simualateur Process Equivalent Management
+ #C Simualateur Process Equivalent Management
# After Binding Ruby Process to C Process
# pause
class Slave < RbProcess
- def initialize(*args)
-
+ def initialize()
+ super()
+ end
+ def run(args)
puts "Hello From Slave"
s_mailbox = "slave" + args[0]
-
-
while true
-
task = RbTask.recieve(s_mailbox)
-
task_name = RbTask.name(task)
-
if ( task_name == "finalize" )
puts "Slave" + s_mailbox + "got finalize msg"
break
end
-
puts "Slave " + s_mailbox + "Processing" + RbTask.name(task)
RbTask.execute(task)
-
end
-
puts "Slave " + s_mailbox + "I'm Done , See You !!"
-
-
end
-
end
require 'msg'
include MSG
+require 'RubyProcess'
+require 'Master'
+require 'RubyHost'
array = Array.new()
array << "Peace!!" << "Hey" << "Euh..."<< "Hey2" << "Euh...2"
+# process = rubyNewInstanceArgs("Master",array)
+
# array.clear
# puts array.empty?
size = array2.size
-
-
-
-
-
-task = Task.new('ninou',12,23)
+task = Task.new('marine',12,23)
puts "Created task :"+task.to_s+" ; name: "+Task.name(task)
+Task.send(task,"MailBox")
+# Task.send(Task.new("finalize",0,0),"alias")
#task2 =RbTask.new('task2')
# task = RbTask.new()
# host = Host.new("Brad")
-
+
#host2 = Host.new()
# task_2 = Task.new('task2',12,23)
# name1 = task_2.name()
# name2 = Task.name(task)
# size = Task.compSize(task)
- name = Task.name(task)
+name = Task.name(task)
# task.bind
number = 56
-process = RbProcess.new("mon nom")
-puts "Created process :"+process.to_s+" ; name: "+RbProcess.name(task)
+process = rubyNewInstance("RbProcess");#RbProcess.new()
+
+p process.class
+# puts "Created process :"+process.to_s+" ; name: "+RbProcess.name(task)
# name = process.name
puts name
+
+argue = ARGV[0]
+puts argue
# puts process.alive?
# Thread.stop
# process2 = RbProcess.new()
#include "surf/surfxml_parse.h"
#include <stdio.h>
+// #define DEBUG
+
static void r_init()
{
static void application_handler_on_start_document(void)
{
+
r_init();
//current One
current = rb_funcall3(rb_const_get(rb_cObject, rb_intern("ApplicationHandler")), rb_intern("new"), 0, 0);
rb_funcall(current,rb_intern("onStartDocument"),0);
-
+ #ifdef DEBUG
+ printf ("application_handler_on_start_document ...Done\n" );
+ #endif
}
static void application_handler_on_begin_process(void)
{
-
-
//r_init();
-
VALUE hostName = rb_str_new2(A_surfxml_process_host);
VALUE function = rb_str_new2(A_surfxml_process_function);
-
+#ifdef DEBUG
+ printf ("On_Begin_Process: %s : %s \n",RSTRING(hostName)->ptr,RSTRING(function)->ptr);
+#endif
rb_funcall(current,rb_intern("onBeginProcess"),2,hostName,function);
-
-
}
static void application_handler_on_process_arg(void)
{
-
//r_init();
-
VALUE arg = rb_str_new2(A_surfxml_argument_value);
-
- rb_funcall(current,rb_intern("onProcessArg"),1,arg);
-
-
+#ifdef DEBUG
+ printf ("On_Process_Args >> Sufxml argument value : %s\n",RSTRING(arg)->ptr);
+#endif
+ rb_funcall(current,rb_intern("onProcessArg"),1,arg);
}
static void application_handler_on_property(void)
#include "rb_msg_process.c"
#include "rb_application_handler.c"
-
+
//Init Msg_Init From Ruby
static void msg_init(VALUE Class,VALUE args)
{
-
char **argv=NULL;
const char *tmp;
int argc,type,i;
VALUE *ptr ;
-
-
-
// Testing The Args Type
type = TYPE(args);
-
if (type != T_ARRAY )
{
- rb_raise(rb_eRuntimeError,"Argh!! Bad Arguments to msg_init");
+ rb_raise(rb_eRuntimeError,"Argh!!! Bad Arguments to msg_init");
return;
}
// Create C Array to Hold Data_Get_Struct
argv = xbt_new0(char *, argc); // argc or argc +1
-
argv[0] = strdup("ruby");
-
-
for (i=0;i<argc;i++)
{
VALUE value = ptr[i];
type = TYPE(value);
-// if (type == T_STRING)
+// if (type == T_STRING)
tmp = RSTRING(value)->ptr;
argv[i+1] = strdup(tmp);
MSG_global_init(&argc,argv);
MSG_set_channel_number(10); // Okey !! Okey !! This Must Be Fixed Dynamiclly , But Later ;)
SIMIX_context_select_factory("ruby");
-
// Free Stuffs
for (i=0;i<argc;i++)
free(argv[i]) ;
return;
}
-
-
-
//Init Msg_Run From Ruby
static void msg_run(VALUE class)
{
xbt_fifo_item_t item = NULL;
m_host_t host = NULL;
VALUE rbHost;
-
+
// Let's Run
if (MSG_OK != MSG_main()){
rb_raise(rb_eRuntimeError,"MSG_clean() failed");
}
-
return;
-
}
-
-
-
// Create Environment
static void msg_createEnvironment(VALUE class,VALUE plateformFile)
{
-
- int type = TYPE(plateformFile);
-
- if ( type != T_STRING )
- rb_raise(rb_eRuntimeError,"Bad Argument's Type");
-
- const char * platform = RSTRING(plateformFile)->ptr;
-
- MSG_create_environment(platform);
+ int type = TYPE(plateformFile);
+ if ( type != T_STRING )
+ rb_raise(rb_eRuntimeError,"Bad Argument's Type");
+ const char * platform = RSTRING(plateformFile)->ptr;
+ MSG_create_environment(platform);
+ printf("Create Environment...Done\n");
return;
}
-// deploy Application
+//deploy Application
static void msg_deployApplication(VALUE class,VALUE deploymentFile )
{
int type = TYPE(deploymentFile);
-
if ( type != T_STRING )
- rb_raise(rb_eRuntimeError,"Bad Argument's Type");
-
+ rb_raise(rb_eRuntimeError,"Bad Argument's Type for deployApplication ");
const char *dep_file = RSTRING(deploymentFile)->ptr;
-
-
surf_parse_reset_parser();
-
surfxml_add_callback(STag_surfxml_process_cb_list,
application_handler_on_begin_process);
- surfxml_add_callback(STag_surfxml_argument_cb_list,
+ surfxml_add_callback(ETag_surfxml_argument_cb_list,
application_handler_on_process_arg);
surfxml_add_callback(STag_surfxml_prop_cb_list,
application_handler_on_property);
- surfxml_add_callback(STag_surfxml_process_cb_list,
+ surfxml_add_callback(ETag_surfxml_process_cb_list,
application_handler_on_end_process);
-
+
surf_parse_open(dep_file);
-
application_handler_on_start_document();
-
if(surf_parse())
- rb_raise(rb_eRuntimeError,"surf_parse() failed");
-
- surf_parse_close();
-
+ rb_raise(rb_eRuntimeError,"surf_parse() failed");
+ surf_parse_close();
application_handler_on_end_document();
-
-
+ printf("Deploy Application...Done\n");
+
}
return DBL2NUM(MSG_get_clock());
-}
+}
//pajeOutput
static void msg_paje_out(VALUE class,VALUE pajeFile)
}
+
+// Ruby intropspection : Instanciate a ruby Class From its Name
+// Used by ProcessFactory::createProcess
+
+static VALUE msg_new_ruby_instance(VALUE class,VALUE className)
+{
+ ruby_init();
+ ruby_init_loadpath();
+ char * p_className = RSTRING(className)->ptr;
+
+ return rb_funcall3(rb_const_get(rb_cObject, rb_intern(p_className)), rb_intern("new"), 0, 0);
+
+}
+
+//This Time With Args
+
+static VALUE msg_new_ruby_instance_with_args(VALUE class,VALUE className,VALUE args)
+{
+ ruby_init();
+ ruby_init_loadpath();
+ char * p_className = RSTRING(className)->ptr;
+ return rb_funcall(rb_const_get(rb_cObject, rb_intern(p_className)), rb_intern("new"), 1, args);
+
+}
/*****************************************************************************************************************
Wrapping MSG module and its Class ( Task,Host) & Methods ( Process's method...ect)
rb_define_method(rb_msg,"info",msg_info,1);
rb_define_method(rb_msg,"getClock",msg_get_clock,0);
rb_define_method(rb_msg,"pajeOutput",msg_paje_out,1);
-
+ rb_define_method(rb_msg,"rubyNewInstance",msg_new_ruby_instance,1);
+ rb_define_method(rb_msg,"rubyNewInstanceArgs",msg_new_ruby_instance_with_args,2);
+
// Associated Process Methods
-// rb_define_method(rb_msg,"processCreate",processCreate,2);
+ rb_define_method(rb_msg,"processCreate",processCreate,2);
rb_define_method(rb_msg,"processSuspend",processSuspend,1);
rb_define_method(rb_msg,"processResume",processResume,1);
rb_define_method(rb_msg,"processIsSuspend",processIsSuspend,1);
rb_define_method(rb_msg,"processKill",processKill,1);
rb_define_method(rb_msg,"processGetHost",processGetHost,1);
- //Classes
+ //Classes
rb_task = rb_define_class_under(rb_msg,"Task",rb_cObject);
rb_host = rb_define_class_under(rb_msg,"Host",rb_cObject);
-
- //Task Methods
+
+ //Task Methods
rb_define_module_function(rb_task,"new",task_new,3);
rb_define_module_function(rb_task,"compSize",task_comp,1);
rb_define_module_function(rb_task,"name",task_name,1);
rb_define_module_function(rb_task,"execute",task_execute,1);
- rb_define_module_function(rb_task,"send",task_send,2);
+ rb_define_module_function(rb_task,"send",task_send,2);
rb_define_module_function(rb_task,"receive",task_receive,1);
rb_define_module_function(rb_task,"receive2",task_receive2,2);
rb_define_module_function(rb_task,"sender",task_sender,1);
rb_define_module_function(rb_host,"getByName",host_get_by_name,1);
rb_define_module_function(rb_host,"name",host_name,1);
rb_define_module_function(rb_host,"speed",host_speed,1);
- rb_define_module_function(rb_host,"number",host_number,0);
+ rb_define_module_function(rb_host,"number",host_number,0);
rb_define_module_function(rb_host,"setData",host_set_data,2);
rb_define_module_function(rb_host,"getData",host_get_data,1);
-// rb_define_module_function(rb_host,"hasData",host_has_data,1);
+ //rb_define_module_function(rb_host,"hasData",host_has_data,1);
rb_define_module_function(rb_host,"isAvail",host_is_avail,1);
-}
+}
// Tools
static void msg_info(VALUE Class,VALUE msg);
-//get Clock
+//get Clock
static VALUE msg_get_clock(VALUE Class);
//pajeOutput
static void msg_paje_output(VALUE Class,VALUE pajeFile);
+// Ruby Introspection : To instanciate a Ruby Class from its Name
+static VALUE msg_new_ruby_instance(VALUE Class,VALUE className);
+
+// The Same ... This Time with Args
+static VALUE msg_new_ruby_instance_with_args(VALUE Class,VALUE className,VALUE args);
+
#endif
\ No newline at end of file
//Nothing to do !!?
}
-
// New Method : return a Host
static VALUE host_get_by_name(VALUE class, VALUE name)
{
- m_host_t host = MSG_get_host_by_name(RSTRING(name)->ptr);
+ const char * h_name = RSTRING(name)->ptr;
+ m_host_t host = MSG_get_host_by_name(h_name);
if(!host)
-
rb_raise(rb_eRuntimeError,"MSG_get_host_by_name() failled");
- return Data_Wrap_Struct(class, 0, host_free, host);
+ return Data_Wrap_Struct(class,0,host_free,host);
}
-
//Get Name
-
static VALUE host_name(VALUE class,VALUE host)
{
// Wrap Ruby Value to m_host_t struct
-
m_host_t ht;
Data_Get_Struct(host, m_host_t, ht);
return rb_str_new2(MSG_host_get_name(ht));
// Get Number
static VALUE host_number(VALUE class)
{
-
- return MSG_get_host_number();
+
+ return INT2NUM(MSG_get_host_number());
}
}
-
// Host Set Data
static void host_set_data(VALUE class,VALUE host,VALUE data)
{
return Qnil;
}
-
-
-
// Host is Avail
static VALUE host_is_avail(VALUE class,VALUE host)
{
// Sending Task
-static VALUE task_send(VALUE class,VALUE task,VALUE mailbox)
+static void task_send(VALUE class,VALUE task,VALUE mailbox)
{
// Wrap Ruby Value to m_task_t struct
+
m_task_t tk;
Data_Get_Struct(task, m_task_t, tk);
- return INT2NUM(MSG_task_send(tk,RSTRING(mailbox)->ptr));
-
+ int res = MSG_task_send(tk,RSTRING(mailbox)->ptr);
+
+ if(res != MSG_OK)
+ rb_raise(rb_eRuntimeError,"MSG_task_send failed");
+ return;
}
// Recieving Task
// Execute Task
static VALUE task_execute(VALUE Class,VALUE task);
-// Sending Task
-static VALUE task_send(VALUE Class,VALUE task,VALUE mailbox);
+// Sending Task
+static void task_send(VALUE Class,VALUE task,VALUE mailbox);
// Recieve : return a task
static VALUE task_receive(VALUE Class,VALUE mailbox);