Class: Quark::MdkDiscovery::Protocol::DiscoClient

Inherits:
DatawireQuarkCore::QuarkObject show all
Extended by:
DatawireQuarkCore::Static
Defined in:
lib/mdk_discovery/protocol.rb

Constant Summary

Constants included from DatawireQuarkCore::Static

DatawireQuarkCore::Static::Unassigned

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DatawireQuarkCore::Static

_lazy_statics, static, unlazy_statics

Methods inherited from DatawireQuarkCore::QuarkObject

#to_s

Constructor Details

#initialize(disco_subscriber, wsclient, runtime) ⇒ DiscoClient

Returns a new instance of DiscoClient.



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/mdk_discovery/protocol.rb', line 108

def initialize(disco_subscriber, wsclient, runtime)
    
    self.__init_fields__
    (self)._subscriber = disco_subscriber
    (self)._wsclient = wsclient
    (self)._wsclient.subscribe(self)
    (self)._failurePolicyFactory = ::DatawireQuarkCore.cast((runtime).dependencies.getService("failurepolicy_factory")) { ::Quark.mdk_discovery.FailurePolicyFactory }
    (self)._timeService = runtime.getTimeService()

    nil
end

Instance Attribute Details

#_dispatcherObject

Returns the value of attribute _dispatcher.



101
102
103
# File 'lib/mdk_discovery/protocol.rb', line 101

def _dispatcher
  @_dispatcher
end

#_failurePolicyFactoryObject

Returns the value of attribute _failurePolicyFactory.



101
102
103
# File 'lib/mdk_discovery/protocol.rb', line 101

def _failurePolicyFactory
  @_failurePolicyFactory
end

#_subscriberObject

Returns the value of attribute _subscriber.



101
102
103
# File 'lib/mdk_discovery/protocol.rb', line 101

def _subscriber
  @_subscriber
end

#_timeServiceObject

Returns the value of attribute _timeService.



101
102
103
# File 'lib/mdk_discovery/protocol.rb', line 101

def _timeService
  @_timeService
end

#_wsclientObject

Returns the value of attribute _wsclient.



101
102
103
# File 'lib/mdk_discovery/protocol.rb', line 101

def _wsclient
  @_wsclient
end

#dlogObject

Returns the value of attribute dlog.



101
102
103
# File 'lib/mdk_discovery/protocol.rb', line 101

def dlog
  @dlog
end

#lastHeartbeatObject

Returns the value of attribute lastHeartbeat.



101
102
103
# File 'lib/mdk_discovery/protocol.rb', line 101

def lastHeartbeat
  @lastHeartbeat
end

#registeredObject

Returns the value of attribute registered.



101
102
103
# File 'lib/mdk_discovery/protocol.rb', line 101

def registered
  @registered
end

#sockObject

Returns the value of attribute sock.



101
102
103
# File 'lib/mdk_discovery/protocol.rb', line 101

def sock
  @sock
end

Instance Method Details

#__init_fields__Object



358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/mdk_discovery/protocol.rb', line 358

def __init_fields__()
    
    self._failurePolicyFactory = nil
    self._dispatcher = nil
    self._timeService = nil
    self._subscriber = nil
    self._wsclient = nil
    self.registered = ::Hash.new()
    self.dlog = ::Quark.quark._getLogger("discovery")
    self.lastHeartbeat = 0
    self.sock = nil

    nil
end

#_getClassObject



284
285
286
287
288
289
# File 'lib/mdk_discovery/protocol.rb', line 284

def _getClass()
    
    return "mdk_discovery.protocol.DiscoClient"

    nil
end

#_getField(name) ⇒ Object



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/mdk_discovery/protocol.rb', line 291

