wamp_client 0.0.1

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.
@@ -0,0 +1,194 @@
1
+ require 'websocket-eventmachine-client'
2
+ require 'wamp_client/session'
3
+ require 'wamp_client/transport'
4
+
5
+ module WampClient
6
+ class Connection
7
+ attr_accessor :options, :transport, :session
8
+
9
+ @reconnect = true
10
+
11
+ @open = false
12
+ def is_open?
13
+ @open
14
+ end
15
+
16
+ # Called when the connection is established
17
+ @on_connect
18
+ def on_connect(&on_connect)
19
+ @on_connect = on_connect
20
+ end
21
+
22
+ # Called when the WAMP session is established
23
+ # @param session [WampClient::Session]
24
+ # @param details [Hash]
25
+ @on_join
26
+ def on_join(&on_join)
27
+ @on_join = on_join
28
+ end
29
+
30
+ # Called when the WAMP session presents a challenge
31
+ # @param authmethod [String]
32
+ # @param extra [Hash]
33
+ @on_challenge
34
+ def on_challenge(&on_challenge)
35
+ @on_challenge = on_challenge
36
+ end
37
+
38
+ # Called when the WAMP session is terminated
39
+ # @param reason [String] The reason the session left the router
40
+ # @param details [Hash] Object containing information about the left session
41
+ @on_leave
42
+ def on_leave(&on_leave)
43
+ @on_leave = on_leave
44
+ end
45
+
46
+ # Called when the connection is terminated
47
+ # @param reason [String] The reason the transport was disconnected
48
+ @on_disconnect
49
+ def on_disconnect(&on_disconnect)
50
+ @on_disconnect = on_disconnect
51
+ end
52
+
53
+ # @param options [Hash] The different options to pass to the connection
54
+ # @option options [String] :uri The uri of the WAMP router to connect to
55
+ # @option options [String] :realm The realm to connect to
56
+ # @option options [String] :protocol The protocol (default if wamp.2.json)
57
+ # @option options [Hash] :headers Custom headers to include during the connection
58
+ # @option options [WampClient::Serializer::Base] :serializer The serializer to use (default is json)
59
+ def initialize(options)
60
+ self.options = options || {}
61
+ end
62
+
63
+ # Opens the connection
64
+ def open
65
+
66
+ raise RuntimeError, 'The connection is already open' if self.is_open?
67
+
68
+ @reconnect = true
69
+ @retry_timer = 1
70
+ @retrying = false
71
+
72
+ EM.run do
73
+ # Create the transport
74
+ self._create_transport
75
+ end
76
+
77
+ end
78
+
79
+ # Closes the connection
80
+ def close
81
+
82
+ raise RuntimeError, 'The connection is already closed' unless self.is_open?
83
+
84
+ # Leave the session
85
+ @reconnect = false
86
+ session.leave
87
+
88
+ end
89
+
90
+ def _create_session
91
+ self.session = WampClient::Session.new(self.transport, self.options)
92
+
93
+ # Setup session callbacks
94
+ self.session.on_challenge do |authmethod, extra|
95
+ self._finish_retry
96
+ @on_challenge.call(authmethod, extra) if @on_challenge
97
+ end
98
+
99
+ self.session.on_join do |details|
100
+ self._finish_retry
101
+ @on_join.call(self.session, details) if @on_join
102
+ end
103
+
104
+ self.session.on_leave do |reason, details|
105
+
106
+ unless @retrying
107
+ @on_leave.call(reason, details) if @on_leave
108
+ end
109
+
110
+ if @reconnect
111
+ # Retry
112
+ self._retry unless @retrying
113
+ else
114
+ # Close the transport
115
+ self.transport.disconnect
116
+ end
117
+ end
118
+
119
+ self.session.join(self.options[:realm])
120
+ end
121
+
122
+ def _create_transport
123
+
124
+ if self.transport
125
+ self.transport.disconnect
126
+ self.transport = nil
127
+ end
128
+
129
+ # Initialize the transport
130
+ if self.options[:transport]
131
+ self.transport = self.options[:transport]
132
+ else
133
+ self.transport = WampClient::Transport::WebSocketTransport.new(self.options)
134
+ end
135
+
136
+ # Setup transport callbacks
137
+ self.transport.on_open do
138
+
139
+ # Call the callback
140
+ @on_connect.call if @on_connect
141
+
142
+ # Create the session
143
+ self._create_session
144
+
145
+ end
146
+
147
+ self.transport.on_close do |reason|
148
+ @open = false
149
+
150
+ unless @retrying
151
+ @on_disconnect.call(reason) if @on_disconnect
152
+ end
153
+
154
+ # Nil out the session since the transport closed underneath it
155
+ self.session = nil
156
+
157
+ if @reconnect
158
+ # Retry
159
+ self._retry unless @retrying
160
+ else
161
+ # Stop the Event Machine
162
+ EM.stop
163
+ end
164
+ end
165
+
166
+ @open = true
167
+
168
+ self.transport.connect
169
+
170
+ end
171
+
172
+ def _finish_retry
173
+ @retry_timer = 1
174
+ @retrying = false
175
+ end
176
+
177
+ def _retry
178
+
179
+ unless self.session and self.session.is_open?
180
+ @retry_timer = 2*@retry_timer unless @retry_timer == 32
181
+ @retrying = true
182
+
183
+ self._create_transport
184
+
185
+ puts "Attempting Reconnect... Next attempt in #{@retry_timer} seconds"
186
+ EM.add_timer(@retry_timer) {
187
+ self._retry if @retrying
188
+ }
189
+ end
190
+
191
+ end
192
+
193
+ end
194
+ end