2001-01-25 23:00:18 +01:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- LuaSocket automated test module
|
2001-03-07 23:38:54 +01:00
|
|
|
-- testsrvr.lua
|
2001-01-25 23:00:18 +01:00
|
|
|
-- This is the server module. It's completely controled by the client module
|
|
|
|
-- by the use of a control connection.
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Read command definitions
|
|
|
|
-----------------------------------------------------------------------------
|
2001-03-12 20:38:39 +01:00
|
|
|
HOST = HOST or "*"
|
2001-01-25 23:00:18 +01:00
|
|
|
assert(dofile("testcmd.lua"))
|
|
|
|
test_debug_mode()
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Start control connection
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
server, err = bind(HOST, PORT)
|
|
|
|
if not server then
|
|
|
|
fail(err)
|
|
|
|
exit(1)
|
|
|
|
end
|
|
|
|
print("server: waiting for control connection...")
|
2001-03-06 21:16:17 +01:00
|
|
|
control = server:accept()
|
2001-01-25 23:00:18 +01:00
|
|
|
print("server: control connection stablished!")
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Executes a command, detecting any possible failures
|
|
|
|
-- Input
|
|
|
|
-- cmd: command to be executed
|
|
|
|
-- par: command parameters, if needed
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
function execute_command(cmd, par)
|
|
|
|
if cmd == CONNECT then
|
|
|
|
print("server: waiting for data connection...")
|
2001-03-06 21:16:17 +01:00
|
|
|
data = server:accept()
|
2001-03-12 20:38:39 +01:00
|
|
|
data:timeout(10)
|
2001-01-25 23:00:18 +01:00
|
|
|
if not data then
|
|
|
|
fail("server: unable to start data connection!")
|
|
|
|
else
|
|
|
|
print("server: data connection stablished!")
|
|
|
|
end
|
|
|
|
elseif cmd == CLOSE then
|
|
|
|
print("server: closing connection with client...")
|
|
|
|
if data then
|
2001-03-06 21:16:17 +01:00
|
|
|
data:close()
|
2001-01-25 23:00:18 +01:00
|
|
|
data = nil
|
|
|
|
end
|
|
|
|
elseif cmd == ECHO_LINE then
|
2001-03-06 21:16:17 +01:00
|
|
|
str, err = data:receive()
|
2001-01-25 23:00:18 +01:00
|
|
|
if err then fail("server: " .. err) end
|
2001-03-06 21:16:17 +01:00
|
|
|
err = data:send(str, "\n")
|
2001-01-25 23:00:18 +01:00
|
|
|
if err then fail("server: " .. err) end
|
|
|
|
elseif cmd == ECHO_BLOCK then
|
2001-03-06 21:16:17 +01:00
|
|
|
str, err = data:receive(par)
|
2001-01-25 23:00:18 +01:00
|
|
|
print(format("server: received %d bytes", strlen(str)))
|
|
|
|
if err then fail("server: " .. err) end
|
|
|
|
print(format("server: sending %d bytes", strlen(str)))
|
2001-03-06 21:16:17 +01:00
|
|
|
err = data:send(str)
|
2001-01-25 23:00:18 +01:00
|
|
|
if err then fail("server: " .. err) end
|
|
|
|
elseif cmd == RECEIVE_BLOCK then
|
2001-03-06 21:16:17 +01:00
|
|
|
str, err = data:receive(par)
|
2001-01-25 23:00:18 +01:00
|
|
|
print(format("server: received %d bytes", strlen(str)))
|
|
|
|
elseif cmd == SEND_BLOCK then
|
|
|
|
print(format("server: sending %d bytes", strlen(str)))
|
2001-03-06 21:16:17 +01:00
|
|
|
err = data:send(str)
|
2001-01-25 23:00:18 +01:00
|
|
|
elseif cmd == ECHO_TIMEOUT then
|
2001-03-06 21:16:17 +01:00
|
|
|
str, err = data:receive(par)
|
2001-01-25 23:00:18 +01:00
|
|
|
if err then fail("server: " .. err) end
|
2001-03-06 21:16:17 +01:00
|
|
|
err = data:send(str)
|
2001-01-25 23:00:18 +01:00
|
|
|
if err then fail("server: " .. err) end
|
|
|
|
elseif cmd == COMMAND then
|
|
|
|
cmd, par = get_command()
|
|
|
|
send_command(cmd, par)
|
|
|
|
elseif cmd == EXIT then
|
|
|
|
print("server: exiting...")
|
|
|
|
exit(0)
|
|
|
|
elseif cmd == SYNC then
|
|
|
|
print("server: synchronizing...")
|
|
|
|
send_command(SYNC)
|
|
|
|
elseif cmd == SLEEP then
|
|
|
|
print("server: sleeping for " .. par .. " seconds...")
|
2001-06-09 00:41:15 +02:00
|
|
|
_sleep(par)
|
2001-01-25 23:00:18 +01:00
|
|
|
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
|