Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
5dce44c98e08441ca5dec6aa95f036d901082c71
[simgrid.git] / src / bindings / ruby / RubyProcess.rb
1
2 #  * $Id$
3 #  *
4 #  * Copyright 2010 Martin Quinson, Mehdi Fekari           
5 #  * All right reserved. 
6 #  *
7 #  * This program is free software; you can redistribute 
8 #  * it and/or modify it under the terms of the license 
9 #  *(GNU LGPL) which comes with this package. 
10
11 require 'msg'
12 require 'Semaphore'
13 include MSG
14 $DEBUG = false  # This is a Global Variable Useful for Debugging
15
16 class RbProcess < Thread 
17   @@nextProcessId = 0
18 # Attributes
19   attr_accessor :bind, :id, :proprieties, :name,
20       :pargs, :schedBegin, :schedEnd
21   
22 # Initialize : USED in ApplicationHandler to Initialize it
23   def initialize(*args)
24     
25     argc = args.size
26 # Default initializer
27     if argc == 0    #>>> new()  
28     super() {
29     @id = 0
30     @bind = 0
31     @name = ""
32     @pargs = Array.new()
33     init_var()
34     start()
35      if $DEBUG
36         puts "Init Default Initialzer...Nothing to do...Bye"
37      end  
38     }
39     end
40        
41     # Init with 2 arguments **********************>>>(HostName,Name) Or (Host , Name)
42     if argc == 2   
43       super(){
44       type = args[0].type()
45       if ( type.to_s == "String")
46         host = Host.getByName(args[0])
47       end
48       if ( type.to_s == "MSG::Host")
49         host = args[0]  
50       end
51       if $DEBUG
52         puts host
53       end
54       raise "Process Name Cannot Be Null"   if args[1].empty?
55       @name = args[1] # First Arg
56       if $DEBUG
57         puts @name
58       end
59       @pargs = Array.new()    # No Args[] Passed in Arguments
60       @@nextProcessId += 1
61       @id = @@nextProcessId
62       init_var()
63       start()
64       createProcess(self,host)
65         if $DEBUG
66           puts "Initilize with 2 args"
67         end
68       }
69     
70     end
71        
72     # Init with 3 arguments **********************(hostName,Name,args[]) or # (Host,Name,args[])
73     
74     if argc == 3  
75       super(){
76         type = args[0].type()
77         if( type.to_s == "String")
78           host =Host.getByName(args[0])
79         end
80         if ( type.to_s == "MSG::Host" )
81         host = args[0]
82         end
83         if $DEBUG
84         puts host
85         end
86       
87         raise "Process Name Cannot Be Null" if args[0].empty? 
88         @name = args[1]
89         type = args[2].type()
90         raise "Third Argument Should be an Array" if type != "Array"
91         @pargs = args[3]
92         @@nextProcessId +=1
93         @id = @@nextProcessId
94         init_var()
95         createProcess(self,host)  
96         
97       if $DEBUG
98         puts "Initilize with 3 args"
99       end
100       
101 #       sleep #keep the thread running
102         }
103     end
104     end
105
106   # Init_var Called By Initialize  
107   def init_var()  
108     @proprieties = Hash.new()
109     # Process Synchronization Tools
110     @schedBegin = Semaphore.new(0)
111     @schedEnd = Semaphore.new(0)    
112   end
113   
114   #main
115   def msg_main(args)
116     # To Be Implemented within The Process...
117     # The Main Code of The Process to be Executed ...
118   end
119      
120   
121   # Start : To keep the Process Alive and waitin' via semaphore
122   def start()
123     
124     @schedBegin.acquire()
125     #execute The Main Code of The Process ( Example Master ; Slave ...)     
126     msg_main(@pargs)
127     processExit(self) #Exite the Native Process
128     @schedEnd.release()
129   end
130     
131 #   NetxId
132   def nextId ()
133     @@nextProcessId +=1
134     return @@nextProcessId
135   end
136
137   if $DEBUG
138     #Process List
139     def processList()
140       Thread.list.each {|t| p t}
141     end
142   end
143   
144   #Get Own ID
145   def getID()
146     return @id
147   end
148   
149   # set Id
150   def setID(id)
151     @id = id
152   end
153   
154   #Get a Process ID
155   def processID(process)
156     return process.id
157   end
158   
159   #Get Own Name
160   def getName()
161     return @name
162   end
163   
164   #Get a Process Name
165   def processName(process)
166     return process.name
167   end
168   
169   #Get Bind
170   def getBind()
171     return @bind
172   end
173   
174   #Get Binds
175   def setBind(bind)
176     @bind = bind
177   end
178     
179   def unschedule() 
180 #     Thread.pass
181     @schedEnd.release()
182     @schedBegin.acquire()
183   end
184   
185   def schedule()
186     @schedBegin.release()
187     @schedEnd.release()
188   end
189   
190    #C Simualateur Process Equivalent  Management
191   # After Binding Ruby Process to C Process
192   
193 #   pause
194   def pause()
195     processSuspend(self)
196   end
197   
198 #   restart
199   def restart()
200     processResume(self)
201   end
202   
203 #   isSuspended
204   def isSuspended()
205     processIsSuspended(self)
206   end
207   
208 #   getHost
209   def getHost()
210     processGetHost(self)
211   end
212   
213 # The Rest of Methods !!! To be Continued ...
214 end