git.s-ol.nu alive / 254b8a6
add editor-support UDP server s-ol 1 year, 6 months ago
3 changed file(s) with 81 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
88 import RTNode from require 'alv.rtnode'
99 import Constant from require 'alv.result'
1010
11 parse_args = (args, out={}) ->
11 parse_args = (args, out={ 'udp-server': false }) ->
1212 local key
1313 for a in *args
1414 if key
3838 @last_modification = 0
3939 @last_modules = {}
4040 @open @args[1] if @args[1]
41
42 if @args['udp-server']
43 import UDPServer from require 'alv.copilot.udp'
44 @adapter = UDPServer @
4145
4246 --- members
4347 -- @section members
9397
9498 --- poll for changes and tick.
9599 tick: =>
100 @adapter\tick! if @adapter
101
96102 assert not COPILOT, "another Copilot is already running!"
97103 return unless @last_modules.__root
98104
1414
1515 class CLICopilot extends Copilot
1616 new: (arg) =>
17 super parse_args arg, { nocolor: false }
17 super parse_args arg, { nocolor: false, 'udp-server': false }
1818 assert @args[1], "no filename given"
1919
2020 run: =>
0 import udp from require 'socket'
1 import encode, decode from require 'dkjson'
2 import fn_invoke, op_invoke from require 'alv.invoke'
3
4 encode_res = (copilot) =>
5 return unless @
6
7 {
8 metatype: @metatype
9 value: @value
10 updated: @updated
11 type: tostring @type
12 }
13
14 class UDPServer
15 new: (@copilot) =>
16 @sock = udp!
17 @sock\settimeout 0
18 assert @sock\setsockname '0.0.0.0', 37123
19
20 tick: =>
21 while true
22 msg, ip, port = @sock\receivefrom!
23 break unless msg
24
25 client = { :ip, :port }
26 res = if msg = decode msg
27 @handle msg, client
28 else
29 error: 'invalid message'
30 @sock\sendto (encode res), ip, port
31
32 handle: (msg, client) =>
33 res = { id: msg.id }
34 switch msg.type
35 when 'tick'
36 res.tick = @copilot.T
37 -- when 'sub'
38 -- @subs[client] = true
39 -- res.ok = true
40 -- when 'unsub'
41 -- @subs[client] = nil
42 -- res.ok = true
43 when 'modules'
44 res.modules = [name for name in pairs @copilot.last_modules]
45 when 'info'
46 mod = @copilot.last_modules[msg.module or '__root']
47 builtin = mod.registry\last msg.tag
48 if builtin and builtin.__class.__name ~= 'DummyReg'
49 res.head_meta = builtin.head.meta
50 res.result = encode_res builtin.node.result
51 res.kind = switch builtin.__class
52 when op_invoke then 'op'
53 when fn_invoke then 'fn'
54 else 'builtin'
55 else
56 res.error = 'not_registered'
57 when 'state'
58 mod = @copilot.last_modules[msg.module or '__root']
59 builtin = mod.registry\last msg.tag
60 if builtin and builtin.__class.__name ~= 'DummyReg'
61 res.value = encode_res builtin.node.result, @copilot
62 if op = builtin.op
63 res.state = op.state
64 else
65 res.error = 'not_registered'
66 else
67 res.error = 'unknown_type'
68 return res
69
70 {
71 :UDPServer
72 }