Advertisement
Freack100

Server CommandLine

Jul 5th, 2014
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.12 KB | None | 0 0
  1. local messageQueue = {}
  2. local history = {}
  3. local commands = {}
  4.  
  5. local baseUrl = "http://pastebin.com/raw.php?i="
  6. local serverUrl = "p13aJ0qT"
  7. local packetUrl = "1ABiyvM3"
  8. local modemUrl = "dfprGaiV"
  9.  
  10. local filesKey = "485616548621"
  11. local authKey  = "134587651549"
  12.  
  13. local running = true
  14.  
  15. --loading the API's
  16. os.unloadAPI("Modem")
  17. os.unloadAPI("Packet")
  18.  
  19. os.loadAPI("Modem")
  20. os.loadAPI("Packet")
  21.  
  22. local function toboolean(str)
  23.     return str:lower() == "true"
  24. end
  25.  
  26. --function to add commands
  27. local function addCommand(name,func)
  28.     commands[name] = func
  29. end
  30.  
  31. --Setting up the standard commands
  32. addCommand("exit",function() running = false end)
  33. addCommand("showQueue",function()
  34.     for k,v in pairs(messageQueue) do
  35.         for key,val in pairs(v) do
  36.             print(key, " = ", val)
  37.             sleep(.1)
  38.         end
  39.         print("Packet Type = ",Packet.getPacket(v.msg))
  40.     end
  41. end)
  42. addCommand("sendMessage", function(side,ch,msg,isPacket,packetID)
  43.     local isPacket = toboolean(isPacket)
  44.     local packet = ""
  45.     if(isPacket and Packet.isPacket(packetID)) then
  46.         packet = packetID
  47.     end
  48.     Modem.send(ch,ch,msg,side)
  49. end)
  50.  
  51. --Setting up the packets
  52. Packet.setNotFoundPacket("Unknow Packet")
  53. Packet.addPacket("AUTH:","Auth Request")
  54. Packet.addPacket("FILES:","Files Request")
  55. Packet.addPacket("_FILES_"..filesKey..":","Files Answer")
  56. Packet.addPacket("_AUTH_"..authKey..":","Auth Answer")
  57. Packet.addPacket("PING","Ping Packet")
  58.  
  59. local function addToQueue(ch,repch,msg,side)
  60.     messageQueue[#messageQueue+1] = {["ch"] = ch,["repch"] = repch, ["msg"] = msg,["side"] = side}
  61. end
  62.  
  63.  
  64. --some kind of user input to manipulate stuff
  65. local function waitForMessages()
  66.     while true do
  67.         local side,ch,repch,msg = Modem.receive({"back","bottom"},{2543},10)
  68.         if(message[1]) then
  69.             addToQueue(ch,repch,msg,side)
  70.         end
  71.         sleep(0)
  72.     end
  73. end
  74.  
  75. --parse the commands entered in the command line
  76. local function performCommand(cmd)
  77.  
  78.     if(cmd:match("%s") or cmd == "") then
  79.         return
  80.     end
  81.  
  82.     local raw  = {}
  83.     local args = {}
  84.     for match in cmd:gmatch("[^ \t]+") do
  85.         raw[#raw+1] = match
  86.     end
  87.     for i = 2,#raw do
  88.         args[i-1] = raw[i]
  89.     end
  90.  
  91.     local command = raw[1]
  92.  
  93.     if commands[command] == nil then
  94.         print("Command not found!")
  95.         return
  96.     else
  97.         commands[command](unpack(args))
  98.     end
  99.  
  100. end
  101.  
  102. --waiting for modem messages and save them to the queue
  103. local function commandLine()
  104.     while running do
  105.  
  106.         term.write("Main Computer>")
  107.         local input = read(nil,history)
  108.         table.insert(history, input)
  109.         performCommand(input)
  110.         sleep(0)
  111.     end
  112. end
  113.  
  114.  
  115. --Working on the queue and sending the messages to the right computer
  116. local function parseQueue()
  117.     while true do
  118.         local queueSize = #messageQueue
  119.         if(queueSize > 0) then
  120.             local currentMessage = messageQueue[queueSize]
  121.             local packetType = Packet.getPacket(currentMessage.msg)
  122.             if(packetType == "Ping Packet") then
  123.               Modem.send(currentMessage.repch,currentMessage.repch,"PONG",currentMessage.side)
  124.             end
  125.             messageQueue[queueSize] = nil
  126.         end
  127.         sleep(0)
  128.     end
  129. end
  130.  
  131.  
  132. --running everything "at the same time"
  133. term.clear()
  134. term.setCursorPos(1,1)
  135. parallel.waitForAny(commandLine,waitForMessages,parseQueue)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement