Class: Lich::InternalAPI::ActiveSessions::Client

Inherits:
Object
  • Object
show all
Defined in:
documented/internal_api/active_sessions/client.rb

Overview

Client for managing active sessions with the Lich internal API.

This class handles communication with the server, including sending requests and receiving responses.

Constant Summary collapse

READ_TIMEOUT =
1

Instance Method Summary collapse

Constructor Details

#initialize(host:, port:, auth_token:, socket_factory: nil) ⇒ void

Initializes a new Client instance.

Parameters:

  • host (String)

    the hostname of the server

  • port (Integer)

    the port number of the server

  • auth_token (String)

    the authentication token for the session

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

    a factory for creating sockets (defaults to TCPSocket)



23
24
25
26
27
28
# File 'documented/internal_api/active_sessions/client.rb', line 23

def initialize(host:, port:, auth_token:, socket_factory: nil)
  @host = host
  @port = port
  @auth_token = auth_token
  @socket_factory = socket_factory || ->(connect_host, connect_port) { TCPSocket.new(connect_host, connect_port) }
end

Instance Method Details

#pingBoolean

Sends a ping request to the server to check connectivity.

Examples:

Check server connectivity

client = Lich::InternalAPI::ActiveSessions::Client.new(host: "localhost", port: 1234, auth_token: "token")
client.ping

Returns:

  • (Boolean)

    true if the server responds positively, false otherwise



56
57
58
# File 'documented/internal_api/active_sessions/client.rb', line 56

def ping
  request('ping').fetch(:ok, false)
end

#remove(pid) ⇒ Hash

Sends a remove request to the server for the specified process ID.

Parameters:

  • pid (String)

    the process ID to remove

Returns:

  • (Hash)

    the server's response to the remove request



70
71
72
# File 'documented/internal_api/active_sessions/client.rb', line 70

def remove(pid)
  request('remove', pid: pid)
end

#request(command, payload = {}) ⇒ Hash

Sends a request to the server with the specified command and payload.

Parameters:

  • command (String)

    the command to send to the server

  • payload (Hash) (defaults to: {})

    the data to include with the command

Returns:

  • (Hash)

    the server's response, including success status and any data or error messages

Raises:

  • (StandardError)

    if an error occurs during the request



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'documented/internal_api/active_sessions/client.rb', line 35

def request(command, payload = {})
  socket = @socket_factory.call(@host, @port)
  socket.write(JSON.dump(command: command, auth: @auth_token, payload: payload) + "\n")
  raw = read_response(socket)
  return { ok: false, error: 'read timeout' } unless raw

  response = JSON.parse(raw.to_s, symbolize_names: true)
  return { ok: false, error: 'invalid response type' } unless response.is_a?(Hash)

  response
rescue StandardError => e
  { ok: false, error: e.message }
ensure
  socket&.close rescue nil
end

#snapshotHash

Sends a snapshot request to the server.

Returns:

  • (Hash)

    the server's response containing the snapshot data



76
77
78
# File 'documented/internal_api/active_sessions/client.rb', line 76

def snapshot
  request('snapshot')
end

#upsert(payload) ⇒ Hash

Sends an upsert request to the server with the provided payload.

Parameters:

  • payload (Hash)

    the data to upsert

Returns:

  • (Hash)

    the server's response to the upsert request



63
64
65
# File 'documented/internal_api/active_sessions/client.rb', line 63

def upsert(payload)
  request('upsert', payload)
end