Thunder7102

networkAPI

Jun 10th, 2014
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.74 KB | None | 0 0
  1. --[[
  2.     Network API is built for the transfer of multiple
  3.     parameters without the need to splice sentences
  4.     individually.
  5.  
  6.     Additionally, it also incorporates a SYN-ACK setup
  7.     to ensure messages are sent and received appropriately.
  8.     ]]
  9.  
  10. version = 0.0.1
  11.  
  12. function listen(timeOut)
  13. --Waits to receive a signal, if it gets an init
  14.     --it opens a connection, receives all sending vars
  15.     --and ensures what it got was all that was meant to be
  16.     --sent
  17.     --Returns whats it gets if it gets everything, otherwise, nil
  18.     local computerID, message, distance = rednet.receive(timeOut)
  19.     local connectionID
  20.     local receiveTable = {}
  21.  
  22.     --Checks to ensure all messages were received
  23.     local receivedMessages = 0
  24.     local toReceiveMessage = 0
  25.    
  26.     --Looking for if the init-starter is received
  27.     if message == "init" then
  28.         rednet.send(computerID, "ack")
  29.         computerID, message, distance = rednet.receive(.5)
  30.  
  31.         --Checks for number of args to be sent to finalize the connection
  32.         if tonumber(message) ~= nil then
  33.             rednet.send(computerID, "ack")
  34.             toReceiveMessage = tonumber(message)
  35.             connectionID = computerID
  36.             --Here is where we start pulling all the values which need to be received.
  37.             for i=0, toReceiveMessage do
  38.                 computerID, message, distance = nil
  39.  
  40.                 computerID, message, distance = rednet.receive(.5)
  41.                 if connectionID == computerID and message ~= nil then
  42.                     rednet.send(connectionID, "ack")
  43.                     receiveTable[i+1] = message
  44.                     receivedMessages = receivedMessages+1
  45.                 end
  46.             end
  47.         end
  48.     end
  49.  
  50.     --Ensures everything was gotten
  51.     if toReceiveMessage == receivedMessages then
  52.         return unpack(receiveTable)
  53.     end
  54. end
  55.  
  56.  
  57. function send(computerID, ...)
  58.     --Sends any number of parameters as individual strings.
  59.     --Will be received in order
  60.     local isSuccess = false
  61.     local args = {...}
  62.     local packetsSent = 0
  63.  
  64.     if sendAck(computerID, "init") and sendAck(computerID, #args) then
  65.         --Loop through all outputs and send them, one at a time, counting
  66.         --that they go through
  67.         for i=0, #args do
  68.             if sendAck(computerID, args[i+1]) then
  69.                 packetsSent = packetsSent+1
  70.             end
  71.             sleep(.1)
  72.         end
  73.     else
  74.         return false
  75.     end
  76.  
  77.     --Ensure everything went through
  78.     if packetsSent == #args then
  79.         return true
  80.     else
  81.         return false
  82.     end
  83. end
  84.  
  85. --=========Utility Stuff Down Here==========
  86.  
  87. function sendAck(computerID, message)
  88.     --Sends a single message to the computer,
  89.     --waits for a response, if failed, it tries 2
  90.     --more times. Returns true or false.
  91.  
  92.     local isSuccess = false
  93.     for i=0, 3 do
  94.         rednet.send(computerID, message)
  95.  
  96.         id, message, distance = rednet.receive(.5)
  97.         if id == computerID and message == "ack" then
  98.             isSuccess = true
  99.             break
  100.         end
  101.     end
  102.  
  103.     if isSuccess then return true
  104.     else return false end
  105. end
Advertisement
Add Comment
Please, Sign In to add comment