Class: Lightstreamer::StreamConnection

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

Instance Method Summary collapse

Constructor Details

#initialize(session) ⇒ StreamConnection

Establishes a new stream connection using the authentication details from the passed session.

Parameters:

  • session (Session)

    The session to create a stream connection for.



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_addressString? (readonly)

The control address to use for this stream connection.

Returns:

  • (String, nil)


15
16
17
# File 'lib/lightstreamer/stream_connection.rb', line 15

def control_address
  @control_address
end

#errorLightstreamerError? (readonly)

If an error occurs on the stream thread that causes the stream to disconnect then the error will be stored in this attribute.

Returns:



21
22
23
# File 'lib/lightstreamer/stream_connection.rb', line 21

def error
  @error
end

#session_idString? (readonly)

The session ID returned from the server when this stream connection was initiated.

Returns:

  • (String, nil)


10
11
12
# File 'lib/lightstreamer/stream_connection.rb', line 10

def session_id
  @session_id
end

Instance Method Details

#connectObject

Establishes a new stream connection using the authentication details from the session that was passed to #initialize. Raises a LightstreamerError subclass on failure.

Raises:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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.

Returns:

  • (Boolean)


54
55
56
# File 'lib/lightstreamer/stream_connection.rb', line 54

def connected?
  !@thread.nil?
end

#disconnectObject

Disconnects this stream connection by shutting down the streaming thread.



59
60
61
62
63
64
65
66
# File 'lib/lightstreamer/stream_connection.rb', line 59

def disconnect
  return unless @thread

  @thread.exit
  @thread.join

  @thread = nil
end

#read_lineString?

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.

Returns:

  • (String, nil)


73
74
75
76
77
# File 'lib/lightstreamer/stream_connection.rb', line 73

def read_line
  return nil if @queue.empty? && @thread.nil?

  @queue.pop
end