Module: OllamaChat::ServerSocket
- Included in:
- Chat
- Defined in:
- lib/ollama_chat/server_socket.rb
Instance Attribute Summary collapse
-
#server_socket_message ⇒ String?
Accessor for the server socket message.
Class Method Summary collapse
-
.runtime_dir ⇒ String
Returns the path to the XDG runtime directory, or a default path if not set.
-
.send_to_server_socket(content, type: :socket_input) ⇒ Object
Sends a message to the server socket.
-
.server_socket_path ⇒ String
Constructs the full path to the server socket file.
Instance Method Summary collapse
-
#init_server_socket ⇒ Object
Initializes a Unix domain socket server for OllamaChat.
Instance Attribute Details
#server_socket_message ⇒ String?
Accessor for the server socket message. Holds the last message received from the Unix socket.
35 36 37 |
# File 'lib/ollama_chat/server_socket.rb', line 35 def @server_socket_message end |
Class Method Details
.runtime_dir ⇒ String
Returns the path to the XDG runtime directory, or a default path if not set.
5 6 7 |
# File 'lib/ollama_chat/server_socket.rb', line 5 def runtime_dir File.(ENV.fetch('XDG_RUNTIME_DIR', '~/.local/run')) end |
.send_to_server_socket(content, type: :socket_input) ⇒ Object
Sends a message to the server socket.
21 22 23 24 25 26 27 |
# File 'lib/ollama_chat/server_socket.rb', line 21 def send_to_server_socket(content, type: :socket_input) FileUtils.mkdir_p runtime_dir = { content:, type: } socket = UNIXSocket.new(server_socket_path) socket.puts JSON() socket.close end |
.server_socket_path ⇒ String
Constructs the full path to the server socket file.
11 12 13 |
# File 'lib/ollama_chat/server_socket.rb', line 11 def server_socket_path File.join(runtime_dir, 'ollama_chat.sock') end |
Instance Method Details
#init_server_socket ⇒ Object
Initializes a Unix domain socket server for OllamaChat.
Creates the necessary runtime directory, checks for existing socket file, and starts a server loop in a new thread. Listens for incoming connections, reads JSON data, and terminates the server upon receiving a message.
Raises Errno::EEXIST if the socket path already exists.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/ollama_chat/server_socket.rb', line 44 def init_server_socket FileUtils.mkdir_p OllamaChat::ServerSocket.runtime_dir if File.exist?(OllamaChat::ServerSocket.server_socket_path) raise Errno::EEXIST, "Path already exists #{OllamaChat::ServerSocket.server_socket_path.inspect}" end Thread.new do Socket.unix_server_loop(OllamaChat::ServerSocket.server_socket_path) do |sock, client_addrinfo| begin data = sock.readline.chomp self. = JSON.load(data) Process.kill :INT, $$ rescue JSON::ParserError ensure sock.close end end rescue Errno::ENOENT ensure FileUtils.rm_f OllamaChat::ServerSocket.server_socket_path end end |