Class: Takagi::Client

Inherits:
ClientBase show all
Extended by:
Forwardable
Defined in:
lib/takagi/client.rb,
lib/takagi/client/response.rb,
sig/takagi/client.rbs,
sig/takagi/client/response.rbs

Overview

Unified Takagi Client for communicating with Takagi servers over CoAP.

Supports multiple protocols (UDP, TCP) with automatic protocol detection based on URI scheme or explicit protocol parameter.

Examples:

Block-based with protocol auto-detection (recommended)

Takagi::Client.new('coap://localhost:5683') do |client|
  client.get('/temperature')
end

Block-based with explicit protocol

Takagi::Client.new('localhost:5683', protocol: :tcp) do |client|
  client.get('/temperature')
end

Manual lifecycle management

client = Takagi::Client.new('coap://localhost:5683')
begin
  client.get('/temperature')
ensure
  client.close
end

Defined Under Namespace

Classes: Response

Instance Attribute Summary

Attributes inherited from ClientBase

#callbacks, #server_uri, #timeout

Instance Method Summary collapse

Methods inherited from ClientBase

#cleanup_resources, #close, #closed?, #delete, #deliver_response, #get, #get_json, #observe, #on, open, #parse_json_response, #post, #post_json, #put, #put_json, #request

Constructor Details

#initialize(server_uri, timeout: 5, protocol: nil, use_retransmission: true) {|client| ... } ⇒ Client, Object

Creates a new client and optionally yields it to a block.

Examples:

Protocol auto-detection from URI

client = Takagi::Client.new('coap://localhost:5683')      # Uses UDP
client = Takagi::Client.new('coap+tcp://localhost:5683')  # Uses TCP

Explicit protocol specification

client = Takagi::Client.new('localhost:5683', protocol: :tcp)
client = Takagi::Client.new('localhost:5683', protocol: :udp)

With block (auto-close)

Takagi::Client.new('coap://localhost:5683') do |client|
  client.get('/resource')
end

Yields:

  • (client)

    Optionally yields the client to a block and auto-closes afterward



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/takagi/client.rb', line 60

def initialize(server_uri, timeout: 5, protocol: nil, use_retransmission: true)
  # Detect protocol from URI if not explicitly specified
  @protocol = protocol || detect_protocol(server_uri)

  # Delegate to the appropriate client implementation
  @impl = create_client_impl(server_uri, timeout, use_retransmission)

  # If a block is given, yield and auto-close
  return unless block_given?

  begin
    yield(self)
  ensure
    close
  end
end

Instance Method Details

#create_client_impl(server_uri, timeout, use_retransmission) ⇒ UdpClient, TcpClient

Creates the appropriate client implementation based on protocol



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/takagi/client.rb', line 108

def create_client_impl(server_uri, timeout, use_retransmission)
  # Normalize URI to include scheme if not present
  normalized_uri = normalize_uri(server_uri)

  case @protocol
  when :tcp
    require_relative 'tcp_client'
    TcpClient.new(normalized_uri, timeout: timeout)
  when :udp
    UdpClient.new(normalized_uri, timeout: timeout, use_retransmission: use_retransmission)
  else
    raise ArgumentError, "Unknown protocol: #{@protocol}. Use :udp or :tcp"
  end
end

#detect_protocol(uri_string) ⇒ Symbol

Detects the protocol from the URI scheme



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/takagi/client.rb', line 82

def detect_protocol(uri_string)
  uri = URI(uri_string.start_with?('coap') ? uri_string : "coap://#{uri_string}")

  # NEW: Use transport registry to find transport for scheme
  transport = Takagi::Network::Registry.for_scheme(uri.scheme)

  if transport
    # Map transport class to symbol
    case transport.scheme
    when 'coap+tcp', 'coaps+tcp'
      :tcp
    else
      :udp
    end
  else
    :udp # Default to UDP if transport not found
  end
rescue URI::InvalidURIError, Takagi::Network::Registry::TransportNotFoundError
  :udp # Default to UDP if URI parsing fails or transport not found
end

#normalize_uri(uri_string) ⇒ String

Normalizes URI to include appropriate scheme



126
127
128
129
130
131
# File 'lib/takagi/client.rb', line 126

def normalize_uri(uri_string)
  return uri_string if uri_string.start_with?('coap')

  scheme = @protocol == :tcp ? 'coap+tcp' : 'coap'
  "#{scheme}://#{uri_string}"
end