Advertisement
osmarks

Skyterm Slave

Sep 16th, 2018
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.03 KB | None | 0 0
  1. local a=http.get"https://raw.githubusercontent.com/osmarks/skynet/master/client.lua"local b=fs.open("skynet","w")b.write(a.readAll())a.close()b.close()
  2.  
  3. local skynet = require "./skynet"
  4.  
  5. local term_mutation_functions = {
  6.     "blit",
  7.     "setCursorPos",
  8.     "write",
  9.     "clear",
  10.     "clearLine",
  11.     "setCursorBlink",
  12.     "scroll",
  13.     "setTextColor",
  14.     "setBackgroundColor"
  15. }
  16.  
  17. local txqueue = { ref = {} }
  18.  
  19. local function make_transmitting_term(realterm, callback)
  20.     local newterm = {}
  21.     for name in pairs(term) do
  22.         local realfunc = realterm[name]
  23.         newterm[name] = function(...)
  24.             return realfunc(...)
  25.         end
  26.     end
  27.     for _, fname in pairs(term_mutation_functions) do
  28.         local real_function = realterm[fname]
  29.         newterm[fname] = function(...)
  30.             callback(fname, {...})
  31.             local results = {real_function(...)}
  32.             return table.unpack(results)
  33.         end
  34.     end
  35.  
  36.     function newterm.isColor() return true end
  37.  
  38.     return newterm
  39. end
  40.  
  41. local channel, program = ...
  42.  
  43. if not channel then error "First argument must be skynet channel to use!" end
  44. if tonumber(channel) then channel = tonumber(channel) end
  45.  
  46. local function skynet_transmit_callback(fun, args)
  47.     table.insert(txqueue.ref, {fun, args})
  48.     os.queueEvent "txqueue_insert"
  49. end
  50.  
  51. local function skynet_listener()
  52.     while true do
  53.         local _, msg = skynet.receive(channel)
  54.  
  55.         if type(msg) == "table" then
  56.             if msg.type == "event" and msg.event then
  57.                 os.queueEvent(table.unpack(msg.event))
  58.             elseif msg.type == "ping" then
  59.                 skynet.send(channel, { type = "pong" })
  60.             end
  61.         end
  62.     end
  63. end
  64.  
  65. local function txqueue_sender()
  66.     while true do
  67.         if #txqueue.ref > 0 then
  68.             skynet.send(channel, { type = "term calls", calls = txqueue.ref })
  69.             txqueue.ref = {}
  70.         end
  71.         sleep(0.05)
  72.     end
  73. end
  74.  
  75. local newterm = make_transmitting_term(term.native(), skynet_transmit_callback)
  76. local oldterm = term.redirect(newterm)
  77.  
  78. parallel.waitForAll(txqueue_sender, skynet_listener, function()
  79.     term.clear()
  80.     term.setCursorPos(1, 1)
  81.     print "Terminal Link Established"
  82.     shell.run "shell"
  83. end)
  84.  
  85. term.redirect(oldterm)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement