Logo AND Algorithmique Numérique Distribuée

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