Class: Backport::Client

Inherits:
Object
  • Object
show all
Includes:
Observable
Defined in:
lib/backport/client.rb

Overview

A client connected to a connectable Backport server.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, output, adapter, remote = {}) ⇒ Client

Returns a new instance of Client.

Parameters:

  • input (IO)
  • output (IO)
  • adapter (Class, Module)
  • remote (Hash) (defaults to: {})


16
17
18
19
20
21
22
23
# 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

Instance Attribute Details

#adapterAdapter (readonly)

Returns:



10
11
12
# File 'lib/backport/client.rb', line 10

def adapter
  @adapter
end

Instance Method Details

#startvoid Also known as: run

This method returns an undefined value.

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



50
51
52
53
54
55
# File 'lib/backport/client.rb', line 50

def start
  return unless stopped?
  @stopped = false
  @adapter.opening
  run_input_thread
end

#stopvoid

Note:

The client sets #stopped? to true and runs the adapter’s #closing

This method returns an undefined value.

Close the client connection.

callback. The server is responsible for implementation details like closing the client’s socket.



38
39
40
41
42
43
44
# File 'lib/backport/client.rb', line 38

def stop
  return if stopped?
  @adapter.closing
  @stopped = true
  changed
  notify_observers self
end

#stopped?Boolean

True if the client is stopped.

Returns:

  • (Boolean)


27
28
29
# File 'lib/backport/client.rb', line 27

def stopped?
  @stopped ||= false
end

#tickvoid

This method returns an undefined value.

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.



64
65
66
67
# File 'lib/backport/client.rb', line 64

def tick
  input = read
  @adapter.receiving input unless input.nil?
end