Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Ruby Msg Binding : new Methods and new Example
[simgrid.git] / src / bindings / ruby / PingPong.rb
1 require 'simgrid'
2
3 include MSG
4
5 #####################
6 #PingPongTask Class
7 #####################
8
9 class PingPongTask < MSG::Task
10   
11
12   def initialize(*args)
13     #Has No Role, Since Its The Task Class ( Created from C ) tht will be called , 
14     # and any instruction here will be ignored
15   end
16   
17
18   
19 end
20
21 ####################
22 # Sender Class
23 ####################
24
25 class Sender < MSG::Process
26   
27  def main(args)
28    MSG::info("Hello from Sender")
29    hostCount = args.size
30    MSG::info("Host count :" + hostCount.to_s)
31    mailboxes = Array.new
32       
33    for i in 0..hostCount-1
34    
35      mailboxes<< MSG::Host.getByName(args[i]).name
36      
37 #      FIXME Handel Exceptions 
38    end
39    
40    for i in 0..hostCount-1
41      time = MSG.getClock.to_s # to send as a data >> must be a string
42      MSG::info("sender time :"+time.to_s)
43      task = PingPongTask.new("PingTask",10000,2000)
44      task.setData(time)
45      p "mailboxe >>> "+ mailboxes[i]
46      task.send(mailboxes[i])
47    end
48    
49    MSG::info("GoodBye !!!")
50      
51    end
52    
53  end
54   
55 ####################
56 # Receiver Class
57 ####################
58
59 class Receiver < MSG::Process
60   
61   def main(args)
62     
63     MSG::info("Hello from Receiver")
64     time = MSG.getClock
65     MSG::info("Try to get a task")
66     host = MSG::Host.getHostProcess(self)
67     task = PingPongTask.receive(host.name)
68     p "task name recevied : "+ task.name
69     p "data in the task :" + task.data
70     timeGot = MSG.getClock
71     timeSent= task.data
72     MSG::info("Got at time: "+timeGot.to_s)
73     MSG::info("Was sent at time "+timeSent.to_s)
74     communicationTime = timeGot - time
75     MSG::info("Communication Time: "+communicationTime.to_s)
76     MSG::info("--- bw "+(100000000/communicationTime).to_s+" ----")
77     MSG::info("GoodBye !!!")
78   end
79   
80   
81 end
82
83 #################################################
84 # main chunck
85 #################################################
86
87 if (ARGV.length == 2) 
88         MSG.createEnvironment(ARGV[0])
89         MSG.deployApplication(ARGV[1])
90 else 
91         MSG.createEnvironment("ping_pong_platform.xml")
92         MSG.deployApplication("ping_pong_deployment.xml")
93
94 end
95
96 MSG.run
97 MSG.exit
98