Class: RSMP::Proxy
Constant Summary
collapse
- WRAPPING_DELIMITER =
"\f"
Instance Attribute Summary collapse
Attributes included from Task
#task
Attributes included from Distributor
#receivers
Attributes included from Logging
#logger
Class Method Summary
collapse
Instance Method Summary
collapse
-
#acknowledge(original) ⇒ Object
-
#acknowledged_first_ingoing(message) ⇒ Object
-
#acknowledged_first_outgoing(message) ⇒ Object
-
#author ⇒ Object
-
#buffer_message(message) ⇒ Object
-
#check_ack_timeout(now) ⇒ Object
-
#check_core_version(message) ⇒ Object
-
#check_ingoing_acknowledged(message) ⇒ Object
-
#check_outgoing_acknowledged(message) ⇒ Object
TODO this might be better handled by a proper event machine using e.g.
-
#check_watchdog_timeout(now) ⇒ Object
-
#clear ⇒ Object
-
#clock ⇒ Object
-
#close ⇒ Object
close connection, but keep our main task running so we can reconnect.
-
#close_socket ⇒ Object
-
#close_stream ⇒ Object
-
#connected? ⇒ Boolean
-
#core_versions ⇒ Object
-
#disconnect ⇒ Object
-
#disconnected? ⇒ Boolean
-
#dont_acknowledge(original, prefix = nil, reason = nil, force: true) ⇒ Object
-
#dont_expect_acknowledgement(message) ⇒ Object
-
#expect_acknowledgement(message) ⇒ Object
-
#expect_version_message(message) ⇒ Object
-
#extraneous_version(message) ⇒ Object
-
#find_original_for_message(message) ⇒ Object
-
#get_schemas ⇒ Object
-
#handshake_complete ⇒ Object
-
#initialize(options) ⇒ Proxy
constructor
-
#inspect ⇒ Object
-
#log(str, options = {}) ⇒ Object
-
#log_acknowledgement_for_original(message, original) ⇒ Object
-
#log_acknowledgement_for_unknown(message) ⇒ Object
-
#log_send(message, reason = nil) ⇒ Object
-
#now ⇒ Object
-
#process_ack(message) ⇒ Object
-
#process_deferred ⇒ Object
-
#process_message(message) ⇒ Object
-
#process_not_ack(message) ⇒ Object
-
#process_packet(json) ⇒ Object
-
#process_version(message) ⇒ Object
-
#process_watchdog(message) ⇒ Object
-
#read_line ⇒ Object
-
#ready? ⇒ Boolean
-
#receive_error(e, options = {}) ⇒ Object
-
#revive(options) ⇒ Object
revive after a reconnect.
-
#run_reader ⇒ Object
-
#run_timer(task, interval) ⇒ Object
-
#send_and_optionally_collect(message, options, &block) ⇒ Object
-
#send_message(message, reason = nil, validate: true, force: false) ⇒ Object
-
#send_version(site_id, core_versions) ⇒ Object
-
#send_watchdog(now = Clock.now) ⇒ Object
-
#set_nts_message_attributes(message) ⇒ Object
-
#set_state(state) ⇒ Object
-
#setup(options) ⇒ Object
-
#should_validate_ingoing_message?(message) ⇒ Boolean
-
#start_reader ⇒ Object
run an async task that reads from @socket.
-
#start_timer ⇒ Object
-
#start_watchdog ⇒ Object
-
#state_changed ⇒ Object
the state changed override to to things like notifications.
-
#status_subscribe_acknowledged(original) ⇒ Object
-
#stop_reader ⇒ Object
-
#stop_subtasks ⇒ Object
-
#stop_task ⇒ Object
-
#stop_timer ⇒ Object
-
#stop_watchdog ⇒ Object
-
#timer(now) ⇒ Object
-
#verify_sequence(message) ⇒ Object
-
#version_acknowledged ⇒ Object
-
#wait_for_reader ⇒ Object
wait for the reader task to complete, which is not expected to happen before the connection is closed.
-
#wait_for_state(state, timeout:) ⇒ Object
-
#watchdog_send_timer(now) ⇒ Object
-
#will_not_handle(message) ⇒ Object
-
#with_watchdog_disabled ⇒ Object
Methods included from Task
#initialize_task, #restart, #run, #start, #stop, #task_status, #wait, #wait_for_condition
Methods included from Inspect
#inspector
#add_receiver, #clear_deferred_distribution, #distribute, #distribute_error, #distribute_immediately, #distribute_queued, #initialize_distributor, #remove_receiver, #with_deferred_distribution
Methods included from Logging
#initialize_logging
Constructor Details
#initialize(options) ⇒ Proxy
Returns a new instance of Proxy.
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/rsmp/proxy.rb', line 17
def initialize options
@node = options[:node]
initialize_logging options
initialize_distributor
initialize_task
setup options
clear
@state = :disconnected
@state_condition = Async::Notification.new
end
|
Instance Attribute Details
#archive ⇒ Object
Returns the value of attribute archive.
16
17
18
|
# File 'lib/rsmp/proxy.rb', line 16
def archive
@archive
end
|
#collector ⇒ Object
Returns the value of attribute collector.
16
17
18
|
# File 'lib/rsmp/proxy.rb', line 16
def collector
@collector
end
|
#connection_info ⇒ Object
Returns the value of attribute connection_info.
16
17
18
|
# File 'lib/rsmp/proxy.rb', line 16
def connection_info
@connection_info
end
|
#core_version ⇒ Object
Returns the value of attribute core_version.
16
17
18
|
# File 'lib/rsmp/proxy.rb', line 16
def core_version
@core_version
end
|
#ip ⇒ Object
Returns the value of attribute ip.
16
17
18
|
# File 'lib/rsmp/proxy.rb', line 16
def ip
@ip
end
|
#node ⇒ Object
Returns the value of attribute node.
16
17
18
|
# File 'lib/rsmp/proxy.rb', line 16
def node
@node
end
|
#port ⇒ Object
Returns the value of attribute port.
16
17
18
|
# File 'lib/rsmp/proxy.rb', line 16
def port
@port
end
|
#state ⇒ Object
Returns the value of attribute state.
16
17
18
|
# File 'lib/rsmp/proxy.rb', line 16
def state
@state
end
|
#sxl ⇒ Object
Returns the value of attribute sxl.
16
17
18
|
# File 'lib/rsmp/proxy.rb', line 16
def sxl
@sxl
end
|
Class Method Details
.version_meets_requirement?(version, requirement) ⇒ Boolean
Use Gem class to check version requirement Requirement must be a string like ‘1.1’, ‘>=1.0.3’ or ‘<2.1.4’, or list of strings, like [‘<=1.4’,‘<1.5’]
675
676
677
|
# File 'lib/rsmp/proxy.rb', line 675
def self.version_meets_requirement? version, requirement
Gem::Requirement.new(requirement).satisfied_by?(Gem::Version.new(version))
end
|
Instance Method Details
#acknowledge(original) ⇒ Object
498
499
500
501
502
503
504
|
# File 'lib/rsmp/proxy.rb', line 498
def acknowledge original
raise InvalidArgument unless original
ack = MessageAck.build_from(original)
ack.original = original.clone
send_message ack, "for #{ack.original.type} #{original.m_id_short}"
check_ingoing_acknowledged original
end
|
#acknowledged_first_ingoing(message) ⇒ Object
571
572
|
# File 'lib/rsmp/proxy.rb', line 571
def acknowledged_first_ingoing message
end
|
#acknowledged_first_outgoing(message) ⇒ Object
568
569
|
# File 'lib/rsmp/proxy.rb', line 568
def acknowledged_first_outgoing message
end
|
#author ⇒ Object
644
645
646
|
# File 'lib/rsmp/proxy.rb', line 644
def author
@node.site_id
end
|
#buffer_message(message) ⇒ Object
361
362
363
364
|
# File 'lib/rsmp/proxy.rb', line 361
def buffer_message message
end
|
#check_ack_timeout(now) ⇒ Object
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
# File 'lib/rsmp/proxy.rb', line 304
def check_ack_timeout now
timeout = @site_settings['timeouts']['acknowledgement']
@awaiting_acknowledgement.clone.each_pair do |m_id, message|
latest = message.timestamp + timeout
if now > latest
str = "No acknowledgements for #{message.type} #{message.m_id_short} within #{timeout} seconds"
log str, level: :error
begin
close
ensure
distribute_error MissingAcknowledgment.new(str)
end
end
end
end
|
#check_core_version(message) ⇒ Object
482
483
484
485
486
487
488
489
490
491
492
493
|
# File 'lib/rsmp/proxy.rb', line 482
def check_core_version message
versions = core_versions
candidates = message.versions & versions
if candidates.any?
@core_version = candidates.sort_by { |v| Gem::Version.new(v) }.last else
reason = "RSMP versions [#{message.versions.join(',')}] requested, but only [#{versions.join(',')}] supported."
dont_acknowledge message, "Version message rejected", reason, force: true
raise HandshakeError.new reason
end
end
|
#check_ingoing_acknowledged(message) ⇒ Object
561
562
563
564
565
566
|
# File 'lib/rsmp/proxy.rb', line 561
def check_ingoing_acknowledged message
unless @ingoing_acknowledged[message.type]
@ingoing_acknowledged[message.type] = true
acknowledged_first_ingoing message
end
end
|
#check_outgoing_acknowledged(message) ⇒ Object
TODO this might be better handled by a proper event machine using e.g. the EventMachine gem
554
555
556
557
558
559
|
# File 'lib/rsmp/proxy.rb', line 554
def check_outgoing_acknowledged message
unless @outgoing_acknowledged[message.type]
@outgoing_acknowledged[message.type] = true
acknowledged_first_outgoing message
end
end
|
#check_watchdog_timeout(now) ⇒ Object
321
322
323
324
325
326
327
328
329
330
|
# File 'lib/rsmp/proxy.rb', line 321
def check_watchdog_timeout now
timeout = @site_settings['timeouts']['watchdog']
latest = @latest_watchdog_received + timeout
left = latest - now
if left < 0
str = "No Watchdog received within #{timeout} seconds"
log str, level: :warning
distribute MissingWatchdog.new(str)
end
end
|
#clear ⇒ Object
150
151
152
153
154
155
156
157
158
159
160
161
|
# File 'lib/rsmp/proxy.rb', line 150
def clear
@awaiting_acknowledgement = {}
@latest_watchdog_received = nil
@watchdog_started = false
@version_determined = false
@ingoing_acknowledged = {}
@outgoing_acknowledged = {}
@latest_watchdog_send_at = nil
@acknowledgements = {}
@acknowledgement_condition = Async::Notification.new
end
|
#clock ⇒ Object
134
135
136
|
# File 'lib/rsmp/proxy.rb', line 134
def clock
@node.clock
end
|
#close ⇒ Object
close connection, but keep our main task running so we can reconnect
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/rsmp/proxy.rb', line 44
def close
log "Closing connection", level: :warning
close_stream
close_socket
stop_reader
set_state :disconnected
distribute_error DisconnectError.new("Connection was closed")
stop_timer
end
|
#close_socket ⇒ Object
83
84
85
86
87
|
# File 'lib/rsmp/proxy.rb', line 83
def close_socket
@socket.close if @socket
ensure
@socket = nil
end
|
#close_stream ⇒ Object
77
78
79
80
81
|
# File 'lib/rsmp/proxy.rb', line 77
def close_stream
@stream.close if @stream
ensure
@stream = nil
end
|
#connected? ⇒ Boolean
142
143
144
|
# File 'lib/rsmp/proxy.rb', line 142
def connected?
@state == :connected || @state == :ready
end
|
#core_versions ⇒ Object
476
477
478
479
480
|
# File 'lib/rsmp/proxy.rb', line 476
def core_versions
return [RSMP::Schema.latest_core_version] if @site_settings["core_versions"] == 'latest'
return RSMP::Schema.core_versions if @site_settings["core_versions"] == 'all'
[@site_settings["core_versions"]].flatten
end
|
#disconnect ⇒ Object
33
34
|
# File 'lib/rsmp/proxy.rb', line 33
def disconnect
end
|
#disconnected? ⇒ Boolean
146
147
148
|
# File 'lib/rsmp/proxy.rb', line 146
def disconnected?
@state == :disconnected
end
|
#dont_acknowledge(original, prefix = nil, reason = nil, force: true) ⇒ Object
506
507
508
509
510
511
512
513
514
515
516
|
# File 'lib/rsmp/proxy.rb', line 506
def dont_acknowledge original, prefix=nil, reason=nil, force: true
raise InvalidArgument unless original
str = [prefix,reason].join(' ')
log str, message: original, level: :warning if reason
message = MessageNotAck.new({
"oMId" => original.m_id,
"rea" => reason || "Unknown reason"
})
message.original = original.clone
send_message message, "for #{original.type} #{original.m_id_short}", force: force
end
|
#dont_expect_acknowledgement(message) ⇒ Object
468
469
470
|
# File 'lib/rsmp/proxy.rb', line 468
def dont_expect_acknowledgement message
@awaiting_acknowledgement.delete message.attribute("oMId")
end
|
#expect_acknowledgement(message) ⇒ Object
462
463
464
465
466
|
# File 'lib/rsmp/proxy.rb', line 462
def expect_acknowledgement message
unless message.is_a?(MessageAck) || message.is_a?(MessageNotAck)
@awaiting_acknowledgement[message.m_id] = message
end
end
|
#expect_version_message(message) ⇒ Object
631
632
633
634
635
|
# File 'lib/rsmp/proxy.rb', line 631
def expect_version_message message
unless message.is_a?(Version) || message.is_a?(MessageAck) || message.is_a?(MessageNotAck)
raise HandshakeError.new "Version must be received first"
end
end
|
472
473
474
|
# File 'lib/rsmp/proxy.rb', line 472
def message
dont_acknowledge message, "Received", "extraneous Version message"
end
|
#find_original_for_message(message) ⇒ Object
549
550
551
|
# File 'lib/rsmp/proxy.rb', line 549
def find_original_for_message message
@awaiting_acknowledgement[ message.attribute("oMId") ]
end
|
#get_schemas ⇒ Object
336
337
338
339
340
341
|
# File 'lib/rsmp/proxy.rb', line 336
def get_schemas
schemas = { core: RSMP::Schema.latest_core_version } schemas[:core] = core_version if core_version
schemas[sxl] = RSMP::Schema.sanitize_version(sxl_version.to_s) if sxl && sxl_version
schemas
end
|
#handshake_complete ⇒ Object
637
638
639
|
# File 'lib/rsmp/proxy.rb', line 637
def handshake_complete
set_state :ready
end
|
#inspect ⇒ Object
128
129
130
131
132
|
# File 'lib/rsmp/proxy.rb', line 128
def inspect
"#<#{self.class.name}:#{self.object_id}, #{inspector(
:@acknowledgements,:@settings,:@site_settings
)}>"
end
|
#log(str, options = {}) ⇒ Object
332
333
334
|
# File 'lib/rsmp/proxy.rb', line 332
def log str, options={}
super str, options.merge(ip: @ip, port: @port, site_id: @site_id)
end
|
#log_acknowledgement_for_original(message, original) ⇒ Object
610
611
612
613
614
615
616
617
618
619
|
# File 'lib/rsmp/proxy.rb', line 610
def log_acknowledgement_for_original message, original
str = "Received #{message.type} for #{original.type} #{message.attribute("oMId")[0..3]}"
if message.type == 'MessageNotAck'
reason = message.attributes["rea"]
str = "#{str}: #{reason}" if reason
log str, message: message, level: :warning
else
log str, message: message, level: :log
end
end
|
#log_acknowledgement_for_unknown(message) ⇒ Object
621
622
623
|
# File 'lib/rsmp/proxy.rb', line 621
def log_acknowledgement_for_unknown message
log "Received #{message.type} for unknown message #{message.attribute("oMId")[0..3]}", message: message, level: :warning
end
|
#log_send(message, reason = nil) ⇒ Object
366
367
368
369
370
371
372
373
374
375
376
377
378
|
# File 'lib/rsmp/proxy.rb', line 366
def log_send message, reason=nil
if reason
str = "Sent #{message.type} #{reason}"
else
str = "Sent #{message.type}"
end
if message.type == "MessageNotAck"
log str, message: message, level: :warning
else
log str, message: message, level: :log
end
end
|
#now ⇒ Object
29
30
31
|
# File 'lib/rsmp/proxy.rb', line 29
def now
node.now
end
|
#process_ack(message) ⇒ Object
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
|
# File 'lib/rsmp/proxy.rb', line 574
def process_ack message
original = find_original_for_message message
if original
dont_expect_acknowledgement message
message.original = original
log_acknowledgement_for_original message, original
case original.type
when "Version"
version_acknowledged
when "StatusSubscribe"
status_subscribe_acknowledged original
end
check_outgoing_acknowledged original
@acknowledgements[ original.m_id ] = message
@acknowledgement_condition.signal message
else
log_acknowledgement_for_unknown message
end
end
|
#process_deferred ⇒ Object
388
389
390
|
# File 'lib/rsmp/proxy.rb', line 388
def process_deferred
@node.process_deferred
end
|
#process_message(message) ⇒ Object
441
442
443
444
445
446
447
448
449
450
451
452
453
454
|
# File 'lib/rsmp/proxy.rb', line 441
def process_message message
case message
when MessageAck
process_ack message
when MessageNotAck
process_not_ack message
when Version
process_version message
when Watchdog
process_watchdog message
else
dont_acknowledge message, "Received", "unknown message (#{message.type})"
end
end
|
#process_not_ack(message) ⇒ Object
597
598
599
600
601
602
603
604
605
606
607
608
|
# File 'lib/rsmp/proxy.rb', line 597
def process_not_ack message
original = find_original_for_message message
if original
dont_expect_acknowledgement message
message.original = original
log_acknowledgement_for_original message, original
@acknowledgements[ original.m_id ] = message
@acknowledgement_condition.signal message
else
log_acknowledgement_for_unknown message
end
end
|
#process_packet(json) ⇒ Object
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
|
# File 'lib/rsmp/proxy.rb', line 396
def process_packet json
attributes = Message.parse_attributes json
message = Message.build attributes, json
message.validate(get_schemas) if should_validate_ingoing_message?(message)
verify_sequence message
with_deferred_distribution do
distribute message
process_message message
end
process_deferred
message
rescue InvalidPacket => e
str = "Received invalid package, must be valid JSON but got #{json.size} bytes: #{e.message}"
distribute_error e.exception(str)
log str, level: :warning
nil
rescue MalformedMessage => e
str = "Received malformed message, #{e.message}"
distribute_error e.exception(str)
log str, message: Malformed.new(attributes), level: :warning
nil
rescue SchemaError, RSMP::Schema::Error => e
reason = "schema errors: #{e.message}"
str = "Received invalid #{message.type}"
distribute_error e.exception(str), message: message
dont_acknowledge message, str, reason
message
rescue InvalidMessage => e
reason = "#{e.message}"
str = "Received invalid #{message.type},"
distribute_error e.exception("#{str} #{message.json}"), message: message
dont_acknowledge message, str, reason
message
rescue FatalError => e
reason = e.message
str = "Rejected #{message.type},"
distribute_error e.exception(str), message: message
dont_acknowledge message, str, reason
close
message
ensure
@node.clear_deferred
end
|
#process_version(message) ⇒ Object
495
496
|
# File 'lib/rsmp/proxy.rb', line 495
def process_version message
end
|
#process_watchdog(message) ⇒ Object
625
626
627
628
629
|
# File 'lib/rsmp/proxy.rb', line 625
def process_watchdog message
log "Received #{message.type}", message: message, level: :log
@latest_watchdog_received = Clock.now
acknowledge message
end
|
#read_line ⇒ Object
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
# File 'lib/rsmp/proxy.rb', line 194
def read_line
json = @protocol.read_line
beginning = Time.now
message = process_packet json
duration = Time.now - beginning
ms = (duration*1000).round(4)
if duration > 0
per_second = (1.0 / duration).round
else
per_second = Float::INFINITY
end
if message
type = message.type
m_id = Logger.shorten_message_id(message.m_id)
else
type = 'Unknown'
m_id = nil
end
str = [type,m_id,"processed in #{ms}ms, #{per_second}req/s"].compact.join(' ')
log str, level: :statistics
end
|
#ready? ⇒ Boolean
138
139
140
|
# File 'lib/rsmp/proxy.rb', line 138
def ready?
@state == :ready
end
|
#receive_error(e, options = {}) ⇒ Object
216
217
218
|
# File 'lib/rsmp/proxy.rb', line 216
def receive_error e, options={}
@node.receive_error e, options
end
|
#revive(options) ⇒ Object
108
109
110
|
# File 'lib/rsmp/proxy.rb', line 108
def revive options
setup options
end
|
#run_reader ⇒ Object
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
# File 'lib/rsmp/proxy.rb', line 171
def run_reader
@stream ||= Async::IO::Stream.new(@socket)
@protocol ||= Async::IO::Protocol::Line.new(@stream,WRAPPING_DELIMITER) loop do
read_line
end
rescue Restart
log "Closing connection", level: :warning
raise
rescue Async::Wrapper::Cancelled
rescue EOFError, Async::Stop
log "Connection closed", level: :warning
rescue IOError => e
log "IOError: #{e}", level: :warning
rescue Errno::ECONNRESET
log "Connection reset by peer", level: :warning
rescue Errno::EPIPE
log "Broken pipe", level: :warning
rescue StandardError => e
distribute_error e, level: :internal
end
|
#run_timer(task, interval) ⇒ Object
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
# File 'lib/rsmp/proxy.rb', line 251
def run_timer task, interval
next_time = Time.now.to_f
loop do
begin
now = Clock.now
timer(now)
rescue RSMP::Schema::Error => e
log "Timer: Schema error: #{e}", level: :warning
rescue EOFError => e
log "Timer: Connection closed: #{e}", level: :warning
rescue IOError => e
log "Timer: IOError", level: :warning
rescue Errno::ECONNRESET
log "Timer: Connection reset by peer", level: :warning
rescue Errno::EPIPE => e
log "Timer: Broken pipe", level: :warning
rescue StandardError => e
distribute_error e, level: :internal
end
ensure
next_time += interval
duration = next_time - Time.now.to_f
task.sleep duration
end
end
|
#send_and_optionally_collect(message, options, &block) ⇒ Object
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
|
# File 'lib/rsmp/proxy.rb', line 648
def send_and_optionally_collect message, options, &block
collect_options = options[:collect] || options[:collect!]
if collect_options
task = @task.async do |task|
task.annotate 'send_and_optionally_collect'
collector = yield collect_options collector.collect
collector.ok! if options[:collect!] collector
end
send_message message, validate: options[:validate]
{ sent: message, collector: task.wait }
else
send_message message, validate: options[:validate]
return { sent: message }
end
end
|
#send_message(message, reason = nil, validate: true, force: false) ⇒ Object
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
|
# File 'lib/rsmp/proxy.rb', line 343
def send_message message, reason=nil, validate: true, force: false
raise NotReady unless connected? unless force
raise IOError unless @protocol
message.direction = :out
message.generate_json
message.validate get_schemas unless validate==false
@protocol.write_lines message.json
expect_acknowledgement message
distribute message
log_send message, reason
rescue EOFError, IOError
buffer_message message
rescue SchemaError, RSMP::Schema::Error => e
str = "Could not send #{message.type} because schema validation failed: #{e.message}"
log str, message: message, level: :error
distribute_error e.exception("#{str} #{message.json}")
end
|
#send_version(site_id, core_versions) ⇒ Object
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
|
# File 'lib/rsmp/proxy.rb', line 529
def send_version site_id, core_versions
if core_versions=='latest'
versions = [RSMP::Schema.latest_core_version]
elsif core_versions=='all'
versions = RSMP::Schema.core_versions
else
versions = [core_versions].flatten
end
versions_array = versions.map {|v| {"vers" => v} }
site_id_array = [site_id].flatten.map {|id| {"sId" => id} }
version_response = Version.new({
"RSMP"=>versions_array,
"siteId"=>site_id_array,
"SXL"=>sxl_version.to_s
})
send_message version_response
end
|
#send_watchdog(now = Clock.now) ⇒ Object
298
299
300
301
302
|
# File 'lib/rsmp/proxy.rb', line 298
def send_watchdog now=Clock.now
message = Watchdog.new( {"wTs" => clock.to_s})
send_message message
@latest_watchdog_send_at = now
end
|
#set_nts_message_attributes(message) ⇒ Object
667
668
669
670
|
# File 'lib/rsmp/proxy.rb', line 667
def set_nts_message_attributes message
message.attributes['ntsOId'] = (main && main.ntsOId) ? main.ntsOId : ''
message.attributes['xNId'] = (main && main.xNId) ? main.xNId : ''
end
|
#set_state(state) ⇒ Object
95
96
97
98
99
|
# File 'lib/rsmp/proxy.rb', line 95
def set_state state
return if state == @state
@state = state
state_changed
end
|
#setup(options) ⇒ Object
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/rsmp/proxy.rb', line 112
def setup options
@settings = options[:settings]
@socket = options[:socket]
@stream = options[:stream]
@protocol = options[:protocol]
@ip = options[:ip]
@port = options[:port]
@connection_info = options[:info]
@sxl = nil
@site_settings = nil if options[:collect]
@collector = RSMP::Collector.new self, options[:collect]
@collector.start
end
end
|
#should_validate_ingoing_message?(message) ⇒ Boolean
380
381
382
383
384
385
386
|
# File 'lib/rsmp/proxy.rb', line 380
def should_validate_ingoing_message? message
return true unless @site_settings
skip = @site_settings.dig('skip_validation')
return true unless skip
klass = message.class.name.split('::').last
!skip.include?(klass)
end
|
#start_reader ⇒ Object
run an async task that reads from @socket
164
165
166
167
168
169
|
# File 'lib/rsmp/proxy.rb', line 164
def start_reader
@reader = @task.async do |task|
task.annotate "reader"
run_reader
end
end
|
#start_timer ⇒ Object
239
240
241
242
243
244
245
246
247
248
249
|
# File 'lib/rsmp/proxy.rb', line 239
def start_timer
return if @timer
name = "timer"
interval = @site_settings['intervals']['timer'] || 1
log "Starting #{name} with interval #{interval} seconds", level: :debug
@latest_watchdog_received = Clock.now
@timer = @task.async do |task|
task.annotate "timer"
run_timer task, interval
end
end
|
#start_watchdog ⇒ Object
220
221
222
223
|
# File 'lib/rsmp/proxy.rb', line 220
def start_watchdog
log "Starting watchdog with interval #{@site_settings['intervals']['watchdog']} seconds", level: :debug
@watchdog_started = true
end
|
#state_changed ⇒ Object
the state changed override to to things like notifications
103
104
105
|
# File 'lib/rsmp/proxy.rb', line 103
def state_changed
@state_condition.signal @state
end
|
#status_subscribe_acknowledged(original) ⇒ Object
679
680
681
682
683
684
685
686
|
# File 'lib/rsmp/proxy.rb', line 679
def status_subscribe_acknowledged original
component = find_component original.attribute('cId')
return unless component
short = Message.shorten_m_id original.m_id
subscribe_list = original.attributes['sS']
log "StatusSubscribe #{short} acknowledged, allowing repeated status values for #{subscribe_list}", level: :info
component.allow_repeat_updates subscribe_list
end
|
#stop_reader ⇒ Object
71
72
73
74
75
|
# File 'lib/rsmp/proxy.rb', line 71
def stop_reader
@reader.stop if @reader
ensure
@reader = nil
end
|
#stop_subtasks ⇒ Object
58
59
60
61
62
63
|
# File 'lib/rsmp/proxy.rb', line 58
def stop_subtasks
stop_timer
stop_reader
clear
super
end
|
#stop_task ⇒ Object
89
90
91
92
|
# File 'lib/rsmp/proxy.rb', line 89
def stop_task
close
super
end
|
#stop_timer ⇒ Object
65
66
67
68
69
|
# File 'lib/rsmp/proxy.rb', line 65
def stop_timer
@timer.stop if @timer
ensure
@timer = nil
end
|
#stop_watchdog ⇒ Object
225
226
227
228
|
# File 'lib/rsmp/proxy.rb', line 225
def stop_watchdog
log "Stopping watchdog", level: :debug
@watchdog_started = false
end
|
#timer(now) ⇒ Object
277
278
279
280
281
|
# File 'lib/rsmp/proxy.rb', line 277
def timer now
watchdog_send_timer now
check_ack_timeout now
check_watchdog_timeout now
end
|
#verify_sequence(message) ⇒ Object
392
393
394
|
# File 'lib/rsmp/proxy.rb', line 392
def verify_sequence message
expect_version_message(message) unless @version_determined
end
|
#version_acknowledged ⇒ Object
641
642
|
# File 'lib/rsmp/proxy.rb', line 641
def version_acknowledged
end
|
#wait_for_reader ⇒ Object
wait for the reader task to complete, which is not expected to happen before the connection is closed
39
40
41
|
# File 'lib/rsmp/proxy.rb', line 39
def wait_for_reader
@reader.wait if @reader
end
|
#wait_for_state(state, timeout:) ⇒ Object
518
519
520
521
522
523
524
525
526
527
|
# File 'lib/rsmp/proxy.rb', line 518
def wait_for_state state, timeout:
states = [state].flatten
return if states.include?(@state)
wait_for_condition(@state_condition,timeout: timeout) do
states.include?(@state)
end
@state
rescue RSMP::TimeoutError
raise RSMP::TimeoutError.new "Did not reach state #{state} within #{timeout}s"
end
|
#watchdog_send_timer(now) ⇒ Object
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
# File 'lib/rsmp/proxy.rb', line 283
def watchdog_send_timer now
return unless @watchdog_started
return if @site_settings['intervals']['watchdog'] == :never
if @latest_watchdog_send_at == nil
send_watchdog now
else
diff = now - @latest_watchdog_send_at
if (diff + 0.5*@site_settings['intervals']['timer']) >= (@site_settings['intervals']['watchdog'])
send_watchdog now
end
end
end
|
#will_not_handle(message) ⇒ Object
456
457
458
459
460
|
# File 'lib/rsmp/proxy.rb', line 456
def will_not_handle message
reason = "since we're a #{self.class.name.downcase}" unless reason
log "Ignoring #{message.type}, #{reason}", message: message, level: :warning
dont_acknowledge message, nil, reason
end
|
#with_watchdog_disabled ⇒ Object
230
231
232
233
234
235
236
|
# File 'lib/rsmp/proxy.rb', line 230
def with_watchdog_disabled
was = @watchdog_started
stop_watchdog if was
yield
ensure
start_watchdog if was
end
|