Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local modem, periSide, args = false, nil, {...}
- local version = 1
- if http then
- local data = nil
- parallel.waitForAll(
- function()
- textutils.slowPrint("Contacting update server...")
- end,
- function()
- data = http.get("https://dl.dropbox.com/u/49227540/cc/wireless_peripheral_server/ver.txt")
- end
- )
- if data then
- local version = data.readLine()
- if version then
- if ver < tonumber(version) then
- print("New version of wireless peripherals found\nRun server update to download newest version")
- end
- end
- end
- end
- if #args == 1 and http then
- if string.lower(args[1]) == "update" then
- local prog = nil
- parallel.waitForAll(
- function()
- textutils.slowPrint("Downloading update...")
- end,
- function()
- prog = http.get("https://dl.dropbox.com/u/49227540/cc/wireless_peripheral_server/prog.lua")
- end
- )
- if prog then
- prog = prog.readAll()
- local file = io.open(shell.getRunningProgram(), "w")
- file:write(prog)
- file:close()
- print "Program was sucessfully updated\nRestart the program, please"
- return
- end
- end
- end
- os.sleep(1.5)
- term.clear()
- if #args == 1 then
- periSide = args[1]
- elseif #args > 1 then
- print("Usage:\nserver [peripheral side]")
- end
- for k,v in pairs(rs.getSides()) do
- if peripheral.getType(v) == "modem" and not modem then
- modem = true
- rednet.open(v)
- elseif peripheral.isPresent(v) and peripheral.getType(v) ~= "modem" and not periSide then
- periSide = v
- end
- end
- if not modem then print "Modem not found" return end
- if not periSide then print "Peripheral not found" return end
- local methods = peripheral.getMethods(periSide)
- local function isFunction(func)
- for k,v in pairs(methods) do
- if v == func then return true end
- end
- return false
- end
- print("Ready to use "..periSide.." "..peripheral.getType(periSide).."\nComputer id is: "..os.getComputerID())
- while true do
- local id, msg = rednet.receive()
- if msg == "#PERI_CLIENT" then
- rednet.send(id, "#PERI_SERVER")
- rednet.send(id, textutils.serialize({peripheral.getType(periSide), methods}))
- print(id.." connected")
- elseif string.lower(msg) ~= "ping" then
- local todo = textutils.unserialize(msg)
- if todo then
- if todo[1] == "call" and isFunction(todo[2]) then
- if type(tonumber(todo[3])) == "number" then
- todo[3] = tonumber(todo[3])
- end
- if type(tonumber(todo[4])) == "number" then
- todo[4] = tonumber(todo[4])
- end
- local response = {}
- if todo[7] then
- response = {peripheral.call(periSide, todo[2], todo[3], todo[4], todo[5], todo[6], todo[7])}
- elseif todo[6] then
- response = {peripheral.call(periSide, todo[2], todo[3], todo[4], todo[5], todo[6])}
- elseif todo[5] then
- response = {peripheral.call(periSide, todo[2], todo[3], todo[4], todo[5])}
- elseif todo[4] then
- response = {peripheral.call(periSide, todo[2], todo[3], todo[4])}
- elseif todo[3] then
- response = {peripheral.call(periSide, todo[2], todo[3])}
- else
- response = {peripheral.call(periSide, todo[2])}
- end
- if #response == 0 then
- rednet.send(id, "nil")
- else
- rednet.send(id, textutils.serialize(response))
- end
- else
- rednet.send(id, "nil")
- end
- print(id.." did "..todo[2])
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement