2003-06-09 20:23:40 +02:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Select sample: simple text line server
|
2003-06-26 20:47:49 +02:00
|
|
|
-- LuaSocket sample files.
|
2003-06-09 20:23:40 +02:00
|
|
|
-- Author: Diego Nehab
|
|
|
|
-- RCS ID: $Id$
|
|
|
|
-----------------------------------------------------------------------------
|
2004-05-30 23:36:22 +02:00
|
|
|
require("socket")
|
2001-03-12 20:42:46 +01:00
|
|
|
host = host or "*"
|
|
|
|
port1 = port1 or 8080
|
2003-06-30 08:10:02 +02:00
|
|
|
port2 = port2 or 8181
|
2001-03-12 20:42:46 +01:00
|
|
|
if arg then
|
|
|
|
host = arg[1] or host
|
|
|
|
port1 = arg[2] or port1
|
|
|
|
port2 = arg[3] or port2
|
|
|
|
end
|
|
|
|
|
2004-05-30 23:36:22 +02:00
|
|
|
server1 = socket.try(socket.bind(host, port1))
|
|
|
|
server2 = socket.try(socket.bind(host, port2))
|
2003-08-16 02:06:04 +02:00
|
|
|
server1:settimeout(1) -- make sure we don't block in accept
|
2004-05-30 23:36:22 +02:00
|
|
|
server2:settimeout(1)
|
2001-03-12 20:42:46 +01:00
|
|
|
|
2003-06-30 08:10:02 +02:00
|
|
|
io.write("Servers bound\n")
|
|
|
|
|
|
|
|
-- simple set implementation
|
|
|
|
-- the select function doesn't care about what is passed to it as long as
|
|
|
|
-- it behaves like a table
|
2003-06-09 20:23:40 +02:00
|
|
|
function newset()
|
|
|
|
local reverse = {}
|
|
|
|
local set = {}
|
|
|
|
setmetatable(set, { __index = {
|
|
|
|
insert = function(set, value)
|
|
|
|
table.insert(set, value)
|
|
|
|
reverse[value] = table.getn(set)
|
|
|
|
end,
|
|
|
|
remove = function(set, value)
|
|
|
|
table.remove(set, reverse[value])
|
|
|
|
reverse[value] = nil
|
|
|
|
end,
|
|
|
|
}})
|
|
|
|
return set
|
|
|
|
end
|
|
|
|
|
2003-06-30 08:10:02 +02:00
|
|
|
set = newset()
|
2002-07-08 22:56:36 +02:00
|
|
|
|
2003-06-30 08:10:02 +02:00
|
|
|
io.write("Inserting servers in set\n")
|
|
|
|
set:insert(server1)
|
|
|
|
set:insert(server2)
|
2001-03-12 20:42:46 +01:00
|
|
|
|
|
|
|
while 1 do
|
2003-06-30 08:10:02 +02:00
|
|
|
local readable, _, error = socket.select(set, nil)
|
2004-05-30 23:36:22 +02:00
|
|
|
for _, input in ipairs(readable) do
|
2003-06-09 20:23:40 +02:00
|
|
|
-- is it a server socket?
|
|
|
|
if input == server1 or input == server2 then
|
2003-06-30 08:10:02 +02:00
|
|
|
io.write("Waiting for clients\n")
|
2003-06-09 20:23:40 +02:00
|
|
|
local new = input:accept()
|
|
|
|
if new then
|
2003-08-16 02:06:04 +02:00
|
|
|
new:settimeout(1)
|
2003-06-30 08:10:02 +02:00
|
|
|
io.write("Inserting client in set\n")
|
|
|
|
set:insert(new)
|
2001-03-12 20:42:46 +01:00
|
|
|
end
|
2002-07-08 22:56:36 +02:00
|
|
|
-- it is a client socket
|
2001-03-12 20:42:46 +01:00
|
|
|
else
|
2003-06-09 20:23:40 +02:00
|
|
|
local line, error = input:receive()
|
2002-07-08 22:56:36 +02:00
|
|
|
if error then
|
2003-06-09 20:23:40 +02:00
|
|
|
input:close()
|
2003-06-30 08:10:02 +02:00
|
|
|
io.write("Removing client from set\n")
|
|
|
|
set:remove(input)
|
2001-03-12 20:42:46 +01:00
|
|
|
else
|
2003-06-30 08:10:02 +02:00
|
|
|
io.write("Broadcasting line '", line, "'\n")
|
2004-05-30 23:36:22 +02:00
|
|
|
writable, error = socket.skip(1, socket.select(nil, set, 1))
|
2002-07-08 22:56:36 +02:00
|
|
|
if not error then
|
2004-05-30 23:36:22 +02:00
|
|
|
for __, output in ipairs(writable) do
|
|
|
|
if output ~= input then
|
|
|
|
output:send(line .. "\n")
|
|
|
|
end
|
2001-03-12 21:00:47 +01:00
|
|
|
end
|
2003-06-30 08:10:02 +02:00
|
|
|
else io.write("No client ready to receive!!!\n") end
|
2001-03-12 21:00:47 +01:00
|
|
|
end
|
2001-03-12 20:42:46 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|