Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot//simgrid/simgrid
[simgrid.git] / examples / lua / SimSplay / sim_splay.lua
1 require "simgrid"
2
3 -- Splay global modules
4 rpc = {}
5 log = {}
6 job = {}
7 events = {}
8 os = {}
9 start = {}
10 misc = {}
11
12 -- Splay global variables
13 job.me = {}
14 job.nodes = {}
15 job.list_type = "random"
16
17 -- Init nodes tables
18 function init_nodes()
19   for i = 1, simgrid.Host.number() do           
20     job.nodes[i] = { ip = simgrid.Host.getPropValue(simgrid.Host.at(i), "ip"),
21                      port = simgrid.Host.getPropValue(simgrid.Host.at(i), "port") }
22   end   
23 end
24
25 function init_jobs()
26   init_nodes()
27 end
28
29 -- Job methods
30 function job.me.ip()
31   return simgrid.Host.getPropValue(simgrid.Host.self(), "ip")
32 end
33
34 function job.me.port()
35   return simgrid.Host.getPropValue(simgrid.Host.self(), "port")
36 end
37
38
39 function job.position()
40   return simgrid.Host.getPropValue(simgrid.Host.self(), "position")
41 end
42
43 -- log Methods
44 function log:print(msg)
45   simgrid.info(msg);
46 end
47
48 -- rpc Methods
49 function rpc.call(node, call)
50   --init_nodes();
51   func = "empty"
52   arg = "empty"
53   mailbox = node
54
55   if type(node) == "table" then
56     mailbox = node.ip..":"..node.port
57   end
58
59   if type(call) == "table" then
60     func = call[1]
61     arg = call[2]
62   end
63   task_call = simgrid.Task.new("splay_task", 10000, 10000)
64   task_call['func_call_name'] = func
65   task_call['func_call_arg'] = arg
66   log:print("Sending Task to mailbox "..mailbox.." to call '"..func.."' with arg '"..arg.."'")
67   simgrid.Task.send(task_call, mailbox)
68
69 end
70
71 function rpc.server(port)
72   -- nothing really to do : no need to open Socket since it's a Simulation
73 end
74
75 -- event Methods
76 function events.sleep(time)
77   my_mailbox = job.me.ip()..":"..job.me.port()
78   task = simgrid.Task.recv_timeout(my_mailbox, time)
79
80   if task ~= nil then
81     -- an RPC call just woke me up
82     call_function(task['func_call_name'], task['func_call_arg'])
83   end
84 end
85
86 -- main function for each process, this is equivalent to the deployment file 
87 function events.thread(main_func)
88   dofile("platform_script.lua")
89   init_jobs()
90 end
91
92 -- OS methods
93 function os.exit()
94   simgrid.Host.destroy(simgrid.Host.self())
95 end
96
97 -- Start Methods
98 function start.loop()
99   simgrid.run()
100   --simgrid.clean()
101 end
102
103 -- Misc Methods
104 function misc.between(a, b)
105   return a
106 end
107
108 -- useful functions
109 function call_function(fct, arg)
110   _G[fct](arg)
111 end
112
113 function SPLAYschool(arg)
114   simgrid.info("Calling me..."..arg)
115 end
116