Advertisement
osmarks

basenet

May 8th, 2020
2,843
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.28 KB | None | 0 0
  1. local channel = settings.get "basenet.channel" or 23032
  2. local modem = peripheral.find "modem"
  3. if not modem then error "modem required" end
  4. modem.open(channel)
  5. local name = settings.get "basenet.name" or _G.basenet_name or error "name required"
  6.  
  7. local basenet = {
  8.     listening_to = {},
  9.     tasks = {}
  10. }
  11.  
  12. function basenet.listen(fn, from)
  13.     if type(from) ~= "string" then error "from must be a string" end
  14.     basenet.listening_to[from] = true
  15.     basenet.run_task(function()
  16.         while true do
  17.             local _, ufrom, data = os.pullEvent "update"
  18.             if ufrom == from then
  19.                 fn(data)
  20.             end
  21.         end
  22.     end)
  23. end
  24.  
  25. function basenet.update(data)
  26.     modem.transmit(channel, channel, { type = "update", from = name, data = data })
  27. end
  28.  
  29. local task_ID = 0
  30. function basenet.run_task(fn, ...)
  31.     local args = {...}
  32.     task_ID = task_ID + 1
  33.     basenet.tasks[task_ID] = { coroutine = coroutine.create(fn), init_args = args, ID = task_ID }
  34.     os.queueEvent "dummy"
  35.     return task_ID
  36. end
  37.  
  38. function basenet.interval(fn, time)
  39.     if not time then error "time required" end
  40.     basenet.run_task(function()
  41.         while true do
  42.             fn()
  43.             sleep(time)
  44.         end
  45.     end)
  46. end
  47.  
  48. local function process_message(msg)
  49.     if msg.type == "update" and type(msg.from) == "string" then
  50.         if basenet.listening_to[msg.from] then
  51.             os.queueEvent("update", msg.from, msg.data)
  52.         end
  53.     end
  54. end
  55.  
  56. basenet.run_task(function()
  57.     while true do
  58.         local _, _, c, rc, msg, distance = os.pullEvent "modem_message"
  59.         if c == channel and type(msg) == "table" then
  60.             process_message(msg)
  61.         end
  62.     end
  63. end)
  64.  
  65. local function tick_task(task, evt)
  66.     if task.init_args then
  67.         local init = task.init_args
  68.         task.init_args = nil
  69.         local ok = tick_task(task, init)
  70.         if not ok then return end
  71.     end
  72.     if coroutine.status(task.coroutine) == "dead" then
  73.         basenet.tasks[task.ID] = nil
  74.     else
  75.         if task.filter == nil or task.filter == evt[1] then
  76.             local ok, result = coroutine.resume(task.coroutine, unpack(evt))
  77.             if ok then
  78.                 task.filter = result
  79.             else
  80.                 printError(("TASK %d ERROR: %s"):format(task.ID, result))
  81.                 basenet.tasks[task.ID] = nil
  82.             end
  83.             return ok
  84.         end
  85.     end
  86. end
  87.  
  88. function basenet.run()
  89.     while true do
  90.         local evt = {os.pullEvent()}
  91.         for ID, task in pairs(basenet.tasks) do
  92.             tick_task(task, evt)
  93.         end
  94.     end
  95. end
  96.  
  97. return basenet
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement