diff options
author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2003-06-09 18:23:40 +0000 |
---|---|---|
committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2003-06-09 18:23:40 +0000 |
commit | 58bdb658aaa1c30a8f3bed46eef880d308fae582 (patch) | |
tree | 5bf880c715daff79c1a2062f2f3ae8336858c83f /samples | |
parent | b2724ad2d1cc3768a04270ed3f8014ec65ad133b (diff) | |
download | luasocket-58bdb658aaa1c30a8f3bed46eef880d308fae582.tar.gz luasocket-58bdb658aaa1c30a8f3bed46eef880d308fae582.tar.bz2 luasocket-58bdb658aaa1c30a8f3bed46eef880d308fae582.zip |
Select re-implemented in a nicer way.
Few changes in internal class and group registration.
Lua modules are compiled and built into library.
Dynamic library tested in Linux and Mac OS X.
Diffstat (limited to 'samples')
-rw-r--r-- | samples/daytimeclnt.lua | 6 | ||||
-rw-r--r-- | samples/echoclnt.lua | 6 | ||||
-rw-r--r-- | samples/echosrvr.lua | 6 | ||||
-rw-r--r-- | samples/listener.lua | 4 | ||||
-rw-r--r-- | samples/talker.lua | 6 | ||||
-rw-r--r-- | samples/tinyirc.lua | 90 |
6 files changed, 75 insertions, 43 deletions
diff --git a/samples/daytimeclnt.lua b/samples/daytimeclnt.lua index 4debc81..85ddca1 100644 --- a/samples/daytimeclnt.lua +++ b/samples/daytimeclnt.lua | |||
@@ -1,3 +1,9 @@ | |||
1 | ----------------------------------------------------------------------------- | ||
2 | -- UDP sample: daytime protocol client | ||
3 | -- LuaSocket 1.5 sample files. | ||
4 | -- Author: Diego Nehab | ||
5 | -- RCS ID: $Id$ | ||
6 | ----------------------------------------------------------------------------- | ||
1 | host = host or "127.0.0.1" | 7 | host = host or "127.0.0.1" |
2 | port = port or 13 | 8 | port = port or 13 |
3 | if arg then | 9 | if arg then |
diff --git a/samples/echoclnt.lua b/samples/echoclnt.lua index cd8b450..bca0b4d 100644 --- a/samples/echoclnt.lua +++ b/samples/echoclnt.lua | |||
@@ -1,3 +1,9 @@ | |||
1 | ----------------------------------------------------------------------------- | ||
2 | -- UDP sample: echo protocol client | ||
3 | -- LuaSocket 1.5 sample files | ||
4 | -- Author: Diego Nehab | ||
5 | -- RCS ID: $Id$ | ||
6 | ----------------------------------------------------------------------------- | ||
1 | host = host or "localhost" | 7 | host = host or "localhost" |
2 | port = port or 7 | 8 | port = port or 7 |
3 | if arg then | 9 | if arg then |
diff --git a/samples/echosrvr.lua b/samples/echosrvr.lua index 6117557..18bd84e 100644 --- a/samples/echosrvr.lua +++ b/samples/echosrvr.lua | |||
@@ -1,3 +1,9 @@ | |||
1 | ----------------------------------------------------------------------------- | ||
2 | -- UDP sample: echo protocol server | ||
3 | -- LuaSocket 1.5 sample files | ||
4 | -- Author: Diego Nehab | ||
5 | -- RCS ID: $Id$ | ||
6 | ----------------------------------------------------------------------------- | ||
1 | host = host or "127.0.0.1" | 7 | host = host or "127.0.0.1" |
2 | port = port or 7 | 8 | port = port or 7 |
3 | if arg then | 9 | if arg then |
diff --git a/samples/listener.lua b/samples/listener.lua index c035ab2..4846419 100644 --- a/samples/listener.lua +++ b/samples/listener.lua | |||
@@ -1,6 +1,8 @@ | |||
1 | ----------------------------------------------------------------------------- | 1 | ----------------------------------------------------------------------------- |
2 | -- Little program to dump lines received at a given port | 2 | -- TCP sample: Little program to dump lines received at a given port |
3 | -- LuaSocket 1.5 sample files | 3 | -- LuaSocket 1.5 sample files |
4 | -- Author: Diego Nehab | ||
5 | -- RCS ID: $Id$ | ||
4 | ----------------------------------------------------------------------------- | 6 | ----------------------------------------------------------------------------- |
5 | host = host or "*" | 7 | host = host or "*" |
6 | port = port or 8080 | 8 | port = port or 8080 |
diff --git a/samples/talker.lua b/samples/talker.lua index 688824f..c7a239a 100644 --- a/samples/talker.lua +++ b/samples/talker.lua | |||
@@ -1,3 +1,9 @@ | |||
1 | ----------------------------------------------------------------------------- | ||
2 | -- TCP sample: Little program to send text lines to a given host/port | ||
3 | -- LuaSocket 1.5 sample files | ||
4 | -- Author: Diego Nehab | ||
5 | -- RCS ID: $Id$ | ||
6 | ----------------------------------------------------------------------------- | ||
1 | host = host or "localhost" | 7 | host = host or "localhost" |
2 | port = port or 8080 | 8 | port = port or 8080 |
3 | if arg then | 9 | if arg then |
diff --git a/samples/tinyirc.lua b/samples/tinyirc.lua index d3e56e7..0b20303 100644 --- a/samples/tinyirc.lua +++ b/samples/tinyirc.lua | |||
@@ -1,16 +1,9 @@ | |||
1 | function set_add(set, sock) | 1 | ----------------------------------------------------------------------------- |
2 | table.insert(set, sock) | 2 | -- Select sample: simple text line server |
3 | end | 3 | -- LuaSocket 1.5 sample files. |
4 | 4 | -- Author: Diego Nehab | |
5 | function set_remove(set, sock) | 5 | -- RCS ID: $Id$ |
6 | for i = 1, table.getn(set) do | 6 | ----------------------------------------------------------------------------- |
7 | if set[i] == sock then | ||
8 | table.remove(set, i) | ||
9 | break | ||
10 | end | ||
11 | end | ||
12 | end | ||
13 | |||
14 | host = host or "*" | 7 | host = host or "*" |
15 | port1 = port1 or 8080 | 8 | port1 = port1 or 8080 |
16 | port2 = port2 or 8081 | 9 | port2 = port2 or 8081 |
@@ -21,49 +14,62 @@ if arg then | |||
21 | end | 14 | end |
22 | 15 | ||
23 | server1, error = socket.bind(host, port1) | 16 | server1, error = socket.bind(host, port1) |
24 | if not server1 then print(error) exit() end | 17 | assert(server1, error) |
25 | server1:timeout(1) | 18 | server1:timeout(1) |
26 | server2, error = socket.bind(host, port2) | 19 | server2, error = socket.bind(host, port2) |
27 | if not server2 then print(error) exit() end | 20 | assert(server2, error) |
28 | server2:timeout(1) | 21 | server2:timeout(1) |
29 | 22 | ||
30 | sock_set = {server1, server2} | 23 | function newset() |
24 | local reverse = {} | ||
25 | local set = {} | ||
26 | setmetatable(set, { __index = { | ||
27 | insert = function(set, value) | ||
28 | table.insert(set, value) | ||
29 | reverse[value] = table.getn(set) | ||
30 | end, | ||
31 | remove = function(set, value) | ||
32 | table.remove(set, reverse[value]) | ||
33 | reverse[value] = nil | ||
34 | end, | ||
35 | id = function(set, value) | ||
36 | return reverse[value] | ||
37 | end | ||
38 | }}) | ||
39 | return set | ||
40 | end | ||
41 | |||
42 | sockets = newset() | ||
31 | 43 | ||
32 | sock_id = {} | 44 | sockets:insert(server1) |
33 | sock_id[server1] = 1 | 45 | sockets:insert(server2) |
34 | sock_id[server2] = 2 | ||
35 | next_id = 3 | ||
36 | 46 | ||
37 | while 1 do | 47 | while 1 do |
38 | local readable, _, error = socket.select(sock_set, nil) | 48 | local readable, _, error = socket.select(sockets, nil) |
39 | for _, sock in readable do | 49 | for _, input in readable do |
40 | -- is it a server socket | 50 | -- is it a server socket? |
41 | if sock_id[sock] < 3 then | 51 | local id = sockets:id(input) |
42 | local incomming = sock:accept() | 52 | if input == server1 or input == server2 then |
43 | if incomming then | 53 | local new = input:accept() |
44 | incomming:timeout(1) | 54 | if new then |
45 | sock_id[incomming] = next_id | 55 | new:timeout(1) |
46 | set_add(sock_set, incomming) | 56 | sockets:insert(new) |
47 | io.write("Added client id ", next_id, ". ", | 57 | io.write("Server ", id, " got client ", sockets:id(new), "\n") |
48 | table.getn(sock_set)-2, " total.\n") | ||
49 | next_id = next_id + 1 | ||
50 | end | 58 | end |
51 | -- it is a client socket | 59 | -- it is a client socket |
52 | else | 60 | else |
53 | local line, error = sock:receive() | 61 | local line, error = input:receive() |
54 | local id = sock_id[sock] | ||
55 | if error then | 62 | if error then |
56 | sock:close() | 63 | input:close() |
57 | set_remove(sock_set, sock) | 64 | io.write("Removing client ", id, "\n") |
58 | io.write("Removed client number ", id, ". ", | 65 | sockets:remove(input) |
59 | getn(sock_set)-2, " total.\n") | ||
60 | else | 66 | else |
61 | io.write("Broadcasting line '", id, "> ", line, "'.\n") | 67 | io.write("Broadcasting line '", id, "> ", line, "'.\n") |
62 | __, writable, error = socket.select(nil, sock_set, 1) | 68 | __, writable, error = socket.select(nil, sockets, 1) |
63 | if not error then | 69 | if not error then |
64 | for ___, outgoing in writable do | 70 | for ___, output in writable do |
65 | io.write("Sending to client ", sock_id[outgoing], "\n") | 71 | io.write("Sending to client ", sockets:id(output), "\n") |
66 | outgoing:send(id, "> ", line, "\r\n") | 72 | output:send(id, "> ", line, "\r\n") |
67 | end | 73 | end |
68 | else io.write("No one ready to listen!!!\n") end | 74 | else io.write("No one ready to listen!!!\n") end |
69 | end | 75 | end |