Class: Lightstreamer::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/lightstreamer/session.rb

Overview

This class is responsible for managing a Lightstreamer session, and along with the Subscription class is the primary interface for working with Lightstreamer.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Session

Initializes this new Lightstreamer session with the passed options.

Parameters:

  • options (Hash) (defaults to: {})

    The options to create the session with.

Options Hash (options):

  • :server_url (String)

    The URL of the Lightstreamer server. Required.

  • :username (String)

    The username to connect to the server with. Optional.

  • :password (String)

    The password to connect to the server with. Optional.

  • :adapter_set (String)

    The name of the adapter set to request from the server. Optional.



29
30
31
32
33
34
35
36
37
# File 'lib/lightstreamer/session.rb', line 29

def initialize(options = {})
  @subscriptions = []
  @subscriptions_mutex = Mutex.new

  @server_url = options.fetch :server_url
  @username = options[:username]
  @password = options[:password]
  @adapter_set = options[:adapter_set]
end

Instance Attribute Details

#adapter_setString (readonly)

Returns The name of the adapter set to request from the Lightstreamer server. Set by #initialize.

Returns:

  • (String)

    The name of the adapter set to request from the Lightstreamer server. Set by #initialize.



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

def adapter_set
  @adapter_set
end

#errorError (readonly)

Returns If an error occurs on the stream connection that causes the session to terminate then details of the error will be stored in this attribute. If the session is terminated as a result of calling #disconnect then the error will be Lightstreamer::SessionEndError.

Returns:

  • (Error)

    If an error occurs on the stream connection that causes the session to terminate then details of the error will be stored in this attribute. If the session is terminated as a result of calling #disconnect then the error will be Lightstreamer::SessionEndError.



20
21
22
# File 'lib/lightstreamer/session.rb', line 20

def error
  @error
end

#passwordString (readonly)

Returns The password to connect to the Lightstreamer server with. Set by #initialize.

Returns:

  • (String)

    The password to connect to the Lightstreamer server with. Set by #initialize.



12
13
14
# File 'lib/lightstreamer/session.rb', line 12

def password
  @password
end

#server_urlString (readonly)

Returns The URL of the Lightstreamer server to connect to. Set by #initialize.

Returns:

  • (String)

    The URL of the Lightstreamer server to connect to. Set by #initialize.



6
7
8
# File 'lib/lightstreamer/session.rb', line 6

def server_url
  @server_url
end

#usernameString (readonly)

Returns The username to connect to the Lightstreamer server with. Set by #initialize.

Returns:

  • (String)

    The username to connect to the Lightstreamer server with. Set by #initialize.



9
10
11
# File 'lib/lightstreamer/session.rb', line 9

def username
  @username
end

Instance Method Details

#connectObject

Creates a new Lightstreamer session using the details passed to #initialize. If an error occurs then an Error subclass will be raised.



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

def connect
  return if @stream_connection

  @error = nil

  create_stream_connection
  create_control_connection
  create_processing_thread
rescue
  @stream_connection = nil
  raise
end

#connected?Boolean

Returns whether this session is currently connected and has an active stream connection.

Returns:

  • (Boolean)


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

def connected?
  !@stream_connection.nil?
end

#disconnectObject

Disconnects this session and terminates the session on the server. All worker threads are exited.



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/lightstreamer/session.rb', line 62

def disconnect
  @control_connection.execute :destroy if @control_connection

  @processing_thread.join 5 if @processing_thread
ensure
  @stream_connection.disconnect if @stream_connection
  @processing_thread.exit if @processing_thread

  @processing_thread = @control_connection = @stream_connection = nil
  @subscriptions = []
end

#force_rebindObject

Requests that the Lightstreamer server terminate the currently active stream connection and require that a new stream connection be initiated by the client. The Lightstreamer server requires closure and re-establishment of the stream connection periodically during normal operation, this method just allows such a reconnection to be requested explicitly by the client. If an error occurs then an Error subclass will be raised.



78
79
80
81
82
# File 'lib/lightstreamer/session.rb', line 78

def force_rebind
  return unless @stream_connection

  @control_connection.execute :force_rebind
end

#subscribe(subscription) ⇒ Object

Subscribes this Lightstreamer session to the specified subscription. If an error occurs then an Error subclass will be raised.

Parameters:

  • subscription (Subscription)

    The new subscription to subscribe to.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/lightstreamer/session.rb', line 88

def subscribe(subscription)
  subscription.clear_data

  @subscriptions_mutex.synchronize { @subscriptions << subscription }

  options = { mode: subscription.mode, items: subscription.items, fields: subscription.fields,
              adapter: subscription.adapter, maximum_update_frequency: subscription.maximum_update_frequency,
              selector: subscription.selector }

  @control_connection.subscription_execute :add, subscription.id, options
rescue
  @subscriptions_mutex.synchronize { @subscriptions.delete subscription }
  raise
end

#subscribed?(subscription) ⇒ Boolean

Returns whether the specified subscription is currently active on this session.

Parameters:

  • subscription (Subscription)

    The subscription to return the status for.

Returns:

  • (Boolean)

    Whether the specified subscription is currently active on this session.



108
109
110
# File 'lib/lightstreamer/session.rb', line 108

def subscribed?(subscription)
  @subscriptions_mutex.synchronize { @subscriptions.include? subscription }
end

#unsubscribe(subscription) ⇒ Object

Unsubscribes this Lightstreamer session from the specified subscription.

Parameters:

  • subscription (Subscription)

    The existing subscription to unsubscribe from.

Raises:

  • (ArgumentError)


115
116
117
118
119
120
121
# File 'lib/lightstreamer/session.rb', line 115

def unsubscribe(subscription)
  raise ArgumentError, 'Unknown subscription' unless subscribed? subscription

  @control_connection.subscription_execute :delete, subscription.id

  @subscriptions_mutex.synchronize { @subscriptions.delete subscription }
end