Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local messageQueue = {}
- local history = {}
- local commands = {}
- local baseUrl = "http://pastebin.com/raw.php?i="
- local serverUrl = "p13aJ0qT"
- local packetUrl = "1ABiyvM3"
- local modemUrl = "dfprGaiV"
- local filesKey = "485616548621"
- local authKey = "134587651549"
- local running = true
- --loading the API's
- os.unloadAPI("Modem")
- os.unloadAPI("Packet")
- os.loadAPI("Modem")
- os.loadAPI("Packet")
- local function toboolean(str)
- return str:lower() == "true"
- end
- --function to add commands
- local function addCommand(name,func)
- commands[name] = func
- end
- --Setting up the standard commands
- addCommand("exit",function() running = false end)
- addCommand("showQueue",function()
- for k,v in pairs(messageQueue) do
- for key,val in pairs(v) do
- print(key, " = ", val)
- sleep(.1)
- end
- print("Packet Type = ",Packet.getPacket(v.msg))
- end
- end)
- addCommand("sendMessage", function(side,ch,msg,isPacket,packetID)
- local isPacket = toboolean(isPacket)
- local packet = ""
- if(isPacket and Packet.isPacket(packetID)) then
- packet = packetID
- end
- Modem.send(ch,ch,msg,side)
- end)
- --Setting up the packets
- Packet.setNotFoundPacket("Unknow Packet")
- Packet.addPacket("AUTH:","Auth Request")
- Packet.addPacket("FILES:","Files Request")
- Packet.addPacket("_FILES_"..filesKey..":","Files Answer")
- Packet.addPacket("_AUTH_"..authKey..":","Auth Answer")
- Packet.addPacket("PING","Ping Packet")
- local function addToQueue(ch,repch,msg,side)
- messageQueue[#messageQueue+1] = {["ch"] = ch,["repch"] = repch, ["msg"] = msg,["side"] = side}
- end
- --some kind of user input to manipulate stuff
- local function waitForMessages()
- while true do
- local side,ch,repch,msg = Modem.receive({"back","bottom"},{2543},10)
- if(message[1]) then
- addToQueue(ch,repch,msg,side)
- end
- sleep(0)
- end
- end
- --parse the commands entered in the command line
- local function performCommand(cmd)
- if(cmd:match("%s") or cmd == "") then
- return
- end
- local raw = {}
- local args = {}
- for match in cmd:gmatch("[^ \t]+") do
- raw[#raw+1] = match
- end
- for i = 2,#raw do
- args[i-1] = raw[i]
- end
- local command = raw[1]
- if commands[command] == nil then
- print("Command not found!")
- return
- else
- commands[command](unpack(args))
- end
- end
- --waiting for modem messages and save them to the queue
- local function commandLine()
- while running do
- term.write("Main Computer>")
- local input = read(nil,history)
- table.insert(history, input)
- performCommand(input)
- sleep(0)
- end
- end
- --Working on the queue and sending the messages to the right computer
- local function parseQueue()
- while true do
- local queueSize = #messageQueue
- if(queueSize > 0) then
- local currentMessage = messageQueue[queueSize]
- local packetType = Packet.getPacket(currentMessage.msg)
- if(packetType == "Ping Packet") then
- Modem.send(currentMessage.repch,currentMessage.repch,"PONG",currentMessage.side)
- end
- messageQueue[queueSize] = nil
- end
- sleep(0)
- end
- end
- --running everything "at the same time"
- term.clear()
- term.setCursorPos(1,1)
- parallel.waitForAny(commandLine,waitForMessages,parseQueue)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement