Class: Backport::Client
- Inherits:
-
Object
- Object
- Backport::Client
- Defined in:
- lib/backport/client.rb
Overview
A client connected to a connectable Backport server.
Instance Attribute Summary collapse
- #adapter ⇒ Adapter readonly
Instance Method Summary collapse
-
#initialize(input, output, adapter, remote = {}) ⇒ Client
constructor
A new instance of Client.
-
#read ⇒ String?
Read the client input.
-
#sending(data) ⇒ Object
Notify the adapter that the client is sending data.
-
#start ⇒ Object
(also: #run)
Start running the client.
-
#stop ⇒ Object
Close the client connection.
- #stopped? ⇒ Boolean
Constructor Details
#initialize(input, output, adapter, remote = {}) ⇒ Client
Returns a new instance of Client.
8 9 10 11 12 13 14 |
# File 'lib/backport/client.rb', line 8 def initialize input, output, adapter, remote = {} @in = input @out = output @adapter = make_adapter(adapter, remote) @stopped = true @buffer = '' end |
Instance Attribute Details
#adapter ⇒ Adapter (readonly)
6 7 8 |
# File 'lib/backport/client.rb', line 6 def adapter @adapter end |
Instance Method Details
#read ⇒ String?
Read the client input. Return nil if the input buffer is empty.
54 55 56 57 58 59 60 61 |
# File 'lib/backport/client.rb', line 54 def read tmp = nil mutex.synchronize do tmp = @buffer.dup @buffer.clear end return tmp unless tmp.empty? end |
#sending(data) ⇒ Object
Notify the adapter that the client is sending data.
47 48 49 |
# File 'lib/backport/client.rb', line 47 def sending data @adapter.sending data end |
#start ⇒ Object Also known as: run
Start running the client. This method will start the thread that reads client input from IO.
35 36 37 38 39 40 |
# File 'lib/backport/client.rb', line 35 def start return unless stopped? @stopped = false @adapter.opening run_input_thread end |
#stop ⇒ Object
Note:
The client sets #stopped? to true and runs the adapter’s #closing
Close the client connection.
callback. The server is responsible for implementation details like closing the client’s socket.
26 27 28 29 30 |
# File 'lib/backport/client.rb', line 26 def stop return if stopped? @adapter.closing @stopped = true end |
#stopped? ⇒ Boolean
16 17 18 |
# File 'lib/backport/client.rb', line 16 def stopped? @stopped ||= false end |