gknova61

Database API

Apr 4th, 2013
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.33 KB | None | 0 0
  1. dbId = 1 --Change me
  2. responseOutput = {}
  3. local function parseReturn(id, msg)
  4.     --print(id..": "..msg)
  5.     msg = textutils.unserialize(msg)
  6.     if type(msg) == "table" then
  7.         if msg[1] == "DB" then
  8.             if msg[2] == "error" then
  9.                 term.setTextColor(colors.red)
  10.                 write("Server at ")
  11.                 term.setTextColor(colors.lightBlue)
  12.                 write(id)
  13.                 term.setTextColor(colors.red)
  14.                 write(" reports error: \"")
  15.                 term.setTextColor(colors.orange)
  16.                 print(msg[3].."\"")
  17.                 term.setTextColor(colors.white)
  18.             elseif msg[2] == "successful" then
  19.                 term.setTextColor(colors.lime)
  20.                 write("Server at ")
  21.                 term.setTextColor(colors.lightBlue)
  22.                 write(id)
  23.                 term.setTextColor(colors.lime)
  24.                 print(" reports that the operation was successful.")
  25.                 term.setTextColor(colors.white)
  26.             elseif msg[2] == "file" then
  27.                 local fileHandle = fs.open(shell.dir().."/"..msg[3], "w")
  28.                 fileHandle.write(msg[4])
  29.                 fileHandle.close()
  30.             elseif msg[2] == "commit_data" then
  31.                 local fileHandle = fs.open(shell.dir().."/commits/"..msg[3].."/"..msg[4], "w")
  32.                 fileHandle.write(msg[5])
  33.                 fileHandle.close()
  34.                 if msg[6] then
  35.                     local msgHandle = fs.open(shell.dir().."/commits/.metadata/"..msg[3].."/"..msg[4], "w")
  36.                     msgHandle.write(msg[5])
  37.                     msgHandle.close()
  38.                 end
  39.             elseif msg[2] == "commits" then
  40.                 local file = msg[4]
  41.                 for i,v in ipairs(msg[5]) do
  42.                     local handle = fs.open(shell.dir().."/commits/"..file.."/"..i, "w")
  43.                     handle.write(v[1])
  44.                     handle.close()
  45.                     if v[2] then
  46.                         local msgHandle = fs.open(shell.dir().."/commits/"..file.."/"..i, "w")
  47.                         msgHandle.write(v[2])
  48.                         msgHandle.close()
  49.                     end
  50.                 end
  51.             elseif msg[2] == "files" then
  52.                 print("FILES: ")
  53.                 textutils.pagedTabulate(msg[3])
  54.             elseif msg[2] == "goodbye" then
  55.                 term.setTextColor(colors.lime)
  56.                 print("You have logged out.")
  57.                 term.setTextColor(colors.white)
  58.             end
  59.         else
  60.             return false
  61.         end
  62.         return true
  63.     else
  64.         return false
  65.     end
  66. end
  67.  
  68. function login(user,pass)
  69.     rednet.send(dbId,textutils.serialize({"login",user,pass}))
  70.  
  71.     local timer = os.startTimer(10)
  72.     while true do
  73.         local event, id, msg = os.pullEvent()
  74.         if event == "timer" and id == timer then
  75.             printError("remote server timed out")
  76.             break
  77.         elseif event == "rednet_message" then
  78.             if parseReturn(id, msg) then
  79.                 break
  80.             end
  81.         end
  82.     end
  83. end
  84.  
  85. function sendFile(localFile,commit)
  86.     if commit == nil then
  87.         shell.run("client "..tostring(dbId).." sendfile "..localFile)
  88.     else
  89.         shell.run("client "..tostring(dbId).." sendfile "..localFile.." "..commit)
  90.     end
  91. end
  92.  
  93. function writeData(file,data,commit)
  94.     if commit == nil then
  95.         shell.run("client "..tostring(dbId).." commit "..file.." "..data)
  96.     else
  97.         shell.run("client "..tostring(dbId).." commit "..file.." "..data.." "..commit)
  98.     end
  99. end
  100.  
  101. function list()
  102.     shell.run("client "..tostring(dbId).." list ")
  103. end
  104.  
  105. function delete(file)
  106.     rednet.send(dbId,textutils.serialize({"delete"}))
  107.     while true do
  108.         local event, id, msg = os.pullEvent()
  109.         if event == "timer" and id == timer then
  110.             printError("remote server timed out")
  111.             break
  112.         elseif event == "rednet_message" then
  113.             if parseReturn(id, msg) then
  114.                 break
  115.             end
  116.         end
  117.     end
  118. end
  119.  
  120. function move(oldFile,newFile)
  121.     rednet.send(dbId,textutils.serialize({"move",oldFile,newFile}))
  122.     while true do
  123.         local event, id, msg = os.pullEvent()
  124.         if event == "timer" and id == timer then
  125.             printError("remote server timed out")
  126.             break
  127.         elseif event == "rednet_message" then
  128.             if parseReturn(id, msg) then
  129.                 break
  130.             end
  131.         end
  132.     end
  133. end
  134.  
  135. function copy(oldFile,newFile)
  136.     rednet.send(dbId,textutils.serialize({"copy",oldFile,newFile}))
  137.     while true do
  138.         local event, id, msg = os.pullEvent()
  139.         if event == "timer" and id == timer then
  140.             printError("remote server timed out")
  141.             break
  142.         elseif event == "rednet_message" then
  143.             if parseReturn(id, msg) then
  144.                 break
  145.             end
  146.         end
  147.     end
  148. end
  149.  
  150. function read(file)
  151.     rednet.send(dbId,textutils.serialize({"read",file}))
  152.     while true do
  153.         local event, id, msg = os.pullEvent()
  154.         if event == "timer" and id == timer then
  155.             printError("remote server timed out")
  156.             break
  157.         elseif event == "rednet_message" then
  158.             if parseReturn(id, msg) then
  159.                 break
  160.             end
  161.         end
  162.     end
  163. end
  164.  
  165. function branch(branch)
  166.     shell.run("client "..tostring(dbId).." switch "..branch)
  167. end
Advertisement
Add Comment
Please, Sign In to add comment