OpenSplice Java 5 DCPS  v6.x
OpenSplice Java 5 OpenSplice Data Distribution Service Data-Centric Publish-Subscribe API
DataReaderImpl.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.ArrayList;
25 import java.util.Collection;
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;
39 
40 public class DataReaderImpl<TYPE> extends AbstractDataReader<TYPE> {
41  private final ReflectionDataReader<TYPE, TYPE> reflectionReader;
42  private PreAllocatorImpl<TYPE> lastPreallocator;
43 
46  DDS.DataReader old) {
47  super(environment, parent, topicDescription);
48  this.setOld(old);
49  this.reflectionReader = new ReflectionDataReader<TYPE, TYPE>(
50  this.environment, this, topicDescription.getTypeSupport()
51  .getType());
52 
53  this.topicDescription.retain();
54  }
55 
59  Collection<Class<? extends Status>> statuses) {
60  super(environment, parent, topicDescription);
61 
62  if (qos == null) {
63  throw new IllegalArgumentExceptionImpl(this.environment,
64  "Supplied DataReaderQos is null.");
65  }
66  if (topicDescription == null) {
67  throw new IllegalArgumentExceptionImpl(this.environment,
68  "Supplied TopicDescription is null.");
69  }
70 
71  DDS.DataReaderQos oldQos;
72 
73  try {
74  oldQos = ((DataReaderQosImpl) qos).convert();
75  } catch (ClassCastException e) {
76  throw new IllegalArgumentExceptionImpl(this.environment,
77  "Cannot create DataReader with non-OpenSplice qos");
78  }
79 
80  if (listener != null) {
81  this.listener = new DataReaderListenerImpl<TYPE>(this.environment,
82  this, listener, true);
83  } else {
84  this.listener = null;
85  }
86  DDS.DataReader old = this.parent.getOld().create_datareader(
87  topicDescription.getOld(), oldQos, this.listener,
88  StatusConverter.convertMask(this.environment, statuses));
89 
90  if (old == null) {
91  Utilities.throwLastErrorException(this.environment);
92  }
93  this.setOld(old);
94  this.reflectionReader = new ReflectionDataReader<TYPE, TYPE>(
95  this.environment, this, topicDescription.getTypeSupport()
96  .getType());
97  this.lastPreallocator = null;
98  this.topicDescription.retain();
99 
100  if (this.listener != null) {
101  this.listener.setInitialised();
102  }
103  }
104 
105  @Override
106  protected void destroy() {
107  super.destroy();
108  this.topicDescription.close();
109  }
110 
111  @Override
113  return this.reflectionReader;
114  }
115 
116  @Override
117  public synchronized PreAllocator<TYPE> getPreAllocator(List<Sample<TYPE>> samples,
118  Class<?> sampleSeqHolderClz, Field sampleSeqHolderValueField) {
119  PreAllocatorImpl<TYPE> lpa = this.lastPreallocator;
120  PreAllocatorImpl<TYPE> pa = null;
121 
122  if (lpa != null && lpa.getSampleList() == samples) {
123  pa = this.lastPreallocator;
124  }
125  if (pa == null) {
126  pa = new PreAllocatorImpl<TYPE>(this.environment,
127  sampleSeqHolderClz, sampleSeqHolderValueField, this
128  .getTopicDescription().getTypeSupport()
129  .getType(), samples);
130  this.lastPreallocator = pa;
131  }
132  return pa;
133  }
134 
135  @Override
136  public TYPE getKeyValue(TYPE keyHolder, InstanceHandle handle) {
137  return this.reflectionReader.getKeyValue(keyHolder, handle);
138  }
139 
140  @Override
141  public TYPE getKeyValue(InstanceHandle handle) {
142  return this.reflectionReader.getKeyValue(handle);
143  }
144 
145  @Override
146  public InstanceHandle lookupInstance(TYPE keyHolder) {
147  return this.reflectionReader.lookupInstance(keyHolder);
148  }
149 
150  @Override
151  public boolean readNextSample(Sample<TYPE> sample) {
152  return this.reflectionReader.readNextSample((SampleImpl<TYPE>) sample);
153  }
154 
155  @Override
156  public boolean takeNextSample(Sample<TYPE> sample) {
157  return this.reflectionReader.takeNextSample((SampleImpl<TYPE>) sample);
158  }
159 
160  @Override
161  public Iterator<TYPE> createIterator(Object sampleSeqHolder,
162  Field sampleSeqHolderValueField, DDS.SampleInfoSeqHolder info) {
163  return new IteratorImpl<TYPE>(this.environment, this, sampleSeqHolder,
164  sampleSeqHolderValueField, info);
165  }
166 }
Since a org.omg.dds.sub.DataReader is a kind of org.omg.dds.core.Entity, it has the ability to have a...
TYPE getKeyValue(TYPE keyHolder, InstanceHandle handle)
This operation can be used to retrieve the instance key that corresponds to an instance handle...
A Sample represents an atom of data information (i.e., one value for one instance) as returned by a o...
Definition: Sample.java:116
DataReaderImpl(OsplServiceEnvironment environment, SubscriberImpl parent, TopicDescriptionExt< TYPE > topicDescription, DataReaderQos qos, DataReaderListener< TYPE > listener, Collection< Class<? extends Status >> statuses)
boolean takeNextSample(Sample< TYPE > sample)
This operation copies the next, non-previously accessed sample from this DataReader and "removes" it ...
InstanceHandle lookupInstance(TYPE keyHolder)
This operation takes as a parameter an instance and returns a handle that can be used in subsequent o...
DataReaderImpl(OsplServiceEnvironment environment, SubscriberImpl parent, TopicDescriptionExt< TYPE > topicDescription, DDS.DataReader old)
synchronized PreAllocator< TYPE > getPreAllocator(List< Sample< TYPE >> samples, Class<?> sampleSeqHolderClz, Field sampleSeqHolderValueField)
boolean readNextSample(Sample< TYPE > sample)
This operation copies the next, non-previously accessed sample from this DataReader.
ReflectionDataReader<?, TYPE > getReflectionReader()
static Set< Class<? extends Status > > convertMask(OsplServiceEnvironment environment, int state)
An opaque handle that can be used to refer to a local or remote entity.
TYPE getKeyValue(InstanceHandle handle)
This operation can be used to retrieve the instance key that corresponds to an instance handle...
TypeSupport< TYPE > getTypeSupport()
Returns the org.omg.dds.type.TypeSupport used to create this TopicDescription.
final TopicDescriptionExt< TYPE > topicDescription
static void throwLastErrorException(OsplServiceEnvironment environment)
Definition: Utilities.java:182
Iterator< TYPE > createIterator(Object sampleSeqHolder, Field sampleSeqHolderValueField, DDS.SampleInfoSeqHolder info)
Status is the abstract root class for all communication status objects.
Definition: Status.java:41