Class: Lightstreamer::StreamConnection
- Inherits:
-
Object
- Object
- Lightstreamer::StreamConnection
- Defined in:
- lib/lightstreamer/stream_connection.rb
Overview
Internal class used by Session that manages a long-running Lightstreamer connection and handles incoming streaming data on a separate thread and makes it available for consumption through #read_line.
Instance Attribute Summary collapse
-
#control_address ⇒ String?
readonly
The control address to use for this stream connection.
-
#error ⇒ LightstreamerError?
readonly
If an error occurs on the stream thread that causes the stream to disconnect then the error will be stored in this attribute.
-
#session_id ⇒ String?
readonly
The session ID returned from the server when this stream connection was initiated.
Instance Method Summary collapse
-
#connect ⇒ Object
Establishes a new stream connection using the authentication details from the session that was passed to #initialize.
-
#connected? ⇒ Boolean
Returns whether or not this stream connection is connected.
-
#disconnect ⇒ Object
Disconnects this stream connection by shutting down the streaming thread.
-
#initialize(session) ⇒ StreamConnection
constructor
Establishes a new stream connection using the authentication details from the passed session.
-
#read_line ⇒ String?
Reads the next line of streaming data.
Constructor Details
#initialize(session) ⇒ StreamConnection
Establishes a new stream connection using the authentication details from the passed session.
26 27 28 29 30 31 32 |
# File 'lib/lightstreamer/stream_connection.rb', line 26 def initialize(session) @session = session @queue = Queue.new @connect_result_mutex = Mutex.new @connect_result_condition_variable = ConditionVariable.new end |
Instance Attribute Details
#control_address ⇒ String? (readonly)
The control address to use for this stream connection.
15 16 17 |
# File 'lib/lightstreamer/stream_connection.rb', line 15 def control_address @control_address end |
#error ⇒ LightstreamerError? (readonly)
If an error occurs on the stream thread that causes the stream to disconnect then the error will be stored in this attribute.
21 22 23 |
# File 'lib/lightstreamer/stream_connection.rb', line 21 def error @error end |
#session_id ⇒ String? (readonly)
The session ID returned from the server when this stream connection was initiated.
10 11 12 |
# File 'lib/lightstreamer/stream_connection.rb', line 10 def session_id @session_id end |
Instance Method Details
#connect ⇒ Object
Establishes a new stream connection using the authentication details from the session that was passed to #initialize. Raises a LightstreamerError subclass on failure.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/lightstreamer/stream_connection.rb', line 36 def connect return if @thread @queue.clear @connect_result_mutex.synchronize do create_stream_thread @connect_result_condition_variable.wait @connect_result_mutex end return unless @error @thread = nil raise @error end |
#connected? ⇒ Boolean
Returns whether or not this stream connection is connected.
55 56 57 |
# File 'lib/lightstreamer/stream_connection.rb', line 55 def connected? !@thread.nil? end |
#disconnect ⇒ Object
Disconnects this stream connection by shutting down the streaming thread.
60 61 62 63 64 65 66 67 |
# File 'lib/lightstreamer/stream_connection.rb', line 60 def disconnect return unless @thread @thread.exit @thread.join @thread = nil end |
#read_line ⇒ String?
Reads the next line of streaming data. If the streaming thread is alive then this method blocks the calling thread until a line of data is available or the streaming thread terminates for any reason. If the streaming thread is not active then any unconsumed lines will be returned and after that the return value will be ‘nil`.
74 75 76 77 78 |
# File 'lib/lightstreamer/stream_connection.rb', line 74 def read_line return nil if @queue.empty? && @thread.nil? @queue.pop end |