forked from cloudwu/hive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcell.lua
397 lines (348 loc) · 8.2 KB
/
cell.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
local c = require "cell.c"
local csocket = require "cell.c.socket"
local coroutine = coroutine
local assert = assert
local select = select
local table = table
local next = next
local pairs = pairs
local type = type
local session = 0
local port = {}
local task_coroutine = {}
local task_session = {}
local task_source = {}
local command = {}
local message = {}
local cell = {}
local self = c.self
local system = c.system
cell.self = self
local event_q1 = {}
local event_q2 = {}
local function new_task(source, session, co, event)
task_coroutine[event] = co
task_session[event] = session
task_source[event] = source
end
function cell.fork(f)
local co = coroutine.create(function() f() return "EXIT" end)
session = session + 1
new_task(nil, nil, co, session)
cell.wakeup(session)
end
function cell.timeout(ti, f)
local co = coroutine.create(function() f() return "EXIT" end)
session = session + 1
c.send(system, 2, self, session, "timeout", ti)
new_task(nil, nil, co, session)
end
function cell.sleep(ti)
session = session + 1
c.send(system, 2, self, session, "timeout", ti)
coroutine.yield("WAIT", session)
end
function cell.wakeup(event)
table.insert(event_q1, event)
end
function cell.event()
session = session + 1
return session
end
function cell.wait(event)
coroutine.yield("WAIT", event)
end
function cell.call(addr, ...)
-- command
session = session + 1
c.send(addr, 2, cell.self, session, ...)
return select(2,assert(coroutine.yield("WAIT", session)))
end
function cell.rawcall(addr, session, ...)
c.send(addr, ...)
return select(2,assert(coroutine.yield("WAIT", session)))
end
function cell.send(addr, ...)
-- message
c.send(addr, 3, ...)
end
cell.rawsend = c.send
function cell.dispatch(p)
local id = assert(p.id)
if p.replace then
assert(port[id])
else
assert(port[id] == nil)
end
port[id] = p
end
function cell.cmd(...)
return cell.call(system, ...)
end
function cell.exit()
cell.send(system, "kill", self)
-- no return
cell.wait(cell.event())
end
function cell.command(cmdfuncs)
command = cmdfuncs
end
function cell.message(msgfuncs)
message = msgfuncs
end
local function suspend(source, session, co, ok, op, ...)
if ok then
if op == "RETURN" then
c.send(source, 1, session, true, ...)
elseif op == "EXIT" then
-- do nothing
elseif op == "WAIT" then
new_task(source, session, co, ...)
else
error ("Unknown op : ".. op)
end
elseif source then
c.send(source, 1, session, false, op)
else
print(cell.self,op)
print(debug.traceback(co))
end
end
local function resume_co(session, ...)
local co = task_coroutine[session]
if co == nil then
error ("Unknown response : " .. tostring(session))
end
local reply_session = task_session[session]
local reply_addr = task_source[session]
task_coroutine[session] = nil
task_session[session] = nil
task_source[session] = nil
suspend(reply_addr, reply_session, co, coroutine.resume(co, ...))
end
local function deliver_event()
while next(event_q1) do
event_q1, event_q2 = event_q2, event_q1
for i = 1, #event_q2 do
local ok, err = pcall(resume_co,event_q2[i])
if not ok then
print(cell.self,err)
end
event_q2[i] = nil
end
end
end
function cell.main() end
------------ sockets api ---------------
local sockets = {}
local sockets_event = {}
local sockets_arg = {}
local sockets_closed = {}
local sockets_fd = nil
local sockets_accept = {}
local socket = {}
local listen_socket = {}
local function close_msg(self)
cell.send(sockets_fd, "disconnect", self.__fd)
end
local socket_meta = {
__index = socket,
__gc = close_msg,
__tostring = function(self)
return "[socket: " .. self.__fd .. "]"
end,
}
local listen_meta = {
__index = listen_socket,
__gc = close_msg,
__tostring = function(self)
return "[socket listen: " .. self.__fd .. "]"
end,
}
--todo:
function listen_socket:disconnect()
sockets_accept[self.__fd] = nil
socket.disconnect(self)
end
function cell.connect(addr, port)
sockets_fd = sockets_fd or cell.cmd("socket")
local obj = { __fd = assert(cell.call(sockets_fd, "connect", self, addr, port), "Connect failed") }
return setmetatable(obj, socket_meta)
end
function cell.listen(port, accepter)
assert(type(accepter) == "function")
sockets_fd = sockets_fd or cell.cmd("socket")
local obj = { __fd = assert(cell.call(sockets_fd, "listen", self, port), "Listen failed") }
sockets_accept[obj.__fd] = function(fd, addr)
return accepter(fd, addr, obj)
end
return setmetatable(obj, listen_meta)
end
function cell.bind(fd)
sockets_fd = sockets_fd or cell.cmd("socket")
local obj = { __fd = fd }
return setmetatable(obj, socket_meta)
end
function socket:disconnect()
assert(sockets_fd)
local fd = self.__fd
sockets[fd] = nil
sockets_closed[fd] = true
if sockets_event[fd] then
cell.wakeup(sockets_event[fd])
end
cell.send(sockets_fd, "disconnect", fd)
end
function socket:write(msg)
local fd = self.__fd
cell.rawsend(sockets_fd, 6, fd, csocket.sendpack(msg))
end
local function socket_wait(fd, sep)
assert(sockets_event[fd] == nil)
sockets_event[fd] = cell.event()
sockets_arg[fd] = sep
cell.wait(sockets_event[fd])
end
function socket:readbytes(bytes)
local fd = self.__fd
if sockets_closed[fd] then
sockets[fd] = nil
return
end
if sockets[fd] then
local data = csocket.pop(sockets[fd], bytes)
if data then
return data
end
end
socket_wait(fd, bytes)
if sockets_closed[fd] then
sockets[fd] = nil
return
end
return csocket.pop(sockets[fd], bytes)
end
function socket:readline(sep)
local fd = self.__fd
if sockets_closed[fd] then
sockets[fd] = nil
return
end
sep = sep or "\n"
if sockets[fd] then
local line = csocket.readline(sockets[fd], sep)
if line then
return line
end
end
socket_wait(fd, sep)
if sockets_closed[fd] then
sockets[fd] = nil
return
end
return csocket.readline(sockets[fd], sep)
end
----------------------------------------
cell.dispatch {
id = 6, -- socket
dispatch = function(fd, sz, msg)
local accepter = sockets_accept[fd]
if accepter then
-- accepter: new fd (sz) , ip addr (msg)
local co = coroutine.create(function()
local forward = accepter(sz,msg) or self
cell.call(sockets_fd, "forward", sz , forward)
return "EXIT"
end)
suspend(nil, nil, co, coroutine.resume(co))
return
end
local ev = sockets_event[fd]
--sockets_event[fd] = nil
if sz == 0 then
sockets_closed[fd] = true
if ev then
cell.wakeup(ev)
sockets_event[fd] = nil
end
else
local buffer, bsz = csocket.push(sockets[fd], msg, sz)
sockets[fd] = buffer
if ev then
local arg = sockets_arg[fd]
if type(arg) == "string" then
local line = csocket.readline(buffer, arg, true)
if line then
cell.wakeup(ev)
sockets_event[fd] = nil
end
else
if bsz >= arg then
cell.wakeup(ev)
sockets_event[fd] = nil
end
end
end
end
end
}
cell.dispatch {
id = 5, -- exit
dispatch = function()
local err = tostring(self) .. " is dead"
for event,session in pairs(task_session) do
local source = task_source[event]
if source ~= self then
c.send(source, 1, session, false, err)
end
end
end
}
cell.dispatch {
id = 4, -- launch
dispatch = function(source, session, report, ...)
local op = report and "RETURN" or "EXIT"
local co = coroutine.create(function(...) return op, cell.main(...) end)
suspend(source, session, co, coroutine.resume(co,...))
end
}
cell.dispatch {
id = 3, -- message
dispatch = function(cmd, ...)
local f = message[cmd]
if f == nil then
print("Unknown message ", cmd)
else
local co = coroutine.create(function(...) return "EXIT", f(...) end)
suspend(source, session, co, coroutine.resume(co,...))
end
end
}
cell.dispatch {
id = 2, -- command
dispatch = function(source, session, cmd, ...)
local f = command[cmd]
if f == nil then
c.send(source, 1, session, false, "Unknown command " .. cmd)
else
local co = coroutine.create(function(...) return "RETURN", f(...) end)
suspend(source, session, co, coroutine.resume(co, ...))
end
end
}
cell.dispatch {
id = 1, -- response
dispatch = function (session, ...)
resume_co(session,...)
end,
}
c.dispatch(function(p,...)
local pp = port[p]
if pp == nil then
deliver_event()
error ("Unknown port : ".. p)
end
pp.dispatch(...)
deliver_event()
end)
return cell