Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- dbId = 1 --Change me
- responseOutput = {}
- local function parseReturn(id, msg)
- --print(id..": "..msg)
- msg = textutils.unserialize(msg)
- if type(msg) == "table" then
- if msg[1] == "DB" then
- if msg[2] == "error" then
- term.setTextColor(colors.red)
- write("Server at ")
- term.setTextColor(colors.lightBlue)
- write(id)
- term.setTextColor(colors.red)
- write(" reports error: \"")
- term.setTextColor(colors.orange)
- print(msg[3].."\"")
- term.setTextColor(colors.white)
- elseif msg[2] == "successful" then
- term.setTextColor(colors.lime)
- write("Server at ")
- term.setTextColor(colors.lightBlue)
- write(id)
- term.setTextColor(colors.lime)
- print(" reports that the operation was successful.")
- term.setTextColor(colors.white)
- elseif msg[2] == "file" then
- local fileHandle = fs.open(shell.dir().."/"..msg[3], "w")
- fileHandle.write(msg[4])
- fileHandle.close()
- elseif msg[2] == "commit_data" then
- local fileHandle = fs.open(shell.dir().."/commits/"..msg[3].."/"..msg[4], "w")
- fileHandle.write(msg[5])
- fileHandle.close()
- if msg[6] then
- local msgHandle = fs.open(shell.dir().."/commits/.metadata/"..msg[3].."/"..msg[4], "w")
- msgHandle.write(msg[5])
- msgHandle.close()
- end
- elseif msg[2] == "commits" then
- local file = msg[4]
- for i,v in ipairs(msg[5]) do
- local handle = fs.open(shell.dir().."/commits/"..file.."/"..i, "w")
- handle.write(v[1])
- handle.close()
- if v[2] then
- local msgHandle = fs.open(shell.dir().."/commits/"..file.."/"..i, "w")
- msgHandle.write(v[2])
- msgHandle.close()
- end
- end
- elseif msg[2] == "files" then
- print("FILES: ")
- textutils.pagedTabulate(msg[3])
- elseif msg[2] == "goodbye" then
- term.setTextColor(colors.lime)
- print("You have logged out.")
- term.setTextColor(colors.white)
- end
- else
- return false
- end
- return true
- else
- return false
- end
- end
- function login(user,pass)
- rednet.send(dbId,textutils.serialize({"login",user,pass}))
- local timer = os.startTimer(10)
- while true do
- local event, id, msg = os.pullEvent()
- if event == "timer" and id == timer then
- printError("remote server timed out")
- break
- elseif event == "rednet_message" then
- if parseReturn(id, msg) then
- break
- end
- end
- end
- end
- function sendFile(localFile,commit)
- if commit == nil then
- shell.run("client "..tostring(dbId).." sendfile "..localFile)
- else
- shell.run("client "..tostring(dbId).." sendfile "..localFile.." "..commit)
- end
- end
- function writeData(file,data,commit)
- if commit == nil then
- shell.run("client "..tostring(dbId).." commit "..file.." "..data)
- else
- shell.run("client "..tostring(dbId).." commit "..file.." "..data.." "..commit)
- end
- end
- function list()
- shell.run("client "..tostring(dbId).." list ")
- end
- function delete(file)
- rednet.send(dbId,textutils.serialize({"delete"}))
- while true do
- local event, id, msg = os.pullEvent()
- if event == "timer" and id == timer then
- printError("remote server timed out")
- break
- elseif event == "rednet_message" then
- if parseReturn(id, msg) then
- break
- end
- end
- end
- end
- function move(oldFile,newFile)
- rednet.send(dbId,textutils.serialize({"move",oldFile,newFile}))
- while true do
- local event, id, msg = os.pullEvent()
- if event == "timer" and id == timer then
- printError("remote server timed out")
- break
- elseif event == "rednet_message" then
- if parseReturn(id, msg) then
- break
- end
- end
- end
- end
- function copy(oldFile,newFile)
- rednet.send(dbId,textutils.serialize({"copy",oldFile,newFile}))
- while true do
- local event, id, msg = os.pullEvent()
- if event == "timer" and id == timer then
- printError("remote server timed out")
- break
- elseif event == "rednet_message" then
- if parseReturn(id, msg) then
- break
- end
- end
- end
- end
- function read(file)
- rednet.send(dbId,textutils.serialize({"read",file}))
- while true do
- local event, id, msg = os.pullEvent()
- if event == "timer" and id == timer then
- printError("remote server timed out")
- break
- elseif event == "rednet_message" then
- if parseReturn(id, msg) then
- break
- end
- end
- end
- end
- function branch(branch)
- shell.run("client "..tostring(dbId).." switch "..branch)
- end
Advertisement
Add Comment
Please, Sign In to add comment