Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.loadAPI("Queue")
- function GetNextInboundMessage()
- return Queue.Dequeue(_queue)
- end
- function Initialize()
- Apnet.MSG_TYPE_SYSTEM = "system"
- Apnet.MSG_TYPE_APPLICATION = "application"
- Apnet.MSG_SUBTYPE_ABORT = "abort"
- math.randomseed(os.time() * 1000)
- _id = os.getComputerID()
- _label = os.getComputerLabel()
- readConfig()
- initModems()
- _queue = Queue.New()
- end
- function Listen()
- local exit = false
- while (exit ~= true) do
- local id, rawMessage, dist = rednet.receive()
- local message = textutils.unserialize(rawMessage)
- Queue.Enqueue(_queue, message)
- end
- end
- function NewMessage()
- local message = {}
- message.Network = _network
- message.Id = generateMessageId()
- message.RouteLength = 0
- message.Route = {}
- message.Destination = getDefaultDestination()
- return message
- end
- function Send(msg)
- addToRoute(msg)
- rednet.broadcast(textutils.serialize(msg))
- end
- function addToRoute(msg)
- msg.RouteLength = msg.RouteLength + 1
- msg.Route[msg.RouteLength] = getComputerDetails()
- end
- function generateMessageId()
- local id = ""
- id = id .. os.day() .. "_"
- id = id .. os.time() * 1000 .. "_"
- id = id .. _id .. "_"
- id = id .. math.random(1000)
- return id
- end
- function getComputerDetails()
- local details = {}
- details.ComputerId = _id
- details.ComputerLabel = _label
- details.ComputerType = _type
- return details
- end
- function getDefaultDestination()
- local destination = {}
- destination.ComputerId = "*"
- destination.ComputerLabel = "*"
- destination.ComputerType = "*"
- return destination
- end
- function initModem(side)
- if (peripheral.getType(side) == "modem") then
- rednet.open(side)
- end
- end
- function initModems()
- initModem("left")
- initModem("right")
- initModem("front")
- initModem("back")
- initModem("bottom")
- initModem("top")
- end
- function readConfig()
- local _configHandle = fs.open("Apnet.config", "r")
- _network = _configHandle.readLine()
- _type = _configHandle.readLine()
- _configHandle.close()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement