wamp_client 0.0.9 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +28 -26
  3. data/lib/{wamp_client.rb → wamp/client.rb} +8 -8
  4. data/lib/{wamp_client → wamp/client}/auth.rb +13 -11
  5. data/lib/wamp/client/check.rb +86 -0
  6. data/lib/wamp/client/connection.rb +249 -0
  7. data/lib/{wamp_client → wamp/client}/defer.rb +29 -27
  8. data/lib/wamp/client/message.rb +1322 -0
  9. data/lib/{wamp_client → wamp/client}/serializer.rb +26 -24
  10. data/lib/wamp/client/session.rb +1001 -0
  11. data/lib/wamp/client/transport/base.rb +152 -0
  12. data/lib/{wamp_client → wamp/client}/transport/event_machine_base.rb +19 -17
  13. data/lib/wamp/client/transport/faye_web_socket.rb +85 -0
  14. data/lib/wamp/client/transport/web_socket_event_machine.rb +88 -0
  15. data/lib/{wamp_client → wamp/client}/version.rb +5 -3
  16. data/scripts/gen_message.rb +54 -53
  17. data/spec/spec_helper.rb +3 -3
  18. data/spec/{auth_spec.rb → wamp/client/auth_spec.rb} +2 -2
  19. data/spec/{check_spec.rb → wamp/client/check_spec.rb} +2 -2
  20. data/spec/{connection_spec.rb → wamp/client/connection_spec.rb} +7 -7
  21. data/spec/{message_spec.rb → wamp/client/message_spec.rb} +298 -298
  22. data/spec/{session_spec.rb → wamp/client/session_spec.rb} +134 -134
  23. data/spec/{transport_spec.rb → wamp/client/transport_spec.rb} +4 -4
  24. data/wamp_client.gemspec +2 -2
  25. metadata +50 -50
  26. data/lib/wamp_client/check.rb +0 -84
  27. data/lib/wamp_client/connection.rb +0 -247
  28. data/lib/wamp_client/message.rb +0 -1348
  29. data/lib/wamp_client/session.rb +0 -1000
  30. data/lib/wamp_client/transport/base.rb +0 -151
  31. data/lib/wamp_client/transport/faye_web_socket.rb +0 -83
  32. data/lib/wamp_client/transport/web_socket_event_machine.rb +0 -86
