Class: Lightstreamer::Session
- Inherits:
-
Object
- Object
- Lightstreamer::Session
- 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
-
#adapter_set ⇒ String?
readonly
The name of the adapter set to request from the Lightstreamer server.
-
#error ⇒ LightstreamerError?
readonly
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.
-
#password ⇒ String?
readonly
The password to connect to the Lightstreamer server with.
-
#server_url ⇒ String
readonly
The URL of the Lightstreamer server to connect to.
-
#username ⇒ String?
readonly
The username to connect to the Lightstreamer server with.
Instance Method Summary collapse
-
#connect ⇒ Object
Creates a new Lightstreamer session using the details passed to #initialize.
-
#connected? ⇒ Boolean
Returns whether this session is currently connected and has an active stream connection.
-
#disconnect ⇒ Object
Disconnects this session and terminates the session on the server.
-
#force_rebind ⇒ Object
Requests that the Lightstreamer server terminate the currently active stream connection and require that a new stream connection be initiated by the client.
-
#initialize(options = {}) ⇒ Session
constructor
Initializes this new Lightstreamer session with the passed options.
-
#subscribe(subscription) ⇒ Object
Subscribes this Lightstreamer session to the specified subscription.
-
#subscribed?(subscription) ⇒ Boolean
Returns whether the specified subscription is currently active on this session.
-
#unsubscribe(subscription) ⇒ Object
Unsubscribes this Lightstreamer session from the specified subscription.
Constructor Details
#initialize(options = {}) ⇒ Session
Initializes this new Lightstreamer session with the passed options.
39 40 41 42 43 44 45 46 47 |
# File 'lib/lightstreamer/session.rb', line 39 def initialize( = {}) @subscriptions = [] @subscriptions_mutex = Mutex.new @server_url = .fetch :server_url @username = [:username] @password = [:password] @adapter_set = [:adapter_set] end |
Instance Attribute Details
#adapter_set ⇒ String? (readonly)
The name of the adapter set to request from the Lightstreamer server. Set by #initialize.
23 24 25 |
# File 'lib/lightstreamer/session.rb', line 23 def adapter_set @adapter_set end |
#error ⇒ LightstreamerError? (readonly)
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 SessionEndError.
30 31 32 |
# File 'lib/lightstreamer/session.rb', line 30 def error @error end |
#password ⇒ String? (readonly)
The password to connect to the Lightstreamer server with. Set by #initialize.
18 19 20 |
# File 'lib/lightstreamer/session.rb', line 18 def password @password end |
#server_url ⇒ String (readonly)
The URL of the Lightstreamer server to connect to. Set by #initialize.
8 9 10 |
# File 'lib/lightstreamer/session.rb', line 8 def server_url @server_url end |
#username ⇒ String? (readonly)
The username to connect to the Lightstreamer server with. Set by #initialize.
13 14 15 |
# File 'lib/lightstreamer/session.rb', line 13 def username @username end |
Instance Method Details
#connect ⇒ Object
Creates a new Lightstreamer session using the details passed to #initialize. If an error occurs then a LightstreamerError subclass will be raised.
51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/lightstreamer/session.rb', line 51 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.
67 68 69 |
# File 'lib/lightstreamer/session.rb', line 67 def connected? !@stream_connection.nil? end |
#disconnect ⇒ Object
Disconnects this session and terminates the session on the server. All worker threads are exited.
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/lightstreamer/session.rb', line 72 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_rebind ⇒ Object
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 a LightstreamerError subclass will be raised.
88 89 90 91 92 |
# File 'lib/lightstreamer/session.rb', line 88 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 a LightstreamerError subclass will be raised.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/lightstreamer/session.rb', line 98 def subscribe(subscription) subscription.clear_data @subscriptions_mutex.synchronize { @subscriptions << subscription } = { 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, rescue @subscriptions_mutex.synchronize { @subscriptions.delete subscription } raise end |
#subscribed?(subscription) ⇒ Boolean
Returns whether the specified subscription is currently active on this session.
118 119 120 |
# File 'lib/lightstreamer/session.rb', line 118 def subscribed?(subscription) @subscriptions_mutex.synchronize { @subscriptions.include? subscription } end |
#unsubscribe(subscription) ⇒ Object
Unsubscribes this Lightstreamer session from the specified subscription.
125 126 127 128 129 130 131 |
# File 'lib/lightstreamer/session.rb', line 125 def unsubscribe(subscription) raise ArgumentError, 'Unknown subscription' unless subscribed? subscription @control_connection.subscription_execute :delete, subscription.id @subscriptions_mutex.synchronize { @subscriptions.delete subscription } end |