def _getField(name)
    
    if ((name) == ("_failurePolicyFactory"))
        return (self)._failurePolicyFactory
    end
    if ((name) == ("_dispatcher"))
        return (self)._dispatcher
    end
    if ((name) == ("_timeService"))
        return (self)._timeService
    end
    if ((name) == ("_subscriber"))
        return (self)._subscriber
    end
    if ((name) == ("_wsclient"))
        return (self)._wsclient
    end
    if ((name) == ("registered"))
        return (self).registered
    end
    if ((name) == ("dlog"))
        return (self).dlog
    end
    if ((name) == ("lastHeartbeat"))
        return (self).lastHeartbeat
    end
    if ((name) == ("sock"))
        return (self).sock
    end
    return nil

    nil
end

#_register(node) ⇒ Object

Register a node with the remote Discovery server.



191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/mdk_discovery/protocol.rb', line 191

def _register(node)
    
    service = (node).service
    if (!((@registered).key?(service)))
        (@registered)[service] = (::Quark.mdk_discovery.Cluster.new((self)._failurePolicyFactory))
    end
    (@registered)[service].add(node)
    if ((self)._wsclient.isConnected())
        self.active(node)
    end

    nil
end

#_setField(name, value) ⇒ Object



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/mdk_discovery/protocol.rb', line 325

def _setField(name, value)
    
    if ((name) == ("_failurePolicyFactory"))
        (self)._failurePolicyFactory = ::DatawireQuarkCore.cast(value) { ::Quark.mdk_discovery.FailurePolicyFactory }
    end
    if ((name) == ("_dispatcher"))
        (self)._dispatcher = ::DatawireQuarkCore.cast(value) { ::Quark.mdk_runtime.actors.MessageDispatcher }
    end
    if ((name) == ("_timeService"))
        (self)._timeService = ::DatawireQuarkCore.cast(value) { ::Quark.mdk_runtime.Time }
    end
    if ((name) == ("_subscriber"))
        (self)._subscriber = ::DatawireQuarkCore.cast(value) { ::Quark.mdk_runtime.actors.Actor }
    end
    if ((name) == ("_wsclient"))
        (self)._wsclient = ::DatawireQuarkCore.cast(value) { ::Quark.mdk_protocol.WSClient }
    end
    if ((name) == ("registered"))
        (self).registered = ::DatawireQuarkCore.cast(value) { ::Hash }
    end
    if ((name) == ("dlog"))
        (self).dlog = value
    end
    if ((name) == ("lastHeartbeat"))
        (self).lastHeartbeat = ::DatawireQuarkCore.cast(value) { ::Integer }
    end
    if ((name) == ("sock"))
        (self).sock = ::DatawireQuarkCore.cast(value) { ::Quark.mdk_runtime.actors.Actor }
    end

    nil
end

#active(node) ⇒ Object



205
206
207
208
209
210
211
212
213
214
# File 'lib/mdk_discovery/protocol.rb', line 205

def active(node)
    
    active = ::Quark.mdk_discovery.protocol.Active.new()
    (active).node = node
    (active).ttl = ((self)._wsclient).ttl
    (self)._dispatcher.tell(self, active.encode(), (self).sock)
    @dlog.info(("active ") + (node.toString()))

    nil
end

#expire(node) ⇒ Object



216
217
218
219
220
221
222
223
224
# File 'lib/mdk_discovery/protocol.rb', line 216

def expire(node)
    
    expire = ::Quark.mdk_discovery.protocol.Expire.new()
    (expire).node = node
    (self)._dispatcher.tell(self, expire.encode(), (self).sock)
    @dlog.info(("expire ") + (node.toString()))

    nil
end

#heartbeatObject

Send all registered services.



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/mdk_discovery/protocol.rb', line 250

def heartbeat()
    
    services = ::DatawireQuarkCore::List.new(((self).registered).keys)
    idx = 0
    while ((idx) < ((services).size)) do
        jdx = 0
        nodes = (((self).registered)[(services)[idx]]).nodes
        while ((jdx) < ((nodes).size)) do
            self.active((nodes)[jdx])
            jdx = (jdx) + (1)
        end
        idx = (idx) + (1)
    end

    nil
