Advertisement
Guest User

Untitled

a guest
May 25th, 2015
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 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.  
  12. math.randomseed(os.time() * 1000)
  13. _id = os.getComputerID()
  14. _label = os.getComputerLabel()
  15. readConfig()
  16. initModems()
  17. _queue = Queue.New()
  18. end
  19.  
  20. function Listen()
  21. local exit = false
  22. while (exit ~= true) do
  23. local id, rawMessage, dist = rednet.receive()
  24. local message = textutils.unserialize(rawMessage)
  25. Queue.Enqueue(_queue, message)
  26. end
  27. end
  28.  
  29. function NewMessage()
  30. local message = {}
  31. message.Network = _network
  32. message.Id = generateMessageId()
  33. message.RouteLength = 0
  34. message.Route = {}
  35. message.Destination = getDefaultDestination()
  36. return message
  37. end
  38.  
  39. function Send(msg)
  40. addToRoute(msg)
  41. rednet.broadcast(textutils.serialize(msg))
  42. end
  43.  
  44. function addToRoute(msg)
  45. msg.RouteLength = msg.RouteLength + 1
  46. msg.Route[msg.RouteLength] = getComputerDetails()
  47. end
  48.  
  49. function generateMessageId()
  50. local id = ""
  51. id = id .. os.day() .. "_"
  52. id = id .. os.time() * 1000 .. "_"
  53. id = id .. _id .. "_"
  54. id = id .. math.random(1000)
  55. return id
  56. end
  57.  
  58. function getComputerDetails()
  59. local details = {}
  60. details.ComputerId = _id
  61. details.ComputerLabel = _label
  62. details.ComputerType = _type
  63. return details
  64. end
  65.  
  66. function getDefaultDestination()
  67. local destination = {}
  68. destination.ComputerId = "*"
  69. destination.ComputerLabel = "*"
  70. destination.ComputerType = "*"
  71. return destination
  72. end
  73.  
  74. function initModem(side)
  75. if (peripheral.getType(side) == "modem") then
  76. rednet.open(side)
  77. end
  78. end
  79.  
  80. function initModems()
  81. initModem("left")
  82. initModem("right")
  83. initModem("front")
  84. initModem("back")
  85. initModem("bottom")
  86. initModem("top")
  87. end
  88.  
  89. function readConfig()
  90. local _configHandle = fs.open("Apnet.config", "r")
  91. _network = _configHandle.readLine()
  92. _type = _configHandle.readLine()
  93. _configHandle.close()
  94. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement