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_messageObject

Returns the value of attribute server_socket_message.



20
21
22
# File 'lib/ollama_chat/server_socket.rb', line 20

def server_socket_message
  @server_socket_message
end

Class Method Details

.runtime_dirObject



3
4
5
# File 'lib/ollama_chat/server_socket.rb', line 3

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

.send_to_server_socket(content) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/ollama_chat/server_socket.rb', line 11

def send_to_server_socket(content)
  FileUtils.mkdir_p runtime_dir
  message = { content: }
  socket = UNIXSocket.new(server_socket_path)
  socket.puts JSON(message)
  socket.close
end

.server_socket_pathObject



7
8
9
# File 'lib/ollama_chat/server_socket.rb', line 7

def server_socket_path
  File.join(runtime_dir, 'ollama_chat.sock')
end

Instance Method Details

#init_server_socketObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ollama_chat/server_socket.rb', line 22

def init_server_socket
  FileUtils.mkdir_p OllamaChat::ServerSocket.runtime_dir
  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
  end
end