rustorb-dds 0.3.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.
@@ -0,0 +1,523 @@
1
+ # frozen_string_literal: true
2
+
3
+ # DDS::Core: the exceptions, values, statuses and policies, as the C++ PSM
4
+ # (DDS-PSM-Cxx 1.0, dds::core) has them, in Ruby's names.
5
+ #
6
+ # Licensed under MIT OR Apache-2.0, like the rest of rustorb-dds.
7
+
8
+ module DDS
9
+ module Core
10
+ # The exceptions of the C++ PSM (dds::core::Exception), for the
11
+ # ReturnCode_t of a call.
12
+ class Error < StandardError; end
13
+ class AlreadyClosedError < Error; end
14
+ class IllegalOperationError < Error; end
15
+ class ImmutablePolicyError < Error; end
16
+ class InconsistentPolicyError < Error; end
17
+ class InvalidArgumentError < Error; end
18
+ class NotEnabledError < Error; end
19
+ class OutOfResourcesError < Error; end
20
+ class PreconditionNotMetError < Error; end
21
+ class TimeoutError < Error; end
22
+ class UnsupportedError < Error; end
23
+ # NO_DATA: no sample (a reader's key_value of an unknown instance...).
24
+ class NoDataError < Error; end
25
+
26
+ # By ReturnCode_t (RDDS_RETCODE_*).
27
+ ERRORS = [nil, Error, UnsupportedError, InvalidArgumentError, PreconditionNotMetError, OutOfResourcesError,
28
+ NotEnabledError, ImmutablePolicyError, InconsistentPolicyError, AlreadyClosedError, TimeoutError,
29
+ NoDataError, IllegalOperationError].freeze
30
+
31
+ # A duration: seconds and nanoseconds.
32
+ class Duration
33
+ include Comparable
34
+
35
+ INFINITE_SEC = 0x7fffffff
36
+ INFINITE_NSEC = 0x7fffffff
37
+
38
+ attr_reader :sec, :nanosec
39
+
40
+ def initialize(sec = 0, nanosec = 0)
41
+ @sec = Integer(sec)
42
+ @nanosec = Integer(nanosec)
43
+ freeze
44
+ end
45
+
46
+ def self.from_secs(s)
47
+ return infinite if s.respond_to?(:infinite?) && s.infinite?
48
+
49
+ whole = s.floor
50
+ new(whole, ((s - whole) * 1_000_000_000).round.clamp(0, 999_999_999))
51
+ end
52
+
53
+ def self.from_millisecs(ms)
54
+ new(ms / 1000, (ms % 1000) * 1_000_000)
55
+ end
56
+
57
+ def self.zero
58
+ new(0, 0)
59
+ end
60
+
61
+ def self.infinite
62
+ new(INFINITE_SEC, INFINITE_NSEC)
63
+ end
64
+
65
+ def infinite?
66
+ @sec == INFINITE_SEC && @nanosec == INFINITE_NSEC
67
+ end
68
+
69
+ def to_f
70
+ infinite? ? Float::INFINITY : @sec + @nanosec / 1e9
71
+ end
72
+
73
+ def <=>(other)
74
+ return nil unless other.is_a?(Duration)
75
+
76
+ [@sec, @nanosec] <=> [other.sec, other.nanosec]
77
+ end
78
+
79
+ def hash
80
+ [@sec, @nanosec].hash
81
+ end
82
+ alias eql? ==
83
+
84
+ def to_native
85
+ [@sec, @nanosec]
86
+ end
87
+
88
+ # A Duration, or seconds (Numeric).
89
+ def self.of(v)
90
+ v.is_a?(Duration) ? v : from_secs(v)
91
+ end
92
+
93
+ def inspect
94
+ infinite? ? "#<DDS::Core::Duration infinite>" : "#<DDS::Core::Duration #{to_f}s>"
95
+ end
96
+ end
97
+
98
+ # A time: seconds and nanoseconds since 1970-01-01T00:00:00Z.
99
+ class Time
100
+ include Comparable
101
+
102
+ INVALID_SEC = -1
103
+ INVALID_NSEC = 0xffffffff
104
+
105
+ attr_reader :sec, :nanosec
106
+
107
+ def initialize(sec = 0, nanosec = 0)
108
+ @sec = Integer(sec)
109
+ @nanosec = Integer(nanosec)
110
+ freeze
111
+ end
112
+
113
+ def self.from_secs(s)
114
+ whole = s.floor
115
+ new(whole, ((s - whole) * 1_000_000_000).round.clamp(0, 999_999_999))
116
+ end
117
+
118
+ def self.invalid
119
+ new(INVALID_SEC, INVALID_NSEC)
120
+ end
121
+
122
+ def self.now
123
+ t = ::Time.now
124
+ new(t.to_i, t.nsec)
125
+ end
126
+
127
+ def valid?
128
+ !(@sec == INVALID_SEC && @nanosec == INVALID_NSEC)
129
+ end
130
+
131
+ def to_f
132
+ @sec + @nanosec / 1e9
133
+ end
134
+
135
+ def to_time
136
+ ::Time.at(@sec, @nanosec, :nsec)
137
+ end
138
+
139
+ def <=>(other)
140
+ return nil unless other.is_a?(Time)
141
+
142
+ [@sec, @nanosec] <=> [other.sec, other.nanosec]
143
+ end
144
+
145
+ def hash
146
+ [@sec, @nanosec].hash
147
+ end
148
+ alias eql? ==
149
+
150
+ def to_native
151
+ [@sec, @nanosec]
152
+ end
153
+
154
+ # A Time, a ::Time or seconds since the epoch (Numeric).
155
+ def self.of(v)
156
+ case v
157
+ when Time then v
158
+ when ::Time then new(v.to_i, v.nsec)
159
+ else from_secs(v)
160
+ end
161
+ end
162
+
163
+ def inspect
164
+ valid? ? "#<DDS::Core::Time #{to_f}>" : "#<DDS::Core::Time invalid>"
165
+ end
166
+ end
167
+
168
+ # An instance handle (16 octets; all zeros is nil): of an instance, or
169
+ # of an entity (its GUID).
170
+ class InstanceHandle
171
+ attr_reader :value
172
+
173
+ def initialize(value = ("\0" * 16).b)
174
+ raise ArgumentError, "16 octets expected" unless value.bytesize == 16
175
+
176
+ @value = value.b.freeze
177
+ freeze
178
+ end
179
+
180
+ NIL = new
181
+
182
+ def self.nil_handle
183
+ NIL
184
+ end
185
+
186
+ def is_nil # rubocop:disable Naming/PredicateName
187
+ @value == NIL.value
188
+ end
189
+ alias nil_handle? is_nil
190
+
191
+ def ==(other)
192
+ other.is_a?(InstanceHandle) && other.value == @value
193
+ end
194
+ alias eql? ==
195
+
196
+ def hash
197
+ @value.hash
198
+ end
199
+
200
+ def inspect
201
+ "#<DDS::Core::InstanceHandle #{@value.unpack1('H*')}>"
202
+ end
203
+ alias to_s inspect
204
+ end
205
+
206
+ module Status
207
+ # StatusKind bits (DDS 1.4, 2.2.4.1).
208
+ module StatusMask
209
+ INCONSISTENT_TOPIC = 1 << 0
210
+ OFFERED_DEADLINE_MISSED = 1 << 1
211
+ REQUESTED_DEADLINE_MISSED = 1 << 2
212
+ OFFERED_INCOMPATIBLE_QOS = 1 << 5
213
+ REQUESTED_INCOMPATIBLE_QOS = 1 << 6
214
+ SAMPLE_LOST = 1 << 7
215
+ SAMPLE_REJECTED = 1 << 8
216
+ DATA_ON_READERS = 1 << 9
217
+ DATA_AVAILABLE = 1 << 10
218
+ LIVELINESS_LOST = 1 << 11
219
+ LIVELINESS_CHANGED = 1 << 12
220
+ PUBLICATION_MATCHED = 1 << 13
221
+ SUBSCRIPTION_MATCHED = 1 << 14
222
+ ALL = 0xffffffff
223
+ NONE = 0
224
+ end
225
+
226
+ def self.handle(bytes)
227
+ InstanceHandle.new(bytes)
228
+ end
229
+
230
+ PublicationMatchedStatus = ::Struct.new(:total_count, :total_count_change, :current_count,
231
+ :current_count_change, :last_subscription_handle) do
232
+ def self.of(a) = new(a[0], a[1], a[2], a[3], Status.handle(a[4]))
233
+ end
234
+ SubscriptionMatchedStatus = ::Struct.new(:total_count, :total_count_change, :current_count,
235
+ :current_count_change, :last_publication_handle) do
236
+ def self.of(a) = new(a[0], a[1], a[2], a[3], Status.handle(a[4]))
237
+ end
238
+ QosPolicyCount = ::Struct.new(:policy_id, :count)
239
+ OfferedIncompatibleQosStatus = ::Struct.new(:total_count, :total_count_change, :last_policy_id, :policies) do
240
+ def self.of(a) = new(a[0], a[1], a[2], a[3].map { |id, n| QosPolicyCount.new(id, n) })
241
+ end
242
+ RequestedIncompatibleQosStatus = ::Struct.new(:total_count, :total_count_change, :last_policy_id, :policies) do
243
+ def self.of(a) = new(a[0], a[1], a[2], a[3].map { |id, n| QosPolicyCount.new(id, n) })
244
+ end
245
+ LivelinessLostStatus = ::Struct.new(:total_count, :total_count_change) do
246
+ def self.of(a) = new(a[0], a[1])
247
+ end
248
+ LivelinessChangedStatus = ::Struct.new(:alive_count, :not_alive_count, :alive_count_change,
249
+ :not_alive_count_change, :last_publication_handle) do
250
+ def self.of(a) = new(a[0], a[1], a[2], a[3], Status.handle(a[4]))
251
+ end
252
+ OfferedDeadlineMissedStatus = ::Struct.new(:total_count, :total_count_change, :last_instance_handle) do
253
+ def self.of(a) = new(a[0], a[1], Status.handle(a[2]))
254
+ end
255
+ RequestedDeadlineMissedStatus = ::Struct.new(:total_count, :total_count_change, :last_instance_handle) do
256
+ def self.of(a) = new(a[0], a[1], Status.handle(a[2]))
257
+ end
258
+ SampleLostStatus = ::Struct.new(:total_count, :total_count_change) do
259
+ def self.of(a) = new(a[0], a[1])
260
+ end
261
+ # last_reason: SampleRejectedState (NOT_REJECTED...).
262
+ SampleRejectedStatus = ::Struct.new(:total_count, :total_count_change, :last_reason, :last_instance_handle) do
263
+ def self.of(a) = new(a[0], a[1], a[2], Status.handle(a[3]))
264
+ end
265
+ InconsistentTopicStatus = ::Struct.new(:total_count, :total_count_change) do
266
+ def self.of(a) = new(a[0], a[1])
267
+ end
268
+
269
+ module SampleRejectedState
270
+ NOT_REJECTED = 0
271
+ REJECTED_BY_INSTANCES_LIMIT = 1
272
+ REJECTED_BY_SAMPLES_LIMIT = 2
273
+ REJECTED_BY_SAMPLES_PER_INSTANCE_LIMIT = 3
274
+ end
275
+
276
+ # The status of each listener call, by its name.
277
+ OF_EVENT = {
278
+ on_offered_deadline_missed: OfferedDeadlineMissedStatus,
279
+ on_offered_incompatible_qos: OfferedIncompatibleQosStatus,
280
+ on_liveliness_lost: LivelinessLostStatus,
281
+ on_publication_matched: PublicationMatchedStatus,
282
+ on_requested_deadline_missed: RequestedDeadlineMissedStatus,
283
+ on_requested_incompatible_qos: RequestedIncompatibleQosStatus,
284
+ on_sample_rejected: SampleRejectedStatus,
285
+ on_liveliness_changed: LivelinessChangedStatus,
286
+ on_subscription_matched: SubscriptionMatchedStatus,
287
+ on_sample_lost: SampleLostStatus,
288
+ on_inconsistent_topic: InconsistentTopicStatus
289
+ }.freeze
290
+ end
291
+
292
+ # What every entity has (dds::core::Entity).
293
+ class Entity
294
+ # Whether close was called (or the parent closed).
295
+ def closed?
296
+ @closed ? true : false
297
+ end
298
+
299
+ # Entities are enabled when they are made.
300
+ def enable; end
301
+
302
+ def listener
303
+ @listener
304
+ end
305
+
306
+ # Sets the listener (nil clears it) for the statuses of mask.
307
+ def set_listener(listener, mask = Status::StatusMask::ALL)
308
+ check_open
309
+ id = listener ? Native.listener(self, listener) : nil
310
+ Native.set_listener(@handle, id, mask)
311
+ @listener = listener
312
+ @listener_mask = mask
313
+ end
314
+
315
+ def close
316
+ return if @closed
317
+
318
+ close_contained
319
+ @closed = true
320
+ Native.release(@handle) if @handle
321
+ @parent&.send(:forget, self)
322
+ end
323
+
324
+ # Releases the native handle without telling the parent (the parent
325
+ # is closing).
326
+ def close_from_parent
327
+ return if @closed
328
+
329
+ close_contained
330
+ @closed = true
331
+ Native.release(@handle) if @handle
332
+ end
333
+
334
+ # The native handle (DDS::Native::Handle).
335
+ attr_reader :handle
336
+
337
+ # When a parent closes it: topics after the rest.
338
+ def close_order
339
+ 0
340
+ end
341
+
342
+ # The entity's StatusCondition (DDS 1.4, 2.2.2.1.1.5); writers and
343
+ # readers have one.
344
+ def status_condition
345
+ check_open
346
+ @status_condition ||= Cond::StatusCondition.new(self, Native.status_condition(@handle))
347
+ end
348
+
349
+ # The statuses that changed since they were last read (StatusMask
350
+ # bits); writers and readers have them.
351
+ def status_changes
352
+ check_open
353
+ Native.status_changes(@handle)
354
+ end
355
+
356
+ # Entities this one made, which close with it.
357
+ def adopt(child)
358
+ (@contained ||= []) << child
359
+ child
360
+ end
361
+
362
+ def forget(child)
363
+ @contained&.delete(child)
364
+ end
365
+
366
+ private
367
+
368
+ def check_open
369
+ raise AlreadyClosedError, "the #{self.class.name.split('::').last} is closed" if @closed
370
+ end
371
+
372
+ def close_contained
373
+ # Writers and readers before the topics they use.
374
+ (@contained || []).sort_by(&:close_order).each(&:close_from_parent)
375
+ @contained = []
376
+ end
377
+
378
+ # A listener call of a participant, publisher, subscriber or topic:
379
+ # the writer, reader, subscriber or topic it is about, found in
380
+ # participant.
381
+ def deliver_group(participant, event, endpoint, arg, listener)
382
+ case event
383
+ when :on_data_on_readers
384
+ sub = participant.subscriber_for(arg)
385
+ listener.on_data_on_readers(sub) if sub && listener.respond_to?(:on_data_on_readers)
386
+ when :on_inconsistent_topic
387
+ topic = participant.find_topic(endpoint)
388
+ if topic && listener.respond_to?(event)
389
+ listener.on_inconsistent_topic(topic, Status::InconsistentTopicStatus.of(arg))
390
+ end
391
+ when :on_data_available
392
+ return unless listener.respond_to?(event)
393
+
394
+ participant.endpoints.deliver(endpoint) { |r| listener.on_data_available(r) }
395
+ else
396
+ return unless listener.respond_to?(event)
397
+
398
+ status = Status::OF_EVENT.fetch(event).of(arg)
399
+ participant.endpoints.deliver(endpoint) { |e| listener.public_send(event, e, status) }
400
+ end
401
+ end
402
+
403
+ # A listener call of a writer's or reader's own listener.
404
+ def deliver_own(event, arg, listener)
405
+ return unless listener.respond_to?(event)
406
+
407
+ if event == :on_data_available
408
+ listener.on_data_available(self)
409
+ else
410
+ listener.public_send(event, self, Status::OF_EVENT.fetch(event).of(arg))
411
+ end
412
+ end
413
+ end
414
+
415
+ # Conditions of WaitSets (dds::core::cond).
416
+ module Cond
417
+ # What every condition has: its trigger value and a handler a
418
+ # WaitSet's dispatch calls.
419
+ class Condition
420
+ attr_reader :handle
421
+
422
+ def trigger_value
423
+ Native.trigger_value(@handle)
424
+ end
425
+
426
+ # The block WaitSet#dispatch calls when the condition is true.
427
+ def handler(&blk)
428
+ @handler = blk
429
+ self
430
+ end
431
+
432
+ def dispatch
433
+ @handler&.call(self)
434
+ end
435
+
436
+ def ==(other)
437
+ other.is_a?(Condition) && Native.condition_equals(@handle, other.handle)
438
+ end
439
+ alias eql? ==
440
+
441
+ def hash
442
+ self.class.hash
443
+ end
444
+ end
445
+
446
+ # A condition the application sets.
447
+ class GuardCondition < Condition
448
+ def initialize
449
+ super
450
+ @handle = Native.guard_condition
451
+ end
452
+
453
+ def trigger_value=(v)
454
+ Native.set_trigger_value(@handle, v)
455
+ end
456
+ end
457
+
458
+ # The condition of an entity's statuses (enabled_statuses).
459
+ class StatusCondition < Condition
460
+ attr_reader :entity
461
+
462
+ def initialize(entity, handle)
463
+ super()
464
+ @entity = entity
465
+ @handle = handle
466
+ end
467
+
468
+ def enabled_statuses
469
+ Native.enabled_statuses(@handle)
470
+ end
471
+
472
+ def enabled_statuses=(mask)
473
+ Native.set_enabled_statuses(@handle, mask)
474
+ end
475
+ end
476
+
477
+ # Waits for conditions.
478
+ class WaitSet
479
+ def initialize
480
+ @handle = Native.waitset_new
481
+ @conditions = []
482
+ @lock = Mutex.new
483
+ end
484
+
485
+ def attach_condition(c)
486
+ @lock.synchronize do
487
+ return self if @conditions.any? { |x| x.equal?(c) }
488
+
489
+ Native.waitset_attach(@handle, c.handle, true)
490
+ @conditions << c
491
+ end
492
+ self
493
+ end
494
+ alias << attach_condition
495
+
496
+ def detach_condition(c)
497
+ @lock.synchronize do
498
+ i = @conditions.index { |x| x.equal?(c) } or return false
499
+ Native.waitset_attach(@handle, c.handle, false)
500
+ @conditions.delete_at(i)
501
+ end
502
+ true
503
+ end
504
+
505
+ def conditions
506
+ @lock.synchronize { @conditions.dup }
507
+ end
508
+
509
+ # The conditions that are true, waiting up to timeout (a Duration
510
+ # or seconds) for one; DDS::Core::TimeoutError when none is.
511
+ def wait(timeout = Duration.infinite)
512
+ attached = conditions
513
+ Native.waitset_wait(@handle, Duration.of(timeout).to_native, attached.size).map { |i| attached[i] }
514
+ end
515
+
516
+ # Waits, then calls the handlers of the conditions that are true.
517
+ def dispatch(timeout = Duration.infinite)
518
+ wait(timeout).each(&:dispatch)
519
+ end
520
+ end
521
+ end
522
+ end
523
+ end
@@ -0,0 +1,133 @@
1
+ # frozen_string_literal: true
2
+
3
+ # DDS::Domain::DomainParticipant (dds::domain of the C++ PSM).
4
+ #
5
+ # Licensed under MIT OR Apache-2.0, like the rest of rustorb-dds.
6
+
7
+ module DDS
8
+ module Domain
9
+ # The listener of a participant: every call of the other listeners,
10
+ # for the entities whose own listeners do not take it.
11
+ module DomainParticipantListener
12
+ include Pub::PublisherListener
13
+ include Sub::SubscriberListener
14
+ include DDS::Topic::TopicListener
15
+ end
16
+
17
+ # A participant in a domain: makes topics, publishers and subscribers,
18
+ # which close with it.
19
+ class DomainParticipant < Core::Entity
20
+ attr_reader :domain_id, :qos, :endpoints
21
+
22
+ # localhost: only the loopback interface (for tests; a rustorb-dds
23
+ # extension).
24
+ def initialize(domain_id = 0, qos = nil, listener = nil, mask = Core::Status::StatusMask::ALL, localhost: false)
25
+ super()
26
+ qos ||= DomainParticipantQos.new
27
+ @qos = qos.dup.freeze
28
+ @handle = Native.participant_new(domain_id, qos.user_data.value, qos.property.value.to_a, localhost ? 1 : 0)
29
+ @domain_id, @guid, = Native.participant_info(@handle)
30
+ @endpoints = Native::Endpoints.new
31
+ @topics = {}
32
+ @subscribers = []
33
+ @lock = Mutex.new
34
+ set_listener(listener, mask) if listener
35
+ end
36
+
37
+ def instance_handle
38
+ Core::InstanceHandle.new(@guid)
39
+ end
40
+
41
+ # How many other participants are known (a rustorb-dds extension).
42
+ def discovered_participants
43
+ check_open
44
+ Native.participant_info(@handle)[2]
45
+ end
46
+
47
+ def assert_liveliness
48
+ check_open
49
+ Native.participant_assert_liveliness(@handle)
50
+ end
51
+
52
+ def default_publisher_qos = Pub::PublisherQos.new
53
+ def default_subscriber_qos = Sub::SubscriberQos.new
54
+ def default_topic_qos = DDS::Topic::TopicQos.new
55
+ def default_datawriter_qos = Pub::DataWriterQos.new
56
+ def default_datareader_qos = Sub::DataReaderQos.new
57
+
58
+ # The topic of that name this participant made, or nil.
59
+ def find_topic(name)
60
+ t = lookup_topicdescription(name)
61
+ t.is_a?(DDS::Topic::Topic) ? t : nil
62
+ end
63
+
64
+ # The topic or content filtered topic of that name, or nil.
65
+ def lookup_topicdescription(name)
66
+ @lock.synchronize { @topics[name] }
67
+ end
68
+
69
+ def current_time
70
+ Core::Time.now
71
+ end
72
+
73
+ # Closes the topics, publishers and subscribers it made.
74
+ def delete_contained_entities
75
+ check_open
76
+ close_contained
77
+ @lock.synchronize do
78
+ @topics.clear
79
+ @subscribers.clear
80
+ end
81
+ end
82
+
83
+ def add_topic(topic)
84
+ @lock.synchronize do
85
+ raise Core::PreconditionNotMetError, "the topic #{topic.name} exists" if @topics.key?(topic.name)
86
+
87
+ @topics[topic.name] = topic
88
+ end
89
+ adopt(topic)
90
+ end
91
+
92
+ def add_subscriber(sub)
93
+ @lock.synchronize { @subscribers << sub }
94
+ adopt(sub)
95
+ end
96
+
97
+ def forget(child)
98
+ super
99
+ @lock.synchronize do
100
+ @topics.delete(child.name) if @topics[child.name].equal?(child)
101
+ @subscribers.delete(child)
102
+ end
103
+ end
104
+
105
+ # The subscriber of a handle the library gave a listener (which is
106
+ # released).
107
+ def subscriber_for(handle)
108
+ return nil unless handle
109
+
110
+ subs = @lock.synchronize { @subscribers.dup }
111
+ subs.find { |s| !s.closed? && Native.subscriber_same(s.handle, handle) }
112
+ ensure
113
+ Native.release(handle) if handle
114
+ end
115
+
116
+ def deliver(event, endpoint, arg, listener)
117
+ deliver_group(self, event, endpoint, arg, listener)
118
+ end
119
+
120
+ def close
121
+ super
122
+ @lock.synchronize do
123
+ @topics.clear
124
+ @subscribers.clear
125
+ end
126
+ end
127
+
128
+ def inspect
129
+ "#<DDS::Domain::DomainParticipant domain=#{@domain_id}#{closed? ? ' closed' : ''}>"
130
+ end
131
+ end
132
+ end
133
+ end