Sha256: d120bd0d5012e9169f9c47509c2d17a34e58ee530150fd32f4d4f6abbba3168f

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 KB

Contents

require 'net/tcp_client'
require 'netstring'
module JRPC
  class TcpClient < BaseClient
    attr_reader :namespace
    def_delegators :@socket, :logger, :logger=, :close, :alive?

    def initialize(uri, options = {})
      super
      @namespace = options.delete(:namespace).to_s
      t = @options.fetch(:timeout, 5)

      @socket = Net::TCPClient.new server: @uri,
                                   connect_retry_count: t,
                                   connect_timeout: t,
                                   read_timeout: t,
                                   # write_timeout: t,
                                   buffered: false # recommended for RPC
    end

    private

    def send_command(request)
      send_request(request)
      receive_response
    end

    def send_notification(request)
      send_request(request)
    end

    def create_message(method, params)
      super("#{namespace}#{method}", params)
    end

    def send_request(request)
      @socket.send Netstring.dump(request.to_s), 0
    end

    def receive_response
      length = get_msg_length
      response = @socket.read(length+1)
      raise ClientError.new('invalid response. missed comma as terminator') if response[-1] != ','
      response.chomp(',')
    end

    def get_msg_length
      length = ''
      while true do
        character = @socket.read(1)
        break if character == ':'
        length += character
      end

      Integer(length)
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jrpc-0.3.0 lib/jrpc/tcp_client.rb