Class: UnixSocks::Server

Inherits:
Object
  • Object
show all
Includes:
FileUtils
Defined in:
lib/unix_socks/server.rb

Overview

Manages Unix socket-based communication, providing both server and client functionality.

Instance Method Summary collapse

Constructor Details

#initialize(socket_name:, runtime_dir: default_runtime_dir) ⇒ Server

Initializes a new UnixSocks::Server instance.

Parameters:

  • socket_name (String)

    The name of the server socket file.

  • runtime_dir (String, nil) (defaults to: default_runtime_dir)

    The path to the runtime directory where the server socket will be created. If not provided, it defaults to the value returned by #default_runtime_dir.



12
13
14
# File 'lib/unix_socks/server.rb', line 12

def initialize(socket_name:, runtime_dir: default_runtime_dir)
  @socket_name, @runtime_dir = socket_name, runtime_dir
end

Instance Method Details

#default_runtime_dirString

Returns the default runtime directory path based on the XDG_RUNTIME_DIR environment variable.

If the XDG_RUNTIME_DIR environment variable is set, its value is used as the runtime directory path. Otherwise, the default path ‘~/.local/run’ is used.

Returns:

  • (String)

    The default runtime directory path.



24
25
26
# File 'lib/unix_socks/server.rb', line 24

def default_runtime_dir
  File.expand_path(ENV.fetch('XDG_RUNTIME_DIR',  '~/.local/run'))
end

#receive(force: false) {|UnixSocks::Message| ... } ⇒ Object

Receives messages from clients connected to the server socket.

This method establishes a connection to the server socket and listens for incoming messages. When a message is received, it is parsed as JSON and converted into a UnixSocks::Message object. The block provided to this method is then called with the message object as an argument.

If the ‘force` parameter is set to true, any existing server socket file will be overwritten without raising an error.

Parameters:

  • force (Boolean) (defaults to: false)

    Whether to overwrite any existing server socket file.

Yields:



75
76
77
78
79
80
81
82
83
# File 'lib/unix_socks/server.rb', line 75

def receive(force: false, &block)
  mkdir_p @runtime_dir
  if !force && socket_path_exist?
    raise Errno::EEXIST, "Path already exists #{server_socket_path.inspect}"
  end
  Socket.unix_server_loop(server_socket_path) do |socket, client_addrinfo|
    message = pop_message(socket) and block.(message)
  end
end

#receive_in_background(force: false) {|UnixSocks::Message| ... } ⇒ Object

The receive_in_background method runs the server socket listener in a separate thread, allowing it to continue executing without blocking the main program flow.

Parameters:

  • force (Boolean) (defaults to: false)

    Whether to overwrite any existing server socket file.

Yields:



91
92
93
94
95
96
97
# File 'lib/unix_socks/server.rb', line 91

def receive_in_background(force: false, &block)
  Thread.new do
    receive(force:, &block)
  ensure
    at_exit { remove_socket_path }
  end
end

#remove_socket_pathObject

Safely removes the server socket file from the filesystem.



107
108
109
# File 'lib/unix_socks/server.rb', line 107

def remove_socket_path
  FileUtils.rm_f server_socket_path
end

#server_socket_pathString

Returns the path to the server socket file.

This method constructs the full path to the server socket by joining the runtime directory and the socket name.

Returns:

  • (String)

    The path to the server socket file.



34
35
36
# File 'lib/unix_socks/server.rb', line 34

def server_socket_path
  File.expand_path(File.join(@runtime_dir, @socket_name))
end

#socket_path_exist?Boolean

Checks if the server socket file exists.

Returns:

  • (Boolean)

    True if the socket file exists, false otherwise.



102
103
104
# File 'lib/unix_socks/server.rb', line 102

def socket_path_exist?
  File.exist?(server_socket_path)
end

#transmit(message) ⇒ Object

The transmit method sends a message over the Unix socket connection.

It first prepares the message by converting it to JSON format, and then establishes a connection to the server socket using UNIXSocket.new.

Finally, it writes the prepared message to the socket using socket.puts, ensuring that the socket is properly closed after use.

Parameters:

  • message (Message)

    The message to be sent over the Unix socket.



47
48
49
50
51
52
# File 'lib/unix_socks/server.rb', line 47

def transmit(message)
  mkdir_p @runtime_dir
  socket = UNIXSocket.new(server_socket_path)
  socket.puts JSON(message)
  socket
end

#transmit_with_response(message) ⇒ Hash?

Sends a message and returns the parsed JSON response.

Parameters:

  • message (Object)

    The message to be sent as JSON.

Returns:

  • (Hash, nil)

    The parsed JSON response or nil if parsing fails.



58
59
60
61
# File 'lib/unix_socks/server.rb', line 58

def transmit_with_response(message)
  socket = transmit(message)
  parse_json_message(socket.gets, socket)
end