Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.loadAPI("Queue")
- function GetComputerDetails()
- local details = {}
- details.ComputerId = _id
- details.ComputerLabel = _label
- details.ComputerType = _type
- return details
- end
- function GetNextInboundMessage()
- return Queue.Dequeue(_queue)
- end
- function Initialize(type, doForwarding)
- Apnet.MSG_TYPE_SYSTEM = "system"
- Apnet.MSG_TYPE_APPLICATION = "application"
- Apnet.MSG_SUBTYPE_ABORT = "abort"
- Apnet.MSG_SUBTYPE_PING_REQUEST = "ping"
- Apnet.MSG_SUBTYPE_PING_RESPONSE = "pingresponse"
- math.randomseed(os.time() * 1000)
- _id = os.getComputerID()
- _label = os.getComputerLabel()
- _type = type
- _doForwarding = doForwarding
- readConfig()
- initModems()
- _queue = Queue.New()
- end
- function Listen()
- local _seen = {}
- Apnet.Kill = false
- while (Apnet.Kill ~= true) do
- local id, rawMessage, dist = rednet.receive()
- local message = textutils.unserialize(rawMessage)
- if (message.Network == _network and _seen[message.Id] ~= "true" and message.Route[1].ComputerId ~= _id) then
- _seen[message.Id] = "true"
- if (_doForwarding == "true") then
- Send(message)
- end
- if (isMatchingDestination(message)) then
- if (message.MessageType == Apnet.MSG_TYPE_SYSTEM) then
- handleSystemMessage(message)
- elseif (message.MessageType == Apnet.MSG_TYPE_APPLICATION) then
- Queue.Enqueue(_queue, message)
- else
- print("Unknown message type")
- end
- end
- end
- end
- end
- function NewMessage()
- local message = {}
- message.Network = _network
- message.Id = generateMessageId()
- message.RouteLength = 0
- message.Route = {}
- message.Destination = getDefaultDestination()
- return message
- end
- function Reply(originalMsg, msg)
- msg.OriginalMessage = originalMsg
- msg.Destination.ComputerId = originalMsg.Route[1].ComputerId
- Send(msg)
- end
- function Send(msg)
- addToRoute(msg)
- rednet.broadcast(textutils.serialize(msg))
- msg.Route[msg.RouteLength] = nil
- msg.RouteLength = msg.RouteLength - 1
- 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 getDefaultDestination()
- local destination = {}
- destination.ComputerId = "*"
- destination.ComputerLabel = "*"
- destination.ComputerType = "*"
- return destination
- end
- function handleSystemMessage(msg)
- if (msg.MessageSubType == Apnet.MSG_SUBTYPE_ABORT) then
- Apnet.Kill = true
- elseif (msg.MessageSubType == Apnet.MSG_SUBTYPE_PING_REQUEST) then
- local response = NewMessage()
- response.MessageType = Apnet.MSG_TYPE_SYSTEM
- response.MessageSubType = Apnet.MSG_SUBTYPE_PING_RESPONSE
- Reply(msg, response)
- elseif (msg.MessageSubType == Apnet.MSG_SUBTYPE_PING_RESPONSE) then
- print("Ping Response received")
- print("Ping Request ID:" .. msg.OriginalMessage.Id)
- print("Ping Request Route:" .. textutils.serialize(msg.OriginalMessage.Route))
- print("Ping Response ID:" .. msg.Id)
- print("Ping Response Route:" .. textutils.serialize(msg.Route))
- print("End of ping details")
- end
- 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 isMatchingDestination(msg)
- local dest = msg.Destination
- if (dest.ComputerId ~= "*" and dest.ComputerId ~= _id) then
- return false
- elseif (dest.ComputerLabel ~= "*" and dest.ComputerLabel ~= _label) then
- return false
- elseif (dest.ComputerType ~= "*" and dest.ComputerType ~= _type) then
- return false
- else
- return true
- end
- end
- function readConfig()
- local configHandle = fs.open("Apnet.config", "r")
- _network = configHandle.readLine()
- configHandle.close()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement