Class: Lich::InternalAPI::ActiveSessions::Server
- Inherits:
-
Object
- Object
- Lich::InternalAPI::ActiveSessions::Server
- Defined in:
- documented/internal_api/active_sessions/server.rb
Overview
Represents a server for handling active sessions.
This class manages client connections and processes requests.
Constant Summary collapse
- READ_TIMEOUT =
1
Instance Attribute Summary collapse
-
#auth_token ⇒ Object
readonly
Returns the value of attribute auth_token.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Instance Method Summary collapse
-
#initialize(host:, port:, registry:, auth_token:, server_factory: nil, accept_thread_factory: nil, client_thread_factory: nil) ⇒ void
constructor
Initializes a new Server instance.
-
#running? ⇒ Boolean
Checks if the server is currently running.
-
#start ⇒ Boolean
Starts the server to accept client connections.
-
#stop ⇒ void
Stops the server and cleans up resources.
Constructor Details
#initialize(host:, port:, registry:, auth_token:, server_factory: nil, accept_thread_factory: nil, client_thread_factory: nil) ⇒ void
Initializes a new Server instance.
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'documented/internal_api/active_sessions/server.rb', line 29 def initialize(host:, port:, registry:, auth_token:, server_factory: nil, accept_thread_factory: nil, client_thread_factory: nil) @host = host @port = port @registry = registry @auth_token = auth_token @server_factory = server_factory || ->(bind_host, bind_port) { TCPServer.new(bind_host, bind_port) } @accept_thread_factory = accept_thread_factory || ->(&block) { Thread.new(&block) } @client_thread_factory = client_thread_factory || ->(socket, &block) { Thread.new(socket, &block) } @server = nil @thread = nil @mutex = Mutex.new @client_threads = [] end |
Instance Attribute Details
#auth_token ⇒ Object (readonly)
Returns the value of attribute auth_token.
18 19 20 |
# File 'documented/internal_api/active_sessions/server.rb', line 18 def auth_token @auth_token end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
17 18 19 |
# File 'documented/internal_api/active_sessions/server.rb', line 17 def host @host end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
17 18 19 |
# File 'documented/internal_api/active_sessions/server.rb', line 17 def port @port end |
Instance Method Details
#running? ⇒ Boolean
Checks if the server is currently running.
95 96 97 |
# File 'documented/internal_api/active_sessions/server.rb', line 95 def running? @thread&.alive? || false end |
#start ⇒ Boolean
Starts the server to accept client connections.
This method initializes the server and begins the accept loop.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'documented/internal_api/active_sessions/server.rb', line 48 def start @mutex.synchronize do return true if running? @server = @server_factory.call(@host, @port) @server.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, 1) rescue nil @port = @server.addr[1] @thread = @accept_thread_factory.call { accept_loop } end true rescue StandardError stop false end |
#stop ⇒ void
This method returns an undefined value.
Stops the server and cleans up resources.
This method closes the server socket and joins any active client threads.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'documented/internal_api/active_sessions/server.rb', line 67 def stop thread = nil server = nil client_threads = [] @mutex.synchronize do thread = @thread server = @server client_threads = @client_threads.dup @client_threads.clear @thread = nil @server = nil end server&.close rescue nil if thread&.alive? thread.join(0.1) thread.kill if thread.alive? end client_threads.each do |client_thread| next unless client_thread.respond_to?(:join) client_thread.join(0.25) client_thread.kill if client_thread.respond_to?(:alive?) && client_thread.alive? end end |