Advertisement
PaymentOption

NetBootServer

Aug 20th, 2012
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.47 KB | None | 0 0
  1. -- Better network boot host, by PaymentionOption --
  2. VERSION = 1.0
  3. -----------
  4.  
  5. -- NOTE:
  6. -- All OS' must have a file titled with their name and another file titled: <osname>_version containing the version of the OS.
  7.  
  8. -- Variables --
  9. local tOS_List = {} -- Holds all of the OS'.
  10. -- ^^ Format: tOS_List[n] = { Name, Version, Path }
  11. ---------------
  12.  
  13. -- Drawing Functions --
  14. -----------------------
  15.  
  16. -- File functions --
  17. function LoadOS_List() -- Load all of the OS' and their respective information from the OS directory.
  18.     if fs.exists( "OS_Directory" ) and fs.isDir( "OS_Directory" ) then -- If the OS directory exists.
  19.         local tOS = fs.list( "OS_Directory" ) -- Get the contents of the OS directory.
  20.        
  21.         if #tOS % 2 == 0 then -- If the amount of files in the OS directory is even.
  22.             local File = nil -- Setup a variable to hold a file instance.
  23.             local FileVersion = nil -- Setup a variable to hold the version of an OS.
  24.            
  25.             for i=1, #tOS, 2 do -- Loop through all of the files in the OS directory.
  26.                 File = fs.open( "OS_Directory/" .. tOS[i+1], "r" ) -- Open the current OS version file.
  27.                 FileVersion = File.readAll() -- Get the version.
  28.                 File.close() -- Close the file instance.
  29.                
  30.                 tOS_List[#tOS_List+1] = { Name = tOS[i], Version = tonumber( FileVersion ), Path = "OS_Directory/" .. tOS[i] }
  31.                 -- ^^ Add the OS to the table.
  32.             end
  33.         else -- If there are too many version files over OS' or vice versa.
  34.             error( "OS Directory format error." ) -- Throw an error.
  35.         end
  36.     else -- If the OS directory does not exist.
  37.         error( "OS Directory not found." ) -- Throw an error.
  38.     end
  39. end
  40.  
  41. function GetOS_FromFile( Index ) -- Gets the OS from the passed index and returns it as a string.
  42.     local File = fs.open( tOS_List[Index].Path, "r" ) -- Open the OS file in read mode.
  43.     local FileContents = File.readAll() -- Get the OS from the file into a string.
  44.     File.close() -- Close the file.
  45.    
  46.     return FileContents -- Return the OS that was read from the file.
  47. end
  48. --------------------
  49.  
  50. -- Networking functions --
  51. function OpenExistantModem() -- Opens any modem that is found on the computer. Returns false if no modem was found.
  52.     local tSides = rs.getSides()
  53.    
  54.     for i=1, #tSides do
  55.         if peripheral.isPresent( tSides[i] ) and peripheral.getType( tSides[i] ) == "modem" then
  56.             rednet.open( tSides[i] )
  57.             return true
  58.         end
  59.     end
  60.    
  61.     return false
  62. end
  63.  
  64. function RespondToPings() -- Waits and responds to any ping received.
  65.     local sender, message = rednet.receive() -- Wait forever for a message.
  66.    
  67.     if message == "PingingYou" then -- If the message was a ping.
  68.         rednet.send( sender, "PingingYouBack" ) -- Respond to the ping to inform the user of our communication.
  69.         return true, sender -- Return true and the sender that pinged us.
  70.     else -- If the message was not a ping.
  71.         return false, nil -- Return false and nil for the sender did not send a valid request.
  72.     end
  73. end
  74.  
  75. function SendOS_List( Reciever ) -- Sends the list of available OS'.
  76.     local sOS_List = "" -- Setup a variable to hold all of the OS' in a string.
  77.    
  78.     for i=1, #tOS_List do -- Iterate through the OS List table.
  79.         sOS_List = sOS_List .. tOS_List[i].Name .. "@#DS!" .. tostring( tOS_List[i].Version ) .. "@#DS!"
  80.         -- ^^ Create the OS List string and build upon it very iteration.
  81.     end
  82.    
  83.     rednet.send( Receiver, sOS_List ) -- Send the list to the intended receiver.
  84. end
  85.  
  86. function SendOS( Receiver, Index ) -- Packages and sends the requested OS to the requester.
  87.     local OS = GetOS_FromFile( Index ) -- Get the OS.
  88.    
  89.     if OS then -- If the OS is not nil.
  90.         rednet.send( Receiver, tostring( OS ) ) -- Send the OS.
  91.         return true -- Report a success.
  92.     else -- If the OS is nil.
  93.         return false -- Report a failure.
  94.     end
  95. end
  96. --------------------------
  97.  
  98. -- Main Loop --
  99. function MainLoop()
  100.     local bPinged, Sender = RespondToPings()
  101.    
  102.     if bPinged then
  103.         local sender, request = rednet.receive( 0.4 )
  104.         -- Handle the request --
  105.         if sender == Sender then
  106.             if request == "AllPlease" then -- Get the list of OS'.
  107.                 SendOS_List( Sender )
  108.             elseif string.find( request, "OS_Number_" ) then -- Request a specific OS.
  109.                 SendOS( Sender, tonumber( string.sub( request, 11, string.len( request ) ) ) )
  110.             end
  111.         else
  112.             return
  113.         end
  114.         ------------------------
  115.     else
  116.         return
  117.     end
  118. end
  119. ---------------
  120.  
  121. -- The program --
  122. if not OpenExistantModem() then
  123.     error( "No modem found." )
  124. end
  125.  
  126. LoadOS_List() -- Load all of the OS' in the OS directory.
  127.  
  128. while true do
  129.     MainLoop()
  130. end
  131. -----------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement