Advertisement
Tatantyler

Database Server Client

Oct 21st, 2012
562
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.81 KB | None | 0 0
  1. local args = {...}
  2. local server = tonumber(args[1])
  3.  
  4. local oldColor = term.setTextColor
  5.  
  6. if not term.isColor or not term.isColor() then
  7.     term.setTextColor = function() return end
  8. end
  9.  
  10. local function parseReturn(id, msg)
  11.     --print(id..": "..msg)
  12.     msg = textutils.unserialize(msg)
  13.     if type(msg) == "table" then
  14.         if msg[1] == "DB" then
  15.             if msg[2] == "error" then
  16.                 term.setTextColor(colors.red)
  17.                 write("Server at ")
  18.                 term.setTextColor(colors.lightBlue)
  19.                 write(id)
  20.                 term.setTextColor(colors.red)
  21.                 write(" reports error: \"")
  22.                 term.setTextColor(colors.orange)
  23.                 print(msg[3].."\"")
  24.                 term.setTextColor(colors.white)
  25.             elseif msg[2] == "successful" then
  26.                 term.setTextColor(colors.lime)
  27.                 write("Server at ")
  28.                 term.setTextColor(colors.lightBlue)
  29.                 write(id)
  30.                 term.setTextColor(colors.lime)
  31.                 print(" reports that the operation was successful.")
  32.                 term.setTextColor(colors.white)
  33.         --[[elseif msg[2] == "clone" then
  34.                 local database = shell.dir().."/"..msg[3].."/"
  35.                 local files = msg[6]
  36.                 local allCommits = msg[5]
  37.                 local messages = msg[4]
  38.                 fs.makeDir(database.."commits/.metadata/")
  39.                 for file, commits in pairs(allCommits) do
  40.                     fs.makeDir(database.."commits/"..file)
  41.                     for commit, data in pairs(commits) do
  42.                         local fileHandle = fs.open(database.."commits/"..file.."/"..commit, "w")
  43.                         fileHandle.write(data)
  44.                         fileHandle.close()
  45.                     end
  46.                 end
  47.                 for file,v in pairs(messages) do
  48.                     fs.makeDir(database.."commits/.metadata/")
  49.                     for commit, data in ipairs(v) do
  50.                         local fileHandle = fs.open(database.."commits/.metadata/"..file.."/"..commit, "w")
  51.                         fileHandle.write(data)
  52.                         fileHandle.close()
  53.                     end
  54.                 end
  55.                 for i,v in pairs(files) do
  56.                     local fileHandle = fs.open(database..i, "w")
  57.                     fileHandle.write(v)
  58.                     fileHandle.close()
  59.                 end]]
  60.             elseif msg[2] == "file" then
  61.                 local fileHandle = fs.open(shell.dir().."/"..msg[3], "w")
  62.                 fileHandle.write(msg[4])
  63.                 fileHandle.close()
  64.             elseif msg[2] == "commit_data" then
  65.                 local fileHandle = fs.open(shell.dir().."/commits/"..msg[3].."/"..msg[4], "w")
  66.                 fileHandle.write(msg[5])
  67.                 fileHandle.close()
  68.                 if msg[6] then
  69.                     local msgHandle = fs.open(shell.dir().."/commits/.metadata/"..msg[3].."/"..msg[4], "w")
  70.                     msgHandle.write(msg[5])
  71.                     msgHandle.close()
  72.                 end
  73.             elseif msg[2] == "commits" then
  74.                 local file = msg[4]
  75.                 for i,v in ipairs(msg[5]) do
  76.                     local handle = fs.open(shell.dir().."/commits/"..file.."/"..i, "w")
  77.                     handle.write(v[1])
  78.                     handle.close()
  79.                     if v[2] then
  80.                         local msgHandle = fs.open(shell.dir().."/commits/"..file.."/"..i, "w")
  81.                         msgHandle.write(v[2])
  82.                         msgHandle.close()
  83.                     end
  84.                 end
  85.             elseif msg[2] == "files" then
  86.                 print("FILES: ")
  87.                 textutils.pagedTabulate(msg[3])
  88.             elseif msg[2] == "goodbye" then
  89.                 term.setTextColor(colors.lime)
  90.                 print("You have logged out.")
  91.                 term.setTextColor(colors.white)
  92.             end
  93.         else
  94.             return false
  95.         end
  96.         return true
  97.     else
  98.         return false
  99.     end
  100. end
  101.  
  102. local servArgs = {}
  103.  
  104. for i=2, #args do
  105.     table.insert(servArgs, args[i])
  106. end
  107.  
  108. for i,v in ipairs(rs.getSides()) do
  109.     if peripheral.getType(v) == "modem" then
  110.         rednet.open(v)
  111.     end
  112. end
  113. if servArgs[1] == "sendfile" then
  114.     local handle = fs.open(servArgs[2], "r")
  115.     local data = handle.readAll()
  116.     handle.close()
  117.     if servArgs[3] == nil then
  118.         servArgs[3] = fs.getName(servArgs[2])
  119.     end
  120.     servArgs = {"commit", servArgs[3], data, servArgs[4]}
  121. end
  122. rednet.send(server, textutils.serialize(servArgs))
  123.  
  124. local timer = os.startTimer(10)
  125. while true do
  126.     local event, id, msg = os.pullEvent()
  127.     if event == "timer" and id == timer then
  128.         printError("remote server timed out")
  129.         break
  130.     elseif event == "rednet_message" then
  131.         if parseReturn(id, msg) then
  132.             break
  133.         end
  134.     end
  135. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement