Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
79c8c9d0f516141ed6f5781577e04eac16fdb4a4
[simgrid.git] / examples / lua / SimSplay / sim_splay.lua
1 require "simgrid"
2 -- Splay global modules
3 rpc = {}
4 log = {}
5 job = {}
6 events = {}
7 os = {}
8 start = {}
9 misc = {}
10 -- Splay global variables
11 job.me ={}
12 job.nodes = {}
13 job.list_type = "random"
14 --Init nodes tables
15 function init_nodes()
16         for i= 1,simgrid.Host.number() do               
17                 job.nodes[i] = simgrid.Host.getPropValue(simgrid.Host.at(i),"ip")..":"..simgrid.Host.getPropValue(simgrid.Host.at(i),"port");
18         end     
19 end
20
21 function init_jobs()
22    init_nodes()
23 end
24
25
26 -- Job methods
27 function job.me.ip()
28      return simgrid.Host.getPropValue(simgrid.Host.self(),"ip");
29 end
30
31
32 function job.me.port()
33  return simgrid.Host.getPropValue(simgrid.Host.self(),"port");
34 end
35
36
37 function job.position()
38   return simgrid.Host.getPropValue(simgrid.Host.self(),"position");
39 end
40
41 -- log Methods
42 function log:print(msg)
43   simgrid.info(msg);
44 end
45
46 -- rpc Methods
47 function rpc.call(node,call)
48  --init_nodes();
49  func = "empty"
50  arg = "empty"
51  mailbox = node
52
53  if type(node) == "table" then
54   mailbox = node.ip..":"..node.port
55  end
56
57  if type(call) == "table" then
58         func = call[1]
59         arg = call[2]
60  end
61  task_call = simgrid.Task.new("splay_task",10000,10000);
62  task_call['func_call_name'] = func;
63  task_call['func_call_arg'] = arg;
64  log:print("Sending Task to mailbox "..mailbox.." to call '"..func.."' with arg '"..arg.."'");
65  simgrid.Task.iSend(task_call,mailbox);
66  
67 end 
68
69 function rpc.server(port)
70  -- nothing really to do : no need to open Socket since it's a Simulation
71 end
72
73
74 -- event Methods
75 function events.sleep(time)
76   my_mailbox = job.me.ip()..":"..job.me.port()
77   tk = simgrid.Task.splay_recv(my_mailbox, time)
78   
79   if type(tk) == "table" then 
80         call_function(task['func_call_name'],task['func_call_arg'])
81   else log:print("task type is :"..type(tk).." it must be table?!");
82   end
83 end
84
85
86 -- main func for each process, this is equivalent to the Deploiment 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