Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
various small cleanups. Not quite there yet
[simgrid.git] / src / bindings / ruby / RubyProcess.rb
1 require 'msg'
2 require 'Semaphore'
3 include MSG
4
5 $debug = true  # This is a Global Variable Useful for Debugging
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         host = Host.getByName(args[0])
68       end
69       
70       if ( type.to_s == "MSG::Host")
71         host = args[0]  
72       end
73       
74       
75       if $debug
76         puts host
77       end
78       
79       
80       raise "Process Name Cannot Be Null"   if args[1].empty?
81       
82       @name = args[1] # First Arg
83       
84       if $debug
85         puts @name
86       end
87       
88       
89       @pargs = Array.new()    # No Args[] Passed in Arguments
90       
91        @@nextProcessId += 1
92        @id = @@nextProcessId
93       
94        init_var()
95       
96        createProcess(self,host) #TODO >> MSG::Process
97       if $debug
98       puts "Initilize with 2 args"
99       end
100       
101 #       sleep  # Keep The Thread Runin' 
102       }
103     
104     
105     end
106     
107        
108 #     Init with 3 arguments
109     
110     if argc == 3  #(hostName,Name,args[]) or # (Host,Name,args[])
111       super(){
112         
113         type = args[0].type()
114         
115         if( type.to_s == "String")
116 #       host = Host.getByName(args[0])
117       host ="Host.getByName(args[0])"
118         end
119       
120         if ( type.to_s == "MSG::Host" )
121         host = args[0]
122         end
123       
124         if $debug
125         puts host
126         end
127       
128       
129         raise "Process Name Cannot Be Null" if args[0].empty? 
130         
131         @name = args[1]
132          
133         type = args[2].type()
134         
135         raise "Third Argument Should be an Array" if type != "Array"
136         
137         @pargs = args[3]
138         
139          
140         @@nextProcessId +=1
141         @id = @@nextProcessId
142       
143         init_var()
144              
145          createProcess(self,host)  #TODO  RubyMsg
146         
147       if $debug
148         puts "Initilize with 3 args"
149       end
150       
151 #       sleep #keep the thread running
152         }
153     end
154
155     end
156
157     
158     # Init_var Called By Initialize  
159     
160     
161   def init_var()
162     
163     
164     @proprieties = Hash.new()
165     # Process Synchronization Tools
166      @schedBegin = Semaphore.new(0)
167      @schedEnd = Semaphore.new(0)
168       
169   end
170   
171   
172   
173   
174 #   NetxId
175   def nextId ()
176     
177     @@nextProcessId +=1
178     return @@nextProcessId
179     
180   end
181   
182   
183     
184   
185   
186   
187   if $debug
188     #Process List
189     def processList()
190       
191       Thread.list.each {|t| p t}
192       
193     end
194   end
195   
196   
197   #Get Own ID
198   
199   def getID()
200     
201     return @id
202     
203   end
204   
205   # set Id
206   
207   def setID(id)
208     
209     @id = id
210     
211   end
212   
213   #Get a Process ID
214   
215   def processID(process)
216     
217     return process.id
218   
219   end
220   
221   
222   #get Own Name
223   
224   def getName()
225    
226     return @name
227   
228   end
229   
230   #get a Process Name
231   
232   def processName(process)
233   
234     return process.name
235   
236   end
237   
238   #get Bind
239   def getBind()
240     
241     return @bind
242     
243   end
244   
245   #set Binds
246   def setBind(bind)
247     
248     @bind = bind
249     
250   end
251     
252   
253   
254   # Stop
255   
256   def unschedule() 
257     
258     Thread.pass
259   
260   end
261   
262   
263   #C Simualateur Process Equivalent  Management
264   # After Binding Ruby Process to C Process
265   
266 #   pause
267   def pause()
268   
269     processSuspend(self)
270   
271   end
272   
273 #   restart
274   def restart()
275    
276     processResume(self)
277   
278   end
279   
280 #   isSuspended
281   def isSuspended()
282   
283     processIsSuspended(self)
284  
285   end
286   
287 #   getHost
288   def getHost()
289   
290     processGetHost(self)
291   
292   end
293   
294 #    The Rest of Methods !!! To be Continued ...
295
296 end