Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Further cleanups of ruby, plus add FIXME in ruby and Java for the next cleanups
[simgrid.git] / src / bindings / ruby / simgrid.rb
index 9d0b847..2cfeacf 100644 (file)
@@ -1,8 +1,7 @@
 require 'simgrid_ruby'
-include MSG
 require 'thread'
 
-$DEBUG = false  # This is a Global Variable Useful for Debugging
+$DEBUG = false  # This is a Global Variable Useful for MSG::debugging
 
 ###########################################################################
 # Class Semaphore 
@@ -15,6 +14,7 @@ class Semaphore
   end
 
   def acquire
+    MSG::debug("Acquire "+self.to_s)
     Thread.critical = true
     if (@counter -= 1) < 0
       @waiting_list.push(Thread.current)
@@ -26,11 +26,15 @@ class Semaphore
   end
 
   def release
+    MSG::debug("Release "+self.to_s)
     Thread.critical = true
     begin
       if (@counter += 1) <= 0
-  t = @waiting_list.shift
-  t.wakeup if t
+        t = @waiting_list.shift
+        t.wakeup if t
+        MSG::debug("Wakeup "+t.to_s)
+      else 
+        MSG::debug("Nobody to wakeup")
       end
     rescue ThreadError
       retry
@@ -44,15 +48,16 @@ end
 ########################################################################
 # Class Process 
 ########################################################################
-class MsgProcess < Thread 
+class MSG::Process < Thread 
   @@nextProcessId = 0
 
 # Attributes
-  attr_reader :bind, :id, :pargs    # Read only
-  attr_accessor :name, :properties  # R/W
+  attr_reader :bind, :id            # Read only
+  attr_accessor :name, :properties, :pargs  # R/W
   
 # Initialize : Used from ApplicationHandler to fill it in
   def initialize(*args)
+    # FIXME: use only one variante (the one with 3 args) and kill the others
     @schedBegin = Semaphore.new(0)
     @schedEnd = Semaphore.new(0)    
     @properties = Hash.new()
@@ -67,14 +72,13 @@ class MsgProcess < Thread
         @name = ""
         @pargs = Array.new()
         start()
-        if $DEBUG
-               puts "Init Default Initializer...Nothing to do...Bye"
-        end  
+        MSG::debug "Initializer without any argument"
       }
        
     # 2 arguments: (HostName,Name) Or (Host , Name)
     elsif argc == 2   
       super(){
+        MSG::debug "Initilize with 2 args"
         type = args[0].type()
         if ( type.to_s == "String")
           host = Host.getByName(args[0])
@@ -94,14 +98,12 @@ class MsgProcess < Thread
         @pargs = Array.new()    # No Args[] Passed in Arguments
         start()
         createProcess(self,host)
-             if $DEBUG
-               puts "Initilize with 2 args"
-        end
       }
        
     # 3 arguments: (hostName,Name,args[]) or (Host,Name,args[])
     elsif argc == 3  
       super(){
+       MSG::debug "Initilize with 3 args"
         type = args[0].type()
         if ( type.to_s == "String")
           host = Host.getByName(args[0])
@@ -121,61 +123,53 @@ class MsgProcess < Thread
         @pargs = args[3]
         createProcess(self,host)  
         
-      if $DEBUG
-       puts "Initilize with 3 args"
-      end      
           }
   else 
-    raise "Bad number of argument: Expecting either 1, 2 or 3, but got "+argc
+    raise "Bad number of argument: Expecting either 1, 2 or 3, but got "+argc.to_s
   end
     end
   
   # main
-  def msg_main(args) 
+  def main(args) 
     # To be overriden by childs
-    raise("You must define a msg_main() function in your process, containing the code of this process")
+    raise("You must define a main() function in your process, containing the code of this process")
   end
      
   # Start : To keep the process alive and waiting via semaphore
   def start()
     @schedBegin.acquire
     # execute the main code of the process     
-    debug("Begin execution")
-    msg_main(@pargs)
+    MSG::debug("Begin execution")
+    main(@pargs)
     processExit(self) # Exit the Native Process
     @schedEnd.release
   end
     
-  def processList()
+  def processList() (KILLME?)
     Thread.list.each {|t| p t}
   end
   
-  #Get Own ID
+  #Get Own ID (KILLME?)
   def getID()
     return @id
   end
   
-  #Get a Process ID
+  #Get a Process ID (KILLME?)
   def processID(process)
     return process.id
   end
   
-  #Get Own Name
+  #Get Own Name (KILLME?)
   def getName()
     return @name
   end
   
-  #Get a Process Name
-  def processName(process)
-    return process.name
-  end
-  
-  #Get Bind
+  #Get Bind (KILLME?)
   def getBind()
     return @bind
   end
   
-  #Get Binds
+  #Get Binds (KILLME?)
   def setBind(bind)
     @bind = bind
   end
@@ -216,90 +210,43 @@ class MsgProcess < Thread
 # The Rest of Methods !!! To be Continued ...
 end
 
-########################################################################
-# Class ProcessFactory
-########################################################################
-
-class ProcessFactory 
-
-#     Attributes
-   attr_accessor :args, :properties, :hostName, :function
-#    Initialize
-    def initialize()
-    
-    @args = Array.new
-    @properties = Hash.new
-    @hostName = nil
-    @function = nil
-    
-    end
-    
-#     setProcessIdentity
-    def setProcessIdentity(hostName,function)
-      @hostName = hostName
-      @function = function
-      
-      if !args.empty?
-              args.clear
-      end
-      
-      if !properties.empty?
-       properties.clear   
-      end
-    
-    end
-
-    def registerProcessArg(arg)
-      @args.push(arg)
-    end
-
-#     CreateProcess
-    def createProcess()
-      process = rubyNewInstance(@function) 
-      size = @args.size
-      for i in 0..size-1
-       process.pargs.push(@args[i]) 
-      end
-      process.name = @function
-      host = Host.getByName(@hostName)
-      processCreate(process,host)
-      process.properties = @properties
-      @properties = Hash.new
-      
-    end
-    
-#     SetProperty
-    def setProperty(id,value)
-      @properties[id] = value
-    end
-end
-
 #########################################################################
 # Class ApplicationHandler
 #########################################################################
 class ApplicationHandler
-  @processFactory  
-  
   def initialize()
-    @processFactory = ProcessFactory.new
+    @hostName = nil
+    @function = nil
   end
   
   def onBeginProcess(hostName,function)
-    @processFactory.setProcessIdentity(hostName,function)
-    debug("onBeginProcess("+hostName+","+function+")")
+    @args = Array.new
+    @properties = Hash.new
+    
+    @hostName = hostName
+    @function = function
+      
+    MSG::debug("onBeginProcess("+hostName+","+function+")")
   end
 
   def onProperty(id,value)
-    @processFactory.setProperty(id,value)
+    @properties[id] = value
   end
   
   def onProcessArg(arg)
-    @processFactory.registerProcessArg(arg)
+    @args.push(arg)
   end
 
   def onEndProcess()
-   @processFactory.createProcess()
-   debug("onEndProcess")
+    # must be in C, called from a callback to the FlexML parser 
+    # newInstance must take args and hostname as argument to initialize everything, *and* bind it to C element
+    # Allows to mark all attributes of process (but properties) to read-only
+    process = MSG::rubyNewInstance(@function) 
+    process.pargs = @args
+    process.name = @function
+    host = MSG::Host.getByName(@hostName)
+    MSG::processCreate(process,host)
+    process.properties = @properties
   end
 end