Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix path for simgrid library.
[simgrid.git] / examples / ruby / PingPong.rb
1 require 'simgrid.so'
2
3 include MSG
4
5 #####################
6 #PingPongTask Class
7 #####################
8
9 class PingPongTask < MSG::Task
10   
11   # The initialize method has no effect 
12   @time 
13   def setTime(t)
14     @time = t
15   end
16   
17   def getTime()
18     return @time
19   end
20 end
21
22 ####################
23 # Sender Class
24 ####################
25
26 class Sender < MSG::Process
27   
28  def main(args)
29    MSG::info("Hello from Sender")
30    hostCount = args.size
31    MSG::info("Host count :" + hostCount.to_s)
32    mailboxes = Array.new
33       
34    for i in 0..hostCount-1
35      mailboxes<< MSG::Host.getByName(args[i]).name
36    end
37    
38    for i in 0..hostCount-1
39      time = MSG.getClock
40      MSG::info("sender time :"+time.to_s)
41      task = PingPongTask.new("PingTask",10000,2000)
42      MSG::info("task created :" + task.name);
43      #task.join(time) -- MSG::task.join(data) is a Native method you can use to attach any data you want to the task
44      task.setTime(time)
45      task.send(mailboxes[i])
46    end
47    MSG::info("Bye!!")     
48    end
49  end
50   
51 ####################
52 # Receiver Class
53 ####################
54
55 class Receiver < MSG::Process
56   
57   def main(args)
58     MSG::info("Hello from Receiver")
59     time = MSG.getClock
60     host = MSG::Host.getHostProcess(self)
61     task = PingPongTask.receive(host.name)
62     timeGot = MSG.getClock
63     MSG::info("Got at time: "+timeGot.to_s)
64     #timeSent = task.data -- 
65     timeSent = task.getTime
66     MSG::info("Was sent at time "+timeSent.to_s)
67     communicationTime = timeGot - time
68     MSG::info("Communication Time: "+communicationTime.to_s)
69     MSG::info("--- bw "+(100000000/communicationTime).to_s+" ----")
70     MSG::info("Bye!!")
71   end
72   
73 end
74
75 #################################################
76 # main chunck
77 #################################################
78
79 if (ARGV.length == 2) 
80         MSG.createEnvironment(ARGV[0])
81         MSG.deployApplication(ARGV[1])
82 else 
83         MSG.createEnvironment("ping_pong_platform.xml")
84         MSG.deployApplication("ping_pong_deployment.xml")
85 end
86
87 MSG.run
88 MSG.exit