-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshell.lua
243 lines (191 loc) · 5.94 KB
/
shell.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
local print = print
KademluaShell = {}
function KademluaShell:prettyprint(what, level)
local level = level or 0
local spacing = " "
if type(what) == "number" then
print(spacing:rep(level) .. tostring(what))
elseif type(what) == "string" then
print(spacing:rep(level) .. ("%q"):format(what))
elseif type(what) == "table" then
for name, value in pairs(what) do
print(spacing:rep(level) .. tostring(name) .. " =>")
self:prettyprint(value, level + 1)
end
else
print(spacing:rep(level) .. tostring(what))
end
end
function KademluaShell:new(node)
local o = {node=node
}
setmetatable(o,self)
self.__index = self
return o
end
function KademluaShell:help(argtable)
-- first argument is just this function, kinda unix argv style.
local calledf, arg1, arg2 = unpack(argtable)
print("dont PANIC!!!")
end
function KademluaShell:findnode(id)
if (type(id) ~= "string")then
return print("! need an id: expected string: findnode <id>")
end
local id = id
if #id == 40 then
local errorfree, convid = pcall(ec.fromhex, id)
if not errorfree then
print("! need an id: could not convert from hex: findnode <id>")
return
end
id = convid
elseif #id == 20 then
-- fine
else
print("! need an id: incorrect length: findnode <id>")
return
end
local nodelist, processed, inorder = self.node:iterativefindnode(id)
for k, node in pairs(nodelist) do
print(node.addr, node.port, ec.tohex(node.id), ec.tohex(ec.xor(node.id, id)))
end
print("while looking for", ec.tohex(id))
end
function KademluaShell:gc(cmd)
local cmd = cmd or ""
print(cmd)
if cmd == "" then
local memusage = collectgarbage("count")
print(("lua memory usage: %1.3f kbytes"):format(memusage))
elseif cmd == "collect" then
local memusage = collectgarbage("count")
print(("lua memory usage before: %1.3f kbytes"):format(memusage))
collectgarbage("collect")
local memusage = collectgarbage("count")
print(("lua memory usage after: %1.3f kbytes"):format(memusage))
elseif cmd == "step" then
local memusage = collectgarbage("count")
print(("lua memory usage before: %1.3f kbytes"):format(memusage))
collectgarbage("step")
local memusage = collectgarbage("count")
print(("lua memory usage after: %1.3f kbytes"):format(memusage))
end
end
function KademluaShell:routing(argtable)
self.node.routingtable:print()
end
function KademluaShell:add(id, what)
local id = id
if (type(id) ~= "string")then
return print("! need an id: expected string: add <id> <what>")
end
if #id == 40 then
local errorfree, convid = pcall(ec.fromhex, id)
if not errorfree then
print("! need an id: could not convert from hex: add <id> <what>")
return
end
id = convid
elseif #id == 20 then
-- fine
else
print("! need an id: incorrect length: findnode <id> <what>")
return
end
self.node:iterativeadd(id, what)
end
function KademluaShell:findvalue(id, maxnodes, maxentriespernode)
local id = id
if (type(id) ~= "string")then
return print("! need an id: expected string: findvalue <id> (maxnodes) (maxentriespernode)")
end
if #id == 40 then
local errorfree, convid = pcall(ec.fromhex, id)
if not errorfree then
print("! need an id: could not convert from hex: findvalue <id> (maxnodes) (maxentriespernode)")
return
end
id = convid
elseif #id == 20 then
-- fine
else
print("! need an id: incorrect length: findvalue <id> (maxnodes) (maxentriespernode)")
return
end
local maxnodes = maxnodes or 3
if type(maxnodes) ~= "number" or maxnodes <= 0 then
print("maxnodes needs to be a valid number")
return
end
local maxentriespernode = maxentriespernode or 6
if type(maxentriespernode) ~= "number" or maxentriespernode <= 0 then
print("maxentriespernode needs to be a valid number")
return
end
local startt = ec.time()
local ret = self.node:iterativefindvalue(id, maxnodes, maxentriespernode)
local endt = ec.time()
ret.n = nil
for i, what in pairs(ret) do
if type(what) == "table" then
print(what.from.addr .. ":" .. what.from.port .. ": " .. tostring(what.retval))
local retval = what.retval
if type(retval) == "table" then
self:prettyprint(retval)
end
else
print(what.from.addr .. ":" .. what.from.port .. ": " .. tostring(what))
end
end
print(("in %4.2f ms"):format((endt - startt) * 1000))
return ret
end
function KademluaShell:main()
local errorfree, errmsg = sregisterforevent("stdin")
print("KademluaShell: register:", errorfree, errmsg)
if not errorfree then
error("KademluaShell could not register for stdin event")
end
local function wrap(fn)
return function(...)
return fn(self, unpack(arg))
end
end
local names = {["help"]=wrap(self.help),
["findnode"]=wrap(self.findnode),
["gc"]=wrap(self.gc),
["sha1"]=ec.sha1,
["routing"]=wrap(self.routing),
["print"]=print,
pprint=wrap(self.prettyprint),
add=wrap(self.add),
findvalue=wrap(self.findvalue),
xor=ec.xor,
tohex=ec.tohex,
fromhex=ec.fromhex,
getbucketno=ec.getbucketno}
local env = {}
for key,value in pairs(names) do
env[key] = value
end
while true do
local numnodes = self.node.routingtable:numnodes()
local totnumnodes = 20 * 2 ^ (numnodes / 20)
io.write(("%i~%i | %4.0f > "):format(numnodes,
totnumnodes,
collectgarbage("count")))
local event = swaitforevent("stdin")
if event and event.raw then
local chunk = loadstring(event.raw)
if chunk then
setfenv(chunk, env)
scall(chunk)
else
print("! syntax error")
end
else
print("errno, errormessage", event.errno, event.errormessage)
end
end
end