Class: Utcp::Providers::UdpProvider

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

Overview

UDP provider (best-effort, may drop packets) tool.provider: { “provider_type”:“udp”, “host”:“127.0.0.1”,“port”:5002,

"message_template":"ping ${name}", "timeout_ms": 1000, "max_bytes": 2048 }

Instance Attribute Summary

Attributes inherited from BaseProvider

#auth, #name, #type

Instance Method Summary collapse

Constructor Details

#initialize(name:) ⇒ UdpProvider

Returns a new instance of UdpProvider.



13
14
15
# File 'lib/utcp/providers/udp_provider.rb', line 13

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

Instance Method Details

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



21
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
47
48
49
50
# File 'lib/utcp/providers/udp_provider.rb', line 21

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

  udp = UDPSocket.new
  udp.connect(host, port)
  begin
    udp.send(msg.to_s, 0)
    if block_given?
      if IO.select([udp], nil, nil, timeout)
        data, _ = udp.recvfrom(max_bytes)
        yield data
      end
      nil
    else
      if IO.select([udp], nil, nil, timeout)
        data, _ = udp.recvfrom(max_bytes)
        data
      else
        nil
      end
    end
  ensure
    udp.close rescue nil
  end
end

#discover_tools!Object

Raises:



17
18
19
# File 'lib/utcp/providers/udp_provider.rb', line 17

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