mirror of
https://github.com/lunarmodules/luasocket.git
synced 2024-12-26 04:28:20 +01:00
Updated for LuaSocket 1.2.
This commit is contained in:
parent
2bb209ab9e
commit
f6b9505225
@ -1,90 +1,104 @@
|
|||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
-- LuaSocket automated test module
|
-- LuaSocket automated test module
|
||||||
-- server.lua
|
-- server.lua
|
||||||
-- This is the server module. It's completely controled by the client module
|
-- This is the server module. It's completely controled by the client module
|
||||||
-- by the use of a control connection.
|
-- by the use of a control connection.
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
-- Read command definitions
|
-- Read command definitions
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
dofile("command.lua")
|
assert(dofile("testcmd.lua"))
|
||||||
test_debug_mode()
|
test_debug_mode()
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
-- Bind to address and wait for control connection
|
-- Get host and port from command line
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
server, err = bind(HOST, PORT)
|
HOST = "localhost"
|
||||||
if not server then
|
PORT = 2020
|
||||||
print(err)
|
if arg then
|
||||||
exit(1)
|
HOST = arg[1] or HOST
|
||||||
end
|
PORT = arg[2] or PORT
|
||||||
print("server: waiting for control connection...")
|
end
|
||||||
control = server:accept()
|
|
||||||
print("server: control connection stablished!")
|
-----------------------------------------------------------------------------
|
||||||
|
-- Start control connection
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
-- Executes a command, detecting any possible failures
|
server, err = bind(HOST, PORT)
|
||||||
-- Input
|
if not server then
|
||||||
-- cmd: command to be executed
|
fail(err)
|
||||||
-- par: command parameters, if needed
|
exit(1)
|
||||||
-----------------------------------------------------------------------------
|
end
|
||||||
function execute_command(cmd, par)
|
print("server: waiting for control connection...")
|
||||||
if cmd == CONNECT then
|
control = accept(server)
|
||||||
print("server: waiting for data connection...")
|
print("server: control connection stablished!")
|
||||||
data = server:accept()
|
|
||||||
if not data then
|
-----------------------------------------------------------------------------
|
||||||
fail("server: unable to start data connection!")
|
-- Executes a command, detecting any possible failures
|
||||||
else
|
-- Input
|
||||||
print("server: data connection stablished!")
|
-- cmd: command to be executed
|
||||||
end
|
-- par: command parameters, if needed
|
||||||
elseif cmd == CLOSE then
|
-----------------------------------------------------------------------------
|
||||||
print("server: closing connection with client...")
|
function execute_command(cmd, par)
|
||||||
if data then
|
if cmd == CONNECT then
|
||||||
data:close()
|
print("server: waiting for data connection...")
|
||||||
data = nil
|
data = accept(server)
|
||||||
end
|
if not data then
|
||||||
elseif cmd == ECHO_LINE then
|
fail("server: unable to start data connection!")
|
||||||
str, err = data:receive()
|
else
|
||||||
if err then fail("server: " .. err) end
|
print("server: data connection stablished!")
|
||||||
err = data:send(str, "\n")
|
end
|
||||||
if err then fail("server: " .. err) end
|
elseif cmd == CLOSE then
|
||||||
elseif cmd == ECHO_BLOCK then
|
print("server: closing connection with client...")
|
||||||
str, err = data:receive(par)
|
if data then
|
||||||
if err then fail("server: " .. err) end
|
close(data)
|
||||||
err = data:send(str)
|
data = nil
|
||||||
if err then fail("server: " .. err) end
|
end
|
||||||
elseif cmd == RECEIVE_BLOCK then
|
elseif cmd == ECHO_LINE then
|
||||||
str, err = data:receive(par)
|
str, err = receive(data)
|
||||||
elseif cmd == SEND_BLOCK then
|
if err then fail("server: " .. err) end
|
||||||
err = data:send(str)
|
err = send(data, str, "\n")
|
||||||
elseif cmd == ECHO_TIMEOUT then
|
if err then fail("server: " .. err) end
|
||||||
str, err = data:receive(par)
|
elseif cmd == ECHO_BLOCK then
|
||||||
if err then fail("server: " .. err) end
|
str, err = receive(data, par)
|
||||||
err = data:send(str)
|
print(format("server: received %d bytes", strlen(str)))
|
||||||
if err then fail("server: " .. err) end
|
if err then fail("server: " .. err) end
|
||||||
elseif cmd == COMMAND then
|
print(format("server: sending %d bytes", strlen(str)))
|
||||||
cmd, par = get_command()
|
err = send(data, str)
|
||||||
send_command(cmd, par)
|
if err then fail("server: " .. err) end
|
||||||
elseif cmd == EXIT then
|
elseif cmd == RECEIVE_BLOCK then
|
||||||
print("server: exiting...")
|
str, err = receive(data, par)
|
||||||
exit(0)
|
print(format("server: received %d bytes", strlen(str)))
|
||||||
elseif cmd == SYNC then
|
elseif cmd == SEND_BLOCK then
|
||||||
print("server: synchronizing...")
|
print(format("server: sending %d bytes", strlen(str)))
|
||||||
send_command(SYNC)
|
err = send(data, str)
|
||||||
elseif cmd == SLEEP then
|
elseif cmd == ECHO_TIMEOUT then
|
||||||
print("server: sleeping for " .. par .. " seconds...")
|
str, err = receive(data, par)
|
||||||
sleep(par)
|
if err then fail("server: " .. err) end
|
||||||
print("server: woke up!")
|
err = send(data, str)
|
||||||
end
|
if err then fail("server: " .. err) end
|
||||||
end
|
elseif cmd == COMMAND then
|
||||||
|
cmd, par = get_command()
|
||||||
-----------------------------------------------------------------------------
|
send_command(cmd, par)
|
||||||
-- Loop forever, accepting and executing commands
|
elseif cmd == EXIT then
|
||||||
-----------------------------------------------------------------------------
|
print("server: exiting...")
|
||||||
while 1 do
|
exit(0)
|
||||||
cmd, par = get_command()
|
elseif cmd == SYNC then
|
||||||
if not cmd then fail("server: " .. par) end
|
print("server: synchronizing...")
|
||||||
print_command(cmd, par)
|
send_command(SYNC)
|
||||||
execute_command(cmd, par)
|
elseif cmd == SLEEP then
|
||||||
end
|
print("server: sleeping for " .. par .. " seconds...")
|
||||||
|
sleep(par)
|
||||||
|
print("server: woke up!")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-----------------------------------------------------------------------------
|
||||||
|
-- Loop forever, accepting and executing commands
|
||||||
|
-----------------------------------------------------------------------------
|
||||||
|
while 1 do
|
||||||
|
cmd, par = get_command()
|
||||||
|
if not cmd then fail("server: " .. par) end
|
||||||
|
print_command(cmd, par)
|
||||||
|
execute_command(cmd, par)
|
||||||
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user