Class: Lightstreamer::StreamConnection
- Inherits:
-
Object
- Object
- Lightstreamer::StreamConnection
- Defined in:
- lib/lightstreamer/stream_connection.rb
Overview
Manages a long-running Lightstreamer connection that handles incoming streaming data on a separate thread and makes it available for consumption via the #read_line method.
Instance Attribute Summary collapse
-
#control_address ⇒ String
readonly
The control address returned from the server when this stream connection was initiated.
-
#error ⇒ Error
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.
-
#thread ⇒ Thread
readonly
The thread used to process incoming streaming data.
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.
21 22 23 24 25 26 27 |
# File 'lib/lightstreamer/stream_connection.rb', line 21 def initialize(session) @session = session @queue = Queue.new @stream_create_url = URI.join(session.server_url, '/lightstreamer/create_session.txt').to_s @stream_bind_url = URI.join(session.server_url, '/lightstreamer/bind_session.txt').to_s end |
Instance Attribute Details
#control_address ⇒ String (readonly)
Returns The control address returned from the server when this stream connection was initiated.
12 13 14 |
# File 'lib/lightstreamer/stream_connection.rb', line 12 def control_address @control_address end |
#error ⇒ Error (readonly)
Returns If an error occurs on the stream thread that causes the stream to disconnect then the error will be stored in this attribute.
16 17 18 |
# File 'lib/lightstreamer/stream_connection.rb', line 16 def error @error end |
#session_id ⇒ String (readonly)
Returns The session ID returned from the server when this stream connection was initiated.
9 10 11 |
# File 'lib/lightstreamer/stream_connection.rb', line 9 def session_id @session_id end |
#thread ⇒ Thread (readonly)
Returns The thread used to process incoming streaming data.
6 7 8 |
# File 'lib/lightstreamer/stream_connection.rb', line 6 def thread @thread end |
Instance Method Details
#connect ⇒ Object
Establishes a new stream connection using the authentication details from the session that was passed to #initialize. Raises an Error subclass on failure.
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/lightstreamer/stream_connection.rb', line 31 def connect return if @thread @session_id = @error = nil @queue.clear create_stream_thread # Wait until the connection result is known until @session_id || @error end raise @error if @error end |
#connected? ⇒ Boolean
Returns whether or not this stream connection is connected.
48 49 50 |
# File 'lib/lightstreamer/stream_connection.rb', line 48 def connected? !@thread.nil? end |
#disconnect ⇒ Object
Disconnects this stream connection by shutting down the streaming thread.
53 54 55 56 57 58 59 60 |
# File 'lib/lightstreamer/stream_connection.rb', line 53 def disconnect if @thread @thread.exit @thread.join end @session_id = @control_address = @error = @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. If the streaming thread is not active then any unconsumed lines will be returned and after that the return value will be nil.
67 68 69 70 71 |
# File 'lib/lightstreamer/stream_connection.rb', line 67 def read_line return nil if @queue.empty? && @thread.nil? @queue.pop end |