5 $DEBUG = false # This is a Global Variable Useful for Debugging
7 ###########################################################################
9 ###########################################################################
12 def initialize(initvalue = 0)
18 Thread.critical = true
19 if (@counter -= 1) < 0
20 @waiting_list.push(Thread.current)
25 Thread.critical = false
29 Thread.critical = true
31 if (@counter += 1) <= 0
32 t = @waiting_list.shift
40 Thread.critical = false
44 ########################################################################
46 ########################################################################
47 class MsgProcess < Thread
51 attr_reader :bind, :id # Read only
52 attr_accessor :name, :properties, :pargs # R/W
54 # Initialize : Used from ApplicationHandler to fill it in
56 @schedBegin = Semaphore.new(0)
57 @schedEnd = Semaphore.new(0)
58 @properties = Hash.new()
59 @id = @@nextProcessId++
63 if argc == 0 # Default initializer
70 debug "Initializer without any argument"
73 # 2 arguments: (HostName,Name) Or (Host , Name)
76 debug "Initilize with 2 args"
78 if ( type.to_s == "String")
79 host = Host.getByName(args[0])
80 elsif ( type.to_s == "MSG::Host")
83 raise "first argument of type "+args[0].type().to_s+", but expecting either String or MSG::Host"
88 raise "Process name cannot be null" if args[1].empty?
93 @pargs = Array.new() # No Args[] Passed in Arguments
95 createProcess(self,host)
98 # 3 arguments: (hostName,Name,args[]) or (Host,Name,args[])
101 debug "Initilize with 3 args"
102 type = args[0].type()
103 if ( type.to_s == "String")
104 host = Host.getByName(args[0])
105 elsif ( type.to_s == "MSG::Host")
108 raise "first argument of type "+args[0].type().to_s+", but expecting either String or MSG::Host"
114 raise "Process name cannot be null" if args[1].empty?
116 type = args[2].type()
117 raise "Third argument should be an Array" if type != "Array"
119 createProcess(self,host)
123 raise "Bad number of argument: Expecting either 1, 2 or 3, but got "+argc.to_s
129 # To be overriden by childs
130 raise("You must define a main() function in your process, containing the code of this process")
133 # Start : To keep the process alive and waiting via semaphore
136 # execute the main code of the process
137 debug("Begin execution")
139 processExit(self) # Exit the Native Process
144 Thread.list.each {|t| p t}
153 def processID(process)
163 def processName(process)
187 #C Simualator Process Equivalent Management
188 # After Binding Ruby Process to C Process
202 processIsSuspended(self)
210 # The Rest of Methods !!! To be Continued ...
213 #########################################################################
214 # Class ApplicationHandler
215 #########################################################################
216 class ApplicationHandler
222 def onBeginProcess(hostName,function)
224 @properties = Hash.new
229 debug("onBeginProcess("+hostName+","+function+")")
232 def onProperty(id,value)
233 @properties[id] = value
236 def onProcessArg(arg)
241 process = rubyNewInstance(@function)
242 process.pargs = @args
243 process.name = @function
244 host = Host.getByName(@hostName)
245 processCreate(process,host)
246 process.properties = @properties
250 #########################
252 #########################