Advertisement
5bitesofcookies

askapro-server

Jul 7th, 2014
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.07 KB | None | 0 0
  1. local modemSide = "back" --change to your modem side
  2. local compModemSide="left"--change to the computer interaction modem side
  3. devPrints = true --set this to false unless modifying code
  4. --copyright 2014 Jackson McNeill
  5. --you are free to edit, and distribute any version(s) of this without need for permission nor the use of the same license.
  6.  
  7.  
  8. --[[how this will work:
  9. data will be stored on disks. A single program
  10. can be on multiple disks, giving theoriticly in
  11. finite storage capacity. This works by:
  12. Finding all disks
  13. When it needs to write something, checks what disks
  14.   have space in order
  15. Writes as much as it can on that disk,
  16. as 'filenamehere'..numberInOrderForReconstruct
  17. then when asked foor the file searches for filenamehere..1-#filenamehere
  18. ]]
  19. modem = peripheral.wrap(modemSide)
  20. modem.open(1)
  21. networkModem=peripheral.wrap(compModemSide)
  22. networkModem.open(1)
  23. function devPrint(txt)
  24.   if devPrints then
  25.     print(txt)
  26.   end
  27. end
  28. function findFreeDisks()--plural for a reason
  29.   local modemsTable = {}
  30.   modemsTable = modem.getNamesRemote()
  31.   --find locations
  32.   local disksTable={}
  33.   for i=1,#modemsTable do
  34.     if modem.callRemote(modemsTable[i],"hasData") then--make sure they're actually disks...
  35.       disksTable[#disksTable+1]=modem.callRemote(modemsTable[i],"getMountPath")
  36.     end
  37.   end
  38.   local freeSpace={}
  39.   local freeDisks={}
  40.   for i=1,#disksTable do--find free disk
  41.     devPrint(i)
  42.     if fs.getFreeSpace(disksTable[i])>14 then
  43.       devPrint("2 "..i)
  44.       table.insert(freeSpace,fs.getFreeSpace(disksTable[i]))
  45.       table.insert(freeDisks,disksTable[i])
  46.     end
  47.   end
  48.   return freeDisks, freeSpace
  49. end
  50. function packProgram(strProg,freeDisks,freeSpace,file)
  51.   --chop the string up based on space. done by
  52.   --#ing it and comparing the size
  53.   local stillTrying = 1
  54.   while stillTrying do
  55.     --try to fit the program
  56.     if #strProg > freeSpace[stillTrying] then
  57.       --write the subbed area to the disk
  58.       fileF=fs.open(freeDisks[stillTrying].."/"..file..tostring(stillTrying),"w")
  59.       fileF.writeLine(string.sub(strProg,1,freeSpace[stillTrying]-20--[[leave some space for other stuff, ex. notes]]))
  60.       fileF.close()
  61.       --let's sub out some lines of strProg
  62.       strProg=string.sub(strProg,freeSpace[stillTrying]-20,#strProg)
  63.       stillTrying=stillTrying+1
  64.     else
  65.       fileF=fs.open(freeDisks[stillTrying].."/"..file..tostring(stillTrying),"w")
  66.       fileF.writeLine(strProg)
  67.       fileF.close()
  68.       stillTrying=nil --kill the loop
  69.     end
  70.   end
  71. end
  72.  
  73. function unpackProgram(file)--unpack the prog split across drives
  74.   --find disks, courtesty of findFreeDisks()
  75.   local modemsTable = {}
  76.   modemsTable = modem.getNamesRemote()
  77.   --find locations
  78.   local disksTable={}
  79.   for i=1,#modemsTable do
  80.     if modem.callRemote(modemsTable[i],"hasData") then--make sure they're actually disks...
  81.       disksTable[#disksTable+1]=modem.callRemote(modemsTable[i],"getMountPath")
  82.     end
  83.   end
  84.   --we has table of modems, must search data.
  85.   stillTrying=0--double purpose variable
  86.   returnDis=""
  87.   local paths = {}
  88.   while stillTrying do
  89.     for i=1,#disksTable do
  90.       if fs.exists(disksTable[i].."/"..file..tostring(stillTrying+1)) then--we found it
  91.         paths[stillTrying+1]=disksTable[i].."/"..file..tostring(stillTrying+1)
  92.         break
  93.       end
  94.     end
  95.     if #paths > stillTrying then
  96.       stillTrying=#paths
  97.     else--we've found 'em all.
  98.       stillTrying=nil--we won't go through the loop again
  99.       --let's return the var
  100.       local reRet = returnDis
  101.       returnDis=nil
  102.       return reRet--we got rid of the global variable yet still returned something
  103.     end
  104.     --we get anything?
  105.     if not paths then
  106.       return false
  107.     end
  108.     --let's stitch together the progrram now
  109.     for i=1,#paths do
  110.       local ReadDis= fs.open(paths[i],"r")
  111.       returnDis=returnDis..ReadDis.readAll()
  112.     end
  113.   end
  114. end
  115.    
  116. function exists(file)
  117.   --code, courtesy of unpackProgram
  118.     --find disks, courtesty of findFreeDisks()
  119.   local modemsTable = {}
  120.   modemsTable = modem.getNamesRemote()
  121.   --find locations
  122.   local disksTable={}
  123.   for i=1,#modemsTable do
  124.     if modem.callRemote(modemsTable[i],"hasData") then--make sure they're actually disks...
  125.       disksTable[#disksTable+1]=modem.callRemote(modemsTable[i],"getMountPath")
  126.     end
  127.   end
  128.   --we has table of modems, must search data.
  129.   stillTrying=0--double purpose variable
  130.   local paths = {}
  131.   while stillTrying do
  132.     for i=1,#disksTable do
  133.       if fs.exists(disksTable[i].."/"..file..tostring(stillTrying+1)) then--we found it
  134.         paths[stillTrying+1]=disksTable[i].."/"..file..tostring(stillTrying+1)
  135.         break
  136.       end
  137.     end
  138.     if #paths > stillTrying then
  139.       stillTrying=#paths
  140.     else--we've found 'em all.
  141.       stillTrying=nil--we won't go through the loop again
  142.     end
  143.     --return t/f
  144.     if paths[1] then
  145.       return true
  146.     else
  147.       return false
  148.     end  
  149.   end
  150. end
  151. function deleteFile(filename)
  152.   --code, courtesy of unpackProgram
  153.   --find disks, courtesty of findFreeDisks()
  154.   local modemsTable = {}
  155.   modemsTable = modem.getNamesRemote()
  156.   --find locations
  157.   local disksTable={}
  158.   for i=1,#modemsTable do
  159.     if modem.callRemote(modemsTable[i],"hasData") then--make sure they're actually disks...
  160.       disksTable[#disksTable+1]=modem.callRemote(modemsTable[i],"getMountPath")
  161.     end
  162.   end
  163.   --we has table of modems, must search data.
  164.   stillTrying=0--double purpose variable
  165.   returnDis=""
  166.   local paths = {}
  167.   while stillTrying do
  168.     for i=1,#disksTable do
  169.       if fs.exists(disksTable[i].."/"..file..tostring(stillTrying+1)) then--we found it
  170.         paths[stillTrying+1]=disksTable[i].."/"..file..tostring(stillTrying+1)
  171.         break
  172.       end
  173.     end
  174.   end
  175.   for i=1,#paths do
  176.     fs.delete(paths[i])
  177.   end
  178. end
  179. function drawUI(freeDisks,freeSpace)
  180.   term.clear() term.setCursorPos(1,1)
  181.   print"////////////////////////"
  182.   print"/       specs:         /"
  183.   print("/ color: "..tostring(term.isColor()).."          /")
  184.   print("/ osver: "..os.version().."   /")
  185.   print("/ wireless: "..tostring(networkModem.isWireless()).."      /")
  186.   print("/ free disks : "..#freeDisks)
  187.   --let's draw a / for this one
  188.   term.setCursorPos(24,6) print"/"
  189.   print"/ total free space:    /"
  190.   --get total free space
  191.   local totalFreeSpace=0
  192.   for i=1,#freeSpace do
  193.     totalFreeSpace = totalFreeSpace+freeSpace[i]
  194.   end
  195.   totalFreeSpace=string.sub(tostring(totalFreeSpace),1,#tostring(totalFreeSpace)-3)
  196.   print("/ ~"..tostring(totalFreeSpace).."KB")
  197.   --draw a / at the end
  198.   term.setCursorPos(24,8)
  199.   print"/"
  200.   print"////////////////////////"
  201.   print"/--  File Server V1  --/"
  202.   print"/ Command list:        /"
  203.   print"/1shutdown -shutdown   /"
  204.   print"/2stop -exit to craftos/"
  205.   print"/3flushUI - refresh UI /"
  206.   print"////////////////////////"
  207. end
  208. function handleServer()
  209.   while true do
  210.     local freeDisks, freeSpace = findFreeDisks()--refresh it every time
  211.     drawUI(freeDisks,freeSpace)
  212.     --let's wait for requests nao.
  213.     local evt,key,_,compID,request,_ = os.pullEvent()
  214.     if evt=="modem_message"then
  215.       print(request)
  216.       --sub the first part
  217.      local Trequest = string.sub(request,1,1)
  218.       --sub out the first part on the other string
  219.      request = string.sub(request,3,#request)
  220.       if Trequest == "e" then--they want to know if a file exists
  221.         networkModem.transmit(1,99,exists(request))
  222.      elseif Trequest=="g" then--they want a file
  223.         networkModem.transmit(1,99,unpackProgram(request))
  224.       elseif Trequest=="w" then--they want to write a file
  225.        print(request)
  226.        request=textutils.unserialize(request)--they must have sent a table
  227.        print(request[1])
  228.        print(request[2])
  229.        packProgram(request[1],freeDisks,freeSpace,request[2])
  230.       elseif Trequest=="d"then--they want to see all files
  231.         findAllFiles(request)
  232.       elseif Trequest=="p" then--they're pinging the server
  233.         networkModem.transmit(1,99,"r")
  234.       elseif Trequest =="f"then--they want to see avialiable space
  235.         --code, courtesy of drawUI()
  236.         local totalFreeSpace=0
  237.         for i=1,#freeSpace do
  238.           totalFreeSpace = totalFreeSpace+freeSpace[i]
  239.         end
  240.         networkModem.transmit(1,99,totalFreeSpace)
  241.       elseif Trequest=="m"then--they want to make a dir
  242.         --code, courtesy of unpackProgram
  243.         local modemsTable = {}
  244.         modemsTable = modem.getNamesRemote()
  245.         --find locations
  246.         local disksTable={}
  247.         for i=1,#modemsTable do
  248.           if modem.callRemote(modemsTable[i],"hasData") then--make sure they're actually disks...
  249.             disksTable[#disksTable+1]=modem.callRemote(modemsTable[i],"getMountPath")
  250.           end
  251.         end
  252.         --make the dir on all disks
  253.         for i=1,#disksTable do
  254.           fs.makeDir(disksTable[i]..request)
  255.         end
  256.       elseif Trequest=="d" then--they want to delete something
  257.         deleteFile(request)
  258.       else
  259.         networkModem.transmit(1,99,"No such command")
  260.       end
  261.     elseif evt=="key" then
  262.  
  263.     end
  264.  
  265.   end
  266. end
  267.  
  268. handleServer()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement