anfbckdo

client.lua

Nov 12th, 2024 (edited)
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.18 KB | None | 0 0
  1. function Client()
  2.     modem = peripheral.find('modem')
  3.     STORE_METHODS = {'FILE','VAR'}
  4.     servers = {}
  5.     computerID = os.getComputerID()
  6.     function FindServers(protocol, hostname)
  7.    
  8.         local servers = modem.lookup(protocol, hostname)
  9.         print("found servers" .. servers)
  10.     end
  11.  
  12.  
  13.     function SendRequest(WHO,KEY, SPR, OPR, DATA, STORE_METHOD)
  14.  
  15.         if type(DATA) ~= "string" then
  16.             error("DATA must be of type STRING", 0)
  17.         end
  18.  
  19.         -- Check if STORE_METHOD is valid (must be in STORE_METHODS)
  20.         local isValidMethod = false
  21.         for _, method in ipairs(STORE_METHODS) do
  22.             if STORE_METHOD == method then
  23.                 isValidMethod = true
  24.                 break
  25.             end
  26.         end
  27.  
  28.         if not isValidMethod then
  29.             error("INVALID STORAGE METHOD", 0)
  30.         end
  31.  
  32.         -- Handle the logic for KEY, SPR, OPR as per your original conditions
  33.         if SPR then
  34.             SPR = SPR
  35.             OPR = nil
  36.         elseif KEY == nil or SPR == nil then
  37.             OPR = OPR
  38.             KEY = nil
  39.             SPR = nil
  40.         end
  41.         -- "; OPR " .. OPR ..
  42.         if OPR == nil then
  43.             request = "KEY " .. KEY .. "; SPR " .. SPR .. "; DATA " .. DATA .. "; STORE " .. STORE_METHOD .. "; END;"
  44.             modem.send(computerID, request, WHO)
  45.         elseif SPR == nil then
  46.             request = "KEY " .. nil .. "; OPR " .. OPR .. "; DATA " .. DATA .. "; STORE " .. STORE_METHOD .. "; END;"
  47.             modem.send(computerID, request, WHO)
  48.         end
  49.     end
  50. end
  51.  
  52. function runtime()
  53.     -- Instantiate the server instance
  54.     local Client = Client()
  55.     if not Client then
  56.         print("Client instance is nil!")
  57.         return
  58.     end
  59.     FindServers("Server",nil)
  60.    
  61.  
  62.     while true do
  63.         print("WHO: ")
  64.         WHO = io.read()
  65.         print("KEY: ")
  66.         KEY = io.read()
  67.         print("SPR: ")
  68.         SPR = io.read()
  69.         print("OPR: ")
  70.         OPR = io.read()
  71.         print("DATA: ")
  72.         DATA = io.read()
  73.         print("STORE_METHOD: ")
  74.         STORE_METHOD = io.read()
  75.  
  76.         SendRequest(WHO,KEY,SPR,OPR,DATA,STORE_METHOD)
  77.     end
  78. end
Advertisement
Add Comment
Please, Sign In to add comment