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.
-
#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.
-
#disconnect ⇒ Object
Disconnects this session and shuts down its stream and processing threads.
-
#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.
24 25 26 27 28 29 30 31 32 |
# File 'lib/lightstreamer/session.rb', line 24 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 |
#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 ProtocolError will be raised.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/lightstreamer/session.rb', line 36 def connect return if @stream_connection @stream_connection = StreamConnection.new self first_line = @stream_connection.read_line if first_line == 'OK' @session_id = read_session_id create_control_connection create_processing_thread elsif first_line == 'ERROR' handle_connection_error end end |
#disconnect ⇒ Object
Disconnects this session and shuts down its stream and processing threads.
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/lightstreamer/session.rb', line 53 def disconnect @stream_connection.disconnect if @stream_connection if @processing_thread Thread.kill @processing_thread @processing_thread.join end @processing_thread = @stream_connection = @control_connection = nil @subscriptions = [] end |
#subscribe(subscription) ⇒ Object
Subscribes this Lightstreamer session to the specified subscription.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/lightstreamer/session.rb', line 68 def subscribe(subscription) subscription.clear_data @subscriptions_mutex.synchronize { @subscriptions << subscription } begin @control_connection.execute table: subscription.id, operation: :add, mode: subscription.mode, items: subscription.items, fields: subscription.fields, adapter: subscription.adapter rescue @subscriptions_mutex.synchronize { @subscriptions.delete subscription } raise end end |
#subscribed?(subscription) ⇒ Boolean
Returns whether the specified subscription is currently active on this session.
88 89 90 |
# File 'lib/lightstreamer/session.rb', line 88 def subscribed?(subscription) @subscriptions_mutex.synchronize { @subscriptions.include? subscription } end |
#unsubscribe(subscription) ⇒ Object
Unsubscribes this Lightstreamer session from the specified subscription.
95 96 97 98 99 100 101 |
# File 'lib/lightstreamer/session.rb', line 95 def unsubscribe(subscription) raise ArgumentError, 'Unknown subscription' unless subscribed? subscription @control_connection.execute table: subscription.id, operation: :delete @subscriptions_mutex.synchronize { @subscriptions.delete subscription } end |