Class: Wamp::Client::Session

Inherits:
Object
  • Object
show all
Includes:
Check, Event
Defined in:
lib/wamp/client/session.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Event

included

Methods included from Check

included

Constructor Details

#initialize(transport, options = {}) ⇒ Session

Constructor

Parameters:

  • transport (Transport::Base)

    The transport that the session will use

  • options (Hash) (defaults to: {})

    Hash containing different session options

Options Hash (options):

  • :authid (String)

    The authentication ID

  • :authmethods (Array)

    Different auth methods that this client supports



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/wamp/client/session.rb', line 56

def initialize(transport, options={})

  # Parameters
  self.options = options || {}

  # Log the event
  logger.info("#{self.class.name} created with options")
  logger.info("   uri: #{options[:uri]}")
  logger.info("   realm: #{options[:realm]}")

  # Create the send message lambda for the request objects
  send_message_lambda = -> m { send_message(m) }

  # Outstanding Requests
  self.request = {
      publish: Request::Publish.new(self, send_message_lambda),
      subscribe: Request::Subscribe.new(self, send_message_lambda) { |s_id, s| self.subscription.add(s_id, s) },
      unsubscribe: Request::Unsubscribe.new(self, send_message_lambda) { |s_id| self.subscription.remove(s_id) },
      call: Request::Call.new(self, send_message_lambda),
      register: Request::Register.new(self, send_message_lambda) { |r_id, r| self.registration.add(r_id, r) },
      unregister: Request::Unregister.new(self, send_message_lambda) { |r_id| self.registration.remove(r_id) },
  }

  # Init Subs and Regs in place
  self.subscription = Manager::Subscription.new(self, send_message_lambda)
  self.registration = Manager::Registration.new(self, send_message_lambda)
  self.establish = Manager::Establish.new(self, send_message_lambda)

  # Setup session callbacks
  self.callback = {}

  # Setup Transport
  self.transport = transport
  self.transport.on :message do |msg|
    receive_message(msg)
  end

end

Instance Attribute Details

#callbackObject

Returns the value of attribute callback.



46
47
48
# File 'lib/wamp/client/session.rb', line 46

def callback
  @callback
end

#establishObject

Returns the value of attribute establish.



46
47
48
# File 'lib/wamp/client/session.rb', line 46

def establish
  @establish
end

#optionsObject

Returns the value of attribute options.



46
47
48
# File 'lib/wamp/client/session.rb', line 46

def options
  @options
end

#registrationObject

Returns the value of attribute registration.



46
47
48
# File 'lib/wamp/client/session.rb', line 46

def registration
  @registration
end

#requestObject

Returns the value of attribute request.



46
47
48
# File 'lib/wamp/client/session.rb', line 46

def request
  @request
end

#subscriptionObject

Returns the value of attribute subscription.



46
47
48
# File 'lib/wamp/client/session.rb', line 46

def subscription
  @subscription
end

#transportObject

Returns the value of attribute transport.



46
47
48
# File 'lib/wamp/client/session.rb', line 46

def transport
  @transport
end

Instance Method Details

#call(procedure, args = nil, kwargs = nil, options = {}, &callback) ⇒ Call

Publishes and event to a topic

Parameters:

  • procedure (String)

    The procedure to invoke

  • args (Array) (defaults to: nil)

    The arguments

  • kwargs (Hash) (defaults to: nil)

    The keyword arguments

  • options (Hash) (defaults to: {})

    The options for the call

  • callback (block)

    The callback(result, error, details) called to signal if the call was a success or not

Returns:

  • (Call)

    An object representing the call



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/wamp/client/session.rb', line 246

def call(procedure, args=nil, kwargs=nil, options={}, &callback)
  check_open

  # Check params
  self.class.check_uri('procedure', procedure)
  self.class.check_dict('options', options)
  self.class.check_list('args', args, true)
  self.class.check_dict('kwargs', kwargs, true)

  # Make the request
  request_id = make_request(:call, :request, procedure, args, kwargs, options, &callback)

  # Create the call object
  call = Request::CallObject.new(self, request_id)

  # Timeout Logic
  if options[:timeout] and options[:timeout] > 0
    # Once the timer expires, if the call hasn't completed, cancel it
    self.transport.add_timer(options[:timeout]) do
      call.cancel
    end
  end

  call
end

#cancel(call, mode = 'skip') ⇒ Object

Cancels a call

Parameters:

  • call (Call)
    • The call object

  • mode (String) (defaults to: 'skip')
    • The mode of the skip. Options are ‘skip’, ‘kill’, ‘killnowait’



276
277
278
279
280
281
282
283
284
# File 'lib/wamp/client/session.rb', line 276

def cancel(call, mode='skip')
  check_open

  # Check params
  self.class.check_nil('call', call, false)

  # Cancel the request
  make_request(:call, :cancel, call.id, mode)
end

#idObject

Returns the ID of the session



103
104
105
# File 'lib/wamp/client/session.rb', line 103

def id
  self.establish.id
end

#is_open?Boolean

Returns ‘true’ if the session is open

Returns:

  • (Boolean)


97
98
99
# File 'lib/wamp/client/session.rb', line 97

def is_open?
  self.establish.is_open?
end

#join(realm) ⇒ Object

