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.



24
25
26
27
28
29
30
31
32
# File 'lib/lightstreamer/session.rb', line 24

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

#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 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

#disconnectObject

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.

Parameters:

  • subscription (Subscription)

    The new subscription to subscribe to.



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.

Parameters:

  • subscription (Subscription)

    The subscription to return the status for.

Returns:

  • (Boolean)

    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.

Parameters:

  • subscription (Subscription)

    The existing subscription to unsubscribe from.

Raises:

  • (ArgumentError)


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