1 # Debug it with this command:
2 # make -C ../.. && valgrind ruby MasterSlave.rb --log=ruby.thres:debug 2>&1 | less
9 #################################################
11 #################################################
13 class Sender < MSG::Process
14 # main : that function that will be executed when running simulation
15 def main(args) # args is an array containing arguments for function master
17 hardworking_friend = MSG::Host.getByName(args[0]).name
18 taskComputeSize = Float(args[1])
19 taskCommunicationSize = Float(args[2])
23 myTable <<1<<-2<<45<<67<<87<<76<<89<<56<<78<<3<<-4<<99
24 MSG::info("Hello " + hardworking_friend + " !!, Please !! I need you to help me to sort my table , Here it is :")
26 # Creates and send Task With the Table inside
27 task = MSG::Task.new("quicksort_task",taskComputeSize, taskCommunicationSize);
29 MSG::debug("Sending "+ task.name + " to " + send_mailbox + " with Comput Size " +
31 task.send(send_mailbox)
32 MSG::debug("Done Sending " + task.name + " to " + send_mailbox)
36 recv_mailbox = self.class
37 res_task = MSG::Task.receive(recv_mailbox.to_s)
38 result = res_task.data
39 MSG::info("Greate !! Thx !!")
40 MSG::info("Here is my table after a quicksort :)")
42 MSG::info("Bye Now :)")
48 #################################################
50 #################################################
51 class Receiver < MSG::Process
55 lazy_friend = MSG::Host.getByName(args[0]).name
56 send_mailbox = args[1]
57 recv_mailbox = self.class
58 MSG::info("Receiving Table from "+lazy_friend)
59 task = MSG::Task.receive(recv_mailbox.to_s)
61 quicksort(table,0,table.size-1)
63 MSG::info("Sort Done ... Sending Back the new table")
64 task.send(send_mailbox)
65 MSG::info("Bye lazy Boy!!")
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]
92 #################################################
94 #################################################
97 MSG.createEnvironment(ARGV[0])
98 MSG.deployApplication(ARGV[1])
100 MSG.createEnvironment("quicksort_platform.xml")
101 MSG.deployApplication("quicksort_deployment.xml")
105 puts "Simulation time : " + MSG.getClock .to_s