Joins the WAMP Router

Parameters:

  • realm (String)

    The name of the realm



116
117
118
119
120
121
122
123
124
# File 'lib/wamp/client/session.rb', line 116

def join(realm)
  check_closed

  # Check params
  self.class.check_uri('realm', realm)

  # Attempt to join
  self.establish.join(realm)
end

#leave(reason = 'wamp.close.normal', message = 'user initiated') ⇒ Object

Leaves the WAMP Router

Parameters:

  • reason (String) (defaults to: 'wamp.close.normal')

    URI signalling the reason for leaving



129
130
131
132
133
134
135
136
137
138
# File 'lib/wamp/client/session.rb', line 129

def leave(reason='wamp.close.normal', message='user initiated')
  check_open

  # Check params
  self.class.check_uri('reason', reason, true)
  self.class.check_string('message', message, true)

  # Leave the session
  self.establish.leave(reason, message)
end

#publish(topic, args = nil, kwargs = nil, options = {}, &callback) ⇒ Object

Publishes and event to a topic

Parameters:

  • topic (String)

    The topic to publish the event to

  • args (Array) (defaults to: nil)

    The arguments

  • kwargs (Hash) (defaults to: nil)

    The keyword arguments

  • options (Hash) (defaults to: {})

    The options for the publish

  • callback (block)

    The callback(publish, error, details) called to signal if the publish was a success or not



179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/wamp/client/session.rb', line 179

def publish(topic, args=nil, kwargs=nil, options={}, &callback)
  check_open

  # Check params
  self.class.check_uri('topic', topic)
  self.class.check_dict('options', options)
  self.class.check_list('args', args, true)
  self.class.check_dict('kwargs', kwargs, true)

  # Make the request
  make_request(:publish, :request, topic, args, kwargs, options, &callback)
end

#realmObject

Returns the realm of the session



109
110
111
# File 'lib/wamp/client/session.rb', line 109

def realm
  self.establish.realm
end

#register(procedure, handler, options = nil, interrupt = nil, &callback) ⇒ Object

Register to a procedure

Parameters:

  • procedure (String)

    The procedure to register for

  • handler (lambda)

    The handler(args, kwargs, details) when an invocation is received

  • options (Hash, nil) (defaults to: nil)

    The options for the registration

  • interrupt (lambda) (defaults to: nil)

    The handler(request, mode) when an interrupt is received

  • callback (block)

    The callback(registration, error, details) called to signal if the registration was a success or not



199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/wamp/client/session.rb', line 199

def register(procedure, handler, options=nil, interrupt=nil, &callback)
  check_open

  options ||= {}

  # Check params
  self.class.check_uri('procedure', procedure)
  self.class.check_nil('handler', handler, false)

  # Make the request
  make_request(:register, :request, procedure, handler, options, interrupt, &callback)
end

#subscribe(topic, handler, options = {}, &callback) ⇒ Object

Subscribes to a topic

Parameters:

  • topic (String)

    The topic to subscribe to

  • handler (lambda)

    The handler(args, kwargs, details) when an event is received

  • options (Hash) (defaults to: {})

    The options for the subscription

  • callback (block)

    The callback(subscription, error) called to signal if the subscription was a success or not



146
147
148
149
150
151
152
153
154
155
156
# File 'lib/wamp/client/session.rb', line 146

def subscribe(topic, handler, options={}, &callback)
  check_open

  # Check params
  self.class.check_uri('topic', topic)
  self.class.check_dict('options', options)
  self.class.check_nil('handler', handler, false)

  # Make the request
  make_request(:subscribe, :request, topic, handler, options, &callback)
end

#unregister(registration, &callback) ⇒ Object

Unregisters from a procedure

Parameters:

  • registration (Registration)

    The registration object from when the registration was created

  • callback (block)

    The callback(registration, error, details) called to signal if the unregistration was a success or not



228
229
230
231
232
233
234
235
236
# File 'lib/wamp/client/session.rb', line 228

def unregister(registration, &callback)
  check_open

  # Check params
  self.class.check_nil('registration', registration, false)

  # Make the request
  make_request(:unregister, :request, registration, &callback)
end

#unsubscribe(subscription, &callback) ⇒ Object

Unsubscribes from a subscription

Parameters:

  • subscription (Subscription)

    The subscription object from when the subscription was created

  • callback (block)

    The callback(subscription, error, details) called to signal if the subscription was a success or not



162
163
164
165
166
167
168
169
170
# File 'lib/wamp/client/session.rb', line 162

def unsubscribe(subscription, &callback)
  check_open

  # Check params
  self.class.check_nil('subscription', subscription, false)

  # Make the request
  make_request(:unsubscribe, :request, subscription, &callback)
end

#yield(request, result, options = {}, check_defer = false) ⇒ Object

Sends a result for the invocation

Parameters:

  • request (Integer)
    • The id of the request

  • result (CallError, CallResult, anything)
    • If it is a CallError, the error will be returned

  • options (Hash) (defaults to: {})
    • The options to be sent with the yield



217
218
219
220
221
222
# File 'lib/wamp/client/session.rb', line 217

def yield(request, result, options={}, check_defer=false)
  check_open

  # Call the registration yield method
  self.registration.yield(request, result, options, check_defer)
end