Class: Backport::Adapter

Inherits:
Object
  • Object
show all
Defined in:
lib/backport/adapter.rb

Overview

The application interface between Backport servers and clients.

Instance Method Summary collapse

Constructor Details

#initialize(output, remote = {}) ⇒ Adapter

Returns a new instance of Adapter.

Parameters:

  • output (IO)
  • remote (Hash{Symbol => String, Integer}) (defaults to: {})


7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/backport/adapter.rb', line 7

def initialize output, remote = {}
  # Store internal data in a singleton method to avoid instance variable
  # collisions in custom adapters
  data = {
    out: output,
    remote: remote,
    closed: false
  }
  define_singleton_method :_data do
    data
  end
end

Instance Method Details

#closeObject

Note:

The adapter sets #closed? to true and runs the #closing callback. The server is responsible for implementation details like closing the client’s socket.

Close the client connection.



79
80
81
82
83
84
# File 'lib/backport/adapter.rb', line 79

def close
  return if closed?
  _data[:closed] = true
  _data[:on_close].call unless _data[:on_close].nil?
  closing
end

#closed?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/backport/adapter.rb', line 69

def closed?
  _data[:closed] ||= false
end

#closingvoid

This method returns an undefined value.

A callback triggered when a client connection is closing. Subclasses and/or modules should override this method to provide their own functionality.



41
# File 'lib/backport/adapter.rb', line 41

def closing; end

#openingvoid

This method returns an undefined value.

A callback triggered when a client connection is opening. Subclasses and/or modules should override this method to provide their own functionality.



34
# File 'lib/backport/adapter.rb', line 34

def opening; end

#receiving(data) ⇒ void

This method returns an undefined value.

A callback triggered when the server receives data from the client. Subclasses and/or modules should override this method to provide their own functionality.

Parameters:

  • data (String)


49
# File 'lib/backport/adapter.rb', line 49

def receiving(data); end

#remoteHash{Symbol => String, Integer}

A hash of information about the client connection. The data can vary based on the transport, e.g., :hostname and :address for TCP connections or :filename for file streams.

Returns:

  • (Hash{Symbol => String, Integer})


25
26
27
# File 'lib/backport/adapter.rb', line 25

def remote
  _data[:remote]
end

#write(data) ⇒ void

This method returns an undefined value.

Send data to the client.

Parameters:

  • data (String)


55
56
57
58
# File 'lib/backport/adapter.rb', line 55

def write data
  _data[:out].write data
  _data[:out].flush
end

#write_line(data) ⇒ void

This method returns an undefined value.

Send a line of data to the client.

Parameters:

  • data (String)


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

def write_line data
  _data[:out].puts data
  _data[:out].flush
end