daily pastebin goal
3%
SHARE
TWEET

Untitled

a guest May 17th, 2018 96 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 .. "@" .. message.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.protocol == api.protocol then
  68.             if message.recipient == api.hostname then
  69.                 if message.from ~= nil then
  70.                     local com = api.commands[message.command]
  71.                     if com then com(modemSide, senderChannel, replyChannel, message, senderDistance) end
  72.                 else
  73.                     print("ERR_MALFORMED_MSG: Missing 'from' in message from unknown host on channel #" .. replyChannel .. ".");
  74.                 end
  75.             else
  76.                 print("INFO_NOT_RECIPIENT: message recieved, different recipient specified, ignoring message");
  77.             end
  78.         else
  79.             pcall(function()
  80.                 print("INFO_UNKNOWN_PROTOCOL: " .. api.msg_address(message, replyChannel) .. " sent a message with an unrecognized protocol; ignoring message")
  81.             end)
  82.         end
  83.     else
  84.         print("ERR_MALFORMED_MSG: Unrecognized message format");
  85.     end
  86.    
  87. end
  88.  
  89.  
  90. -----------------------------
  91. -- message commands action --
  92.  
  93. function api.commands.LOADSTRING (modemSide, senderChannel, replyChannel, message, senderDistance)
  94.     if type(message.content) == "string" then
  95.         local success, result = pcall(loadstring, message.content);
  96.         if success then
  97.             setfenv(result, getfenv())
  98.             local call_success, call_result = pcall(result)
  99.             assert(call_success, "ERR_LOADSTRING_CALL: error occured when calling content from " .. api.msg_address(message, replyChannel) .. " :: " .. call_result);
  100.         else
  101.             print("ERR_LOADSTRING: failed to perform loadstring on content from " .. api.msg_address(message, replyChannel) .. " :: " .. result);
  102.         end
  103.     else
  104.         print("ERR_MALFORMED_CMD: expected content to be of type string, " .. api.msg_address(message, replyChannel) .. " sent content of type " .. type(message.content));
  105.     end
  106. end
  107.  
  108. function api.commands.PRINT (modemSide, senderChannel, replyChannel, message, senderDistance)
  109.     local suc, res = pcall(tostring, message.content)
  110.     if not suc then
  111.         print("ERR_PRINT: could not convert content to string, from " .. api.msg_address(message, replyChannel))
  112.     else
  113.         print(api.msg_address(message, replyChannel) .. ": " .. res)
  114.     end
  115. end
  116.  
  117.  
  118.  
  119.  
  120. 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