diff options
author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2000-12-29 22:15:09 +0000 |
---|---|---|
committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2000-12-29 22:15:09 +0000 |
commit | 17c4d1c30544f0ed638879835f179ada96249868 (patch) | |
tree | 46ca8042ba8fda147f56af61e26ec2ceec41f614 /test/testsrvr.lua | |
parent | 6f9d15b66027cef58441549f8ac0603ca42da0ac (diff) | |
download | luasocket-17c4d1c30544f0ed638879835f179ada96249868.tar.gz luasocket-17c4d1c30544f0ed638879835f179ada96249868.tar.bz2 luasocket-17c4d1c30544f0ed638879835f179ada96249868.zip |
Initial revision
Diffstat (limited to 'test/testsrvr.lua')
-rw-r--r-- | test/testsrvr.lua | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/test/testsrvr.lua b/test/testsrvr.lua new file mode 100644 index 0000000..99ecd2a --- /dev/null +++ b/test/testsrvr.lua | |||
@@ -0,0 +1,90 @@ | |||
1 | ----------------------------------------------------------------------------- | ||
2 | -- LuaSocket automated test module | ||
3 | -- server.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 | |||
8 | ----------------------------------------------------------------------------- | ||
9 | -- Read command definitions | ||
10 | ----------------------------------------------------------------------------- | ||
11 | dofile("command.lua") | ||
12 | test_debug_mode() | ||
13 | |||
14 | ----------------------------------------------------------------------------- | ||
15 | -- Bind to address and wait for control connection | ||
16 | ----------------------------------------------------------------------------- | ||
17 | server, err = bind(HOST, PORT) | ||
18 | if not server then | ||
19 | print(err) | ||
20 | exit(1) | ||
21 | end | ||
22 | print("server: waiting for control connection...") | ||
23 | control = server:accept() | ||
24 | print("server: control connection stablished!") | ||
25 | |||
26 | ----------------------------------------------------------------------------- | ||
27 | -- Executes a command, detecting any possible failures | ||
28 | -- Input | ||
29 | -- cmd: command to be executed | ||
30 | -- par: command parameters, if needed | ||
31 | ----------------------------------------------------------------------------- | ||
32 | function execute_command(cmd, par) | ||
33 | if cmd == CONNECT then | ||
34 | print("server: waiting for data connection...") | ||
35 | data = server:accept() | ||
36 | if not data then | ||
37 | fail("server: unable to start data connection!") | ||
38 | else | ||
39 | print("server: data connection stablished!") | ||
40 | end | ||
41 | elseif cmd == CLOSE then | ||
42 | print("server: closing connection with client...") | ||
43 | if data then | ||
44 | data:close() | ||
45 | data = nil | ||
46 | end | ||
47 | elseif cmd == ECHO_LINE then | ||
48 | str, err = data:receive() | ||
49 | if err then fail("server: " .. err) end | ||
50 | err = data:send(str, "\n") | ||
51 | if err then fail("server: " .. err) end | ||
52 | elseif cmd == ECHO_BLOCK then | ||
53 | str, err = data:receive(par) | ||
54 | if err then fail("server: " .. err) end | ||
55 | err = data:send(str) | ||
56 | if err then fail("server: " .. err) end | ||
57 | elseif cmd == RECEIVE_BLOCK then | ||
58 | str, err = data:receive(par) | ||
59 | elseif cmd == SEND_BLOCK then | ||
60 | err = data:send(str) | ||
61 | elseif cmd == ECHO_TIMEOUT then | ||
62 | str, err = data:receive(par) | ||
63 | if err then fail("server: " .. err) end | ||
64 | err = data:send(str) | ||
65 | if err then fail("server: " .. err) end | ||
66 | elseif cmd == COMMAND then | ||
67 | cmd, par = get_command() | ||
68 | send_command(cmd, par) | ||
69 | elseif cmd == EXIT then | ||
70 | print("server: exiting...") | ||
71 | exit(0) | ||
72 | elseif cmd == SYNC then | ||
73 | print("server: synchronizing...") | ||
74 | send_command(SYNC) | ||
75 | elseif cmd == SLEEP then | ||
76 | print("server: sleeping for " .. par .. " seconds...") | ||
77 | sleep(par) | ||
78 | print("server: woke up!") | ||
79 | end | ||
80 | end | ||
81 | |||
82 | ----------------------------------------------------------------------------- | ||
83 | -- Loop forever, accepting and executing commands | ||
84 | ----------------------------------------------------------------------------- | ||
85 | while 1 do | ||
86 | cmd, par = get_command() | ||
87 | if not cmd then fail("server: " .. par) end | ||
88 | print_command(cmd, par) | ||
89 | execute_command(cmd, par) | ||
90 | end | ||