Class: Lightstreamer::StreamConnection

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

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.



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

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

Returns:

  • (String)

    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

#errorError (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.

Returns:

  • (Error)

    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_idString (readonly)

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

Returns:

  • (String)

    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

#threadThread (readonly)

Returns The thread used to process incoming streaming data.

Returns:

  • (Thread)

    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

#connectObject

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

Raises:



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.

Returns:

  • (Boolean)


48
49
50
# File 'lib/lightstreamer/stream_connection.rb', line 48

def connected?
  !@thread.nil?
end

#disconnectObject

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_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)


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