Advertisement
HeyApplejack

Untitled

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