end

#onActive(active) ⇒ Object



233
234
235
236
237
238
# File 'lib/mdk_discovery/protocol.rb', line 233

def onActive(active)
    
    (self)._dispatcher.tell(self, ::Quark.mdk_discovery.NodeActive.new((active).node), (self)._subscriber)

    nil
end

#onExpire(expire) ⇒ Object



240
241
242
243
244
245
# File 'lib/mdk_discovery/protocol.rb', line 240

def onExpire(expire)
    
    (self)._dispatcher.tell(self, ::Quark.mdk_discovery.NodeExpired.new((expire).node), (self)._subscriber)

    nil
end

#onMessage(origin, message) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/mdk_discovery/protocol.rb', line 137

def onMessage(origin, message)
    
    klass = (::Quark.quark.reflect.QuarkClass.get(::DatawireQuarkCore._getClass(message))).id
    if ((klass) == ("mdk_discovery.RegisterNode"))
        register = ::DatawireQuarkCore.cast(message) { ::Quark.mdk_discovery.RegisterNode }
        self._register((register).node)
        return
    end
    ::Quark.mdk_protocol._subscriberDispatch(self, message)

    nil
end

#onMessageFromServer(message) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/mdk_discovery/protocol.rb', line 150

def onMessageFromServer(message)
    
    type = (::Quark.quark.reflect.QuarkClass.get(::DatawireQuarkCore._getClass(message))).id
    if ((type) == ("mdk_discovery.protocol.Active"))
        active = ::DatawireQuarkCore.cast(message) { ::Quark.mdk_discovery.protocol.Active }
        self.onActive(active)
        return
    end
    if ((type) == ("mdk_discovery.protocol.Expire"))
        expire = ::DatawireQuarkCore.cast(message) { ::Quark.mdk_discovery.protocol.Expire }
        self.onExpire(expire)
        return
    end

    nil
end

#onPumpObject



176
177
178
179
180
181
182
183
184
185
186
# File 'lib/mdk_discovery/protocol.rb', line 176

def onPump()
    
    rightNow = (((self)._timeService.time()) * (1000.0)).round()
    heartbeatInterval = (((((self)._wsclient).ttl) / (2.0)) * (1000.0)).round()
    if (((rightNow) - ((self).lastHeartbeat)) >= (heartbeatInterval))
        (self).lastHeartbeat = rightNow
        self.heartbeat()
    end

    nil
end

#onStart(dispatcher) ⇒ Object



123
124
125
126
127
128
# File 'lib/mdk_discovery/protocol.rb', line 123

def onStart(dispatcher)
    
    (self)._dispatcher = dispatcher

    nil
end

#onStopObject



130
131
132
133
134
135
# File 'lib/mdk_discovery/protocol.rb', line 130

def onStop()
    
    self.shutdown()

    nil
end

#onWSConnected(websocket) ⇒ Object



167
168
169
170
171
172
173
174
# File 'lib/mdk_discovery/protocol.rb', line 167

def onWSConnected(websocket)
    
    (self).sock = websocket
    (self).lastHeartbeat = (((self)._timeService.time()) * (1000.0)).round()
    self.heartbeat()

    nil
end

#resolve(node) ⇒ Object



226
227
228
229
230
231
# File 'lib/mdk_discovery/protocol.rb', line 226

def resolve(node)
    
    nil

    nil
end

#shutdownObject



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/mdk_discovery/protocol.rb', line 267

def shutdown()
    
    services = ::DatawireQuarkCore::List.new(((self).registered).keys)
    idx = 0
    while ((idx) < ((services).size)) do
        jdx = 0
        nodes = (((self).registered)[(services)[idx]]).nodes
        while ((jdx) < ((nodes).size)) do
            self.expire((nodes)[jdx])
            jdx = (jdx) + (1)
        end
        idx = (idx) + (1)
    end

    nil
end