class BBS::Server
The telnet listener. It accepts connections on a TCP port and runs a BBS::Session for each caller in its own thread, up to BBS.config.max_sessions callers at once and BBS.config.max_sessions_per_ip from one public address.
BBS::Server.new(port: 2323).run
Constants
- ACCEPT_PAUSE
-
Seconds the accept loop rests after a failed accept (out of file descriptors and the like).
- BACKTRACE_LINES
-
Lines of the backtrace logged with a session error.
- DEFAULT_TIMEOUT
-
Seconds a silent line may stay connected when
BBS.config.idle_secondsis not set. - REFUSALS
-
What a caller turned away hears, by the verdict of
admit.
Public Class Methods
Source
# File lib/bbs/server.rb, line 29 def initialize(port:) @port = port @mu = Mutex.new @sessions = 0 @calls = Hash.new(0) end
Creates a server that will listen on port on all interfaces. Nothing is opened until run.
Public Instance Methods
Source
# File lib/bbs/server.rb, line 65 def admit(address) @mu.synchronize do return :busy if @sessions >= BBS.config.max_sessions return :crowded if crowded?(address) @sessions += 1 @calls[address] += 1 :ok end end
Takes a call from the IP address (a String, nil when unknown) when there is room, and counts it. Returns the verdict:
:ok-
admitted;
releasemust follow when the call ends :busy-
BBS.config.max_sessionscallers are on :crowded-
BBS.config.max_sessions_per_ipcalls come from this address already; a local one (a proxy such as bbs-web on the Docker network, seeBBS::Address.local?) or an unknown one has no such limit
Source
# File lib/bbs/server.rb, line 77 def release(address) @mu.synchronize do @sessions -= 1 @calls[address] -= 1 @calls.delete(address) unless @calls[address].positive? end end
Ends the count of a call admit took from address.
Source
# File lib/bbs/server.rb, line 45 def run server = TCPServer.new('0.0.0.0', @port) puts "BBS listening on port #{@port} β connect with: telnet localhost #{@port}" stop_on_term loop { serve(accept(server)) } rescue Interrupt puts "\nServer stopped." ensure server&.close end
Opens the port and serves callers until the process is interrupted (Ctrl-C) or stopped (SIGTERM, as docker stop sends). Blocks the calling thread.
Each accepted connection gets an idle timeout and a BBS::Session in a new thread. A caller admit refuses hears why and is disconnected. Session errors are logged and end only that session; BBS.config.on_session_end is called with the session when it finishes. On a stop it prints βServer stopped.β and returns.