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.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +34 -32
  3. data/lib/wamp/client/auth.rb +0 -27
  4. data/lib/wamp/client/check.rb +0 -27
  5. data/lib/wamp/client/connection.rb +40 -113
  6. data/lib/wamp/client/event.rb +78 -0
  7. data/lib/wamp/client/manager/base.rb +39 -0
  8. data/lib/wamp/client/manager/base_multiple.rb +37 -0
  9. data/lib/wamp/client/manager/establish.rb +168 -0
  10. data/lib/wamp/client/manager/registration.rb +183 -0
  11. data/lib/wamp/client/manager/require.rb +3 -0
  12. data/lib/wamp/client/manager/subscription.rb +55 -0
  13. data/lib/wamp/client/request/base.rb +125 -0
  14. data/lib/wamp/client/request/call.rb +111 -0
  15. data/lib/wamp/client/request/publish.rb +72 -0
  16. data/lib/wamp/client/request/register.rb +79 -0
  17. data/lib/wamp/client/request/require.rb +6 -0
  18. data/lib/wamp/client/request/subscribe.rb +78 -0
  19. data/lib/wamp/client/request/unregister.rb +71 -0
  20. data/lib/wamp/client/request/unsubscribe.rb +72 -0
  21. data/lib/wamp/client/response.rb +136 -0
  22. data/lib/wamp/client/serializer.rb +0 -29
  23. data/lib/wamp/client/session.rb +172 -839
  24. data/lib/wamp/client/transport/base.rb +4 -77
  25. data/lib/wamp/client/transport/event_machine_base.rb +0 -27
  26. data/lib/wamp/client/transport/faye_web_socket.rb +4 -31
  27. data/lib/wamp/client/transport/web_socket_event_machine.rb +3 -30
  28. data/lib/wamp/client/version.rb +1 -28
  29. data/lib/wamp/client.rb +1 -28
  30. data/spec/spec_helper.rb +3 -137
  31. data/spec/support/faye_web_socket_client_stub.rb +43 -0
  32. data/spec/support/test_transport.rb +50 -0
  33. data/spec/support/web_socket_event_machine_client_stub.rb +39 -0
  34. data/spec/wamp/client/connection_spec.rb +4 -4
  35. data/spec/wamp/client/session_spec.rb +135 -135
  36. data/spec/wamp/client/transport_spec.rb +2 -2
  37. data/wamp_client.gemspec +10 -9
  38. metadata +59 -38
  39. data/lib/wamp/client/defer.rb +0 -70
@@ -1,82 +1,16 @@
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 'wamp/client/serializer'
2
+ require 'wamp/client/event'
29
3
 
30
4
  module Wamp
31
5
  module Client
32
6
  module Transport
33
7
  class Base
34
-
35
- # Callback when the socket is opened
36
- @on_open
37
- def on_open(&on_open)
38
- @on_open = on_open
39
- end
40
-
41
- # Callback when the socket is closed. Parameters are
42
- # @param reason [String] String telling the reason it was closed
43
- @on_close
44
- def on_close(&on_close)
45
- @on_close = on_close
46
- end
47
-
48
- # Callback when a message is received. Parameters are
49
- # @param msg [Array] The parsed message that was received
50
- @on_message
51
- def on_message(&on_message)
52
- @on_message = on_message
53
- end
54
-
55
- # Callback when there is an error. Parameters are
56
- # @param msg [String] The message from the error
57
- @on_error
58
- def on_error(&on_error)
59
- @on_error = on_error
60
- end
61
-
62
- # Simple setter for callbacks
63
- def on(event, &callback)
64
- case event
65
- when :open
66
- self.on_open(&callback)
67
- when :close
68
- self.on_close(&callback)
69
- when :message
70
- self.on_message(&callback)
71
- when :error
72
- self.on_error(&callback)
73
- else
74
- raise RuntimeError, "Unknown on(event) '#{event}'"
75
- end
76
- end
8
+ include Event
77
9
 
78
10
  attr_accessor :uri, :proxy, :headers, :protocol, :serializer, :connected
79
11
 
12
+ create_event [:open, :close, :message, :error]
13
+
80
14
  # Constructor for the transport
81
15
  # @param options [Hash] The connection options. the different options are as follows
82
16
  # @option options [String] :uri The url to connect to
@@ -96,13 +30,6 @@ module Wamp
96
30
 
97
31
  # Add the wamp.2.json protocol header
98
32
  self.headers['Sec-WebSocket-Protocol'] = self.protocol
99
-
100
- # Initialize callbacks
101
- @on_open = nil
102
- @on_close = nil
103
- @on_message = nil
104
- @on_error = nil
105
-
106
33
  end
107
34
 
108
35
  # Connects to the WAMP Server using the transport
@@ -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 'eventmachine'
29
2
  require_relative 'base'
30
3
 
@@ -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_relative 'event_machine_base'
29
2
 
30
3
  # This implementation uses the 'faye-websocket' Gem.
@@ -49,20 +22,20 @@ module Wamp
49
22
 
50
23
  self.socket.on(:open) do |event|
51
24
  self.connected = true
52
- @on_open.call if @on_open
25
+ trigger :open
53
26
  end
54
27
 
55
28
  self.socket.on(:message) do |event|
56
- @on_message.call(self.serializer.deserialize(event.data)) if @on_message
29
+ trigger :message, self.serializer.deserialize(event.data)
57
30
  end
58
31
 
59
32
  self.socket.on(:close) do |event|
60
33
  self.connected = false
61
- @on_close.call(event.reason) if @on_close
34
+ trigger :close, event.reason
62
35
  end
63
36
 
64
37
  self.socket.on(:error) do |event|
65
- @on_error.call(event.message) if @on_error
38
+ trigger :error, event.message
66
39
  end
67
40
  end
68
41
 
@@ -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_relative 'event_machine_base'
29
2
 
30
3
  # This implementation uses the 'websocket-eventmachine-client' Gem.
@@ -56,16 +29,16 @@ module Wamp
56
29
 
57
30
  self.socket.onopen do
58
31
  self.connected = true
59
- @on_open.call if @on_open
32
+ trigger :open
60
33
  end
61
34
 
62
35
  self.socket.onmessage do |msg, type|
63
- @on_message.call(self.serializer.deserialize(msg)) if @on_message
36
+ trigger :message, self.serializer.deserialize(msg)
64
37
  end
65
38
 
66
39
  self.socket.onclose do |code, reason|
67
40
  self.connected = false
68
- @on_close.call(reason) if @on_close
41
+ trigger :close, reason
69
42
  end
70
43
  end
71
44
 
@@ -1,32 +1,5 @@
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
  module Wamp
29
2
  module Client
30
- VERSION = '0.1.4'
3
+ VERSION = '0.2.0'
31
4
  end
32
5
  end
data/lib/wamp/client.rb CHANGED
@@ -1,37 +1,10 @@
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 'wamp/client/version'
29
2
  require 'wamp/client/message'
30
3
  require 'wamp/client/serializer'
31
4
  require 'wamp/client/connection'
32
5
  require 'wamp/client/session'
33
6
  require 'wamp/client/auth'
34
- require 'wamp/client/defer'
7
+ require 'wamp/client/response'
35
8
  require 'logger'
36
9
 
37
10
  module Wamp
data/spec/spec_helper.rb CHANGED
@@ -1,150 +1,16 @@
1
1
  require 'simplecov'
2
2
  SimpleCov.start
3
3
 
4
- require_relative '../lib/wamp/client'
4
+ require 'wamp/client'
5
5
 
6
6
  if ENV['CODECOV_TOKEN']
7
7
  require 'codecov'
8
8
  SimpleCov.formatter = SimpleCov::Formatter::Codecov
9
9
  end
10
10
 
11
- require 'wamp/client'
11
+ Dir[File.expand_path('spec/support/**/*.rb')].each { |f| require f }
12
+
12
13
  require "rspec/em"
13
14
 
14
15
  Wamp::Client.log_level = :error
15
16
 
16
- module SpecHelper
17
-
18
- class TestTransport < Wamp::Client::Transport::EventMachineBase
19
- @@event_machine_on = false
20
- attr_accessor :messages
21
-
22
- def initialize(options)
23
- super(options)
24
- @connected = true
25
- self.messages = []
26
- end
27
-
28
- def connect
29
- self.add_timer(1000) do
30
- @on_open.call if @on_open
31
- end
32
- end
33
-
34
- def disconnect
35
- @connected = false
36
- @on_close.call if @on_close
37
- end
38
-
39
- def self.start_event_machine(&block)
40
- @@event_machine_on = true
41
- block.call
42
- end
43
-
44
- def self.stop_event_machine
45
- @@event_machine_on = false
46
- end
47
-
48
- def self.event_machine_on?
49
- @@event_machine_on
50
- end
51
-
52
- def send_message(msg)
53
- self.messages.push(msg)
54
- end
55
-
56
- def receive_message(msg)
57
-
58
- # Emulate serialization/deserialization
59
- serialize = self.serializer.serialize(msg)
60
- deserialize = self.serializer.deserialize(serialize)
61
-
62
- # Call the received message
63
- @on_message.call(deserialize) if @on_message
64
- end
65
-
66
- end
67
-
68
- class WebSocketEventMachineClientStub
69
- attr_accessor :last_message
70
-
71
- def initialize
72
- EM.add_timer(1) {
73
- @onopen.call if @onopen != nil
74
- }
75
- end
76
-
77
- @onopen
78
- def onopen(&onopen)
79
- @onopen = onopen
80
- end
81
-
82
- @onmessage
83
- def onmessage(&onmessage)
84
- @onmessage = onmessage
85
- end
86
-
87
- @onclose
88
- def onclose(&onclose)
89
- @onclose = onclose
90
- end
91
-
92
- def close
93
- @onclose.call if @onclose != nil
94
- true
95
- end
96
-
97
- def send(message, type)
98
- self.last_message = message
99
- end
100
-
101
- def receive(message)
102
- @onmessage.call(message, {type:'text'}) if @onmessage != nil
103
- end
104
-
105
- end
106
-
107
- class FayeWebSocketClientStub
108
- class Event
109
- attr_accessor :data, :reason
110
- end
111
-
112
- attr_accessor :last_message
113
-
114
- def initialize
115
- EM.add_timer(1) {
116
- @on_open.call(Event.new) if @on_open != nil
117
- }
118
- end
119
-
120
- @on_open
121
- @on_message
122
- @on_close
123
- def on(event, &block)
124
- if event == :open
125
- @on_open = block
126
- elsif event == :close
127
- @on_close = block
128
- elsif event == :message
129
- @on_message = block
130
- end
131
- end
132
-
133
- def close
134
- event = Event.new
135
- event.reason = 'closed'
136
- @on_close.call(event) if @on_close != nil
137
- end
138
-
139
- def send(message)
140
- self.last_message = message
141
- end
142
-
143
- def receive(message)
144
- event = Event.new
145
- event.data = message
146
- @on_message.call(event) if @on_message != nil
147
- end
148
-
149
- end
150
- end
@@ -0,0 +1,43 @@
1
+ class FayeWebSocketClientStub
2
+ class Event
3
+ attr_accessor :data, :reason
4
+ end
5
+
6
+ attr_accessor :last_message
7
+
8
+ def initialize
9
+ EM.add_timer(1) {
10
+ @on_open.call(Event.new) if @on_open != nil
11
+ }
12
+ end
13
+
14
+ @on_open
15
+ @on_message
16
+ @on_close
17
+ def on(event, &block)
18
+ if event == :open
19
+ @on_open = block
20
+ elsif event == :close
21
+ @on_close = block
22
+ elsif event == :message
23
+ @on_message = block
24
+ end
25
+ end
26
+
27
+ def close
28
+ event = Event.new
29
+ event.reason = 'closed'
30
+ @on_close.call(event) if @on_close != nil
31
+ end
32
+
33
+ def send(message)
34
+ self.last_message = message
35
+ end
36
+
37
+ def receive(message)
38
+ event = Event.new
39
+ event.data = message
40
+ @on_message.call(event) if @on_message != nil
41
+ end
42
+
43
+ end
@@ -0,0 +1,50 @@
1
+ class TestTransport < Wamp::Client::Transport::EventMachineBase
2
+ @@event_machine_on = false
3
+ attr_accessor :messages
4
+
5
+ def initialize(options)
6
+ super(options)
7
+ @connected = true
8
+ self.messages = []
9
+ end
10
+
11
+ def connect
12
+ self.add_timer(1000) do
13
+ trigger :open
14
+ end
15
+ end
16
+
17
+ def disconnect
18
+ @connected = false
19
+ trigger :close
20
+ end
21
+
22
+ def self.start_event_machine(&block)
23
+ @@event_machine_on = true
24
+ block.call
25
+ end
26
+
27
+ def self.stop_event_machine
28
+ @@event_machine_on = false
29
+ end
30
+
31
+ def self.event_machine_on?
32
+ @@event_machine_on
33
+ end
34
+
35
+ def send_message(msg)
36
+ self.messages.push(msg)
37
+ end
38
+
39
+ def receive_message(msg)
40
+
41
+ # Emulate serialization/deserialization
42
+ serialize = self.serializer.serialize(msg)
43
+ deserialize = self.serializer.deserialize(serialize)
44
+
45
+ # Call the received message
46
+ trigger :message, deserialize
47
+ end
48
+
49
+ end
50
+
@@ -0,0 +1,39 @@
1
+ class WebSocketEventMachineClientStub
2
+ attr_accessor :last_message
3
+
4
+ def initialize
5
+ EM.add_timer(1) {
6
+ @onopen.call if @onopen != nil
7
+ }
8
+ end
9
+
10
+ @onopen
11
+ def onopen(&onopen)
12
+ @onopen = onopen
13
+ end
14
+
15
+ @onmessage
16
+ def onmessage(&onmessage)
17
+ @onmessage = onmessage
18
+ end
19
+
20
+ @onclose
21
+ def onclose(&onclose)
22
+ @onclose = onclose
23
+ end
24
+
25
+ def close
26
+ @onclose.call if @onclose != nil
27
+ true
28
+ end
29
+
30
+ def send(message, type)
31
+ self.last_message = message
32
+ end
33
+
34
+ def receive(message)
35
+ @onmessage.call(message, {type:'text'}) if @onmessage != nil
36
+ end
37
+
38
+ end
39
+
@@ -6,13 +6,13 @@ describe Wamp::Client::Connection do
6
6
  before { clock.stub }
7
7
  after { clock.reset }
8
8
 
9
- before(:each) { SpecHelper::TestTransport.stop_event_machine }
9
+ before(:each) { TestTransport.stop_event_machine }
10
10
 
11
11
  let(:options) {
12
12
  {
13
13
  uri: 'wss://example.com',
14
14
  realm: 'realm1',
15
- transport: SpecHelper::TestTransport,
15
+ transport: TestTransport,
16
16
  }
17
17
  }
18
18
  let(:connection) { described_class.new(options) }
@@ -37,11 +37,11 @@ describe Wamp::Client::Connection do
37
37
  end
38
38
 
39
39
  def check_em_on
40
- expect(SpecHelper::TestTransport.event_machine_on?).to eq(true)
40
+ expect(TestTransport.event_machine_on?).to eq(true)
41
41
  end
42
42
 
43
43
  def check_em_off
44
- expect(SpecHelper::TestTransport.event_machine_on?).to eq(false)
44
+ expect(TestTransport.event_machine_on?).to eq(false)
45
45
  end
46
46
 
47
47
  describe 'transport' do