Class: Lich::InternalAPI::ActiveSessions::Server

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.

Parameters:

  • host (String)

    the host address to bind the server to

  • port (Integer)

    the port number to listen on

  • registry (Object)

    the registry for managing session data

  • auth_token (String)

    the token used for authenticating requests

  • server_factory (Proc, nil) (defaults to: nil)

    optional factory for creating the server

  • accept_thread_factory (Proc, nil) (defaults to: nil)

    optional factory for creating accept threads

  • client_thread_factory (Proc, nil) (defaults to: nil)

    optional factory for creating client threads



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_tokenObject (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

#hostObject (readonly)

Returns the value of attribute host.



17
18
19
# File 'documented/internal_api/active_sessions/server.rb', line 17

def host
  @host
end

#portObject (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.

Returns:

  • (Boolean)

    true if the server is running, false otherwise



95
96
97
# File 'documented/internal_api/active_sessions/server.rb', line 95

def running?
  @thread&.alive? || false
end

#startBoolean

Starts the server to accept client connections.

This method initializes the server and begins the accept loop.

Returns:

  • (Boolean)

    true if the server started successfully, false otherwise

Raises:

  • (StandardError)

    if an error occurs during startup



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

#stopvoid

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