aboutsummaryrefslogtreecommitdiffstats
path: root/alv/copilot/udp.moon
blob: 7babd044961a5bda2584710eea414a08f200c558 (plain)
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
----
-- Editor Integration UDP Server
--
-- @classmod UDPServer
import udp from require 'socket'
import encode, decode from require 'dkjson'
import fn_invoke, op_invoke from require 'alv.invoke'

encode_res = =>
  return unless @

  {
    metatype: @metatype
    value: @value
    updated: @updated
    type: tostring @type
  }

encode_opts = {
  exception: (reason, value, state, msg) ->
    if reason == "unsupported type"
      return "null"

    nil, msg
}

class UDPServer
  new: (@copilot) =>
    @sock = udp!
    @sock\settimeout 0
    assert @sock\setsockname '0.0.0.0', 37123

  tick: =>
    while true
      msg, ip, port = @sock\receivefrom!
      break unless msg

      client = { :ip, :port }
      res = if msg = decode msg
        @handle msg, client
      else
        error: 'invalid message'
      data = encode res, encode_opts
      @sock\sendto data, ip, port

  handle: (msg, client) =>
    res = { id: msg.id }
    switch msg.type
      when 'tick'
        res.tick = @copilot.T
      when 'modules'
        res.modules = [name for name in pairs @copilot.last_modules]
      when 'info'
        res.tag = msg.tag
        res.module = msg.module or '__root'
        mod = @copilot.last_modules[res.module]
        builtin = mod.registry\last msg.tag
        if builtin and builtin.__class.__name ~= 'DummyReg'
          res.head_meta = builtin.head.meta
          res.result = encode_res builtin.node.result
          res.vis = builtin\vis!
          res.kind = switch builtin.__class
            when op_invoke then 'op'
            when fn_invoke then 'fn'
            else 'builtin'
        else
          res.error = 'not_registered'
      else
        res.error = 'unknown_type'
    return res

{
  :UDPServer
}