Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local modemSide = "back" --change to your modem side
- local compModemSide="left"--change to the computer interaction modem side
- devPrints = true --set this to false unless modifying code
- --copyright 2014 Jackson McNeill
- --you are free to edit, and distribute any version(s) of this without need for permission nor the use of the same license.
- --[[how this will work:
- data will be stored on disks. A single program
- can be on multiple disks, giving theoriticly in
- finite storage capacity. This works by:
- Finding all disks
- When it needs to write something, checks what disks
- have space in order
- Writes as much as it can on that disk,
- as 'filenamehere'..numberInOrderForReconstruct
- then when asked foor the file searches for filenamehere..1-#filenamehere
- ]]
- modem = peripheral.wrap(modemSide)
- modem.open(1)
- networkModem=peripheral.wrap(compModemSide)
- networkModem.open(1)
- function devPrint(txt)
- if devPrints then
- print(txt)
- end
- end
- function findFreeDisks()--plural for a reason
- local modemsTable = {}
- modemsTable = modem.getNamesRemote()
- --find locations
- local disksTable={}
- for i=1,#modemsTable do
- if modem.callRemote(modemsTable[i],"hasData") then--make sure they're actually disks...
- disksTable[#disksTable+1]=modem.callRemote(modemsTable[i],"getMountPath")
- end
- end
- local freeSpace={}
- local freeDisks={}
- for i=1,#disksTable do--find free disk
- devPrint(i)
- if fs.getFreeSpace(disksTable[i])>14 then
- devPrint("2 "..i)
- table.insert(freeSpace,fs.getFreeSpace(disksTable[i]))
- table.insert(freeDisks,disksTable[i])
- end
- end
- return freeDisks, freeSpace
- end
- function packProgram(strProg,freeDisks,freeSpace,file)
- --chop the string up based on space. done by
- --#ing it and comparing the size
- local stillTrying = 1
- while stillTrying do
- --try to fit the program
- if #strProg > freeSpace[stillTrying] then
- --write the subbed area to the disk
- fileF=fs.open(freeDisks[stillTrying].."/"..file..tostring(stillTrying),"w")
- fileF.writeLine(string.sub(strProg,1,freeSpace[stillTrying]-20--[[leave some space for other stuff, ex. notes]]))
- fileF.close()
- --let's sub out some lines of strProg
- strProg=string.sub(strProg,freeSpace[stillTrying]-20,#strProg)
- stillTrying=stillTrying+1
- else
- fileF=fs.open(freeDisks[stillTrying].."/"..file..tostring(stillTrying),"w")
- fileF.writeLine(strProg)
- fileF.close()
- stillTrying=nil --kill the loop
- end
- end
- end
- function unpackProgram(file)--unpack the prog split across drives
- --find disks, courtesty of findFreeDisks()
- local modemsTable = {}
- modemsTable = modem.getNamesRemote()
- --find locations
- local disksTable={}
- for i=1,#modemsTable do
- if modem.callRemote(modemsTable[i],"hasData") then--make sure they're actually disks...
- disksTable[#disksTable+1]=modem.callRemote(modemsTable[i],"getMountPath")
- end
- end
- --we has table of modems, must search data.
- stillTrying=0--double purpose variable
- returnDis=""
- local paths = {}
- while stillTrying do
- for i=1,#disksTable do
- if fs.exists(disksTable[i].."/"..file..tostring(stillTrying+1)) then--we found it
- paths[stillTrying+1]=disksTable[i].."/"..file..tostring(stillTrying+1)
- break
- end
- end
- if #paths > stillTrying then
- stillTrying=#paths
- else--we've found 'em all.
- stillTrying=nil--we won't go through the loop again
- --let's return the var
- local reRet = returnDis
- returnDis=nil
- return reRet--we got rid of the global variable yet still returned something
- end
- --we get anything?
- if not paths then
- return false
- end
- --let's stitch together the progrram now
- for i=1,#paths do
- local ReadDis= fs.open(paths[i],"r")
- returnDis=returnDis..ReadDis.readAll()
- end
- end
- end
- function exists(file)
- --code, courtesy of unpackProgram
- --find disks, courtesty of findFreeDisks()
- local modemsTable = {}
- modemsTable = modem.getNamesRemote()
- --find locations
- local disksTable={}
- for i=1,#modemsTable do
- if modem.callRemote(modemsTable[i],"hasData") then--make sure they're actually disks...
- disksTable[#disksTable+1]=modem.callRemote(modemsTable[i],"getMountPath")
- end
- end
- --we has table of modems, must search data.
- stillTrying=0--double purpose variable
- local paths = {}
- while stillTrying do
- for i=1,#disksTable do
- if fs.exists(disksTable[i].."/"..file..tostring(stillTrying+1)) then--we found it
- paths[stillTrying+1]=disksTable[i].."/"..file..tostring(stillTrying+1)
- break
- end
- end
- if #paths > stillTrying then
- stillTrying=#paths
- else--we've found 'em all.
- stillTrying=nil--we won't go through the loop again
- end
- --return t/f
- if paths[1] then
- return true
- else
- return false
- end
- end
- end
- function deleteFile(filename)
- --code, courtesy of unpackProgram
- --find disks, courtesty of findFreeDisks()
- local modemsTable = {}
- modemsTable = modem.getNamesRemote()
- --find locations
- local disksTable={}
- for i=1,#modemsTable do
- if modem.callRemote(modemsTable[i],"hasData") then--make sure they're actually disks...
- disksTable[#disksTable+1]=modem.callRemote(modemsTable[i],"getMountPath")
- end
- end
- --we has table of modems, must search data.
- stillTrying=0--double purpose variable
- returnDis=""
- local paths = {}
- while stillTrying do
- for i=1,#disksTable do
- if fs.exists(disksTable[i].."/"..file..tostring(stillTrying+1)) then--we found it
- paths[stillTrying+1]=disksTable[i].."/"..file..tostring(stillTrying+1)
- break
- end
- end
- end
- for i=1,#paths do
- fs.delete(paths[i])
- end
- end
- function drawUI(freeDisks,freeSpace)
- term.clear() term.setCursorPos(1,1)
- print"////////////////////////"
- print"/ specs: /"
- print("/ color: "..tostring(term.isColor()).." /")
- print("/ osver: "..os.version().." /")
- print("/ wireless: "..tostring(networkModem.isWireless()).." /")
- print("/ free disks : "..#freeDisks)
- --let's draw a / for this one
- term.setCursorPos(24,6) print"/"
- print"/ total free space: /"
- --get total free space
- local totalFreeSpace=0
- for i=1,#freeSpace do
- totalFreeSpace = totalFreeSpace+freeSpace[i]
- end
- totalFreeSpace=string.sub(tostring(totalFreeSpace),1,#tostring(totalFreeSpace)-3)
- print("/ ~"..tostring(totalFreeSpace).."KB")
- --draw a / at the end
- term.setCursorPos(24,8)
- print"/"
- print"////////////////////////"
- print"/-- File Server V1 --/"
- print"/ Command list: /"
- print"/1shutdown -shutdown /"
- print"/2stop -exit to craftos/"
- print"/3flushUI - refresh UI /"
- print"////////////////////////"
- end
- function handleServer()
- while true do
- local freeDisks, freeSpace = findFreeDisks()--refresh it every time
- drawUI(freeDisks,freeSpace)
- --let's wait for requests nao.
- local evt,key,_,compID,request,_ = os.pullEvent()
- if evt=="modem_message"then
- print(request)
- --sub the first part
- local Trequest = string.sub(request,1,1)
- --sub out the first part on the other string
- request = string.sub(request,3,#request)
- if Trequest == "e" then--they want to know if a file exists
- networkModem.transmit(1,99,exists(request))
- elseif Trequest=="g" then--they want a file
- networkModem.transmit(1,99,unpackProgram(request))
- elseif Trequest=="w" then--they want to write a file
- print(request)
- request=textutils.unserialize(request)--they must have sent a table
- print(request[1])
- print(request[2])
- packProgram(request[1],freeDisks,freeSpace,request[2])
- elseif Trequest=="d"then--they want to see all files
- findAllFiles(request)
- elseif Trequest=="p" then--they're pinging the server
- networkModem.transmit(1,99,"r")
- elseif Trequest =="f"then--they want to see avialiable space
- --code, courtesy of drawUI()
- local totalFreeSpace=0
- for i=1,#freeSpace do
- totalFreeSpace = totalFreeSpace+freeSpace[i]
- end
- networkModem.transmit(1,99,totalFreeSpace)
- elseif Trequest=="m"then--they want to make a dir
- --code, courtesy of unpackProgram
- local modemsTable = {}
- modemsTable = modem.getNamesRemote()
- --find locations
- local disksTable={}
- for i=1,#modemsTable do
- if modem.callRemote(modemsTable[i],"hasData") then--make sure they're actually disks...
- disksTable[#disksTable+1]=modem.callRemote(modemsTable[i],"getMountPath")
- end
- end
- --make the dir on all disks
- for i=1,#disksTable do
- fs.makeDir(disksTable[i]..request)
- end
- elseif Trequest=="d" then--they want to delete something
- deleteFile(request)
- else
- networkModem.transmit(1,99,"No such command")
- end
- elseif evt=="key" then
- end
- end
- end
- handleServer()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement