wamp_client 0.1.4 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +34 -32
- data/lib/wamp/client/auth.rb +0 -27
- data/lib/wamp/client/check.rb +0 -27
- data/lib/wamp/client/connection.rb +40 -113
- data/lib/wamp/client/event.rb +78 -0
- data/lib/wamp/client/manager/base.rb +39 -0
- data/lib/wamp/client/manager/base_multiple.rb +37 -0
- data/lib/wamp/client/manager/establish.rb +168 -0
- data/lib/wamp/client/manager/registration.rb +183 -0
- data/lib/wamp/client/manager/require.rb +3 -0
- data/lib/wamp/client/manager/subscription.rb +55 -0
- data/lib/wamp/client/request/base.rb +125 -0
- data/lib/wamp/client/request/call.rb +111 -0
- data/lib/wamp/client/request/publish.rb +72 -0
- data/lib/wamp/client/request/register.rb +79 -0
- data/lib/wamp/client/request/require.rb +6 -0
- data/lib/wamp/client/request/subscribe.rb +78 -0
- data/lib/wamp/client/request/unregister.rb +71 -0
- data/lib/wamp/client/request/unsubscribe.rb +72 -0
- data/lib/wamp/client/response.rb +136 -0
- data/lib/wamp/client/serializer.rb +0 -29
- data/lib/wamp/client/session.rb +172 -839
- data/lib/wamp/client/transport/base.rb +4 -77
- data/lib/wamp/client/transport/event_machine_base.rb +0 -27
- data/lib/wamp/client/transport/faye_web_socket.rb +4 -31
- data/lib/wamp/client/transport/web_socket_event_machine.rb +3 -30
- data/lib/wamp/client/version.rb +1 -28
- data/lib/wamp/client.rb +1 -28
- data/spec/spec_helper.rb +3 -137
- data/spec/support/faye_web_socket_client_stub.rb +43 -0
- data/spec/support/test_transport.rb +50 -0
- data/spec/support/web_socket_event_machine_client_stub.rb +39 -0
- data/spec/wamp/client/connection_spec.rb +4 -4
- data/spec/wamp/client/session_spec.rb +135 -135
- data/spec/wamp/client/transport_spec.rb +2 -2
- data/wamp_client.gemspec +10 -9
- metadata +59 -38
- data/lib/wamp/client/defer.rb +0 -70
@@ -0,0 +1,72 @@
|
|
1
|
+
require_relative "base"
|
2
|
+
require "wamp/client/message"
|
3
|
+
|
4
|
+
module Wamp
|
5
|
+
module Client
|
6
|
+
module Request
|
7
|
+
|
8
|
+
class Message::Published
|
9
|
+
def request_id
|
10
|
+
self.publish_request
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class Publish < Base
|
15
|
+
|
16
|
+
def create_request(request_id, topic, args=nil, kwargs=nil, options={}, &callback)
|
17
|
+
|
18
|
+
# Create the lookup
|
19
|
+
lookup = options[:acknowledge] ? {t: topic, a: args, k: kwargs, o: options, c: callback} : nil
|
20
|
+
|
21
|
+
# Create the message
|
22
|
+
message = Message::Publish.new(request_id, options, topic, args, kwargs)
|
23
|
+
|
24
|
+
# Return
|
25
|
+
[lookup, message]
|
26
|
+
end
|
27
|
+
|
28
|
+
def process_success(message, lookup)
|
29
|
+
if lookup
|
30
|
+
# Get the params
|
31
|
+
topic = lookup[:t]
|
32
|
+
args = lookup[:a]
|
33
|
+
kwargs = lookup[:k]
|
34
|
+
options = lookup[:o]
|
35
|
+
callback = lookup[:c]
|
36
|
+
|
37
|
+
# Create the details
|
38
|
+
details = {}
|
39
|
+
details[:topic] = topic
|
40
|
+
details[:type] = 'publish'
|
41
|
+
details[:publication] = message.publication
|
42
|
+
|
43
|
+
# Return the values
|
44
|
+
[callback, { args: args, kwargs: kwargs, options: options }, details]
|
45
|
+
else
|
46
|
+
[nil, nil, nil]
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def process_error(message, lookup)
|
51
|
+
if lookup
|
52
|
+
# Get the params
|
53
|
+
topic = lookup[:t]
|
54
|
+
callback = lookup[:c]
|
55
|
+
|
56
|
+
# Create the details
|
57
|
+
details = message.details || {}
|
58
|
+
details[:topic] = topic unless details[:topic]
|
59
|
+
details[:type] = 'publish'
|
60
|
+
|
61
|
+
# Return the values
|
62
|
+
[callback, details]
|
63
|
+
else
|
64
|
+
[nil, nil]
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require_relative "base"
|
2
|
+
require "wamp/client/message"
|
3
|
+
require "wamp/client/manager/registration"
|
4
|
+
|
5
|
+
module Wamp
|
6
|
+
module Client
|
7
|
+
module Request
|
8
|
+
|
9
|
+
class Message::Registered
|
10
|
+
def request_id
|
11
|
+
self.register_request
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class Register < Base
|
16
|
+
|
17
|
+
def create_request(request_id, procedure, handler, options=nil, interrupt=nil, &callback)
|
18
|
+
|
19
|
+
# Create the lookup
|
20
|
+
lookup = {p: procedure, h: handler, i: interrupt, o: options, c: callback}
|
21
|
+
|
22
|
+
# Create the message
|
23
|
+
message = Message::Register.new(request_id, options, procedure)
|
24
|
+
|
25
|
+
# Return
|
26
|
+
[lookup, message]
|
27
|
+
end
|
28
|
+
|
29
|
+
def process_success(message, lookup)
|
30
|
+
if lookup
|
31
|
+
# Get the params
|
32
|
+
procedure = lookup[:p]
|
33
|
+
handler = lookup[:h]
|
34
|
+
options = lookup[:o]
|
35
|
+
interrupt = lookup[:i]
|
36
|
+
callback = lookup[:c]
|
37
|
+
|
38
|
+
# Create the subscription
|
39
|
+
r_id = message.registration
|
40
|
+
r = Manager::RegistrationObject.new(procedure, handler, options, interrupt, self.session, r_id)
|
41
|
+
|
42
|
+
# Create the details
|
43
|
+
details = {}
|
44
|
+
details[:procedure] = procedure
|
45
|
+
details[:type] = 'register'
|
46
|
+
|
47
|
+
# Call the on_success method
|
48
|
+
self.on_success.call(r_id, r)
|
49
|
+
|
50
|
+
# Return the values
|
51
|
+
[callback, r, details]
|
52
|
+
else
|
53
|
+
[nil, nil, nil]
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def process_error(message, lookup)
|
58
|
+
if lookup
|
59
|
+
# Get the params
|
60
|
+
procedure = lookup[:p]
|
61
|
+
callback = lookup[:c]
|
62
|
+
|
63
|
+
# Create the details
|
64
|
+
details = message.details || {}
|
65
|
+
details[:procedure] = procedure unless details[:procedure]
|
66
|
+
details[:type] = 'register'
|
67
|
+
|
68
|
+
# Return the values
|
69
|
+
[callback, details]
|
70
|
+
else
|
71
|
+
[nil, nil]
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require_relative "base"
|
2
|
+
require "wamp/client/message"
|
3
|
+
require "wamp/client/manager/subscription"
|
4
|
+
|
5
|
+
module Wamp
|
6
|
+
module Client
|
7
|
+
module Request
|
8
|
+
|
9
|
+
class Message::Subscribed
|
10
|
+
def request_id
|
11
|
+
self.subscribe_request
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class Subscribe < Base
|
16
|
+
|
17
|
+
def create_request(request_id, topic, handler, options={}, &callback)
|
18
|
+
|
19
|
+
# Create the lookup
|
20
|
+
lookup = {t: topic, h: handler, o: options, c: callback}
|
21
|
+
|
22
|
+
# Create the message
|
23
|
+
message = Message::Subscribe.new(request_id, options, topic)
|
24
|
+
|
25
|
+
# Return
|
26
|
+
[lookup, message]
|
27
|
+
end
|
28
|
+
|
29
|
+
def process_success(message, lookup)
|
30
|
+
if lookup
|
31
|
+
# Get the params
|
32
|
+
topic = lookup[:t]
|
33
|
+
handler = lookup[:h]
|
34
|
+
options = lookup[:o]
|
35
|
+
callback = lookup[:c]
|
36
|
+
|
37
|
+
# Create the subscription
|
38
|
+
s_id = message.subscription
|
39
|
+
s = Manager::SubscriptionObject.new(topic, handler, options, self.session, s_id)
|
40
|
+
|
41
|
+
# Create the details
|
42
|
+
details = {}
|
43
|
+
details[:topic] = topic unless details[:topic]
|
44
|
+
details[:type] = 'subscribe'
|
45
|
+
|
46
|
+
# Call the on_success method
|
47
|
+
self.on_success.call(s_id, s)
|
48
|
+
|
49
|
+
# Return the values
|
50
|
+
[callback, s, details]
|
51
|
+
else
|
52
|
+
[nil, nil, nil]
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def process_error(message, lookup)
|
57
|
+
if lookup
|
58
|
+
# Get the params
|
59
|
+
topic = lookup[:t]
|
60
|
+
callback = lookup[:c]
|
61
|
+
|
62
|
+
# Create the details
|
63
|
+
details = message.details || {}
|
64
|
+
details[:topic] = topic unless details[:topic]
|
65
|
+
details[:type] = 'subscribe'
|
66
|
+
|
67
|
+
# Return the values
|
68
|
+
[callback, details]
|
69
|
+
else
|
70
|
+
[nil, nil]
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require_relative "base"
|
2
|
+
require "wamp/client/message"
|
3
|
+
|
4
|
+
module Wamp
|
5
|
+
module Client
|
6
|
+
module Request
|
7
|
+
|
8
|
+
class Message::Unregistered
|
9
|
+
def request_id
|
10
|
+
self.unregister_request
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class Unregister < Base
|
15
|
+
|
16
|
+
def create_request(request_id, registration, &callback)
|
17
|
+
|
18
|
+
# Create the lookup
|
19
|
+
lookup = { r: registration, c: callback }
|
20
|
+
|
21
|
+
# Create the message
|
22
|
+
message = Message::Unregister.new(request_id, registration.id)
|
23
|
+
|
24
|
+
# Return
|
25
|
+
[lookup, message]
|
26
|
+
end
|
27
|
+
|
28
|
+
def process_success(message, lookup)
|
29
|
+
if lookup
|
30
|
+
# Get the params
|
31
|
+
registration = lookup[:r]
|
32
|
+
callback = lookup[:c]
|
33
|
+
|
34
|
+
# Create the details
|
35
|
+
details = {}
|
36
|
+
details[:procedure] = registration.procedure
|
37
|
+
details[:type] = 'unregister'
|
38
|
+
|
39
|
+
# Call the on_success method
|
40
|
+
self.on_success.call(registration.id)
|
41
|
+
|
42
|
+
# Return the values
|
43
|
+
[callback, registration, details]
|
44
|
+
else
|
45
|
+
[nil, nil, nil]
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def process_error(message, lookup)
|
50
|
+
if lookup
|
51
|
+
# Get the params
|
52
|
+
registration = lookup[:r]
|
53
|
+
callback = lookup[:c]
|
54
|
+
|
55
|
+
# Create the details
|
56
|
+
details = message.details || {}
|
57
|
+
details[:procedure] = registration.procedure unless details[:procedure]
|
58
|
+
details[:type] = 'unregister'
|
59
|
+
|
60
|
+
# Return the values
|
61
|
+
[callback, details]
|
62
|
+
else
|
63
|
+
[nil, nil]
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require_relative "base"
|
2
|
+
require "wamp/client/message"
|
3
|
+
|
4
|
+
module Wamp
|
5
|
+
module Client
|
6
|
+
module Request
|
7
|
+
|
8
|
+
class Message::Unsubscribed
|
9
|
+
def request_id
|
10
|
+
self.unsubscribe_request
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class Unsubscribe < Base
|
15
|
+
|
16
|
+
def create_request(request_id, subscription, &callback)
|
17
|
+
|
18
|
+
# Create the lookup
|
19
|
+
lookup = { s: subscription, c: callback }
|
20
|
+
|
21
|
+
# Create the message
|
22
|
+
message = Message::Unsubscribe.new(request_id, subscription.id)
|
23
|
+
|
24
|
+
# Return
|
25
|
+
[lookup, message]
|
26
|
+
end
|
27
|
+
|
28
|
+
def process_success(message, lookup)
|
29
|
+
if lookup
|
30
|
+
# Get the params
|
31
|
+
subscription = lookup[:s]
|
32
|
+
callback = lookup[:c]
|
33
|
+
|
34
|
+
# Create the details
|
35
|
+
details = {}
|
36
|
+
details[:topic] = subscription.topic
|
37
|
+
details[:type] = 'unsubscribe'
|
38
|
+
|
39
|
+
# Call the on_success method
|
40
|
+
self.on_success.call(subscription.id)
|
41
|
+
|
42
|
+
# Return the values
|
43
|
+
[callback, subscription, details]
|
44
|
+
else
|
45
|
+
[nil, nil, nil]
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def process_error(message, lookup)
|
50
|
+
if lookup
|
51
|
+
# Get the params
|
52
|
+
subscription = lookup[:s]
|
53
|
+
callback = lookup[:c]
|
54
|
+
|
55
|
+
# Create the details
|
56
|
+
details = message.details || {}
|
57
|
+
details[:topic] = subscription.topic unless details[:topic]
|
58
|
+
details[:type] = 'unsubscribe'
|
59
|
+
|
60
|
+
# Return the values
|
61
|
+
[callback, details]
|
62
|
+
else
|
63
|
+
[nil, nil]
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,136 @@
|
|
1
|
+
require "wamp/client/event"
|
2
|
+
|
3
|
+
module Wamp
|
4
|
+
module Client
|
5
|
+
module Response
|
6
|
+
DEFAULT_ERROR = "wamp.error.runtime"
|
7
|
+
|
8
|
+
# This method wraps the handling of the result from a procedure.
|
9
|
+
# or interrupt. It is intended to standardize the processing
|
10
|
+
#
|
11
|
+
# @param [Bool] - "true" is we want an error out of this
|
12
|
+
# @return [CallResult, CallError, CallDefer] - A response object
|
13
|
+
def self.invoke_handler(error: false, &callback)
|
14
|
+
logger = Wamp::Client.logger
|
15
|
+
|
16
|
+
# Invoke the request
|
17
|
+
begin
|
18
|
+
result = callback.call
|
19
|
+
rescue CallError => e
|
20
|
+
result = e
|
21
|
+
rescue StandardError => e
|
22
|
+
logger.error("Wamp::Client::Response - #{e.message}")
|
23
|
+
e.backtrace.each { |line| logger.error(" #{line}") }
|
24
|
+
result = CallError.new(DEFAULT_ERROR, [e.message], { backtrace: e.backtrace })
|
25
|
+
end
|
26
|
+
|
27
|
+
# Ensure an expected class is returned
|
28
|
+
if error
|
29
|
+
CallError.ensure(result)
|
30
|
+
else
|
31
|
+
CallResult.ensure(result, allow_error: true, allow_defer: true)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# This method will instantiate either a CallResult or CallError based
|
36
|
+
# on the payload
|
37
|
+
#
|
38
|
+
# @param hash [Hash] - The hash
|
39
|
+
# @return [CallResult, CallError] - The result
|
40
|
+
def self.from_hash(hash)
|
41
|
+
if hash[:error] != nil
|
42
|
+
CallError.from_hash(hash)
|
43
|
+
else
|
44
|
+
CallResult.from_hash(hash)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
class CallResult
|
49
|
+
attr_reader :args, :kwargs
|
50
|
+
|
51
|
+
def initialize(args=nil, kwargs=nil)
|
52
|
+
@args = args || []
|
53
|
+
@kwargs = kwargs || {}
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.from_hash(hash)
|
57
|
+
self.new(hash[:args], hash[:kwargs])
|
58
|
+
end
|
59
|
+
|
60
|
+
def to_hash
|
61
|
+
{ args: self.args, kwargs: self.kwargs }
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.from_yield_message(msg)
|
65
|
+
self.new(msg.yield_arguments, msg.yield_argumentskw)
|
66
|
+
end
|
67
|
+
|
68
|
+
def self.ensure(result, allow_error: false, allow_defer: false)
|
69
|
+
unless result.is_a?(self) or
|
70
|
+
(allow_error and result.is_a?(CallError)) or
|
71
|
+
(allow_defer and result.is_a?(CallDefer))
|
72
|
+
result = result != nil ? self.new([result]) : self.new
|
73
|
+
end
|
74
|
+
|
75
|
+
result
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
class CallError < StandardError
|
80
|
+
attr_reader :error, :args, :kwargs
|
81
|
+
|
82
|
+
def initialize(error, args=nil, kwargs=nil)
|
83
|
+
@error = error
|
84
|
+
@args = args || []
|
85
|
+
@kwargs = kwargs || {}
|
86
|
+
end
|
87
|
+
|
88
|
+
def self.from_hash(hash)
|
89
|
+
self.new(hash[:error], hash[:args], hash[:kwargs])
|
90
|
+
end
|
91
|
+
|
92
|
+
def to_hash
|
93
|
+
{ error: self.error, args: self.args, kwargs: self.kwargs }
|
94
|
+
end
|
95
|
+
|
96
|
+
def self.from_message(msg)
|
97
|
+
self.new(msg.error, msg.arguments, msg.argumentskw)
|
98
|
+
end
|
99
|
+
|
100
|
+
def self.ensure(result)
|
101
|
+
unless result.is_a?(self)
|
102
|
+
args = result != nil ? [result] : nil
|
103
|
+
result = self.new(DEFAULT_ERROR, args)
|
104
|
+
end
|
105
|
+
|
106
|
+
result
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
class CallDefer
|
111
|
+
include Event
|
112
|
+
attr_accessor :request, :registration
|
113
|
+
|
114
|
+
create_event [:complete, :error, :progress]
|
115
|
+
|
116
|
+
def succeed(result)
|
117
|
+
trigger :complete, self, result
|
118
|
+
end
|
119
|
+
|
120
|
+
def fail(error)
|
121
|
+
trigger :error, self, error
|
122
|
+
end
|
123
|
+
|
124
|
+
end
|
125
|
+
|
126
|
+
class ProgressiveCallDefer < CallDefer
|
127
|
+
|
128
|
+
def progress(result)
|
129
|
+
trigger :progress, self, result
|
130
|
+
end
|
131
|
+
|
132
|
+
end
|
133
|
+
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
@@ -1,30 +1,3 @@
|
|
1
|
-
=begin
|
2
|
-
|
3
|
-
Copyright (c) 2018 Eric Chapman
|
4
|
-
|
5
|
-
MIT License
|
6
|
-
|
7
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
8
|
-
a copy of this software and associated documentation files (the
|
9
|
-
"Software"), to deal in the Software without restriction, including
|
10
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
11
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
12
|
-
permit persons to whom the Software is furnished to do so, subject to
|
13
|
-
the following conditions:
|
14
|
-
|
15
|
-
The above copyright notice and this permission notice shall be
|
16
|
-
included in all copies or substantial portions of the Software.
|
17
|
-
|
18
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
19
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
20
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
21
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
22
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
23
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
24
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
25
|
-
|
26
|
-
=end
|
27
|
-
|
28
1
|
require 'json'
|
29
2
|
|
30
3
|
module Wamp
|
@@ -37,14 +10,12 @@ module Wamp
|
|
37
10
|
# Serializes the object
|
38
11
|
# @param object - The object to serialize
|
39
12
|
def serialize(object)
|
40
|
-
|
41
13
|
end
|
42
14
|
|
43
15
|
# Deserializes the object
|
44
16
|
# @param string [String] - The string to deserialize
|
45
17
|
# @return The deserialized object
|
46
18
|
def deserialize(string)
|
47
|
-
|
48
19
|
end
|
49
20
|
|
50
21
|
end
|