Advertisement
blunty666

remoteExecuteClient

Mar 26th, 2015
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.27 KB | None | 0 0
  1. local serverID = false
  2. local remoteExecuteProtocol = "remoteExecute"
  3.  
  4. local function findFunction(functionTable)
  5.     if type(functionTable) == "table" then
  6.         local func = _G
  7.         for index, tableKey in ipairs(functionTable) do
  8.             if type(func) == "table" and func[tableKey] ~= nil then
  9.                 func = func[tableKey]
  10.             else
  11.                 return false, index
  12.             end
  13.         end
  14.         if type(func) == "function" then
  15.             return func
  16.         else
  17.             return false, -1
  18.         end
  19.     else
  20.         return false, 0
  21.     end
  22. end
  23.  
  24. local forwardEvent = {
  25.     modem_message = true,
  26.     rednet_message = true,
  27.     peripheral = true,
  28.     peripheral_detach = true,
  29. }
  30.  
  31. local currCommandNum = false
  32. local function run()
  33.     local event, forward
  34.     while serverID do
  35.         event, forward = {coroutine.yield()}, true
  36.         if event[1] == "rednet_message" then
  37.             local id, message, protocol = event[2], event[3], event[4]
  38.             if id == serverID and protocol == remoteExecuteProtocol and type(message) == "table" then
  39.                 forward = false
  40.                 if message.commandNum then
  41.                     currCommandNum = message.commandNum
  42.                     local func, index = findFunction(message.func)
  43.                     if func then
  44.                         local result = {func(unpack(message.args))}
  45.                         local reply = {
  46.                             commandResponse = true,
  47.                             commandNum = currCommandNum,
  48.                             success = true,
  49.                             result = result,
  50.                         }
  51.                         rednet.send(serverID, reply, remoteExecuteProtocol)
  52.                     else
  53.                         local reply = {
  54.                             commandResponse = true,
  55.                             commandNum = currCommandNum,
  56.                             success = false,
  57.                             result = {"invalid_function", index},
  58.                         }
  59.                         rednet.send(serverID, reply, remoteExecuteProtocol)
  60.                     end
  61.                     currCommandNum = false
  62.                 elseif message.connection then
  63.                     if message.connect then
  64.                         rednet.send(serverID, "connected", remoteExecuteProtocol)
  65.                     elseif message.disconnect then
  66.                         print("disconnected from server")
  67.                         serverID = false
  68.                     end
  69.                 end
  70.             end
  71.         elseif event[1] == "modem_message" and type(event[5]) == "table" and event[5].sProtocol == remoteExecuteProtocol then
  72.             forward = false
  73.         end
  74.        
  75.         if forward and forwardEvent[event[1]] then
  76.             local forwardedEvent = {
  77.                 remoteEvent = true,
  78.                 event = event,
  79.             }
  80.             rednet.send(serverID, forwardedEvent, remoteExecuteProtocol)
  81.         end
  82.        
  83.         if event[1] == "terminate" then
  84.             error("terminate")
  85.         end
  86.     end
  87. end
  88.  
  89. local found = false
  90. for _, side in ipairs(rs.getSides()) do
  91.     if peripheral.getType(side) == "modem" then
  92.         rednet.open(side)
  93.         found = true
  94.         break
  95.     end
  96. end
  97. if not found then
  98.     error("no modem found")
  99. end
  100.  
  101. while true do
  102.  
  103.     while not serverID do
  104.         local id, message, protocol = rednet.receive(remoteExecuteProtocol)
  105.         if type(message) == "table" and message.connection and message.connect then
  106.             serverID = id
  107.             rednet.send(serverID, "connected", remoteExecuteProtocol)
  108.             print("connected to ", serverID)
  109.         end
  110.     end
  111.  
  112.     --main loop
  113.     while serverID do
  114.         local ok, err = pcall(run)
  115.         if not ok then
  116.             if serverID then
  117.                 if currCommandNum then
  118.                     local reply = {
  119.                         commandNum = currCommandNum,
  120.                         success = false,
  121.                         result = {"runtime_error", err},
  122.                     }
  123.                     rednet.send(serverID, reply, remoteExecuteProtocol)
  124.                 end
  125.             end
  126.             print("errored with: ", err)
  127.             if string.find(err, "terminate") then
  128.                 return
  129.             end
  130.         end
  131.     end
  132.  
  133. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement