Bjornir90

Server.lua

Nov 13th, 2012
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.68 KB | None | 0 0
  1. --This is only a non-tested version, so easy-to-fix bugs may occurs
  2.  
  3. function createConfig()
  4.  if fs.exists("/serverFolder/server.config") then
  5.   return "nothing"
  6.  else
  7.  print("Thanks you for download this rednet protocol")
  8.  os.sleep(2)
  9.  shell.run("clear")
  10.  print("Enter the side of your modem")
  11.  local modemSide = io.read()
  12.  shell.run("clear")
  13.  print("Enter the id of this computer")
  14.  local idServ = io.read()
  15.  shell.run("clear")
  16.  print("You have finished the configuration of your program")
  17.  fs.makeDir("serverFolder")
  18.  fs.makeDir("usermade")
  19.  local conf = io.open("/serverFolder/server.config", "w")
  20.  local data = {side = modemSide, id = idServ}
  21.  local dataS = textutils.serialize(data)
  22.  local writeIsFail = conf:write(dataS)
  23.  conf:close()
  24.   print("Config succesfully written")
  25. end
  26. end
  27.  
  28. function loadConfig()
  29.  shell.run("clear")
  30.  if fs.exists("/serverFolder/server.config") then
  31.   local conf = fs.open("/serverFolder/server.config", "r")
  32.   local dataS = conf.readAll()
  33.   conf.close()
  34.   local data = textutils.unserialize(dataS)
  35.   local side = data.side
  36.   local id = data.id
  37.   rednet.open(side)
  38.   return id
  39.  else
  40.   print("Cannot find the config file, please restart this program")
  41.   os.sleep(3)
  42.   return
  43.  end
  44. end
  45.  
  46. function server()
  47. while exit == 0  do
  48.  id, idToSend = rednet.receive()
  49.  print("Id of the addressee received")
  50.  print("The id is :")
  51.  write(idToSend)
  52.  print(" ")
  53.  msgVerification = 0
  54.  idToSend1 = tonumber(idToSend)
  55.  if idToSend1 == id then
  56.   print("Message for the server received")
  57.   id, msg = rednet.receive()
  58.   if msg == "p" then
  59.    id, msg1 = rednet.receive()
  60.    id, msg2 = rednet.receive()
  61.    shell.run(msg1, msg2)
  62.   elseif msg == "f" then
  63.    id, msgDo = rednet.receive()
  64.    id, msg1 = rednet.receive()
  65.    if msgDo == "e" then
  66.    loadstring(msg1) ()
  67.    elseif msgDo == "s" then
  68.    msgName = rednet.receive()
  69.    newFile = io.open("/usermade/"..msgName, "w")
  70.    newFile:write(msg1)
  71.    newFile:close()
  72.    end
  73.  
  74.   elseif msg == "exit" then
  75.    shell.run("clear")
  76.    rednet.close("right")
  77.    return
  78.   end
  79.  else
  80.   id, msgType = rednet.receive()
  81.   id, msgCommande = rednet.receive()
  82.   print("Message received")
  83.   while msgVerification == 0 do
  84.    rednet.send(idToSend1, msgType)
  85.    rednet.send(idToSend1, msgCommande)
  86.    if msgType == "p" then
  87.     id, msgArg = rednet.receive()
  88.     rednet.send(idToSend1, msgArg)
  89.    end
  90.    id, msgVerification = rednet.receive(5)
  91.   end
  92.  print("Message send succesfully")
  93.  end
  94. end
  95. end
  96.  
  97. msgVerification = 0
  98. exit = 0
  99. shell.run("clear")
  100. shell.run("id")
  101. createConfig()
  102. id = tonumber(loadConfig())
  103. server()
Advertisement
Add Comment
Please, Sign In to add comment