Class: Backport::Client

Inherits:
Object
  • Object
show all
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.



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

#adapterAdapter (readonly)

Returns:



6
7
8
# File 'lib/backport/client.rb', line 6

def adapter
  @adapter
end

Instance Method Details

#readString?

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

Returns:

  • (String, nil)


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.

Parameters:

  • data (String)


47
48
49
# File 'lib/backport/client.rb', line 47

def sending data
  @adapter.sending data
end

#startObject 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

#stopObject

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

Returns:

  • (Boolean)


16
17
18
# File 'lib/backport/client.rb', line 16

def stopped?
  @stopped ||= false
end