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,273 @@
1
+ # frozen_string_literal: true
2
+
3
+ # DDS::Sub::Subscriber, DataReader, their samples and ReadCondition
4
+ # (dds::sub of the C++ PSM).
5
+ #
6
+ # Licensed under MIT OR Apache-2.0, like the rest of rustorb-dds.
7
+
8
+ module DDS
9
+ module Sub
10
+ # The listener of a reader.
11
+ module DataReaderListener
12
+ def on_requested_deadline_missed(reader, status); end
13
+ def on_requested_incompatible_qos(reader, status); end
14
+ def on_sample_rejected(reader, status); end
15
+ def on_liveliness_changed(reader, status); end
16
+ def on_data_available(reader); end
17
+ def on_subscription_matched(reader, status); end
18
+ def on_sample_lost(reader, status); end
19
+ end
20
+
21
+ # The listener of a subscriber: on_data_on_readers, and the calls of
22
+ # its readers' listeners. When it (or the participant's) takes
23
+ # DATA_ON_READERS, on_data_available is not called
24
+ # (subscriber.notify_datareaders calls it).
25
+ module SubscriberListener
26
+ include DataReaderListener
27
+
28
+ def on_data_on_readers(subscriber); end
29
+ end
30
+
31
+ module Status
32
+ # Sample, view and instance states (DDS 1.4, 2.2.2.5.1.8).
33
+ module SampleState
34
+ READ = 1
35
+ NOT_READ = 2
36
+ ANY = 3
37
+ end
38
+
39
+ module ViewState
40
+ NEW = 1
41
+ NOT_NEW = 2
42
+ ANY = 3
43
+ end
44
+
45
+ module InstanceState
46
+ ALIVE = 1
47
+ NOT_ALIVE_DISPOSED = 2
48
+ NOT_ALIVE_NO_WRITERS = 4
49
+ NOT_ALIVE = 6
50
+ ANY = 7
51
+ end
52
+ end
53
+
54
+ SampleInfo = ::Struct.new(:sample_state, :view_state, :instance_state, :disposed_generation_count,
55
+ :no_writers_generation_count, :sample_rank, :generation_rank,
56
+ :absolute_generation_rank, :source_timestamp, :instance_handle,
57
+ :publication_handle, :sequence_number, :valid_data) do
58
+ def self.of(a)
59
+ new(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], Core::Time.new(*a[8]), Core::InstanceHandle.new(a[9]),
60
+ Core::InstanceHandle.new(a[10]), a[11], a[12])
61
+ end
62
+
63
+ def valid?
64
+ valid_data
65
+ end
66
+ end
67
+
68
+ # A sample: its data (nil when the info has no valid data) and info.
69
+ Sample = ::Struct.new(:data, :info) do
70
+ def valid?
71
+ info.valid_data
72
+ end
73
+ end
74
+
75
+ # Makes readers, which close with it.
76
+ class Subscriber < Core::Entity
77
+ attr_reader :participant, :qos
78
+
79
+ def initialize(participant, qos = nil, listener = nil, mask = Core::Status::StatusMask::ALL)
80
+ super()
81
+ @participant = participant
82
+ @qos = (qos || SubscriberQos.new).dup.freeze
83
+ @handle = Native.create_subscriber(participant.handle, @qos.partition.name)
84
+ @parent = participant
85
+ participant.add_subscriber(self)
86
+ set_listener(listener, mask) if listener
87
+ end
88
+
89
+ def default_datareader_qos = DataReaderQos.new
90
+
91
+ # Calls on_data_available of the listeners of its readers that have
92
+ # new data.
93
+ def notify_datareaders
94
+ check_open
95
+ Native.notify_datareaders(@handle)
96
+ end
97
+
98
+ def deliver(event, endpoint, arg, listener)
99
+ deliver_group(@participant, event, endpoint, arg, listener)
100
+ end
101
+
102
+ def inspect
103
+ "#<DDS::Sub::Subscriber #{@qos.partition.name.inspect}>"
104
+ end
105
+ end
106
+
107
+ # Reads the samples of a topic (or content filtered topic).
108
+ class DataReader < Core::Entity
109
+ attr_reader :subscriber, :topic_description, :qos
110
+
111
+ LENGTH_UNLIMITED = -1
112
+
113
+ def initialize(subscriber, topic, qos = nil, listener = nil, mask = Core::Status::StatusMask::ALL)
114
+ super()
115
+ @subscriber = subscriber
116
+ @topic_description = topic
117
+ @type = topic.type
118
+ @qos = (qos || DataReaderQos.new).dup.freeze
119
+ slot = listener ? Native::Slot.new : nil
120
+ id = slot ? Native.listener(slot, listener) : nil
121
+ begin
122
+ @handle = Native.create_reader(subscriber.handle, topic.handle, @qos.to_native, id, mask)
123
+ rescue StandardError
124
+ Native.forget_listener(id) if id
125
+ raise
126
+ end
127
+ @listener = listener
128
+ @guid = Native.reader_guid(@handle)
129
+ @parent = subscriber
130
+ subscriber.adopt(self)
131
+ participant.endpoints.add(@guid, self)
132
+ slot.entity = self if slot
133
+ end
134
+
135
+ alias topic topic_description
136
+
137
+ def participant
138
+ @subscriber.participant
139
+ end
140
+
141
+ def instance_handle
142
+ Core::InstanceHandle.new(@guid)
143
+ end
144
+
145
+ # The samples (DDS::Sub::Sample), left in the reader: at most
146
+ # max_samples of those in the states given, of instance (a handle),
147
+ # or of the instance after next_instance (a handle, nil handle for
148
+ # the first), or those of condition (a ReadCondition).
149
+ def read(**selection)
150
+ select(false, **selection)
151
+ end
152
+
153
+ # The samples, taken from the reader (as read).
154
+ def take(**selection)
155
+ select(true, **selection)
156
+ end
157
+
158
+ # The values of the valid samples taken.
159
+ def take_data(**selection)
160
+ take(**selection).select(&:valid?).map(&:data)
161
+ end
162
+
163
+ # The values of the valid samples read.
164
+ def read_data(**selection)
165
+ read(**selection).select(&:valid?).map(&:data)
166
+ end
167
+
168
+ # The key of an instance, as a sample (its other members zero).
169
+ def key_value(handle)
170
+ check_open
171
+ Types.read(@type, Native.key_value(@handle, handle.value))
172
+ end
173
+
174
+ # The handle of the instance of the sample's key, or the nil handle.
175
+ def lookup_instance(sample)
176
+ check_open
177
+ Core::InstanceHandle.new(Native.reader_lookup_instance(@handle, Types.out(@type, sample)))
178
+ end
179
+
180
+ # Waits until a sample is there (a rustorb-dds extension;
181
+ # DDS::Core::TimeoutError after timeout).
182
+ def wait_for_data(timeout = Core::Duration.infinite)
183
+ check_open
184
+ Native.wait(@handle, 2, 0, Core::Duration.of(timeout).to_native)
185
+ end
186
+
187
+ # Waits until count writers match (a rustorb-dds extension).
188
+ def wait_for_publications(count = 1, timeout = Core::Duration.infinite)
189
+ check_open
190
+ Native.wait(@handle, 3, count, Core::Duration.of(timeout).to_native)
191
+ end
192
+
193
+ # The samples of a TRANSIENT_LOCAL or stronger durability come as the
194
+ # writers are matched; nothing to wait for here.
195
+ def wait_for_historical_data(_timeout); end
196
+
197
+ def matched_publications
198
+ check_open
199
+ Native.matched(@handle).map { |g| Core::InstanceHandle.new(g) }
200
+ end
201
+
202
+ def subscription_matched_status = status(:subscription_matched, Core::Status::SubscriptionMatchedStatus)
203
+ def requested_incompatible_qos_status = status(:requested_incompatible_qos,
204
+ Core::Status::RequestedIncompatibleQosStatus)
205
+ def liveliness_changed_status = status(:liveliness_changed, Core::Status::LivelinessChangedStatus)
206
+ def requested_deadline_missed_status = status(:requested_deadline_missed,
207
+ Core::Status::RequestedDeadlineMissedStatus)
208
+ def sample_lost_status = status(:sample_lost, Core::Status::SampleLostStatus)
209
+ def sample_rejected_status = status(:sample_rejected, Core::Status::SampleRejectedStatus)
210
+
211
+ def deliver(event, _endpoint, arg, listener)
212
+ deliver_own(event, arg, listener)
213
+ end
214
+
215
+ def close
216
+ participant.endpoints.remove(@guid) unless closed?
217
+ super
218
+ end
219
+
220
+ def close_from_parent
221
+ participant.endpoints.remove(@guid) unless closed?
222
+ super
223
+ end
224
+
225
+ def inspect
226
+ "#<DDS::Sub::DataReader of #{@topic_description.name}>"
227
+ end
228
+
229
+ private
230
+
231
+ def status(name, cls)
232
+ check_open
233
+ cls.of(Native.status(@handle, name))
234
+ end
235
+
236
+ def select(take, max_samples: LENGTH_UNLIMITED, sample_state: Status::SampleState::ANY,
237
+ view_state: Status::ViewState::ANY, instance_state: Status::InstanceState::ANY,
238
+ instance: nil, next_instance: nil, condition: nil)
239
+ check_open
240
+ raw = if condition
241
+ Native.select_w_condition(@handle, take, max_samples, condition.handle)
242
+ elsif instance
243
+ Native.select(@handle, take, max_samples, sample_state, view_state, instance_state, 1, instance.value)
244
+ elsif !next_instance.nil?
245
+ Native.select(@handle, take, max_samples, sample_state, view_state, instance_state, 2,
246
+ next_instance.value)
247
+ else
248
+ Native.select(@handle, take, max_samples, sample_state, view_state, instance_state, 0, nil)
249
+ end
250
+ raw.map do |info, inp|
251
+ Sample.new(inp ? Types.read(@type, inp) : nil, SampleInfo.of(info))
252
+ end
253
+ end
254
+ end
255
+
256
+ module Cond
257
+ # The condition of a reader's samples in some states.
258
+ class ReadCondition < Core::Cond::Condition
259
+ attr_reader :reader, :sample_state, :view_state, :instance_state
260
+
261
+ def initialize(reader, sample_state: Status::SampleState::ANY, view_state: Status::ViewState::ANY,
262
+ instance_state: Status::InstanceState::ANY)
263
+ super()
264
+ @reader = reader
265
+ @sample_state = sample_state
266
+ @view_state = view_state
267
+ @instance_state = instance_state
268
+ @handle = Native.create_readcondition(reader.handle, sample_state, view_state, instance_state)
269
+ end
270
+ end
271
+ end
272
+ end
273
+ end
@@ -0,0 +1,115 @@
1
+ # frozen_string_literal: true
2
+
3
+ # DDS::Topic::Topic and ContentFilteredTopic (dds::topic 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 Topic
9
+ # The listener of a topic.
10
+ module TopicListener
11
+ def on_inconsistent_topic(topic, status); end
12
+ end
13
+
14
+ # A topic: a name and a type, a class (or module) rustorb-idl -bruby
15
+ # -Wbdds_only (or -Wbdds) wrote, or a DDS::Types::Type.
16
+ class Topic < Core::Entity
17
+ attr_reader :name, :type, :participant, :qos
18
+
19
+ def initialize(participant, name, type, qos = nil, listener = nil, mask = Core::Status::StatusMask::ALL)
20
+ super()
21
+ @participant = participant
22
+ @name = name.to_s.dup.freeze
23
+ @type = Types.of(type)
24
+ unless @type.resolved.is_a?(Types::StructType) || @type.resolved.is_a?(Types::UnionType)
25
+ raise ArgumentError, "the type of a topic is a struct or a union, not #{@type.name}"
26
+ end
27
+
28
+ @qos = (qos || TopicQos.new).dup.freeze
29
+ @handle = Native.create_topic(participant.handle, @name, @type.native)
30
+ @parent = participant
31
+ begin
32
+ participant.add_topic(self)
33
+ rescue StandardError
34
+ Native.release(@handle)
35
+ raise
36
+ end
37
+ set_listener(listener, mask) if listener
38
+ end
39
+
40
+ def type_name
41
+ @type.name
42
+ end
43
+
44
+ def close_order
45
+ 2
46
+ end
47
+
48
+ def inconsistent_topic_status
49
+ check_open
50
+ Core::Status::InconsistentTopicStatus.of(Native.topic_inconsistent_status(@handle))
51
+ end
52
+
53
+ def deliver(event, endpoint, arg, listener)
54
+ deliver_group(@participant, event, endpoint, arg, listener)
55
+ end
56
+
57
+ def inspect
58
+ "#<DDS::Topic::Topic #{@name} (#{type_name})>"
59
+ end
60
+ end
61
+
62
+ # A topic whose readers get the samples of the related topic that pass
63
+ # the filter (DDS 1.4, 2.2.2.3.3 and Annex B), with parameters for its
64
+ # %0 .. %99.
65
+ class ContentFilteredTopic < Core::Entity
66
+ attr_reader :name, :topic, :filter_expression, :participant
67
+
68
+ def initialize(topic, name, filter_expression, parameters = [])
69
+ super()
70
+ @topic = topic
71
+ @participant = topic.participant
72
+ @name = name.to_s.dup.freeze
73
+ @filter_expression = filter_expression.to_s.dup.freeze
74
+ @parameters = parameters.map(&:to_s).freeze
75
+ @handle = Native.create_cftopic(@participant.handle, @name, topic.handle, @filter_expression, @parameters)
76
+ @parent = @participant
77
+ begin
78
+ @participant.add_topic(self)
79
+ rescue StandardError
80
+ Native.release(@handle)
81
+ raise
82
+ end
83
+ end
84
+
85
+ alias related_topic topic
86
+
87
+ def close_order
88
+ 1
89
+ end
90
+
91
+ def type
92
+ @topic.type
93
+ end
94
+
95
+ def type_name
96
+ @topic.type_name
97
+ end
98
+
99
+ def expression_parameters
100
+ @parameters
101
+ end
102
+
103
+ def expression_parameters=(parameters)
104
+ check_open
105
+ p = parameters.map(&:to_s).freeze
106
+ Native.cftopic_set_parameters(@handle, p)
107
+ @parameters = p
108
+ end
109
+
110
+ def inspect
111
+ "#<DDS::Topic::ContentFilteredTopic #{@name} of #{@topic.name}: #{@filter_expression}>"
112
+ end
113
+ end
114
+ end
115
+ end