@@ -1,151 +0,0 @@
1
- =begin
2
-
3
- Copyright (c) 2016 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
- require 'wamp_client/serializer'
29
-
30
- module WampClient
31
- module Transport
32
- class Base
33
-
34
- # Callback when the socket is opened
35
- @on_open
36
- def on_open(&on_open)
37
- @on_open = on_open
38
- end
39
-
40
- # Callback when the socket is closed. Parameters are
41
- # @param reason [String] String telling the reason it was closed
42
- @on_close
43
- def on_close(&on_close)
44
- @on_close = on_close
45
- end
46
-
47
- # Callback when a message is received. Parameters are
48
- # @param msg [Array] The parsed message that was received
49
- @on_message
50
- def on_message(&on_message)
51
- @on_message = on_message
52
- end
53
-
54
- # Callback when there is an error. Parameters are
55
- # @param msg [String] The message from the error
56
- @on_error
57
- def on_error(&on_error)
58
- @on_error = on_error
59
- end
60
-
61
- # Simple setter for callbacks
62
- def on(event, &callback)
63
- case event
64
- when :open
65
- self.on_open(&callback)
66
- when :close
67
- self.on_close(&callback)
68
- when :message
69
- self.on_message(&callback)
70
- when :error
71
- self.on_error(&callback)
72
- else
73
- raise RuntimeError, "Unknown on(event) '#{event}'"
74
- end
75
- end
76
-
77
- attr_accessor :uri, :proxy, :headers, :protocol, :serializer, :connected
78
-
79
- # Constructor for the transport
80
- # @param options [Hash] The connection options. the different options are as follows
81
- # @option options [String] :uri The url to connect to
82
- # @option options [String] :proxy The proxy to use
83
- # @option options [String] :protocol The protocol
84
- # @option options [Hash] :headers Custom headers to include during the connection
85
- # @option options [WampClient::Serializer::Base] :serializer The serializer to use
86
- def initialize(options)
87
-
88
- # Initialize the parameters
89
- self.connected = false
90
- self.uri = options[:uri]
91
- self.proxy = options[:proxy]
92
- self.headers = options[:headers] || {}
93
- self.protocol = options[:protocol] || 'wamp.2.json'
94
- self.serializer = options[:serializer] || WampClient::Serializer::JSONSerializer.new
95
-
96
- # Add the wamp.2.json protocol header
97
- self.headers['Sec-WebSocket-Protocol'] = self.protocol
98
-
99
- # Initialize callbacks
100
- @on_open = nil
101
- @on_close = nil
102
- @on_message = nil
103
- @on_error = nil
104
-
105
- end
106
-
107
- # Connects to the WAMP Server using the transport
108
- def connect
109
- # Implement in subclass
110
- end
111
-
112
- # Disconnects from the WAMP Server
113
- def disconnect
114
- # Implement in subclass
115
- end
116
-
117
- # Returns true if the transport it connected
118
- def connected?
119
- self.connected
120
- end
121
-
122
- # Sends a Message
123
- # @param [Array] msg - The message payload to send
124
- def send_message(msg)
125
- # Implement in subclass
126
- end
127
-
128
- # Process the callback when the timer expires
129
- # @param [Integer] milliseconds - The number
130
- # @param [block] callback - The callback that is fired when the timer expires
131
- def self.add_timer(milliseconds, &callback)
132
- # Implement in subclass
133
- end
134
- def add_timer(milliseconds, &callback)
135
- self.class.add_timer(milliseconds, &callback)
136
- end
137
-
138
- # Method to start the event machine for the socket
139
- def self.start_event_machine(&block)
140
- # Implement in subclass
141
- end
142
-
143
- # Method to stop the vent machine
144
- def self.stop_event_machine
145
- # Implement in subclass
146
- end
147
-
148
- end
149
-
150
- end
151
- end
@@ -1,83 +0,0 @@
1
- =begin
2
-
3
- Copyright (c) 2016 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
- require_relative 'event_machine_base'
29
-
30
- # This implementation uses the 'faye-websocket' Gem.
31
- module WampClient
32
- module Transport
33
- class FayeWebSocket < EventMachineBase
34
- attr_accessor :socket
35
-
36
- def initialize(options)
37
- super(options)
38
- self.socket = nil
39
-
40
- # Only make them include the gem if they are going to use it
41
- require 'faye/websocket'
42
- end
43
-
44
- def connect
45
- options = { :headers => self.headers }
46
- options[:proxy] = self.proxy if self.proxy != nil
47
- self.socket = Faye::WebSocket::Client.new(self.uri, [self.protocol], options)
48
-
49
- self.socket.on(:open) do |event|
50
- self.connected = true
51
- @on_open.call if @on_open
52
- end
53
-
54
- self.socket.on(:message) do |event|
55
- @on_message.call(self.serializer.deserialize(event.data)) if @on_message
56
- end
57
-
58
- self.socket.on(:close) do |event|
59
- self.connected = false
60
- @on_close.call(event.reason) if @on_close
61
- end
62
-
63
- self.socket.on(:error) do |event|
64
- @on_error.call(event.message) if @on_error
65
- end
66
- end
67
-
68
- def disconnect
69
- self.socket.close
70
- self.connected = false
71
- end
72
-
73
- def send_message(msg)
74
- if self.connected
75
- self.socket.send(self.serializer.serialize(msg))
76
- else
77
- raise RuntimeError, "Socket must be open to call 'send_message'"
78
- end
79
- end
80
-
81
- end
82
- end
83
- end
@@ -1,86 +0,0 @@
1
- =begin
2
-
3
- Copyright (c) 2016 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
- require_relative 'event_machine_base'
29
-
30
- # This implementation uses the 'websocket-eventmachine-client' Gem.
31
- # This is the default if no transport is included
32
- module WampClient
33
- module Transport
34
- class WebSocketEventMachine < EventMachineBase
35
- attr_accessor :socket
36
-
37
- def initialize(options)
38
- super(options)
39
- self.socket = nil
40
-
41
- # Only make them include the gem if they are going to use it
42
- require 'websocket-eventmachine-client'
43
-
44
- # Raise an exception if proxy was included (not supported)
45
- if self.proxy != nil
46
- raise RuntimeError, "The WebSocketEventMachine transport does not support 'proxy'. Try using 'faye-websocket' transport instead"
47
- end
48
- end
49
-
50
- def connect
51
- self.socket = WebSocket::EventMachine::Client.connect(
52
- :uri => self.uri,
53
- :headers => self.headers
54
- )
55
-
56
- self.socket.onopen do
57
- self.connected = true
58
- @on_open.call if @on_open
59
- end
60
-
61
- self.socket.onmessage do |msg, type|
62
- @on_message.call(self.serializer.deserialize(msg)) if @on_message
63
- end
64
-
65
- self.socket.onclose do |code, reason|
66
- self.connected = false
67
- @on_close.call(reason) if @on_close
68
- end
69
- end
70
-
71
- def disconnect
72
- self.connected = !self.socket.close # close returns 'true' if the connection was closed immediately
73
- end
74
-
75
- def send_message(msg)
76
- if self.connected
77
- self.socket.send(self.serializer.serialize(msg), {type: 'text'})
78
- else
79
- raise RuntimeError, "Socket must be open to call 'send_message'"
80
- end
81
- end
82
-
83
- end
84
- end
85
- end
86
-