OpenSplice Java 5 DCPS  v6.x
OpenSplice Java 5 OpenSplice Data Distribution Service Data-Centric Publish-Subscribe API
ReflectionDataReader.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.lang.reflect.InvocationTargetException;
25 import java.lang.reflect.Method;
26 import java.util.HashSet;
27 import java.util.List;
28 import java.util.Set;
29 import java.util.concurrent.TimeUnit;
30 import java.util.concurrent.TimeoutException;
31 
32 import org.omg.dds.core.DDSObject;
33 import org.omg.dds.core.Duration;
36 import org.omg.dds.core.Time;
44 import org.omg.dds.sub.DataReaderQos;
45 import org.omg.dds.sub.ReadCondition;
46 import org.omg.dds.sub.Sample;
47 import org.omg.dds.sub.Sample.Iterator;
59 
60 import DDS.SampleInfoHolder;
61 import DDS.SampleInfoSeqHolder;
62 
63 public class ReflectionDataReader<TYPE, OUT_TYPE> extends AbstractDDSObject
64  implements
65  DDSObject {
66  private final OsplServiceEnvironment environment;
67  private final AbstractDataReader<OUT_TYPE> reader;
68  private final DDS.DataReader old;
69  private final Class<?> sampleSeqHolderClz;
70  private final Field sampleSeqHolderValueField;
71 
72  private final Class<?> sampleHolderClz;
73  private final Field sampleHolderValueField;
74 
75  private final Method read;
76  private final Method take;
77  private final Method readCondition;
78  private final Method takeCondition;
79  private final Method readNextSample;
80  private final Method takeNextSample;
81  private final Method readInstance;
82  private final Method takeInstance;
83  private final Method readNextInstance;
84  private final Method takeNextInstance;
85  private final Method readNextInstanceCondition;
86  private final Method takeNextInstanceCondition;
87  private final Method returnLoan;
88  private final Method getKeyValue;
89  private final Method lookupInstance;
90 
92  AbstractDataReader<OUT_TYPE> reader, Class<TYPE> ddsTypeClz) {
93  this.environment = environment;
94  this.reader = reader;
95  this.old = reader.getOld();
96 
97  Class<?> typedReaderClz;
98  String typedReaderClzName = ddsTypeClz.getName() + "DataReaderImpl";
99 
100  try {
101  typedReaderClz = Class.forName(typedReaderClzName);
102 
103  this.sampleHolderClz = Class.forName(ddsTypeClz.getName() + "Holder");
104  this.sampleHolderValueField = this.sampleHolderClz
105  .getDeclaredField("value");
106 
107  this.sampleSeqHolderClz = Class.forName(ddsTypeClz.getName()
108  + "SeqHolder");
109  this.sampleSeqHolderValueField = this.sampleSeqHolderClz
110  .getDeclaredField("value");
111 
112  this.read = typedReaderClz.getMethod("read",
113  this.sampleSeqHolderClz, SampleInfoSeqHolder.class,
114  int.class, int.class, int.class, int.class);
115  this.take = typedReaderClz.getMethod("take",
116  this.sampleSeqHolderClz, SampleInfoSeqHolder.class,
117  int.class, int.class, int.class, int.class);
118 
119  this.readCondition = typedReaderClz.getMethod("read_w_condition",
120  this.sampleSeqHolderClz, SampleInfoSeqHolder.class,
121  int.class, DDS.ReadCondition.class);
122  this.takeCondition = typedReaderClz.getMethod("take_w_condition",
123  this.sampleSeqHolderClz, SampleInfoSeqHolder.class,
124  int.class, DDS.ReadCondition.class);
125 
126  this.readNextSample = typedReaderClz.getMethod("read_next_sample",
127  this.sampleHolderClz, SampleInfoHolder.class);
128  this.takeNextSample = typedReaderClz.getMethod("take_next_sample",
129  this.sampleHolderClz, SampleInfoHolder.class);
130 
131  this.readInstance = typedReaderClz.getMethod("read_instance",
132  this.sampleSeqHolderClz, SampleInfoSeqHolder.class,
133  int.class, long.class, int.class, int.class, int.class);
134  this.takeInstance = typedReaderClz.getMethod("take_instance",
135  this.sampleSeqHolderClz, SampleInfoSeqHolder.class,
136  int.class, long.class, int.class, int.class, int.class);
137 
138  this.readNextInstance = typedReaderClz.getMethod(
139  "read_next_instance", this.sampleSeqHolderClz,
140  SampleInfoSeqHolder.class, int.class, long.class,
141  int.class, int.class, int.class);
142  this.takeNextInstance = typedReaderClz.getMethod(
143  "take_next_instance", this.sampleSeqHolderClz,
144  SampleInfoSeqHolder.class, int.class, long.class,
145  int.class, int.class, int.class);
146 
147  this.readNextInstanceCondition = typedReaderClz.getMethod(
148  "read_next_instance_w_condition", this.sampleSeqHolderClz,
149  SampleInfoSeqHolder.class, int.class, long.class,
150  DDS.ReadCondition.class);
151  this.takeNextInstanceCondition = typedReaderClz.getMethod(
152  "take_next_instance_w_condition", this.sampleSeqHolderClz,
153  SampleInfoSeqHolder.class, int.class, long.class,
154  DDS.ReadCondition.class);
155 
156  this.returnLoan = typedReaderClz.getMethod("return_loan",
157  this.sampleSeqHolderClz, SampleInfoSeqHolder.class);
158 
159  this.getKeyValue = typedReaderClz.getMethod("get_key_value",
160  this.sampleHolderClz, long.class);
161  this.lookupInstance = typedReaderClz.getMethod("lookup_instance",
162  ddsTypeClz);
163  } catch (ClassNotFoundException e) {
165  environment,
166  "Cannot find Typed DataReader '"
167  + typedReaderClzName
168  + "' that should have been generated manually with the OpenSplice IDL pre-processor.("
169  + e.getMessage() + ").");
170  } catch (NoSuchMethodException e) {
171  throw new DDSExceptionImpl(environment,
172  "Cannot find correct methods in OpenSplice IDL pre-processor generated class: "
173  + typedReaderClzName + " (" + e.getMessage() + ").");
174  } catch (NoSuchFieldException e) {
175  throw new DDSExceptionImpl(
176  environment,
177  "Cannot find 'value' field in "
178  + "the typed sampleHolderClass "
179  + "that should have been generated by the OpenSplice IDL pre-processor ("
180  + e.getMessage() + ").");
181  } catch (SecurityException e) {
183  environment,
184  "Insufficient rights to find methods/fields in code that has been generated by the OpenSplice IDL pre-processor ("
185  + e.getMessage() + ").");
186  }
187  }
188 
189  @Override
191  return this.environment;
192  }
193 
195  DDS.DataReaderQosHolder holder = new DDS.DataReaderQosHolder();
196  int rc = this.old.get_qos(holder);
197  Utilities.checkReturnCode(rc, this.environment,
198  "DataReader.getQos() failed.");
199 
200  return DataReaderQosImpl.convert(this.environment, holder.value);
201  }
202 
203  public void setQos(DataReaderQos qos) {
205 
206  if (qos == null) {
207  throw new IllegalArgumentExceptionImpl(this.environment,
208  "Supplied DataReaderQos is null.");
209  }
210  try {
211  q = (DataReaderQosImpl) qos;
212  } catch (ClassCastException e) {
213  throw new IllegalArgumentExceptionImpl(this.environment,
214  "Setting non-OpenSplice Qos not supported.");
215  }
216  int rc = this.old.set_qos(q.convert());
217  Utilities.checkReturnCode(rc, this.environment,
218  "DataReader.setQos() failed.");
219  }
220 
222  DDS.SampleRejectedStatusHolder holder = new DDS.SampleRejectedStatusHolder();
223 
224  int rc = this.old.get_sample_rejected_status(holder);
225  Utilities.checkReturnCode(rc, this.environment,
226  "DataReader.getSampleRejectedStatus() failed.");
227 
228  return StatusConverter.convert(this.environment, holder.value);
229  }
230 
232  DDS.LivelinessChangedStatusHolder holder = new DDS.LivelinessChangedStatusHolder();
233 
234  int rc = this.old.get_liveliness_changed_status(holder);
235  Utilities.checkReturnCode(rc, this.environment,
236  "DataReader.getLivelinessChangedStatus() failed.");
237 
238  return StatusConverter.convert(this.environment, holder.value);
239  }
240 
242  DDS.RequestedDeadlineMissedStatusHolder holder = new DDS.RequestedDeadlineMissedStatusHolder();
243 
244  int rc = this.old.get_requested_deadline_missed_status(holder);
245  Utilities.checkReturnCode(rc, this.environment,
246  "DataReader.getRequestedDeadlineMissedStatus() failed.");
247 
248  return StatusConverter.convert(this.environment, holder.value);
249  }
250 
252  DDS.RequestedIncompatibleQosStatusHolder holder = new DDS.RequestedIncompatibleQosStatusHolder();
253 
254  int rc = this.old.get_requested_incompatible_qos_status(holder);
255  Utilities.checkReturnCode(rc, this.environment,
256  "DataReader.getRequestedIncompatibleQosStatus() failed.");
257 
258  return StatusConverter.convert(this.environment, holder.value);
259  }
260 
262  DDS.SubscriptionMatchedStatusHolder holder = new DDS.SubscriptionMatchedStatusHolder();
263 
264  int rc = this.old.get_subscription_matched_status(holder);
265  Utilities.checkReturnCode(rc, this.environment,
266  "DataReader.getSubscriptionMatchedStatus() failed.");
267 
268  return StatusConverter.convert(this.environment, holder.value);
269  }
270 
272  DDS.SampleLostStatusHolder holder = new DDS.SampleLostStatusHolder();
273 
274  int rc = this.old.get_sample_lost_status(holder);
275  Utilities.checkReturnCode(rc, this.environment,
276  "DataReader.getSampleLostStatus() failed.");
277 
278  return StatusConverter.convert(this.environment, holder.value);
279  }
280 
281  public void waitForHistoricalData(Duration maxWait) throws TimeoutException {
282  int rc = this.old.wait_for_historical_data(Utilities.convert(this.environment,
283  maxWait));
284  Utilities.checkReturnCodeWithTimeout(rc, this.environment,
285  "DataReader.waitForHistoricalData() failed.");
286 
287  }
288 
289  public void waitForHistoricalData(long maxWait, TimeUnit unit)
290  throws TimeoutException {
291  this.waitForHistoricalData(this.environment.getSPI().newDuration(
292  maxWait, unit));
293  }
294 
295  public void waitForHistoricalData(String filterExpression,
296  List<String> filterParameters, Time minSourceTimestamp,
297  Time maxSourceTimestamp, ResourceLimits resourceLimits,
298  Duration maxWait) throws TimeoutException {
299  if (resourceLimits == null) {
300  throw new IllegalArgumentExceptionImpl(environment,
301  "Invalid resourceLimits (null) supplied.");
302  }
303  String[] params;
304 
305  if(filterParameters != null){
306  params = filterParameters.toArray(new String[filterParameters
307  .size()]);
308  } else {
309  params = null;
310  }
311  int rc = this.old.wait_for_historical_data_w_condition(
312  filterExpression, params,
313  Utilities.convert(this.environment, minSourceTimestamp),
314  Utilities.convert(this.environment, maxSourceTimestamp),
315  PolicyConverter.convert(this.environment, resourceLimits),
316  Utilities.convert(this.environment, maxWait));
317  Utilities.checkReturnCodeWithTimeout(rc, this.environment,
318  "DataReader.waitForHistoricalData() failed.");
319  }
320 
321  public void waitForHistoricalData(String filterExpression,
322  List<String> filterParameters, Time minSourceTimestamp,
323  Time maxSourceTimestamp, ResourceLimits resourceLimits,
324  long maxWait, TimeUnit unit) throws TimeoutException {
325  this.waitForHistoricalData(filterExpression, filterParameters,
326  minSourceTimestamp, maxSourceTimestamp, resourceLimits,
327  this.environment.getSPI().newDuration(maxWait, unit));
328  }
329 
330  public Set<InstanceHandle> getMatchedPublications() {
331  DDS.InstanceHandleSeqHolder holder = new DDS.InstanceHandleSeqHolder();
332  Set<InstanceHandle> handles;
333 
334  int rc = this.old.get_matched_publications(holder);
335  Utilities.checkReturnCode(rc, this.environment,
336  "DataReader.getMatchedPublications() failed.");
337 
338  handles = new HashSet<InstanceHandle>();
339 
340  for (long handle : holder.value) {
341  handles.add(Utilities.convert(this.environment, handle));
342  }
343  return handles;
344  }
345 
347  InstanceHandle publicationHandle) {
348  DDS.PublicationBuiltinTopicDataHolder holder = new DDS.PublicationBuiltinTopicDataHolder();
349  int rc = this.old.get_matched_publication_data(holder,
350  Utilities.convert(this.environment, publicationHandle));
351  Utilities.checkReturnCode(rc, this.environment,
352  "DataReader.getMatchedPublicationData() failed.");
353  if (holder.value != null) {
354  return new PublicationBuiltinTopicDataImpl(this.environment,
355  holder.value);
356  }
357  throw new PreconditionNotMetExceptionImpl(this.environment,
358  "No data for this instanceHandle.");
359  }
360 
361  public TYPE getKeyValue(TYPE keyHolder, InstanceHandle handle) {
362  Object sampleHolder;
363 
364  if (keyHolder == null) {
365  throw new IllegalArgumentException(
366  "Invalid key holder (null) provided.");
367  }
368 
369  try {
370  sampleHolder = this.sampleHolderClz.newInstance();
371  this.sampleHolderValueField.set(sampleHolder, keyHolder);
372  int rc = (Integer) this.getKeyValue.invoke(this.old, sampleHolder,
373  Utilities.convert(this.environment, handle));
374  Utilities.checkReturnCode(rc, this.environment,
375  "DataReader.getKeyValue() failed.");
376  } catch (InstantiationException e) {
377  throw new DDSExceptionImpl(environment, "Internal error ("
378  + e.getMessage() + ").");
379  } catch (IllegalAccessException e) {
380  throw new DDSExceptionImpl(environment, "Internal error ("
381  + e.getMessage() + ").");
382  } catch (IllegalArgumentExceptionImpl e) {
383  throw e;
384  } catch (IllegalArgumentException e) {
385  throw new DDSExceptionImpl(environment, "Internal error ("
386  + e.getMessage() + ").");
387  } catch (InvocationTargetException e) {
388  throw new DDSExceptionImpl(environment, "Internal error ("
389  + e.getMessage() + ").");
390  } catch (ClassCastException e) {
391  throw new DDSExceptionImpl(environment, "Internal error ("
392  + e.getMessage() + ").");
393  }
394  return keyHolder;
395  }
396 
397  @SuppressWarnings("unchecked")
398  public TYPE getKeyValue(InstanceHandle handle) {
399  Object sampleHolder;
400 
401 
402  try {
403  sampleHolder = this.sampleHolderClz.newInstance();
404  int rc = (Integer) this.getKeyValue.invoke(this.old, sampleHolder,
405  Utilities.convert(this.environment, handle));
406  Utilities.checkReturnCode(rc, this.environment,
407  "DataReader.getKeyValue() failed.");
408 
409  return (TYPE) this.sampleHolderValueField.get(sampleHolder);
410  } catch (InstantiationException e) {
411  throw new DDSExceptionImpl(environment, "Internal error ("
412  + e.getMessage() + ").");
413  } catch (IllegalAccessException e) {
414  throw new DDSExceptionImpl(environment, "Internal error ("
415  + e.getMessage() + ").");
416  } catch (IllegalArgumentExceptionImpl e) {
417  throw e;
418  } catch (IllegalArgumentException e) {
419  throw new DDSExceptionImpl(environment, "Internal error ("
420  + e.getMessage() + ").");
421  } catch (InvocationTargetException e) {
422  throw new DDSExceptionImpl(environment, "Internal error ("
423  + e.getMessage() + ").");
424  } catch (ClassCastException e) {
425  throw new DDSExceptionImpl(environment, "Internal error ("
426  + e.getMessage() + ").");
427  }
428  }
429 
430  public InstanceHandle lookupInstance(TYPE keyHolder) {
431  long oldHandle;
432 
433  if (keyHolder == null) {
434  throw new IllegalArgumentException(
435  "Invalid key holder (null) provided.");
436  }
437 
438  try {
439  oldHandle = (Long) this.lookupInstance.invoke(this.old, keyHolder);
440  } catch (IllegalAccessException e) {
441  throw new DDSExceptionImpl(environment, "Internal error ("
442  + e.getMessage() + ").");
443  } catch (IllegalArgumentExceptionImpl e) {
444  throw e;
445  } catch (IllegalArgumentException e) {
446  throw new DDSExceptionImpl(environment, "Internal error ("
447  + e.getMessage() + ").");
448  } catch (InvocationTargetException e) {
449  throw new DDSExceptionImpl(environment, "Internal error ("
450  + e.getMessage() + ").");
451  } catch (ClassCastException e) {
452  throw new DDSExceptionImpl(environment, "Internal error ("
453  + e.getMessage() + ").");
454  }
455  return Utilities.convert(this.environment, oldHandle);
456  }
457 
458  public void returnLoan(Object sampleSeqHolder,
459  DDS.SampleInfoSeqHolder infoSeqHolder) {
460  try {
461  int rc = (Integer) this.returnLoan.invoke(this.old,
462  sampleSeqHolder, infoSeqHolder);
463  Utilities.checkReturnCode(rc, this.environment,
464  "Return loan failed.");
465  } catch (IllegalAccessException e) {
466  throw new DDSExceptionImpl(environment, "Internal error ("
467  + e.getMessage() + ").");
468  } catch (InvocationTargetException e) {
469  throw new DDSExceptionImpl(environment, "Internal error ("
470  + e.getMessage() + ").");
471  }
472  }
473 
475  return this.read(DDS.LENGTH_UNLIMITED.value);
476  }
477 
478  @SuppressWarnings("unchecked")
479  public Iterator<OUT_TYPE> read(
480  org.omg.dds.sub.DataReader.Selector<OUT_TYPE> query) {
481  DDS.SampleInfoSeqHolder info = new DDS.SampleInfoSeqHolder();
482  Object sampleSeqHolder;
483 
484  if (query == null) {
485  throw new IllegalArgumentExceptionImpl(this.environment,
486  "Invalid Selector (null) provided.");
487  }
488  ReadCondition<OUT_TYPE> condition = null;
489 
490  if (query.getQueryExpression() != null) {
491  condition = query.getCondition();
492  }
493  InstanceHandle instance = query.getInstance();
494 
495  try {
496  DataStateImpl state = (DataStateImpl) query.getDataState();
497  InstanceHandleImpl handle = (InstanceHandleImpl) instance;
498  sampleSeqHolder = this.sampleSeqHolderClz.newInstance();
499  int rc;
500 
501  if (condition != null) {
502  DDS.ReadCondition oldCondition = ((ReadConditionImpl<OUT_TYPE>) condition)
503  .getOld();
504 
505  if (query.retrieveNextInstance()) {
506  // read_next_instance_w_condition
507  rc = (Integer) this.readNextInstanceCondition.invoke(
508  this.old, sampleSeqHolder, info,
509  query.getMaxSamples(), handle.getValue(),
510  oldCondition);
511  } else {
512  // read_w_condition
513  rc = (Integer) this.readCondition.invoke(this.old,
514  sampleSeqHolder, info, query.getMaxSamples(),
515  oldCondition);
516  }
517  } else {
518  // read_next_instance
519  if (query.retrieveNextInstance()) {
520  rc = (Integer) this.readNextInstance.invoke(this.old,
521  sampleSeqHolder, info, query.getMaxSamples(),
522  handle.getValue(), state.getOldSampleState(),
523  state.getOldViewState(),
524  state.getOldInstanceState());
525  } else {
526  // read
527  if (instance.isNil()) {
528  rc = (Integer) this.read.invoke(this.old,
529  sampleSeqHolder, info, query.getMaxSamples(),
530  state.getOldSampleState(),
531  state.getOldViewState(),
532  state.getOldInstanceState());
533  }
534  // read_instance
535  else {
536  rc = (Integer) this.readInstance.invoke(this.old,
537  sampleSeqHolder, info, query.getMaxSamples(),
538  handle.getValue(), state.getOldSampleState(),
539  state.getOldViewState(),
540  state.getOldInstanceState());
541  }
542  }
543  }
544  Utilities.checkReturnCode(rc, this.environment,
545  "DataReader.read() failed.");
546  } catch (InstantiationException e) {
547  throw new DDSExceptionImpl(this.environment, "Internal error ("
548  + e.getMessage() + ").");
549  } catch (IllegalAccessException e) {
550  throw new DDSExceptionImpl(this.environment, "Internal error ("
551  + e.getMessage() + ").");
552  } catch (IllegalArgumentExceptionImpl e) {
553  throw e;
554  } catch (IllegalArgumentException e) {
555  throw new DDSExceptionImpl(this.environment, "Internal error ("
556  + e.getMessage() + ").");
557  } catch (InvocationTargetException e) {
558  throw new DDSExceptionImpl(this.environment, "Internal error ("
559  + e.getMessage() + ").");
560  } catch (ClassCastException e) {
562  this.environment,
563  "Reading with non-OpenSplice DataState, InstanceHandle or ReadCondition not supported");
564  }
565  return (Iterator<OUT_TYPE>) this.reader.createIterator(
566  sampleSeqHolder, this.sampleSeqHolderValueField, info);
567  }
568 
569  @SuppressWarnings("unchecked")
570  public Iterator<OUT_TYPE> read(int maxSamples) {
571  DDS.SampleInfoSeqHolder info = new DDS.SampleInfoSeqHolder();
572  Object sampleSeqHolder;
573 
574  try {
575  sampleSeqHolder = this.sampleSeqHolderClz.newInstance();
576  int rc = (Integer) this.read.invoke(this.old, sampleSeqHolder,
577  info, maxSamples, DDS.ANY_SAMPLE_STATE.value,
578  DDS.ANY_VIEW_STATE.value, DDS.ANY_INSTANCE_STATE.value);
579  Utilities.checkReturnCode(rc, this.environment,
580  "DataReader.read() failed.");
581  } catch (InstantiationException e) {
582  throw new DDSExceptionImpl(environment, "Internal error ("
583  + e.getMessage() + ").");
584  } catch (IllegalAccessException e) {
585  throw new DDSExceptionImpl(environment, "Internal error ("
586  + e.getMessage() + ").");
587  } catch (IllegalArgumentExceptionImpl e) {
588  throw e;
589  } catch (IllegalArgumentException e) {
590  throw new DDSExceptionImpl(environment, "Internal error ("
591  + e.getMessage() + ").");
592  } catch (InvocationTargetException e) {
593  throw new DDSExceptionImpl(environment, "Internal error ("
594  + e.getMessage() + ").");
595  }
596  return (Iterator<OUT_TYPE>) this.reader.createIterator(
597  sampleSeqHolder, this.sampleSeqHolderValueField, info);
598  }
599 
601  return this.take(DDS.LENGTH_UNLIMITED.value);
602  }
603 
604  @SuppressWarnings("unchecked")
605  public Iterator<OUT_TYPE> take(int maxSamples) {
606  DDS.SampleInfoSeqHolder info = new DDS.SampleInfoSeqHolder();
607  Object sampleSeqHolder;
608 
609  try {
610  sampleSeqHolder = this.sampleSeqHolderClz.newInstance();
611  int rc = (Integer) this.take.invoke(this.old, sampleSeqHolder,
612  info, maxSamples, DDS.ANY_SAMPLE_STATE.value,
613  DDS.ANY_VIEW_STATE.value, DDS.ANY_INSTANCE_STATE.value);
614  Utilities.checkReturnCode(rc, this.environment,
615  "DataReader.take() failed.");
616  } catch (InstantiationException e) {
617  throw new DDSExceptionImpl(environment, "Internal error ("
618  + e.getMessage() + ").");
619  } catch (IllegalAccessException e) {
620  throw new DDSExceptionImpl(environment, "Internal error ("
621  + e.getMessage() + ").");
622  } catch (IllegalArgumentExceptionImpl e) {
623  throw e;
624  } catch (IllegalArgumentException e) {
625  throw new DDSExceptionImpl(environment, "Internal error ("
626  + e.getMessage() + ").");
627  } catch (InvocationTargetException e) {
628  throw new DDSExceptionImpl(environment, "Internal error ("
629  + e.getMessage() + ").");
630  }
631  return (Iterator<OUT_TYPE>) this.reader.createIterator(
632  sampleSeqHolder, this.sampleSeqHolderValueField, info);
633  }
634 
636  return this.sampleSeqHolderValueField;
637  }
638 
639  @SuppressWarnings("unchecked")
640  public Iterator<OUT_TYPE> take(
641  org.omg.dds.sub.DataReader.Selector<OUT_TYPE> query) {
642  DDS.SampleInfoSeqHolder info = new DDS.SampleInfoSeqHolder();
643  Object sampleSeqHolder;
644 
645  if (query == null) {
646  throw new IllegalArgumentExceptionImpl(this.environment,
647  "Invalid Selector (null) provided.");
648  }
649  ReadCondition<OUT_TYPE> condition = null;
650 
651  if (query.getQueryExpression() != null) {
652  condition = query.getCondition();
653  }
654  InstanceHandle instance = query.getInstance();
655 
656  try {
657  DataStateImpl state = (DataStateImpl) query.getDataState();
658  InstanceHandleImpl handle = (InstanceHandleImpl) instance;
659  sampleSeqHolder = this.sampleSeqHolderClz.newInstance();
660  int rc;
661 
662  if (condition != null) {
663  DDS.ReadCondition oldCondition = ((ReadConditionImpl<OUT_TYPE>) condition)
664  .getOld();
665 
666  if (query.retrieveNextInstance()) {
667  // take_next_instance_w_condition
668  rc = (Integer) this.takeNextInstanceCondition.invoke(
669  this.old, sampleSeqHolder, info,
670  query.getMaxSamples(), handle.getValue(),
671  oldCondition);
672  } else {
673  // take_w_condition
674  rc = (Integer) this.takeCondition.invoke(this.old,
675  sampleSeqHolder, info, query.getMaxSamples(),
676  oldCondition);
677  }
678  } else {
679  // take_next_instance
680  if (query.retrieveNextInstance()) {
681  rc = (Integer) this.takeNextInstance.invoke(this.old,
682  sampleSeqHolder, info, query.getMaxSamples(),
683  handle.getValue(), state.getOldSampleState(),
684  state.getOldViewState(),
685  state.getOldInstanceState());
686  } else {
687  // take
688  if (instance.isNil()) {
689  rc = (Integer) this.take.invoke(this.old,
690  sampleSeqHolder, info, query.getMaxSamples(),
691  state.getOldSampleState(),
692  state.getOldViewState(),
693  state.getOldInstanceState());
694  }
695  // take_instance
696  else {
697  rc = (Integer) this.takeInstance.invoke(this.old,
698  sampleSeqHolder, info, query.getMaxSamples(),
699  handle.getValue(), state.getOldSampleState(),
700  state.getOldViewState(),
701  state.getOldInstanceState());
702  }
703  }
704  }
705  Utilities.checkReturnCode(rc, this.environment,
706  "DataReader.take() failed.");
707  } catch (InstantiationException e) {
708  throw new DDSExceptionImpl(this.environment, "Internal error ("
709  + e.getMessage() + ").");
710  } catch (IllegalAccessException e) {
711  throw new DDSExceptionImpl(this.environment, "Internal error ("
712  + e.getMessage() + ").");
713  } catch (IllegalArgumentExceptionImpl e) {
714  throw e;
715  } catch (IllegalArgumentException e) {
716  throw new DDSExceptionImpl(this.environment, "Internal error ("
717  + e.getMessage() + ").");
718  } catch (InvocationTargetException e) {
719  throw new DDSExceptionImpl(this.environment, "Internal error ("
720  + e.getMessage() + ").");
721  } catch (ClassCastException e) {
723  this.environment,
724  "Taking with non-OpenSplice DataState, InstanceHandle or ReadCondition not supported");
725  }
726  return (Iterator<OUT_TYPE>) this.reader.createIterator(
727  sampleSeqHolder, this.sampleSeqHolderValueField, info);
728  }
729 
730  @SuppressWarnings("unchecked")
731  public boolean readNextSample(SampleImpl<TYPE> sample) {
732  DDS.SampleInfoHolder info;
733  Object sampleHolder;
734  boolean result;
735 
736  if (sample == null) {
737  throw new IllegalArgumentExceptionImpl(this.environment,
738  "Provided an invalid null sample.");
739  }
740  try {
741  info = new DDS.SampleInfoHolder();
742  sampleHolder = this.sampleHolderClz.newInstance();
743 
744  this.sampleHolderValueField.set(sampleHolder, sample.getData());
745  int rc = (Integer) this.readNextSample.invoke(this.old,
746  sampleHolder, info);
747  Utilities.checkReturnCode(rc, this.environment,
748  "DataReader.readNextSample() failed.");
749 
750  if (rc == DDS.RETCODE_OK.value) {
751  sample.setContent(
752  (TYPE) this.sampleHolderValueField.get(sampleHolder),
753  info.value);
754  result = true;
755  } else {
756  result = false;
757  }
758 
759  } catch (InstantiationException e) {
760  throw new DDSExceptionImpl(environment, "Internal error ("
761  + e.getMessage() + ").");
762  } catch (IllegalAccessException e) {
763  throw new DDSExceptionImpl(environment, "Internal error ("
764  + e.getMessage() + ").");
765  } catch (IllegalArgumentExceptionImpl e) {
766  throw e;
767  } catch (IllegalArgumentException e) {
768  throw new DDSExceptionImpl(environment, "Internal error ("
769  + e.getMessage() + ").");
770  } catch (InvocationTargetException e) {
771  throw new DDSExceptionImpl(environment, "Internal error ("
772  + e.getMessage() + ").");
773  } catch (ClassCastException e) {
774  throw new DDSExceptionImpl(environment, "Internal error ("
775  + e.getMessage() + ").");
776  }
777  return result;
778  }
779 
780  @SuppressWarnings("unchecked")
781  public boolean takeNextSample(SampleImpl<TYPE> sample) {
782  DDS.SampleInfoHolder info;
783  Object sampleHolder;
784  boolean result;
785 
786  if (sample == null) {
787  throw new IllegalArgumentExceptionImpl(this.environment,
788  "Provided an invalid null sample.");
789  }
790  try {
791  info = new DDS.SampleInfoHolder();
792  sampleHolder = this.sampleHolderClz.newInstance();
793  this.sampleHolderValueField.set(sampleHolder, sample.getData());
794  int rc = (Integer) this.takeNextSample.invoke(this.old,
795  sampleHolder, info);
796  Utilities.checkReturnCode(rc, this.environment,
797  "DataReader.readNextSample() failed.");
798 
799  if (rc == DDS.RETCODE_OK.value) {
800  sample.setContent(
801  (TYPE) this.sampleHolderValueField.get(sampleHolder),
802  info.value);
803  result = true;
804  } else {
805  result = false;
806  }
807 
808  } catch (InstantiationException e) {
809  throw new DDSExceptionImpl(environment, "Internal error ("
810  + e.getMessage() + ").");
811  } catch (IllegalAccessException e) {
812  throw new DDSExceptionImpl(environment, "Internal error ("
813  + e.getMessage() + ").");
814  } catch (IllegalArgumentExceptionImpl e) {
815  throw e;
816  } catch (IllegalArgumentException e) {
817  throw new DDSExceptionImpl(environment, "Internal error ("
818  + e.getMessage() + ").");
819  } catch (InvocationTargetException e) {
820  throw new DDSExceptionImpl(environment, "Internal error ("
821  + e.getMessage() + ").");
822  } catch (ClassCastException e) {
823  throw new DDSExceptionImpl(environment, "Internal error ("
824  + e.getMessage() + ").");
825  }
826  return result;
827  }
828 
829  public List<Sample<OUT_TYPE>> take(List<Sample<OUT_TYPE>> samples) {
830  PreAllocator<OUT_TYPE> pa = this.reader.getPreAllocator(samples,
831  this.sampleSeqHolderClz, this.sampleSeqHolderValueField);
832 
833  try {
834  int rc = (Integer) this.take.invoke(this.old,
836  DDS.LENGTH_UNLIMITED.value, DDS.ANY_SAMPLE_STATE.value,
837  DDS.ANY_VIEW_STATE.value, DDS.ANY_INSTANCE_STATE.value);
838  Utilities.checkReturnCode(rc, this.environment,
839  "DataReader.read() failed.");
840  } catch (IllegalAccessException e) {
841  throw new DDSExceptionImpl(environment, "Internal error ("
842  + e.getMessage() + ").");
843  } catch (IllegalArgumentExceptionImpl e) {
844  throw e;
845  } catch (IllegalArgumentException e) {
846  throw new DDSExceptionImpl(environment, "Internal error ("
847  + e.getMessage() + ").");
848  } catch (InvocationTargetException e) {
849  throw new DDSExceptionImpl(environment, "Internal error ("
850  + e.getMessage() + ").");
851  }
852  pa.updateReferences();
853 
854  return pa.getSampleList();
855  }
856 
857  public List<Sample<OUT_TYPE>> take(List<Sample<OUT_TYPE>> samples,
858  org.omg.dds.sub.DataReader.Selector<OUT_TYPE> selector) {
859  if (selector == null) {
860  return this.take(samples);
861  }
862 
863  PreAllocator<OUT_TYPE> pa = this.reader.getPreAllocator(samples,
864  this.sampleSeqHolderClz, this.sampleSeqHolderValueField);
865  ReadCondition<OUT_TYPE> condition = null;
866 
867  if (selector.getQueryExpression() != null) {
868  condition = selector.getCondition();
869  }
870  InstanceHandle instance = selector.getInstance();
871 
872  try {
873  DataStateImpl state = (DataStateImpl) selector.getDataState();
874  InstanceHandleImpl handle = (InstanceHandleImpl) instance;
875  int rc;
876 
877  if (condition != null) {
878  DDS.ReadCondition oldCondition = ((ReadConditionImpl<OUT_TYPE>) condition)
879  .getOld();
880 
881  if (selector.retrieveNextInstance()) {
882  // take_next_instance_w_condition
883  rc = (Integer) this.takeNextInstanceCondition.invoke(
884  this.old, pa.getDataSeqHolder(),
885  pa.getInfoSeqHolder(), selector.getMaxSamples(),
886  handle.getValue(), oldCondition);
887  } else {
888  // take_w_condition
889  rc = (Integer) this.takeCondition.invoke(this.old,
891  selector.getMaxSamples(), oldCondition);
892  }
893  } else {
894  // take_next_instance
895  if (selector.retrieveNextInstance()) {
896  rc = (Integer) this.takeNextInstance.invoke(this.old,
898  selector.getMaxSamples(), handle.getValue(),
899  state.getOldSampleState(), state.getOldViewState(),
900  state.getOldInstanceState());
901  } else {
902  // take
903  if (instance.isNil()) {
904  rc = (Integer) this.take.invoke(this.old,
906  selector.getMaxSamples(),
907  state.getOldSampleState(),
908  state.getOldViewState(),
909  state.getOldInstanceState());
910  }
911  // take_instance
912  else {
913  rc = (Integer) this.takeInstance.invoke(this.old,
915  selector.getMaxSamples(), handle.getValue(),
916  state.getOldSampleState(),
917  state.getOldViewState(),
918  state.getOldInstanceState());
919  }
920  }
921  }
922  Utilities.checkReturnCode(rc, this.environment,
923  "DataReader.take() failed.");
924  } catch (IllegalAccessException e) {
925  throw new DDSExceptionImpl(this.environment, "Internal error ("
926  + e.getMessage() + ").");
927  } catch (IllegalArgumentExceptionImpl e) {
928  throw e;
929  } catch (IllegalArgumentException e) {
930  throw new DDSExceptionImpl(this.environment, "Internal error ("
931  + e.getMessage() + ").");
932  } catch (InvocationTargetException e) {
933  throw new DDSExceptionImpl(this.environment, "Internal error ("
934  + e.getMessage() + ").");
935  } catch (ClassCastException e) {
937  this.environment,
938  "Reading with non-OpenSplice DataState, InstanceHandle or ReadCondition not supported");
939  }
940  pa.updateReferences();
941 
942  return pa.getSampleList();
943  }
944 
945  public List<Sample<OUT_TYPE>> read(List<Sample<OUT_TYPE>> samples,
946  org.omg.dds.sub.DataReader.Selector<OUT_TYPE> selector) {
947 
948  if (selector == null) {
949  return this.read(samples);
950  }
951 
952  PreAllocator<OUT_TYPE> pa = this.reader.getPreAllocator(samples,
953  this.sampleSeqHolderClz, this.sampleSeqHolderValueField);
954 
955  ReadCondition<OUT_TYPE> condition = null;
956 
957  if (selector.getQueryExpression() != null) {
958  condition = selector.getCondition();
959  }
960  InstanceHandle instance = selector.getInstance();
961 
962  try {
963  DataStateImpl state = (DataStateImpl) selector.getDataState();
964  InstanceHandleImpl handle = (InstanceHandleImpl) instance;
965  int rc;
966 
967  if (condition != null) {
968  DDS.ReadCondition oldCondition = ((ReadConditionImpl<OUT_TYPE>) condition)
969  .getOld();
970 
971  if (selector.retrieveNextInstance()) {
972  // read_next_instance_w_condition
973  rc = (Integer) this.readNextInstanceCondition.invoke(
974  this.old, pa.getDataSeqHolder(),
975  pa.getInfoSeqHolder(), selector.getMaxSamples(),
976  handle.getValue(), oldCondition);
977  } else {
978  // read_w_condition
979  rc = (Integer) this.readCondition.invoke(this.old,
981  selector.getMaxSamples(), oldCondition);
982  }
983  } else {
984  // read_next_instance
985  if (selector.retrieveNextInstance()) {
986  rc = (Integer) this.readNextInstance.invoke(this.old,
988  selector.getMaxSamples(), handle.getValue(),
989  state.getOldSampleState(), state.getOldViewState(),
990  state.getOldInstanceState());
991  } else {
992  // read
993  if (instance.isNil()) {
994  rc = (Integer) this.read.invoke(this.old,
996  selector.getMaxSamples(),
997  state.getOldSampleState(),
998  state.getOldViewState(),
999  state.getOldInstanceState());
1000  }
1001  // read_instance
1002  else {
1003  rc = (Integer) this.readInstance.invoke(this.old,
1005  selector.getMaxSamples(), handle.getValue(),
1006  state.getOldSampleState(),
1007  state.getOldViewState(),
1008  state.getOldInstanceState());
1009  }
1010  }
1011  }
1012  Utilities.checkReturnCode(rc, this.environment,
1013  "DataReader.read() failed.");
1014  } catch (IllegalAccessException e) {
1015  throw new DDSExceptionImpl(this.environment, "Internal error ("
1016  + e.getMessage() + ").");
1017  } catch (IllegalArgumentExceptionImpl e) {
1018  throw e;
1019  } catch (IllegalArgumentException e) {
1020  throw new DDSExceptionImpl(this.environment, "Internal error ("
1021  + e.getMessage() + ").");
1022  } catch (InvocationTargetException e) {
1023  throw new DDSExceptionImpl(this.environment, "Internal error ("
1024  + e.getMessage() + ").");
1025  } catch (ClassCastException e) {
1026  throw new IllegalArgumentExceptionImpl(
1027  this.environment,
1028  "Reading with non-OpenSplice DataState, InstanceHandle or ReadCondition not supported");
1029  }
1030  pa.updateReferences();
1031 
1032  return pa.getSampleList();
1033  }
1034 
1035  public List<Sample<OUT_TYPE>> read(List<Sample<OUT_TYPE>> samples) {
1036  PreAllocator<OUT_TYPE> pa = this.reader.getPreAllocator(samples,
1037  this.sampleSeqHolderClz, this.sampleSeqHolderValueField);
1038 
1039  try {
1040  int rc = (Integer) this.read.invoke(this.old,
1042  DDS.LENGTH_UNLIMITED.value, DDS.ANY_SAMPLE_STATE.value,
1043  DDS.ANY_VIEW_STATE.value, DDS.ANY_INSTANCE_STATE.value);
1044  Utilities.checkReturnCode(rc, this.environment,
1045  "DataReader.read() failed.");
1046  } catch (IllegalAccessException e) {
1047  throw new DDSExceptionImpl(environment, "Internal error ("
1048  + e.getMessage() + ").");
1049  } catch (IllegalArgumentExceptionImpl e) {
1050  throw e;
1051  } catch (IllegalArgumentException e) {
1052  throw new DDSExceptionImpl(environment, "Internal error ("
1053  + e.getMessage() + ").");
1054  } catch (InvocationTargetException e) {
1055  throw new DDSExceptionImpl(environment, "Internal error ("
1056  + e.getMessage() + ").");
1057  }
1058  pa.updateReferences();
1059 
1060  return pa.getSampleList();
1061  }
1062 }
List< Sample< OUT_TYPE > > read(List< Sample< OUT_TYPE >> samples, org.omg.dds.sub.DataReader.Selector< OUT_TYPE > selector)
A sample has been lost (never received).
A DataReader allows the application (1) to declare the data it wishes to receive (i.e., make a subscription) and (2) to access the data received by the attached org.omg.dds.sub.Subscriber.
static void checkReturnCodeWithTimeout(int retCode, OsplServiceEnvironment environment, String message)
Definition: Utilities.java:176
RequestedIncompatibleQosStatus getRequestedIncompatibleQosStatus()
RequestedDeadlineMissedStatus getRequestedDeadlineMissedStatus()
A Sample represents an atom of data information (i.e., one value for one instance) as returned by a o...
Definition: Sample.java:116
SampleInfoSeqHolder getInfoSeqHolder()
List< Sample< OUT_TYPE > > take(List< Sample< OUT_TYPE >> samples, org.omg.dds.sub.DataReader.Selector< OUT_TYPE > selector)
List< Sample< OUT_TYPE > > read(List< Sample< OUT_TYPE >> samples)
static DDS.Duration_t convert(OsplServiceEnvironment environment, Duration d)
Definition: Utilities.java:232
static QosPolicyCount [] convert(OsplServiceEnvironment env, DDS.QosPolicyCount[] old)
List< Sample< TYPE > > getSampleList()
abstract Sample.Iterator<?> createIterator(Object sampleSeqHolder, Field sampleSeqHolderValueField, DDS.SampleInfoSeqHolder info)
An opaque handle that can be used to refer to a local or remote entity.
static void checkReturnCode(int retCode, OsplServiceEnvironment environment, String message)
Definition: Utilities.java:33
static DDS.UserDataQosPolicy convert(OsplServiceEnvironment env, UserData p)
A org.omg.dds.core.policy.QosPolicy value was incompatible with what is offered.
void waitForHistoricalData(String filterExpression, List< String > filterParameters, Time minSourceTimestamp, Time maxSourceTimestamp, ResourceLimits resourceLimits, Duration maxWait)
PublicationBuiltinTopicData getMatchedPublicationData(InstanceHandle publicationHandle)
A supertype of all DDS classes and interfaces.
Definition: DDSObject.java:25
ReadCondition objects are conditions specifically dedicated to read operations and attached to one or...
This class contains the statistics about the discovered number of org.omg.dds.pub.DataWriters that are compatible with the org.omg.dds.sub.DataReader to which the Status is attached.
void waitForHistoricalData(String filterExpression, List< String > filterParameters, Time minSourceTimestamp, Time maxSourceTimestamp, ResourceLimits resourceLimits, long maxWait, TimeUnit unit)
List< Sample< OUT_TYPE > > take(List< Sample< OUT_TYPE >> samples)
The DCPSPublication topic communicates the existence of datawriters by means of the PublicationBuilti...
The liveliness of one or more org.omg.dds.pub.DataWriters that were writing instances read through th...
void returnLoan(Object sampleSeqHolder, DDS.SampleInfoSeqHolder infoSeqHolder)
A span of elapsed time expressed with nanosecond precision.
Definition: Duration.java:35
ReflectionDataReader(OsplServiceEnvironment environment, AbstractDataReader< OUT_TYPE > reader, Class< TYPE > ddsTypeClz)
DDS implementations are rooted in this class, a concrete subclass of which can be instantiated based ...
A (received) sample has been rejected.
Specifies the resources that the Service can consume in order to meet the requested QoS...
TYPE getKeyValue(TYPE keyHolder, InstanceHandle handle)
Selector class encapsulates different ways of selecting samples from a org.omg.dds.sub.DataReader.
The deadline that the org.omg.dds.sub.DataReader was expecting through its org.omg.dds.core.policy.Deadline was not respected for a specific instance.
void waitForHistoricalData(long maxWait, TimeUnit unit)
SubscriptionMatchedStatus getSubscriptionMatchedStatus()
A moment in time expressed with nanosecond precision (though not necessarily nanosecond accuracy)...
Definition: Time.java:34
abstract PreAllocator< TYPE > getPreAllocator(List< Sample< TYPE >> samples, Class<?> sampleSeqHolderClz, Field sampleSeqHolderValueField)