Advertisement
Tomlacko

ComputerCraft Modem API

Apr 27th, 2019
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.86 KB | None | 0 0
  1. --------------------------------------------------
  2. --UTILS--
  3.  
  4. local function RandomString(length)
  5.   local result = ""
  6.   for i=1,length do
  7.     local n = math.random(0, 61)
  8.     if n < 10 then
  9.       result = result .. string.char(n + 48)
  10.     elseif n < 36 then
  11.       result = result .. string.char(n + 55)
  12.     else
  13.       result = result .. string.char(n + 61)
  14.     end
  15.   end
  16.   return result
  17. end
  18.  
  19. --------------------------------------------------
  20. --MODEM--
  21.  
  22. local Modem = {}
  23. Modem.__index = Modem
  24. function Init(side)
  25.   local m = peripheral.wrap(side)
  26.   if not m then
  27.     error("Error: Modem not found! ("..side..")")
  28.   end
  29.   local modem = {
  30.     modem=m,
  31.     side=side,
  32.     ports={}
  33.   }
  34.   setmetatable(modem, Modem)
  35.   return modem
  36. end
  37.  
  38.  
  39.  
  40.  
  41. function Modem:SimpleSend(port, message)
  42.   self.modem.transmit(port, port, message)
  43. end
  44.  
  45. function Modem:Send(port, data, signature)
  46.   local stamp
  47.   if signature then
  48.     stamp = signature
  49.   else
  50.     stamp = RandomString(8)
  51.   end
  52.  
  53.   local packet = {DATA=data, SIGNATURE=stamp}
  54.   self.modem.transmit(port, port, textutils.serialize(packet))
  55.  
  56.   return stamp
  57. end
  58.  
  59.  
  60.  
  61. function Modem:SimpleReceive(port, timeout)
  62.   local wasOpen = self.modem.isOpen(port)
  63.   if not wasOpen then
  64.     self.modem.open(port)
  65.     self.ports[port] = true
  66.   end
  67.  
  68.   local timerID
  69.   if timeout then
  70.     timerID = os.startTimer(timeout)
  71.   end
  72.  
  73.   local message
  74.  
  75.   while true do
  76.     local e, s, p, r, msg = os.pullEvent()
  77.     if e == "modem_message" and s == self.side and p == port then
  78.       message = msg
  79.       break
  80.     elseif timeout and e == "timer" and s == timerID then
  81.       message = false
  82.       break
  83.     end
  84.   end
  85.  
  86.   if not wasOpen then
  87.     self.modem.close(port)
  88.     self.ports[port] = nil
  89.   end
  90.  
  91.   return message
  92. end
  93.  
  94.  
  95. function Modem:Receive(port, signature, timeout)
  96.   local wasOpen = self.modem.isOpen(port)
  97.   if not wasOpen then
  98.     self.modem.open(port)
  99.     self.ports[port] = true
  100.   end
  101.  
  102.   local timerID
  103.   if timeout then
  104.     timerID = os.startTimer(timeout)
  105.   end
  106.  
  107.   local data, stamp
  108.  
  109.   while true do
  110.     local e, s, p, r, msg = os.pullEvent()
  111.     if e == "modem_message" and s == self.side and p == port and type(msg) == "string" then
  112.       local packet = textutils.unserialize(msg)
  113.       if packet and packet.SIGNATURE then
  114.         if signature then
  115.           if packet.SIGNATURE == signature then
  116.             data = packet.DATA
  117.             stamp = packet.SIGNATURE
  118.             break
  119.           end
  120.         else
  121.           data = packet.DATA
  122.           stamp = packet.SIGNATURE
  123.           break
  124.         end
  125.       end
  126.     elseif timeout and e == "timer" and s == timerID then
  127.       data = false
  128.       stamp = false
  129.       break
  130.     end
  131.   end
  132.  
  133.   if not wasOpen then
  134.     self.modem.close(port)
  135.     self.ports[port] = nil
  136.   end
  137.  
  138.   return data, stamp
  139. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement