OpenSplice Java 5 DCPS  v6.x
OpenSplice Java 5 OpenSplice Data Distribution Service Data-Centric Publish-Subscribe API
DataReaderProtobuf.java
Go to the documentation of this file.
1 /*
2  * Vortex OpenSplice
3  *
4  * This software and documentation are Copyright 2006 to 2024 ADLINK
5  * Technology Limited, its affiliated companies and licensors. All rights
6  * reserved.
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 package org.opensplice.dds.sub;
22 
23 import java.lang.reflect.Field;
24 import java.util.Collection;
25 import java.util.HashMap;
26 import java.util.List;
27 
29 import org.omg.dds.core.status.Status;
31 import org.omg.dds.sub.DataReaderQos;
32 import org.omg.dds.sub.Sample;
33 import org.omg.dds.sub.Sample.Iterator;
40 
41 import DDS.SampleInfoSeqHolder;
42 
43 public class DataReaderProtobuf<PROTOBUF_TYPE, DDS_TYPE> extends
44  AbstractDataReader<PROTOBUF_TYPE> {
45  private final HashMap<List<Sample<PROTOBUF_TYPE>>, PreAllocatorProtobuf<PROTOBUF_TYPE, DDS_TYPE>> preallocated;
46  private final ReflectionDataReader<DDS_TYPE, PROTOBUF_TYPE> reflectionReader;
47  private final TypeSupportProtobuf<PROTOBUF_TYPE, DDS_TYPE> typeSupport;
48 
49  @SuppressWarnings("unchecked")
51  SubscriberImpl parent,
53  DataReaderQos qos, DataReaderListener<PROTOBUF_TYPE> listener,
54  Collection<Class<? extends Status>> statuses) {
55  super(environment, parent, topicDescription);
56  if (qos == null) {
57  throw new IllegalArgumentExceptionImpl(this.environment,
58  "Supplied DataReaderQos is null.");
59  }
60  if (topicDescription == null) {
61  throw new IllegalArgumentExceptionImpl(this.environment,
62  "Supplied TopicDescription is null.");
63  }
64  DDS.DataReaderQos oldQos;
65 
66  try {
67  oldQos = ((DataReaderQosImpl) qos).convert();
68  } catch (ClassCastException e) {
69  throw new IllegalArgumentExceptionImpl(this.environment,
70  "Cannot create DataReader with non-OpenSplice qos");
71  }
72 
73  if (listener != null) {
74  this.listener = new DataReaderListenerImpl<PROTOBUF_TYPE>(
75  this.environment, this, listener, true);
76  } else {
77  this.listener = null;
78  }
79  DDS.DataReader old = this.parent.getOld().create_datareader(
80  topicDescription.getOld(), oldQos, this.listener,
81  StatusConverter.convertMask(this.environment, statuses));
82 
83  if (old == null) {
84  Utilities.throwLastErrorException(this.environment);
85  }
86  this.setOld(old);
87  this.preallocated = new HashMap<List<Sample<PROTOBUF_TYPE>>, PreAllocatorProtobuf<PROTOBUF_TYPE, DDS_TYPE>>();
88  this.typeSupport = (TypeSupportProtobuf<PROTOBUF_TYPE, DDS_TYPE>) topicDescription
89  .getTypeSupport();
90  this.reflectionReader = new ReflectionDataReader<DDS_TYPE, PROTOBUF_TYPE>(
91  this.environment, this, this.typeSupport
92  .getTypeSupportStandard().getType());
93  this.topicDescription.retain();
94 
95  if (this.listener != null) {
96  this.listener.setInitialised();
97  }
98  }
99 
100  @Override
101  protected void destroy() {
102  super.destroy();
103  this.topicDescription.close();
104  }
105 
106  @Override
108  List<Sample<PROTOBUF_TYPE>> samples, Class<?> sampleSeqHolderClz,
109  Field sampleSeqHolderValueField) {
111 
112  synchronized (this.preallocated) {
113  if (samples != null) {
114  pa = this.preallocated.get(samples);
115  } else {
116  pa = null;
117  }
118  if (pa == null) {
120  this.environment, this, sampleSeqHolderClz,
121  sampleSeqHolderValueField, samples);
122  this.preallocated.put(pa.getSampleList(), pa);
123  } else {
124  pa.setSampleList(samples);
125  }
126  }
127  return pa;
128  }
129 
130  @Override
132  return this.reflectionReader;
133  }
134 
135  @Override
136  public PROTOBUF_TYPE getKeyValue(PROTOBUF_TYPE keyHolder,
137  InstanceHandle handle) {
138  return this.getKeyValue(handle);
139  }
140 
141  @Override
142  public PROTOBUF_TYPE getKeyValue(InstanceHandle handle) {
143  DDS_TYPE ddsData = this.reflectionReader.getKeyValue(handle);
144 
145  if (ddsData != null) {
146  return this.typeSupport.ddsKeyToProtobuf(ddsData);
147  }
148  return null;
149  }
150 
151  @Override
152  public InstanceHandle lookupInstance(PROTOBUF_TYPE keyHolder) {
153  return this.reflectionReader.lookupInstance(this.typeSupport
154  .protobufToDds(keyHolder));
155  }
156 
157  @Override
158  public boolean readNextSample(Sample<PROTOBUF_TYPE> sample) {
159  SampleImpl<DDS_TYPE> ddsSample;
160  boolean result;
161 
162  if (sample == null) {
163  throw new IllegalArgumentExceptionImpl(this.environment,
164  "Provided an invalid null sample.");
165  }
166  ddsSample = new SampleImpl<DDS_TYPE>(this.environment,
167  this.typeSupport.protobufToDds(sample.getData()),
168  ((SampleImpl<PROTOBUF_TYPE>) sample).getInfo());
169  result = this.reflectionReader.readNextSample(ddsSample);
170 
171  if (result == true) {
172  if (ddsSample.getInfo().valid_data) {
173  ((SampleImpl<PROTOBUF_TYPE>) sample).setContent(
174  this.typeSupport.ddsToProtobuf(ddsSample.getData()),
175  ddsSample.getInfo());
176  } else {
177  ((SampleImpl<PROTOBUF_TYPE>) sample).setContent(
178  this.typeSupport.ddsKeyToProtobuf(ddsSample
179  .getKeyValue()), ddsSample.getInfo());
180  }
181  }
182  return result;
183  }
184 
185  @Override
186  public boolean takeNextSample(Sample<PROTOBUF_TYPE> sample) {
187  SampleImpl<DDS_TYPE> ddsSample;
188  boolean result;
189 
190  if (sample == null) {
191  throw new IllegalArgumentExceptionImpl(this.environment,
192  "Provided an invalid null sample.");
193  }
194  ddsSample = new SampleImpl<DDS_TYPE>(this.environment,
195  this.typeSupport.protobufToDds(sample.getData()),
196  ((SampleImpl<PROTOBUF_TYPE>) sample).getInfo());
197  result = this.reflectionReader.takeNextSample(ddsSample);
198 
199  if (result == true) {
200  if (ddsSample.getInfo().valid_data) {
201  ((SampleImpl<PROTOBUF_TYPE>) sample).setContent(
202  this.typeSupport.ddsToProtobuf(ddsSample.getData()),
203  ddsSample.getInfo());
204  } else {
205  ((SampleImpl<PROTOBUF_TYPE>) sample).setContent(
206  this.typeSupport.ddsKeyToProtobuf(ddsSample
207  .getKeyValue()), ddsSample.getInfo());
208  }
209  }
210  return result;
211  }
212 
213  @Override
214  public Iterator<PROTOBUF_TYPE> createIterator(Object sampleSeqHolder,
215  Field sampleSeqHolderValueField, SampleInfoSeqHolder info) {
216  return new IteratorProtobuf<PROTOBUF_TYPE, DDS_TYPE>(this.environment,
217  this, sampleSeqHolder, sampleSeqHolderValueField, info);
218  }
219 }
Since a org.omg.dds.sub.DataReader is a kind of org.omg.dds.core.Entity, it has the ability to have a...
A Sample represents an atom of data information (i.e., one value for one instance) as returned by a o...
Definition: Sample.java:116
void setSampleList(List< Sample< PROTOBUF_TYPE >> preAllocated)
List< Sample< PROTOBUF_TYPE > > getSampleList()
ReflectionDataReader<?, PROTOBUF_TYPE > getReflectionReader()
PROTOBUF_TYPE getKeyValue(PROTOBUF_TYPE keyHolder, InstanceHandle handle)
static Set< Class<? extends Status > > convertMask(OsplServiceEnvironment environment, int state)
TYPE getData()
Get the data associated with this Sample, if any.
Definition: SampleImpl.java:53
Iterator< PROTOBUF_TYPE > createIterator(Object sampleSeqHolder, Field sampleSeqHolderValueField, SampleInfoSeqHolder info)
An opaque handle that can be used to refer to a local or remote entity.
TYPE getData()
Get the data associated with this Sample, if any.
boolean takeNextSample(Sample< PROTOBUF_TYPE > sample)
PROTOBUF_TYPE getKeyValue(InstanceHandle handle)
PreAllocator< PROTOBUF_TYPE > getPreAllocator(List< Sample< PROTOBUF_TYPE >> samples, Class<?> sampleSeqHolderClz, Field sampleSeqHolderValueField)
static void throwLastErrorException(OsplServiceEnvironment environment)
Definition: Utilities.java:182
boolean readNextSample(Sample< PROTOBUF_TYPE > sample)
InstanceHandle lookupInstance(PROTOBUF_TYPE keyHolder)
Status is the abstract root class for all communication status objects.
Definition: Status.java:41