Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Acknoledge the fact that JniException is a runtime exception now: kill all its occure...
[simgrid.git] / src / bindings / ruby / simgrid.rb
1 require 'simgrid_ruby'
2 include MSG
3 require 'thread'
4
5 $DEBUG = false  # This is a Global Variable Useful for Debugging
6
7 ###########################################################################
8 # Class Semaphore 
9 ###########################################################################
10
11 class Semaphore
12   def initialize(initvalue = 0)
13     @counter = initvalue
14     @waiting_list = []
15   end
16
17   def acquire
18     Thread.critical = true
19     if (@counter -= 1) < 0
20       @waiting_list.push(Thread.current)
21       Thread.stop
22     end
23     self
24   ensure
25     Thread.critical = false
26   end
27
28   def release
29     Thread.critical = true
30     begin
31       if (@counter += 1) <= 0
32   t = @waiting_list.shift
33   t.wakeup if t
34       end
35     rescue ThreadError
36       retry
37     end
38     self
39   ensure
40     Thread.critical = false
41   end
42 end
43
44 ########################################################################
45 # Class Process 
46 ########################################################################
47 class MsgProcess < Thread 
48   @@nextProcessId = 0
49
50 # Attributes
51   attr_reader :bind, :id            # Read only
52   attr_accessor :name, :properties, :pargs  # R/W
53   
54 # Initialize : Used from ApplicationHandler to fill it in
55   def initialize(*args)
56     @schedBegin = Semaphore.new(0)
57     @schedEnd = Semaphore.new(0)    
58     @properties = Hash.new()
59     @id = @@nextProcessId++
60     
61     argc = args.size
62
63     if argc == 0 # Default initializer
64       super() {
65         @id = 0
66         @bind = 0
67         @name = ""
68         @pargs = Array.new()
69         start()
70         debug "Initializer without any argument"
71       }
72        
73     # 2 arguments: (HostName,Name) Or (Host , Name)
74     elsif argc == 2   
75       super(){
76         debug "Initilize with 2 args"
77         type = args[0].type()
78         if ( type.to_s == "String")
79           host = Host.getByName(args[0])
80         elsif ( type.to_s == "MSG::Host")
81           host = args[0]
82         else 
83           raise "first argument of type "+args[0].type().to_s+", but expecting either String or MSG::Host"
84         end
85         if $DEBUG
86           puts host
87         end
88         raise "Process name cannot be null" if args[1].empty?
89         @name = args[1] 
90         if $DEBUG
91           puts @name
92         end
93         @pargs = Array.new()    # No Args[] Passed in Arguments
94         start()
95         createProcess(self,host)
96       }
97        
98     # 3 arguments: (hostName,Name,args[]) or (Host,Name,args[])
99     elsif argc == 3  
100       super(){
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")
106           host = args[0]
107         else 
108           raise "first argument of type "+args[0].type().to_s+", but expecting either String or MSG::Host"
109         end
110         if $DEBUG
111           puts host
112         end
113       
114         raise "Process name cannot be null" if args[1].empty?
115         @name = args[1]
116         type = args[2].type()
117         raise "Third argument should be an Array" if type != "Array"
118         @pargs = args[3]
119         createProcess(self,host)  
120         
121            }
122   else 
123     raise "Bad number of argument: Expecting either 1, 2 or 3, but got "+argc.to_s
124   end
125     end
126   
127   # main
128   def main(args) 
129     # To be overriden by childs
130     raise("You must define a main() function in your process, containing the code of this process")
131   end
132      
133   # Start : To keep the process alive and waiting via semaphore
134   def start()
135     @schedBegin.acquire
136     # execute the main code of the process     
137     debug("Begin execution")
138     main(@pargs)
139     processExit(self) # Exit the Native Process
140     @schedEnd.release
141   end
142     
143   def processList()
144     Thread.list.each {|t| p t}
145   end
146   
147   #Get Own ID
148   def getID()
149     return @id
150   end
151   
152   #Get a Process ID
153   def processID(process)
154     return process.id
155   end
156   
157   #Get Own Name
158   def getName()
159     return @name
160   end
161   
162   #Get a Process Name
163   def processName(process)
164     return process.name
165   end
166   
167   #Get Bind
168   def getBind()
169     return @bind
170   end
171   
172   #Get Binds
173   def setBind(bind)
174     @bind = bind
175   end
176     
177   def unschedule()
178     @schedEnd.release
179     @schedBegin.acquire
180   end
181   
182   def schedule()
183     @schedBegin.release
184     @schedEnd.acquire
185   end
186   
187    #C Simualator Process Equivalent  Management
188   # After Binding Ruby Process to C Process
189   
190 #   pause
191   def pause()
192     processSuspend(self)
193   end
194   
195 #   restart
196   def restart()
197     processResume(self)
198   end
199   
200 #   isSuspended
201   def isSuspended()
202     processIsSuspended(self)
203   end
204   
205 #   getHost
206   def getHost()
207     processGetHost(self)
208   end
209   
210 # The Rest of Methods !!! To be Continued ...
211 end
212
213 #########################################################################
214 # Class ApplicationHandler
215 #########################################################################
216 class ApplicationHandler
217   def initialize()
218     @hostName = nil
219     @function = nil
220   end
221   
222   def onBeginProcess(hostName,function)
223     @args = Array.new
224     @properties = Hash.new
225     
226     @hostName = hostName
227     @function = function
228       
229     debug("onBeginProcess("+hostName+","+function+")")
230   end
231
232   def onProperty(id,value)
233     @properties[id] = value
234   end
235   
236   def onProcessArg(arg)
237     @args.push(arg)
238   end
239
240   def onEndProcess()
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
247   end
248 end
249  
250 #########################
251 # Main chunck 
252 #########################
253 MSG.init(ARGV)