Class: Sappho::Socket::SafeServer
- Inherits:
-
Object
- Object
- Sappho::Socket::SafeServer
- Defined in:
- lib/sappho-socket/safe_server.rb
Instance Method Summary collapse
- #clientCount ⇒ Object
-
#initialize(name, port, maxClients = 10, timeout = 10, detailedLogging = false) ⇒ SafeServer
constructor
A new instance of SafeServer.
- #serve ⇒ Object
Constructor Details
#initialize(name, port, maxClients = 10, timeout = 10, detailedLogging = false) ⇒ SafeServer
Returns a new instance of SafeServer.
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/sappho-socket/safe_server.rb', line 15 def initialize name, port, maxClients = 10, timeout = 10, detailedLogging = false @name = name @port = port @maxClients = maxClients @timeout = timeout @detailedLogging = detailedLogging @clients = {} @mutex = Mutex.new @log = Sappho::ApplicationAutoFlushLog.instance end |
Instance Method Details
#clientCount ⇒ Object
63 64 65 66 67 |
# File 'lib/sappho-socket/safe_server.rb', line 63 def clientCount @mutex.synchronize do @clients.size end end |
#serve ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/sappho-socket/safe_server.rb', line 26 def serve Thread.new do begin @log.info "opening #{@name} server port #{@port}" @server = TCPServer.open @port @log.info "#{@name} server port #{@port} is now open" loop do if clientCount >= @maxClients sleep 1 else @log.info "listening for new clients on #{@name} server port #{@port}" if @detailedLogging client = @server.accept port, ip = Socket.unpack_sockaddr_in(client.getpeername) @mutex.synchronize do @clients[client] = ip log ip, 'connected' if @detailedLogging end Thread.new client, ip do | client, ip | socket = SafeSocket.new @timeout socket.attach client begin yield socket, ip, @name, @port rescue => error @log.error error end socket.close @mutex.synchronize do @clients.delete client log ip, 'disconnected' if @detailedLogging end end end end end end end |