Class: Utcp::Providers::TcpProvider

Inherits:
BaseProvider show all
Defined in:
lib/utcp/providers/tcp_provider.rb

Overview

Raw TCP provider tool.provider: { “provider_type”:“tcp”, “host”:“127.0.0.1”,“port”:5001,

"message_template":"hello ${name}\n",
"read_until":"\n", "timeout_ms": 2000 }

Instance Attribute Summary

Attributes inherited from BaseProvider

#auth, #name, #type

Instance Method Summary collapse

Constructor Details

#initialize(name:) ⇒ TcpProvider



14
15
16
# File 'lib/utcp/providers/tcp_provider.rb', line 14

def initialize(name:)
  super(name: name, provider_type: "tcp", auth: nil)
end

Instance Method Details

#call_tool(tool, arguments = {}, &block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/utcp/providers/tcp_provider.rb', line 22

def call_tool(tool, arguments = {}, &block)
  p = tool.provider
  host = p["host"] || "127.0.0.1"
  port = (p["port"] || 5001).to_i
  timeout = (p["timeout_ms"] || 2000).to_i / 1000.0
  msg = compose_message(p, arguments)

  socket = TCPSocket.new(host, port)
  begin
    socket.write(msg) if msg
    if block_given?
      stream_read(socket, timeout: timeout) { |chunk| yield chunk }
      nil
    else
      read_until = p["read_until"]
      if read_until
        read_until_delim(socket, read_until, timeout: timeout)
      else
        socket.read
      end
    end
  ensure
    socket.close rescue nil
  end
end

#discover_tools!Object

Raises:



18
19
20
# File 'lib/utcp/providers/tcp_provider.rb', line 18

def discover_tools!
  raise ProviderError, "TCP is an execution provider only"
end