Class: UnixSocks::Server
- Inherits:
-
Object
- Object
- UnixSocks::Server
- 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
-
#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) ⇒ Server
constructor
Initializes a new UnixSocks::Server instance.
-
#receive(force: false) {|UnixSocks::Message| ... } ⇒ Object
Receives messages from clients connected to the server socket.
-
#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.
-
#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.
-
#transmit(message) ⇒ Object
The transmit method sends a message over the Unix socket connection.
-
#transmit_with_response(message) ⇒ Hash?
Sends a message and returns the parsed JSON response.
Constructor Details
#initialize(socket_name:, runtime_dir: default_runtime_dir) ⇒ Server
Initializes a new UnixSocks::Server instance.
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_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.
24 25 26 |
# File 'lib/unix_socks/server.rb', line 24 def default_runtime_dir File.(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.
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| = (socket) and block.() 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.
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_path ⇒ Object
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_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.
34 35 36 |
# File 'lib/unix_socks/server.rb', line 34 def server_socket_path File.(File.join(@runtime_dir, @socket_name)) end |
#socket_path_exist? ⇒ Boolean
Checks if the server socket file exists.
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.
47 48 49 50 51 52 |
# File 'lib/unix_socks/server.rb', line 47 def transmit() mkdir_p @runtime_dir socket = UNIXSocket.new(server_socket_path) socket.puts JSON() socket end |
#transmit_with_response(message) ⇒ Hash?
Sends a message and returns the parsed JSON response.
58 59 60 61 |
# File 'lib/unix_socks/server.rb', line 58 def transmit_with_response() socket = transmit() (socket.gets, socket) end |