daily pastebin goal
60%
SHARE
TWEET

Untitled

a guest May 17th, 2018 117 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local initd = false;
  2. local api = {
  3.     meta = {
  4.         connect = {}
  5.     },
  6.     commands = {},
  7. }
  8.  
  9. function api.init (channel, protocol, side)
  10.     api.channel = channel
  11.     api.hostname = os.getComputerLabel() or ("computer_" .. math.random(1, 2^31-1));
  12.     api.protocol = protocol;
  13.     api.modem = peripheral.wrap(side);
  14.     api.modem.open(channel);
  15.     initd = true;
  16. end
  17.  
  18. function api.start_listener ()
  19.     assert(initd, "init needs to be called before starting listener")
  20.     coroutine.resume(coroutine.create(function()
  21.         while 1 do
  22.             local event, modemSide, senderChannel,
  23.                 replyChannel, message, senderDistance = os.pullEvent("modem_message")
  24.                
  25.             api.proccess_message(modemSide, senderChannel,
  26.                 replyChannel, message, senderDistance);
  27.         end
  28.     end))
  29. end
  30. function api.send_message (recipient, channel, command, content)
  31.     api.modem.transmit(channel, 1, {protocol=api.protocol, recipient=recipient,
  32.         from=api.hostname, command=command, content=content})
  33. end
  34.  
  35. function api.connect (remotehost, channel)
  36.     return setmetatable({
  37.         protocol = api.protocol,
  38.         remotehost = recipient,
  39.     }, {__index == api.meta.connect});
  40.     --send_message(remotehost, channel, "API_CONNECT",
  41. end
  42.  
  43. function api.msg_address (message, replyChannel)
  44.     return message.from .. "@" .. api.protocol .. "#" .. replyChannel;
  45. end
  46. ----------------------------
  47. -- connect object methods --
  48. function api.meta.connect:message(command, content)
  49.     send_message(self.remotehost, self.channel, command, content)
  50. end
  51.  
  52. function api.meta.connect:print (msg)
  53.     self:message("PRINT", msg);
  54. end
  55.  
  56.  
  57. function api.meta.connect:loadstring (script)
  58.     self:message("LOADSTRING", script)
  59. end
  60. -----------------------
  61. -- message processor --
  62.  
  63. function api.proccess_message (modemSide, senderChannel,
  64.         replyChannel, message, senderDistance)
  65.        
  66.     if type(message) == "table" and message.protocol == api.protocol then
  67.         if message.recipient == api.hostname then
  68.             if message.from ~= nil then
  69.                 local com = api.commands[message.command]
  70.                 if com then com(modemSide, senderChannel, replyChannel, message, senderDistance) end
  71.             else
  72.                 print("ERR_MALFORMED_MSG: Missing 'from' in message from unknown host on channel #" .. replyChannel .. ".");
  73.             end
  74.         else
  75.             print("INFO_NOT_RECIPIENT: message recieved, different recipient specified, ignoring message");
  76.         end
  77.     else
  78.         print("ERR_MALFORMED_MSG: Unrecognized message format");
  79.     end
  80.    
  81. end
  82.  
  83.  
  84. -----------------------------
  85. -- message commands action --
  86.  
  87. function api.commands.LOADSTRING (modemSide, senderChannel, replyChannel, message, senderDistance)
  88.     if type(message.content) == "string" then
  89.         local success, result = pcall(loadstring, message.content);
  90.         if success then
  91.             setfenv(result, getfenv())
  92.             local call_success, call_result = pcall(result)
  93.             assert(call_success, "ERR_LOADSTRING_CALL: error occured when calling content from " .. api.msg_address(message, replyChannel) .. " :: " .. call_result);
  94.         else
  95.             print("ERR_LOADSTRING: failed to perform loadstring on content from " .. api.msg_address(message, replyChannel) .. " :: " .. result);
  96.         end
  97.     else
  98.         print("ERR_MALFORMED_CMD: expected content to be of type string, " .. api.msg_address(message, replyChannel) .. " sent content of type " .. type(message.content));
  99.     end
  100. end
  101.  
  102. function api.commands.PRINT (modemSide, senderChannel, replyChannel, message, senderDistance)
  103.     local suc, res = pcall(tostring, message.content)
  104.     if not suc then
  105.         print("ERR_PRINT: could not convert content to string, from " .. api.msg_address(message, replyChannel))
  106.     else
  107.         print(api.msg_address(message, replyChannel) .. ": " .. res)
  108.     end
  109. end
  110.  
  111.  
  112.  
  113.  
  114. return api
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top