Class: UnixSocks::DomainSocketServer
- Inherits:
-
Object
- Object
- UnixSocks::DomainSocketServer
- Includes:
- FileUtils, ServerShared
- Defined in:
- lib/unix_socks/domain_socket_server.rb
Overview
Manages Unix socket-based communication, providing both server and client functionality.
Class Method Summary collapse
-
.default_runtime_dir ⇒ String
Returns the default runtime directory path based on the XDG_RUNTIME_DIR environment variable.
Instance Method Summary collapse
-
#default_runtime_dir ⇒ String
Returns the default runtime directory path based on the XDG_RUNTIME_DIR environment variable.
-
#initialize(socket_name:, runtime_dir: default_runtime_dir) ⇒ DomainSocketServer
constructor
Initializes a new UnixSocks::DomainSocketServer instance.
-
#receive(force: false, permission: 0600) {|UnixSocks::Message| ... } ⇒ Object
Receives messages from clients connected to the server socket.
-
#receive_in_background(force: false) {|UnixSocks::Message| ... } ⇒ Thread
Runs the message receiver in a background thread to prevent blocking.
-
#remove_socket_path ⇒ Object
Safely removes the server socket file from the filesystem.
-
#server_socket_path ⇒ String
Returns the path to the server socket file.
-
#socket_path_exist? ⇒ Boolean
Checks if the server socket file exists.
-
#to_url ⇒ String
Returns the URL representation of the server socket configuration.
-
#transmit(message, close: false) ⇒ UNIXSocket
The transmit method sends a message over the Unix socket connection.
Methods included from ServerShared
#to_uri, #transmit_with_response
Constructor Details
#initialize(socket_name:, runtime_dir: default_runtime_dir) ⇒ DomainSocketServer
Initializes a new UnixSocks::DomainSocketServer instance.
13 14 15 |
# File 'lib/unix_socks/domain_socket_server.rb', line 13 def initialize(socket_name:, runtime_dir: default_runtime_dir) @socket_name, @runtime_dir = socket_name, runtime_dir end |
Class Method Details
.default_runtime_dir ⇒ String
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 returned.
48 49 50 |
# File 'lib/unix_socks/domain_socket_server.rb', line 48 def self.default_runtime_dir File.(ENV.fetch('XDG_RUNTIME_DIR', '~/.local/run')) end |
Instance Method Details
#default_runtime_dir ⇒ String
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.
36 37 38 |
# File 'lib/unix_socks/domain_socket_server.rb', line 36 def default_runtime_dir self.class.default_runtime_dir end |
#receive(force: false, permission: 0600) {|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.
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/unix_socks/domain_socket_server.rb', line 93 def receive(force: false, permission: 0600, &block) block or raise ArgumentError, 'require &block argument to receive messages' mkdir_p @runtime_dir if !force && socket_path_exist? raise UnixSocks::ServerError.build( Errno::EEXIST, "Path already exists #{server_socket_path.inspect}" ) end loop_for_path(server_socket_path, permission:, &block) end |
#receive_in_background(force: false) {|UnixSocks::Message| ... } ⇒ Thread
Runs the message receiver in a background thread to prevent blocking.
This method starts a new thread that continuously listens for incoming messages from connected clients. The server socket is created in the background, allowing the main execution flow to continue without waiting for messages.
115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/unix_socks/domain_socket_server.rb', line 115 def receive_in_background(force: false, &block) if !force && socket_path_exist? raise UnixSocks::ServerError.build( Errno::EEXIST, "Path already exists #{server_socket_path.inspect}" ) end Thread.new do receive(force:, &block) rescue Errno::ENOENT ensure at_exit { remove_socket_path } end end |
#remove_socket_path ⇒ Object
Safely removes the server socket file from the filesystem.
137 138 139 |
# File 'lib/unix_socks/domain_socket_server.rb', line 137 def remove_socket_path FileUtils.rm_f server_socket_path end |
#server_socket_path ⇒ String
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.
58 59 60 |
# File 'lib/unix_socks/domain_socket_server.rb', line 58 def server_socket_path File.(File.join(@runtime_dir, @socket_name)) end |
#socket_path_exist? ⇒ Boolean
Checks if the server socket file exists.
132 133 134 |
# File 'lib/unix_socks/domain_socket_server.rb', line 132 def socket_path_exist? File.exist?(server_socket_path) end |
#to_url ⇒ String
Returns the URL representation of the server socket configuration.
This method constructs and returns a URL string in the format “unix://path” that represents the Unix socket server’s file path configuration.
24 25 26 |
# File 'lib/unix_socks/domain_socket_server.rb', line 24 def to_url "unix://#{server_socket_path}" end |
#transmit(message, close: false) ⇒ UNIXSocket
The transmit method sends a message over the Unix socket connection
This method prepares a message by converting it to JSON format, establishes a connection to the server socket using UNIXSocket.new, writes the prepared message to the socket, and then returns the created socket
72 73 74 75 76 77 78 79 |
# File 'lib/unix_socks/domain_socket_server.rb', line 72 def transmit(, close: false) mkdir_p @runtime_dir socket = UNIXSocket.new(server_socket_path) socket.puts JSON() socket ensure close and socket&.close end |