Advertisement
Guest User

Apnet

a guest
May 26th, 2015
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.92 KB | None | 0 0
  1. os.loadAPI("Queue")
  2.  
  3. function GetComputerDetails()
  4.  local details = {}
  5.  details.ComputerId = _id
  6.  details.ComputerLabel = _label
  7.  details.ComputerType = _type
  8.  return details
  9. end
  10.  
  11. function GetNextInboundMessage()
  12.  return Queue.Dequeue(_queue)
  13. end
  14.  
  15. function Initialize(type, doForwarding)
  16.  Apnet.MSG_TYPE_SYSTEM = "system"
  17.  Apnet.MSG_TYPE_APPLICATION = "application"
  18.  Apnet.MSG_SUBTYPE_ABORT = "abort"
  19.  Apnet.MSG_SUBTYPE_PING_REQUEST = "ping"
  20.  Apnet.MSG_SUBTYPE_PING_RESPONSE = "pingresponse"
  21.  
  22.  math.randomseed(os.time() * 1000)
  23.  _id = os.getComputerID()
  24.  _label = os.getComputerLabel()
  25.  _type = type
  26.  _doForwarding = doForwarding
  27.  readConfig()
  28.  initModems()
  29.  _queue = Queue.New()
  30. end
  31.  
  32. function Listen()
  33.  local _seen = {}
  34.  
  35.  Apnet.Kill = false
  36.  while (Apnet.Kill ~= true) do
  37.   local id, rawMessage, dist = rednet.receive()
  38.   local message = textutils.unserialize(rawMessage)
  39.   if (message.Network == _network and _seen[message.Id] ~= "true" and message.Route[1].ComputerId ~= _id) then
  40.    _seen[message.Id] = "true"
  41.    if (_doForwarding == "true") then
  42.     Send(message)
  43.    end
  44.    if (isMatchingDestination(message)) then
  45.     if (message.MessageType == Apnet.MSG_TYPE_SYSTEM) then
  46.      handleSystemMessage(message)
  47.     elseif (message.MessageType == Apnet.MSG_TYPE_APPLICATION) then
  48.      Queue.Enqueue(_queue, message)
  49.     else
  50.      print("Unknown message type")
  51.     end
  52.    end
  53.   end
  54.  end
  55. end
  56.  
  57. function NewMessage()
  58.  local message = {}
  59.  message.Network = _network
  60.  message.Id = generateMessageId()
  61.  message.RouteLength = 0
  62.  message.Route = {}
  63.  message.Destination = getDefaultDestination()
  64.  return message
  65. end
  66.  
  67. function Reply(originalMsg, msg)
  68.  msg.OriginalMessage = originalMsg
  69.  msg.Destination.ComputerId = originalMsg.Route[1].ComputerId
  70.  Send(msg)
  71. end
  72.  
  73. function Send(msg)
  74.  addToRoute(msg)
  75.  rednet.broadcast(textutils.serialize(msg))
  76.  msg.Route[msg.RouteLength] = nil
  77.  msg.RouteLength = msg.RouteLength - 1
  78. end
  79.  
  80. function addToRoute(msg)
  81.  msg.RouteLength = msg.RouteLength + 1
  82.  msg.Route[msg.RouteLength] = GetComputerDetails()
  83. end
  84.  
  85. function generateMessageId()
  86.  local id = ""
  87.  id = id .. os.day() .. "_"
  88.  id = id .. os.time() * 1000 .. "_"
  89.  id = id .. _id .. "_"
  90.  id = id .. math.random(1000)
  91.  return id
  92. end
  93.  
  94. function getDefaultDestination()
  95.  local destination = {}
  96.  destination.ComputerId = "*"
  97.  destination.ComputerLabel = "*"
  98.  destination.ComputerType = "*"
  99.  return destination
  100. end
  101.  
  102. function handleSystemMessage(msg)
  103.  if (msg.MessageSubType == Apnet.MSG_SUBTYPE_ABORT) then
  104.   Apnet.Kill = true
  105.  elseif (msg.MessageSubType == Apnet.MSG_SUBTYPE_PING_REQUEST) then
  106.   local response = NewMessage()
  107.   response.MessageType = Apnet.MSG_TYPE_SYSTEM
  108.   response.MessageSubType = Apnet.MSG_SUBTYPE_PING_RESPONSE
  109.   Reply(msg, response)
  110.  elseif (msg.MessageSubType == Apnet.MSG_SUBTYPE_PING_RESPONSE) then
  111.   print("Ping Response received")
  112.   print("Ping Request ID:" .. msg.OriginalMessage.Id)
  113.   print("Ping Request Route:" .. textutils.serialize(msg.OriginalMessage.Route))
  114.   print("Ping Response ID:" .. msg.Id)
  115.   print("Ping Response Route:" .. textutils.serialize(msg.Route))
  116.   print("End of ping details")
  117.  end
  118. end
  119.  
  120. function initModem(side)
  121.  if (peripheral.getType(side) == "modem") then
  122.   rednet.open(side)
  123.  end
  124. end
  125.  
  126. function initModems()
  127.  initModem("left")
  128.  initModem("right")
  129.  initModem("front")
  130.  initModem("back")
  131.  initModem("bottom")
  132.  initModem("top")  
  133. end
  134.  
  135. function isMatchingDestination(msg)
  136.  local dest = msg.Destination
  137.  if (dest.ComputerId ~= "*" and dest.ComputerId ~= _id) then
  138.   return false
  139.  elseif (dest.ComputerLabel ~= "*" and dest.ComputerLabel ~= _label) then
  140.   return false
  141.  elseif (dest.ComputerType ~= "*" and dest.ComputerType ~= _type) then
  142.   return false
  143.  else
  144.   return true
  145.  end
  146. end
  147.  
  148. function readConfig()
  149.  local configHandle = fs.open("Apnet.config", "r")
  150.  _network = configHandle.readLine()
  151.  configHandle.close()
  152. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement