Sha256: c0e679057a2b31b1fee34c0e4f62cf7da7d80061703708483084a0167a68bbc5

Contents?: true

Size: 1.53 KB

Versions: 3

Compression:

Stored size: 1.53 KB

Contents

require 'sanford-protocol/fake_socket'

class SimpleClient

  def self.call_with_request(service_host, name, params)
    self.new(service_host).call_with_request(name, params)
  end

  def self.call_with_msg_body(service_host, *args)
    self.new(service_host).call_with_msg_body(*args)
  end

  def self.call_with_encoded_msg_body(service_host, *args)
    self.new(service_host).call_with_encoded_msg_body(*args)
  end

  def self.call_with(service_host, bytes)
    self.new(service_host).call(bytes)
  end

  def initialize(service_host, options = {})
    @host, @port = service_host.ip, service_host.port
    @delay = options[:with_delay]
  end

  def call_with_request(*args)
    self.call_using_fake_socket(:with_request, *args)
  end

  def call_with_msg_body(*args)
    self.call_using_fake_socket(:with_msg_body, *args)
  end

  def call_with_encoded_msg_body(*args)
    self.call_using_fake_socket(:with_encoded_msg_body, *args)
  end

  def call_with_keep_alive
    socket = TCPSocket.new(@host, @port)
  ensure
    socket.close rescue false
  end

  def call(bytes)
    socket = TCPSocket.new(@host, @port)
    socket.setsockopt(::Socket::IPPROTO_TCP, ::Socket::TCP_NODELAY, true)
    connection = Sanford::Protocol::Connection.new(socket)
    sleep(@delay) if @delay
    socket.send(bytes, 0)
    socket.close_write
    Sanford::Protocol::Response.parse(connection.read)
  ensure
    socket.close rescue false
  end

  protected

  def call_using_fake_socket(method, *args)
    self.call(Sanford::Protocol::FakeSocket.send(method, *args).in)
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
sanford-0.10.1 test/support/simple_client.rb
sanford-0.10.0 test/support/simple_client.rb
sanford-0.9.0 test/support/simple_client.rb