Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ed92544d0ec89e7e7149a0064dc3d49390730cd8
[simgrid.git] / src / bindings / ruby / RubyProcess.rb
1 require 'msg'
2 require 'Semaphore'
3 include MSG
4
5 $debug = true  # This is a Gloabl Variable Useful for Debbuging
6
7
8 class RbProcess < Thread
9   
10   
11   
12   
13   @@nextProcessId = 0
14 # Attributes
15   attr_accessor :bind, :id, :proprieties, :name,
16       :pargs, :schedBegin, :schedEnd
17   
18   
19   
20 # Initialize
21   
22   
23   # Used in ApplicationHandler to Initialize it
24  
25   
26   
27   def initialize(*args)
28     
29    
30     argc = args.size
31     
32     
33     
34 #      Default Init
35     if argc == 0    #>>> new()
36     super() {
37     
38     @id = 0
39     @bind = 0
40     @name = ""
41     @pargs = Array.new()
42     
43     init_var()
44
45          
46     
47      if $debug
48     puts "Init Default Initialzer..."
49      end
50     
51 #     Thread.pass   #yield ?!!
52 #     sleep   # Sleep Forever ... To Keep Thread Alive ?!!
53     
54     }
55     end
56     
57     
58     # Initi with 2 arguments
59     
60     if argc == 2   # >>>(HostName,Name) Or (Host , Name)
61       super(){
62         
63       
64       type = args[0].type()
65       
66       if ( type.to_s == "String")
67           
68        host = Host.getByName(args[0])
69        
70       end
71       
72       if ( type.to_s == "MSG::Host")
73       host = args[0]  
74       end
75       
76       
77       if $debug
78       puts host
79       end
80       
81       
82       
83       
84       raise "Process Name Cannot Be Null"   if args[1].empty?
85       
86       @name = args[1] # First Arg
87       
88       if $debug
89       puts @name
90       end
91       
92       
93       @pargs = Array.new()    # No Args[] Passed in Arguments
94       
95        @@nextProcessId += 1
96        @id = @@nextProcessId
97       
98        init_var()
99       
100        createProcess(self,host) #TODO >> MSG::Process
101       if $debug
102       puts "Initilize with 2 args"
103       end
104       
105 #       sleep  # Keep The Thread Runin' 
106       }
107     
108     
109     end
110     
111        
112 #     Init with 3 arguments
113     
114     if argc == 3  #(hostName,Name,args[]) or # (Host,Name,args[])
115       super(){
116         
117         type = args[0].type()
118         
119         if( type.to_s == "String")
120 #       host = Host.getByName(args[0])
121       host ="Host.getByName(args[0])"
122         end
123       
124         if ( type.to_s == "MSG::Host" )
125         host = args[0]
126         end
127       
128         if $debug
129         puts host
130         end
131       
132       
133         raise "Process Name Cannot Be Null" if args[0].empty? 
134         
135         @name = args[1]
136          
137         type = args[2].type()
138         
139         raise "Third Argument Should be an Array" if type != "Array"
140         
141         @pargs = args[3]
142         
143          
144         @@nextProcessId +=1
145         @id = @@nextProcessId
146       
147         init_var()
148              
149          createProcess(self,host)  #TODO  RubyMsg
150         
151       if $debug
152         puts "Initilize with 3 args"
153       end
154       
155 #       sleep #keep the thread running
156         }
157     end
158
159     end
160
161     
162     # Init_var Called By Initialize  
163     
164     
165   def init_var()
166     
167     
168     @proprieties = Hash.new()
169     # Process Synchronization Tools
170      @schedBegin = Semaphore.new(0)
171      @schedEnd = Semaphore.new(0)
172       
173   end
174   
175   
176   
177   
178 #   NetxId
179   def nextId ()
180     
181     @@nextProcessId +=1
182     return @@nextProcessId
183     
184   end
185   
186   
187     
188   
189   
190   
191   if $debug
192     #Process List
193     def processList()
194       
195       Thread.list.each {|t| p t}
196       
197     end
198   end
199   
200   
201   #Get Own ID
202   
203   def getID()
204     
205     return @id
206     
207   end
208   
209   # set Id
210   
211   def setID(id)
212     
213     @id = id
214     
215   end
216   
217   #Get a Process ID
218   
219   def processID(process)
220     
221     return process.id
222   
223   end
224   
225   
226   #get Own Name
227   
228   def getName()
229    
230     return @name
231   
232   end
233   
234   #get a Process Name
235   
236   def processName(process)
237   
238     return process.name
239   
240   end
241   
242   #get Bind
243   def getBind()
244     
245     return @bind
246     
247   end
248   
249   #set Binds
250   def setBind(bind)
251     
252     @bind = bind
253     
254   end
255     
256   
257   
258   # Stop
259   
260   def unschedule() 
261     
262     Thread.pass
263   
264   end
265   
266   
267   #C Simualateur Process Equivalent  Management
268   # After Binding Ruby Process to C Process
269   
270 #   pause
271   def pause()
272   
273     processSuspend(self)
274   
275   end
276   
277 #   restart
278   def restart()
279    
280     processResume(self)
281   
282   end
283   
284 #   isSuspended
285   def isSuspended()
286   
287     processIsSuspended(self)
288  
289   end
290   
291 #   getHost
292   def getHost()
293   
294     processGetHost(self)
295   
296   end
297   
298 #    The Rest of Methods !!! To be Continued ...
299
300 end