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
33
34
35
# File 'lib/lightstreamer/stream_connection.rb', line 26

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

  @connect_result_mutex = Mutex.new
  @connect_result_condition_variable = ConditionVariable.new
end

Instance Attribute Details

#control_addressString? (readonly)

The control address returned from the server when this stream connection was initiated.

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:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/lightstreamer/stream_connection.rb', line 39

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)


57
58
59
# File 'lib/lightstreamer/stream_connection.rb', line 57

def connected?
  !@thread.nil?
end

#disconnectObject

Disconnects this stream connection by shutting down the streaming thread.



62
63
64
65
66
67
68
69
# File 'lib/lightstreamer/stream_connection.rb', line 62

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)


76
77
78
79
80
# File 'lib/lightstreamer/stream_connection.rb', line 76

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

  @queue.pop
end