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 ⇒ Error
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.
29 30 31 32 33 34 35 36 37 |
# File 'lib/lightstreamer/session.rb', line 29 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)
Returns 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 |
#error ⇒ Error (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.
20 21 22 |
# File 'lib/lightstreamer/session.rb', line 20 def error @error end |
#password ⇒ String (readonly)
Returns 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_url ⇒ String (readonly)
Returns 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 |
#username ⇒ String (readonly)
Returns 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
#connect ⇒ Object
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.
57 58 59 |
# File 'lib/lightstreamer/session.rb', line 57 def connected? !@stream_connection.nil? end |
#disconnect ⇒ Object
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_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 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.
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 } = { 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.
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.
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 |