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.

  • :password (String)

    The password to connect to the server with.

  • :adapter_set (String)

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



39
40
41
42
43
44
45
46
47
# File 'lib/lightstreamer/session.rb', line 39

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)

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

Returns:

  • (String, nil)


23
24
25
# File 'lib/lightstreamer/session.rb', line 23

def adapter_set
  @adapter_set
end

#errorLightstreamerError? (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.

Returns:



30
31
32
# File 'lib/lightstreamer/session.rb', line 30

def error
  @error
end

#passwordString? (readonly)

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

Returns:

  • (String, nil)


18
19
20
# File 'lib/lightstreamer/session.rb', line 18

def password
  @password
end

#server_urlString (readonly)

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

Returns:

  • (String)


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

def server_url
  @server_url
end

#usernameString? (readonly)

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

Returns:

  • (String, nil)


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

def username
  @username
end

Instance Method Details

#connectObject

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.

Returns:

  • (Boolean)


67
68
69
# File 'lib/lightstreamer/session.rb', line 67

def connected?
  !@stream_connection.nil?
end

#disconnectObject

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

Parameters:

  • subscription (Subscription)

    The new subscription to subscribe to.



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 }

  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.



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.

Parameters:

  • subscription (Subscription)

    The existing subscription to unsubscribe from.

Raises:

  • (ArgumentError)


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