-
Notifications
You must be signed in to change notification settings - Fork 7
/
riddle.lua
executable file
·169 lines (156 loc) · 4.4 KB
/
riddle.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/local/bin/luajit -i
local ok = pcall(dofile, 'include.lua')
if not ok then pcall(dofile, '../include.lua') end
print('Robot', Config.net.robot.wired)
-- Important libraries in the global space
mp = require'msgpack.MessagePack'
si = require'simple_ipc'
local libs = {
'Config',
'util',
'vector',
'torch',
'unix'
}
-- Load the libraries
for _,lib in ipairs(libs) do
local ok, lib_tbl = pcall(require, lib)
if ok then
_G[lib] = lib_tbl
else
print("Failed to load", lib)
end
end
-- Track the network usage
local sent_usage = {
fsm = {},
shm = {},
}
local recv_usage = {
fsm = {},
shm = {},
}
function net()
for name, usage in pairs(sent_usage) do
local sum = 0
for i, use in ipairs(usage) do
local t_use, bytes_used = unpack(use)
sum = sum + bytes_used
end
print(string.format('%s sent %d bytes', name, sum))
end
for name, usage in pairs(recv_usage) do
local sum = 0
for i, use in ipairs(usage) do
local t_use, bytes_used = unpack(use)
sum = sum + bytes_used
end
print(string.format('%s received %d bytes', name, sum))
end
end
local rpc_req = si.new_requester(Config.net.rpc.tcp_reply, Config.net.robot.wired)
local rpc_udp = si.new_sender(Config.net.robot.wired, Config.net.rpc.udp)
-- FSM communicationg
fsm_chs = {}
local fsm_send = function(t, evt)
local msg = mp.pack({fsm=t.fsm,evt=evt})
table.insert(sent_usage.fsm, {unix.time(), #msg})
rpc_req:send(msg)
local data = unpack(rpc_req:receive())
table.insert(recv_usage.fsm, {unix.time(), #data})
local result = mp.unpack(data)
return result
end
for sm, en in pairs(Config.fsm.enabled) do
local fsm_name = sm..'FSM'
table.insert(fsm_chs, fsm_name)
_G[sm:lower()..'_ch'] = en and {fsm=sm, send=fsm_send} or si.new_dummy()
end
-- Shared memory
local shm_send = function(t, func)
local seg, key = func:match('_(%a+)_(%g+)')
local tbl = {shm = t.shm, seg = seg, key = key}
return function(val)
tbl.val = val
local msg = mp.pack(tbl)
table.insert(sent_usage.shm, {unix.time(), #msg})
local ret = rpc_req:send(msg)
local data = unpack(rpc_req:receive())
table.insert(recv_usage.shm, {unix.time(), #data})
local result = mp.unpack(data)
return type(result)=='table' and vector.new(result) or result
end
end
-- This should overwrite memory calls...
local listing = unix.readdir(HOME..'/Memory')
local shm_vars = {}
for _,mem in ipairs(listing) do
local found, found_end = mem:find'cm'
if found then
local name = mem:sub(1,found_end)
table.insert(shm_vars, name)
_G[name] = setmetatable({shm=name},{__index=shm_send})
end
end
_G.dcm = setmetatable({shm='dcm'},{__index=shm_send})
_G.Body = setmetatable({},{
__index = function(t, k)
if k:find'get' then
return function()
local cmd = 'Body.'..k..'()'
print('Sending',cmd)
local msg = mp.pack({raw = cmd})
rpc_req:send(msg)
local data = unpack(rpc_req:receive())
if type(data)~='string' then return end
return mp.unpack(data)
end
elseif k:find'set' then
return function(v)
if type(v)~='table' then return end
local cmd = 'Body.'..k..'('..tostring(vector.new(v))..')'
print('Sending',cmd)
local msg = mp.pack({raw = cmd})
rpc_req:send(msg)
local data = unpack(rpc_req:receive())
if type(data)~='string' then return end
return mp.unpack(data)
end
end
end
})
function pstart(scriptname, idx)
if scriptname=='rpc' then return end
local request
if tostring(idx) then
request = string.format('pstart("%s", %d)', scriptname, idx)
else
request = string.format('pstart("%s")', scriptname)
end
local msg = mp.pack({raw = request})
rpc_req:send(msg)
local data = unpack(rpc_req:receive())
if type(data)~='string' then return end
return mp.unpack(data)
end
function pkill(scriptname, idx)
if scriptname:find'rpc' then return end
local request
if tostring(idx) then
request = string.format('pkill("%s", %d)', scriptname, idx)
else
request = string.format('pkill("%s")', scriptname)
end
local msg = mp.pack({raw = request})
rpc_req:send(msg)
local data = unpack(rpc_req:receive())
if type(data)~='string' then return end
return mp.unpack(data)
end
print(util.color('FSM Channel', 'yellow'), table.concat(fsm_chs, ' '))
print(util.color('SHM access', 'blue'), table.concat(shm_vars, ' '))
if arg and arg[-1]=='-i' and jit then
-- Interactive LuaJIT
package.path = package.path..';'..HOME..'/Tools/iluajit/?.lua'
dofile'Tools/iluajit/iluajit.lua'
end