Class: Backport::Adapter
- Inherits:
-
Object
- Object
- Backport::Adapter
- Defined in:
- lib/backport/adapter.rb
Overview
The application interface between Backport servers and clients.
Instance Method Summary collapse
-
#close ⇒ Object
Close the client connection.
- #closed? ⇒ Boolean
-
#closing ⇒ void
A callback triggered when a client connection is closing.
-
#initialize(output, remote = {}) ⇒ Adapter
constructor
A new instance of Adapter.
-
#opening ⇒ void
A callback triggered when a client connection is opening.
-
#receiving(data) ⇒ void
A callback triggered when the server receives data from the client.
-
#remote ⇒ Hash{Symbol => String, Integer}
A hash of information about the client connection.
-
#write(data) ⇒ void
Send data to the client.
-
#write_line(data) ⇒ void
Send a line of data to the client.
Constructor Details
#initialize(output, remote = {}) ⇒ Adapter
Returns a new instance of Adapter.
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
#close ⇒ Object
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
69 70 71 |
# File 'lib/backport/adapter.rb', line 69 def closed? _data[:closed] ||= false end |
#closing ⇒ void
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 |
#opening ⇒ void
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.
49 |
# File 'lib/backport/adapter.rb', line 49 def receiving(data); end |
#remote ⇒ Hash{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.
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.
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.
64 65 66 67 |
# File 'lib/backport/adapter.rb', line 64 def write_line data _data[:out].puts data _data[:out].flush end |