diff options
| author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2001-01-25 22:00:18 +0000 |
|---|---|---|
| committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2001-01-25 22:00:18 +0000 |
| commit | f6b95052256fa9609a8f8a2c8abfe0af1e18706f (patch) | |
| tree | 68e72db02326d4938f12428f8ef8cf0d90c877fc /test | |
| parent | 2bb209ab9eb746fdfb9b8f5c83eee12b40fc211e (diff) | |
| download | luasocket-f6b95052256fa9609a8f8a2c8abfe0af1e18706f.tar.gz luasocket-f6b95052256fa9609a8f8a2c8abfe0af1e18706f.tar.bz2 luasocket-f6b95052256fa9609a8f8a2c8abfe0af1e18706f.zip | |
Updated for LuaSocket 1.2.
Diffstat (limited to 'test')
| -rw-r--r-- | test/testsrvr.lua | 194 |
1 files changed, 104 insertions, 90 deletions
diff --git a/test/testsrvr.lua b/test/testsrvr.lua index 99ecd2a..d69b5ab 100644 --- a/test/testsrvr.lua +++ b/test/testsrvr.lua | |||
| @@ -1,90 +1,104 @@ | |||
| 1 | ----------------------------------------------------------------------------- | 1 | ----------------------------------------------------------------------------- |
| 2 | -- LuaSocket automated test module | 2 | -- LuaSocket automated test module |
| 3 | -- server.lua | 3 | -- server.lua |
| 4 | -- This is the server module. It's completely controled by the client module | 4 | -- This is the server module. It's completely controled by the client module |
| 5 | -- by the use of a control connection. | 5 | -- by the use of a control connection. |
| 6 | ----------------------------------------------------------------------------- | 6 | ----------------------------------------------------------------------------- |
| 7 | 7 | ||
| 8 | ----------------------------------------------------------------------------- | 8 | ----------------------------------------------------------------------------- |
| 9 | -- Read command definitions | 9 | -- Read command definitions |
| 10 | ----------------------------------------------------------------------------- | 10 | ----------------------------------------------------------------------------- |
| 11 | dofile("command.lua") | 11 | assert(dofile("testcmd.lua")) |
| 12 | test_debug_mode() | 12 | test_debug_mode() |
| 13 | 13 | ||
| 14 | ----------------------------------------------------------------------------- | 14 | ----------------------------------------------------------------------------- |
| 15 | -- Bind to address and wait for control connection | 15 | -- Get host and port from command line |
| 16 | ----------------------------------------------------------------------------- | 16 | ----------------------------------------------------------------------------- |
| 17 | server, err = bind(HOST, PORT) | 17 | HOST = "localhost" |
| 18 | if not server then | 18 | PORT = 2020 |
| 19 | print(err) | 19 | if arg then |
| 20 | exit(1) | 20 | HOST = arg[1] or HOST |
| 21 | end | 21 | PORT = arg[2] or PORT |
| 22 | print("server: waiting for control connection...") | 22 | end |
| 23 | control = server:accept() | 23 | |
| 24 | print("server: control connection stablished!") | 24 | ----------------------------------------------------------------------------- |
| 25 | 25 | -- Start control connection | |
| 26 | ----------------------------------------------------------------------------- | 26 | ----------------------------------------------------------------------------- |
| 27 | -- Executes a command, detecting any possible failures | 27 | server, err = bind(HOST, PORT) |
| 28 | -- Input | 28 | if not server then |
| 29 | -- cmd: command to be executed | 29 | fail(err) |
| 30 | -- par: command parameters, if needed | 30 | exit(1) |
| 31 | ----------------------------------------------------------------------------- | 31 | end |
| 32 | function execute_command(cmd, par) | 32 | print("server: waiting for control connection...") |
| 33 | if cmd == CONNECT then | 33 | control = accept(server) |
| 34 | print("server: waiting for data connection...") | 34 | print("server: control connection stablished!") |
| 35 | data = server:accept() | 35 | |
| 36 | if not data then | 36 | ----------------------------------------------------------------------------- |
| 37 | fail("server: unable to start data connection!") | 37 | -- Executes a command, detecting any possible failures |
| 38 | else | 38 | -- Input |
| 39 | print("server: data connection stablished!") | 39 | -- cmd: command to be executed |
| 40 | end | 40 | -- par: command parameters, if needed |
| 41 | elseif cmd == CLOSE then | 41 | ----------------------------------------------------------------------------- |
| 42 | print("server: closing connection with client...") | 42 | function execute_command(cmd, par) |
| 43 | if data then | 43 | if cmd == CONNECT then |
| 44 | data:close() | 44 | print("server: waiting for data connection...") |
| 45 | data = nil | 45 | data = accept(server) |
| 46 | end | 46 | if not data then |
| 47 | elseif cmd == ECHO_LINE then | 47 | fail("server: unable to start data connection!") |
| 48 | str, err = data:receive() | 48 | else |
| 49 | if err then fail("server: " .. err) end | 49 | print("server: data connection stablished!") |
| 50 | err = data:send(str, "\n") | 50 | end |
| 51 | if err then fail("server: " .. err) end | 51 | elseif cmd == CLOSE then |
| 52 | elseif cmd == ECHO_BLOCK then | 52 | print("server: closing connection with client...") |
| 53 | str, err = data:receive(par) | 53 | if data then |
| 54 | if err then fail("server: " .. err) end | 54 | close(data) |
| 55 | err = data:send(str) | 55 | data = nil |
| 56 | if err then fail("server: " .. err) end | 56 | end |
| 57 | elseif cmd == RECEIVE_BLOCK then | 57 | elseif cmd == ECHO_LINE then |
| 58 | str, err = data:receive(par) | 58 | str, err = receive(data) |
| 59 | elseif cmd == SEND_BLOCK then | 59 | if err then fail("server: " .. err) end |
| 60 | err = data:send(str) | 60 | err = send(data, str, "\n") |
| 61 | elseif cmd == ECHO_TIMEOUT then | 61 | if err then fail("server: " .. err) end |
| 62 | str, err = data:receive(par) | 62 | elseif cmd == ECHO_BLOCK then |
| 63 | if err then fail("server: " .. err) end | 63 | str, err = receive(data, par) |
| 64 | err = data:send(str) | 64 | print(format("server: received %d bytes", strlen(str))) |
| 65 | if err then fail("server: " .. err) end | 65 | if err then fail("server: " .. err) end |
| 66 | elseif cmd == COMMAND then | 66 | print(format("server: sending %d bytes", strlen(str))) |
| 67 | cmd, par = get_command() | 67 | err = send(data, str) |
| 68 | send_command(cmd, par) | 68 | if err then fail("server: " .. err) end |
| 69 | elseif cmd == EXIT then | 69 | elseif cmd == RECEIVE_BLOCK then |
| 70 | print("server: exiting...") | 70 | str, err = receive(data, par) |
| 71 | exit(0) | 71 | print(format("server: received %d bytes", strlen(str))) |
| 72 | elseif cmd == SYNC then | 72 | elseif cmd == SEND_BLOCK then |
| 73 | print("server: synchronizing...") | 73 | print(format("server: sending %d bytes", strlen(str))) |
| 74 | send_command(SYNC) | 74 | err = send(data, str) |
| 75 | elseif cmd == SLEEP then | 75 | elseif cmd == ECHO_TIMEOUT then |
| 76 | print("server: sleeping for " .. par .. " seconds...") | 76 | str, err = receive(data, par) |
| 77 | sleep(par) | 77 | if err then fail("server: " .. err) end |
| 78 | print("server: woke up!") | 78 | err = send(data, str) |
| 79 | end | 79 | if err then fail("server: " .. err) end |
| 80 | end | 80 | elseif cmd == COMMAND then |
| 81 | 81 | cmd, par = get_command() | |
| 82 | ----------------------------------------------------------------------------- | 82 | send_command(cmd, par) |
| 83 | -- Loop forever, accepting and executing commands | 83 | elseif cmd == EXIT then |
| 84 | ----------------------------------------------------------------------------- | 84 | print("server: exiting...") |
| 85 | while 1 do | 85 | exit(0) |
| 86 | cmd, par = get_command() | 86 | elseif cmd == SYNC then |
| 87 | if not cmd then fail("server: " .. par) end | 87 | print("server: synchronizing...") |
| 88 | print_command(cmd, par) | 88 | send_command(SYNC) |
| 89 | execute_command(cmd, par) | 89 | elseif cmd == SLEEP then |
| 90 | end | 90 | print("server: sleeping for " .. par .. " seconds...") |
| 91 | sleep(par) | ||
| 92 | print("server: woke up!") | ||
| 93 | end | ||
| 94 | end | ||
| 95 | |||
| 96 | ----------------------------------------------------------------------------- | ||
| 97 | -- Loop forever, accepting and executing commands | ||
| 98 | ----------------------------------------------------------------------------- | ||
| 99 | while 1 do | ||
| 100 | cmd, par = get_command() | ||
| 101 | if not cmd then fail("server: " .. par) end | ||
| 102 | print_command(cmd, par) | ||
| 103 | execute_command(cmd, par) | ||
| 104 | end | ||
