class Backport::Client

A client connected to a connectable Backport server.

Attributes

adapter[R]

@return [Adapter]

mutex[R]

@return [Mutex]

Public Class Methods

new(input, output, adapter, remote = {}) click to toggle source

@param input [IO] @param output [IO] @param adapter [Class, Module] @param remote [Hash]

# File lib/backport/client.rb, line 16
def initialize input, output, adapter, remote = {}
  @in = input
  @out = output
  @mutex = Mutex.new
  @adapter = make_adapter(adapter, remote)
  @stopped = true
  @buffer = ''
end

Public Instance Methods

run()

@deprecated Prefer start to run for non-blocking client/server methods

Alias for: start
start() click to toggle source

Start running the client. This method will start the thread that reads client input from IO.

@return [void]

# File lib/backport/client.rb, line 50
def start
  return unless stopped?
  @stopped = false
  @adapter.opening
  run_input_thread
end
Also aliased as: run
stop() click to toggle source

Close the client connection.

@note The client sets stopped? to true and runs the adapter's closing callback. The server is responsible for implementation details like closing the client's socket.

@return [void]

# File lib/backport/client.rb, line 38
def stop
  return if stopped?
  @adapter.closing
  @stopped = true
  changed
  notify_observers self
end
stopped?() click to toggle source

True if the client is stopped.

# File lib/backport/client.rb, line 27
def stopped?
  @stopped ||= false
end
tick() click to toggle source

Handle a tick from the server. This method will check for client input and update the adapter accordingly, or stop the client if the adapter is closed.

@return [void]

# File lib/backport/client.rb, line 64
def tick
  input = read
  @adapter.receiving input unless input.nil?
end

Private Instance Methods

make_adapter(mod_cls, remote) click to toggle source

@param mod_cls [Module, Class] The Adapter module or class @param remote [Hash] Remote client data @return [Adapter]

# File lib/backport/client.rb, line 86
def make_adapter mod_cls, remote
  if mod_cls.is_a?(Class) && mod_cls <= Backport::Adapter
    @adapter = mod_cls.new(@out, remote)
  elsif mod_cls.class == Module
    @adapter = Adapter.new(@out, remote)
    @adapter.extend mod_cls
  else
    raise TypeError, "#{mod_cls} is not a valid Backport adapter"
  end
end
read() click to toggle source

Read the client input. Return nil if the input buffer is empty.

@return [String, nil]

# File lib/backport/client.rb, line 74
def read
  tmp = nil
  mutex.synchronize do
    tmp = @buffer.dup
    @buffer.clear
  end
  return tmp unless tmp.empty?
end
read_input() click to toggle source

Read input from the client.

@return [void]

# File lib/backport/client.rb, line 112
def read_input
  begin
    @in.flush
    chars = @in.sysread(255)
  rescue EOFError, IOError, Errno::ECONNRESET, Errno::ENOTSOCK
    chars = nil
  end
  if chars.nil?
    stop
  else
    mutex.synchronize { @buffer.concat chars }
    changed
    notify_observers self
  end
end
run_input_thread() click to toggle source

Start the thread that checks the input IO for client data.

@return [void]

# File lib/backport/client.rb, line 103
def run_input_thread
  Thread.new do
    read_input until stopped?
  end
end