1 # Debug it with this command:
2 # make -C ../.. && valgrind ruby MasterSlave.rb --log=ruby.thres:debug 2>&1 | less
8 #################################################
10 #################################################
12 class Sender < MSG::Process
13 # main : that function that will be executed when running simulation
14 def main(args) # args is an array containing arguments for function master
16 hardworking_friend = MSG::Host.getByName(args[0]).name
17 taskComputeSize = Float(args[1])
18 taskCommunicationSize = Float(args[2])
22 myTable <<1<<-2<<45<<67<<87<<76<<89<<56<<78<<3<<-4<<99
23 MSG::info("Hello " + hardworking_friend + " !!, Please !! I need you to help me to sort my table , Here it is :")
25 # Creates and send Task With the Table inside
26 task = MSG::Task.new("quicksort_task",taskComputeSize, taskCommunicationSize);
27 task.setData(myTable);
28 MSG::debug("Sending "+ task.name + " to " + send_mailbox + " with Comput Size " +
30 task.send(send_mailbox)
31 MSG::debug("Done Sending " + task.name + " to " + send_mailbox)
35 recv_mailbox = self.class
36 res_task = MSG::Task.receive(recv_mailbox.to_s)
37 result = res_task.data
38 MSG::info("Greate !! Thx Dude , you're my Best Friend !!")
39 MSG::info("Here is my table after a quicksort :)")
41 MSG::info("Bye !! I finished My HomeWork !! Time to Sleep :)")
47 #################################################
49 #################################################
50 class Receiver < MSG::Process
54 lazy_friend = MSG::Host.getByName(args[0]).name
55 send_mailbox = args[1]
56 recv_mailbox = self.class
58 MSG::info("Oh Not Again !! Grrr")
59 task = MSG::Task.receive(recv_mailbox.to_s)
61 quicksort(table,0,table.size-1)
63 MSG::info("Ok "+lazy_friend+ "I did it, next time try to do it on your own :)")
64 task.send(send_mailbox)
65 MSG::info("Bye lazy Friend !!")
69 def quicksort(list, p, r)
71 q = partition(list, p, r)
72 quicksort(list, p, q-1)
73 quicksort(list, q+1, r)
77 def partition(list, p, r)
83 list[i], list[j] = list[j],list[i]
86 list[i+1],list[r] = list[r],list[i+1]
93 #################################################
95 #################################################
98 MSG.createEnvironment(ARGV[0])
99 MSG.deployApplication(ARGV[1])
101 MSG.createEnvironment("quicksort_platform.xml")
102 MSG.deployApplication("quicksort_deployment.xml")
106 puts "Simulation time : " + MSG.getClock .to_s