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

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

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 =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

1

Instance Method Summary collapse

Constructor Details

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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