diff options
Diffstat (limited to 'test/testsrvr.lua')
-rw-r--r-- | test/testsrvr.lua | 114 |
1 files changed, 21 insertions, 93 deletions
diff --git a/test/testsrvr.lua b/test/testsrvr.lua index 7b89987..227e341 100644 --- a/test/testsrvr.lua +++ b/test/testsrvr.lua | |||
@@ -1,96 +1,24 @@ | |||
1 | ----------------------------------------------------------------------------- | 1 | HOST = HOST or "localhost" |
2 | -- LuaSocket automated test module | 2 | PORT = PORT or "8080" |
3 | -- testsrvr.lua | ||
4 | -- This is the server module. It's completely controled by the client module | ||
5 | -- by the use of a control connection. | ||
6 | ----------------------------------------------------------------------------- | ||
7 | 3 | ||
8 | ----------------------------------------------------------------------------- | 4 | server, error = bind(HOST, PORT) |
9 | -- Read command definitions | 5 | if not server then print("server: " .. tostring(error)) exit() end |
10 | ----------------------------------------------------------------------------- | ||
11 | HOST = HOST or "*" | ||
12 | assert(dofile("testcmd.lua")) | ||
13 | test_debug_mode() | ||
14 | |||
15 | ----------------------------------------------------------------------------- | ||
16 | -- Start control connection | ||
17 | ----------------------------------------------------------------------------- | ||
18 | server, err = bind(HOST, PORT) | ||
19 | if not server then | ||
20 | fail(err) | ||
21 | exit(1) | ||
22 | end | ||
23 | print("server: waiting for control connection...") | ||
24 | control = server:accept() | ||
25 | print("server: control connection stablished!") | ||
26 | |||
27 | ----------------------------------------------------------------------------- | ||
28 | -- Executes a command, detecting any possible failures | ||
29 | -- Input | ||
30 | -- cmd: command to be executed | ||
31 | -- par: command parameters, if needed | ||
32 | ----------------------------------------------------------------------------- | ||
33 | function execute_command(cmd, par) | ||
34 | if cmd == CONNECT then | ||
35 | print("server: waiting for data connection...") | ||
36 | data = server:accept() | ||
37 | data:timeout(10) | ||
38 | if not data then | ||
39 | fail("server: unable to start data connection!") | ||
40 | else | ||
41 | print("server: data connection stablished!") | ||
42 | end | ||
43 | elseif cmd == CLOSE then | ||
44 | print("server: closing connection with client...") | ||
45 | if data then | ||
46 | data:close() | ||
47 | data = nil | ||
48 | end | ||
49 | elseif cmd == ECHO_LINE then | ||
50 | str, err = data:receive() | ||
51 | if err then fail("server: " .. err) end | ||
52 | err = data:send(str, "\n") | ||
53 | if err then fail("server: " .. err) end | ||
54 | elseif cmd == ECHO_BLOCK then | ||
55 | str, err = data:receive(par) | ||
56 | print(format("server: received %d bytes", strlen(str))) | ||
57 | if err then fail("server: " .. err) end | ||
58 | print(format("server: sending %d bytes", strlen(str))) | ||
59 | err = data:send(str) | ||
60 | if err then fail("server: " .. err) end | ||
61 | elseif cmd == RECEIVE_BLOCK then | ||
62 | str, err = data:receive(par) | ||
63 | print(format("server: received %d bytes", strlen(str))) | ||
64 | elseif cmd == SEND_BLOCK then | ||
65 | print(format("server: sending %d bytes", strlen(str))) | ||
66 | err = data:send(str) | ||
67 | elseif cmd == ECHO_TIMEOUT then | ||
68 | str, err = data:receive(par) | ||
69 | if err then fail("server: " .. err) end | ||
70 | err = data:send(str) | ||
71 | if err then fail("server: " .. err) end | ||
72 | elseif cmd == COMMAND then | ||
73 | cmd, par = get_command() | ||
74 | send_command(cmd, par) | ||
75 | elseif cmd == EXIT then | ||
76 | print("server: exiting...") | ||
77 | exit(0) | ||
78 | elseif cmd == SYNC then | ||
79 | print("server: synchronizing...") | ||
80 | send_command(SYNC) | ||
81 | elseif cmd == SLEEP then | ||
82 | print("server: sleeping for " .. par .. " seconds...") | ||
83 | _sleep(par) | ||
84 | print("server: woke up!") | ||
85 | end | ||
86 | end | ||
87 | |||
88 | ----------------------------------------------------------------------------- | ||
89 | -- Loop forever, accepting and executing commands | ||
90 | ----------------------------------------------------------------------------- | ||
91 | while 1 do | 6 | while 1 do |
92 | cmd, par = get_command() | 7 | print("server: waiting for client connection..."); |
93 | if not cmd then fail("server: " .. par) end | 8 | control = server:accept() |
94 | print_command(cmd, par) | 9 | while 1 do |
95 | execute_command(cmd, par) | 10 | command, error = control:receive() |
11 | if error then | ||
12 | control:close() | ||
13 | print("server: closing connection...") | ||
14 | break | ||
15 | end | ||
16 | error = control:send("\n") | ||
17 | if error then | ||
18 | control:close() | ||
19 | print("server: closing connection...") | ||
20 | break | ||
21 | end | ||
22 | dostring(command) | ||
23 | end | ||
96 | end | 24 | end |