Advertisement
Guest User

server

a guest
Jan 4th, 2013
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.25 KB | None | 0 0
  1. local modem, periSide, args = false, nil, {...}
  2. local version = 1
  3.  
  4. if http then
  5.     local data = nil
  6.     parallel.waitForAll(
  7.         function()
  8.             textutils.slowPrint("Contacting update server...")
  9.         end,
  10.         function()
  11.             data = http.get("https://dl.dropbox.com/u/49227540/cc/wireless_peripheral_server/ver.txt")
  12.         end
  13.     )
  14.     if data then
  15.         local version = data.readLine()
  16.         if version then
  17.             if ver < tonumber(version) then
  18.                 print("New version of wireless peripherals found\nRun server update to download newest version")
  19.             end
  20.         end
  21.     end
  22. end
  23.  
  24. if #args == 1 and http then
  25.     if string.lower(args[1]) == "update" then
  26.         local prog = nil
  27.         parallel.waitForAll(
  28.             function()
  29.                 textutils.slowPrint("Downloading update...")
  30.             end,
  31.             function()
  32.                 prog = http.get("https://dl.dropbox.com/u/49227540/cc/wireless_peripheral_server/prog.lua")
  33.             end
  34.         )
  35.         if prog then
  36.             prog = prog.readAll()
  37.             local file = io.open(shell.getRunningProgram(), "w")
  38.             file:write(prog)
  39.             file:close()
  40.             print "Program was sucessfully updated\nRestart the program, please"
  41.             return
  42.         end
  43.     end
  44. end
  45.  
  46. os.sleep(1.5)
  47. term.clear()
  48.  
  49. if #args == 1 then
  50.     periSide = args[1]
  51. elseif #args > 1 then
  52.     print("Usage:\nserver [peripheral side]")
  53. end
  54.  
  55. for k,v in pairs(rs.getSides()) do
  56.     if peripheral.getType(v) == "modem" and not modem then
  57.         modem = true
  58.         rednet.open(v)
  59.     elseif peripheral.isPresent(v) and peripheral.getType(v) ~= "modem" and not periSide then
  60.         periSide = v
  61.     end
  62. end
  63.  
  64. if not modem then print "Modem not found" return end
  65. if not periSide then print "Peripheral not found" return end
  66.  
  67. local methods = peripheral.getMethods(periSide)
  68.  
  69. local function isFunction(func)
  70.     for k,v in pairs(methods) do
  71.         if v == func then return true end
  72.     end
  73.     return false
  74. end
  75. print("Ready to use "..periSide.." "..peripheral.getType(periSide).."\nComputer id is: "..os.getComputerID())
  76. while true do
  77.     local id, msg = rednet.receive()
  78.     if msg == "#PERI_CLIENT" then
  79.         rednet.send(id, "#PERI_SERVER")
  80.         rednet.send(id, textutils.serialize({peripheral.getType(periSide), methods}))
  81.         print(id.." connected")
  82.     elseif string.lower(msg) ~= "ping" then
  83.         local todo = textutils.unserialize(msg)
  84.         if todo then
  85.             if todo[1] == "call" and isFunction(todo[2]) then
  86.                 if type(tonumber(todo[3])) == "number" then
  87.                     todo[3] = tonumber(todo[3])
  88.                 end
  89.                 if type(tonumber(todo[4])) == "number" then
  90.                     todo[4] = tonumber(todo[4])
  91.                 end
  92.                 local response = {}
  93.                 if todo[7] then
  94.                     response = {peripheral.call(periSide, todo[2], todo[3], todo[4], todo[5], todo[6], todo[7])}
  95.                 elseif todo[6] then
  96.                     response = {peripheral.call(periSide, todo[2], todo[3], todo[4], todo[5], todo[6])}
  97.                 elseif todo[5] then
  98.                     response = {peripheral.call(periSide, todo[2], todo[3], todo[4], todo[5])}
  99.                 elseif todo[4] then
  100.                     response = {peripheral.call(periSide, todo[2], todo[3], todo[4])}
  101.                 elseif todo[3] then
  102.                     response = {peripheral.call(periSide, todo[2], todo[3])}
  103.                 else
  104.                     response = {peripheral.call(periSide, todo[2])}
  105.                 end
  106.                 if #response == 0 then
  107.                     rednet.send(id, "nil")
  108.                 else
  109.                     rednet.send(id, textutils.serialize(response))
  110.                 end
  111.             else
  112.                 rednet.send(id, "nil")
  113.             end
  114.             print(id.." did "..todo[2])
  115.         end
  116.     end
  117. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement