Module: OllamaChat::ServerSocket

Included in:
Chat
Defined in:
lib/ollama_chat/server_socket.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#server_socket_messageString?

Accessor for the server socket message. Holds the last message received from the Unix socket.

Returns:

  • (String, nil)

    the message content, or nil if not set

See Also:



35
36
37
# File 'lib/ollama_chat/server_socket.rb', line 35

def server_socket_message
  @server_socket_message
end

Class Method Details

.runtime_dirString

Returns the path to the XDG runtime directory, or a default path if not set.

Returns:

  • (String)

    the expanded path to the XDG runtime directory



5
6
7
# File 'lib/ollama_chat/server_socket.rb', line 5

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

.send_to_server_socket(content, type: :socket_input) ⇒ Object

Sends a message to the server socket.

Parameters:

  • content (String)

    the content to send

  • type (Symbol) (defaults to: :socket_input)

    the type of message (default: :socket_input)

Raises:

  • (Errno::ENOENT)

    if the socket file does not exist

  • (Errno::ECONNREFUSED)

    if the socket is not listening (server no running)



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
  message = { content:, type: }
  socket = UNIXSocket.new(server_socket_path)
  socket.puts JSON(message)
  socket.close
end

.server_socket_pathString

Constructs the full path to the server socket file.

Returns:

  • (String)

    the full path to the Unix socket



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_socketObject

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.server_socket_message = 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