OpenSplice Java 5 DCPS  v6.x
OpenSplice Java 5 OpenSplice Data Distribution Service Data-Centric Publish-Subscribe API
TypeSupportProtobuf.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.type;
22 
23 import java.lang.reflect.Constructor;
24 import java.lang.reflect.InvocationTargetException;
25 import java.util.Collection;
26 
28 import org.omg.dds.core.status.Status;
30 import org.omg.dds.pub.DataWriterQos;
32 import org.omg.dds.sub.DataReaderQos;
34 import org.omg.dds.topic.TopicQos;
47 
48 import DDS.TypeSupport;
49 
50 public abstract class TypeSupportProtobuf<PROTOBUF_TYPE, DDS_TYPE> extends
51  AbstractTypeSupport<PROTOBUF_TYPE> {
52  protected final Class<PROTOBUF_TYPE> dataType;
55  private final byte[] metaData;
56  private final byte[] typeHash;
57  private final byte[] extentions = null;
58 
60  Class<PROTOBUF_TYPE> dataType,
61  TypeSupportImpl<DDS_TYPE> ddsTypeSupport,
62  final byte[] metaData,
63  final byte[] metaHash) {
64  this.environment = environment;
65  this.dataType = dataType;
66  this.metaData = metaData;
67  this.typeHash = metaHash;
68  this.ddsTypeSupport = ddsTypeSupport;
69 
70  org.opensplice.dds.dcps.TypeSupportImpl oldTypeSupport = (org.opensplice.dds.dcps.TypeSupportImpl)ddsTypeSupport.getOldTypeSupport();
71  oldTypeSupport.set_data_representation_id(DDS.GPB_REPRESENTATION.value);
72  oldTypeSupport.set_meta_data(this.metaData);
73  oldTypeSupport.set_type_hash(this.typeHash);
74  }
75 
76  @SuppressWarnings("unchecked")
77  public static <SOME_TYPE> org.omg.dds.type.TypeSupport<SOME_TYPE> getInstance(
78  OsplServiceEnvironment environment, Class<SOME_TYPE> dataType,
79  String registeredName) {
80  String typeSupportName = dataType.getName().replaceAll("\\$", "")
81  + "TypeSupportProtobuf";
82 
83  try {
84  Class<?> typeSupportClass = Class.forName(typeSupportName);
85  Constructor<?> c = typeSupportClass.getConstructor(
86  OsplServiceEnvironment.class, String.class);
87 
88  return (org.omg.dds.type.TypeSupport<SOME_TYPE>) c.newInstance(
89  environment, registeredName);
90 
91  } catch (ClassNotFoundException e) {
92  throw new PreconditionNotMetExceptionImpl(environment,
93  "Allocating new TypeSupport failed (" + typeSupportName
94  + "); " + e.getMessage());
95  } catch (InstantiationException e) {
96  throw new PreconditionNotMetExceptionImpl(environment,
97  "Allocating new TypeSupport failed. " + e.getMessage());
98  } catch (IllegalAccessException e) {
99  throw new PreconditionNotMetExceptionImpl(environment,
100  "Allocating new TypeSupport failed. " + e.getMessage());
101  } catch (IllegalArgumentException e) {
102  throw new PreconditionNotMetExceptionImpl(environment,
103  "Allocating new TypeSupport failed. " + e.getMessage());
104  } catch (InvocationTargetException e) {
105  throw new PreconditionNotMetExceptionImpl(environment,
106  "Allocating new TypeSupport failed. " + e.getMessage());
107  } catch (NoSuchMethodException e) {
108  throw new PreconditionNotMetExceptionImpl(environment,
109  "Allocating new TypeSupport failed. " + e.getMessage());
110  } catch (SecurityException e) {
111  throw new PreconditionNotMetExceptionImpl(environment,
112  "Allocating new TypeSupport failed. " + e.getMessage());
113  }
114  }
115 
116  @Override
118  return this.environment;
119  }
120 
121  @Override
123  return this.ddsTypeSupport.getOldTypeSupport();
124  }
125 
126  @Override
127  public PROTOBUF_TYPE newData() {
128  try {
129  return dataType.newInstance();
130  } catch (InstantiationException e) {
132  "Unable to instantiate data; " + e.getMessage());
133  } catch (IllegalAccessException e) {
135  "Unable to instantiate data; " + e.getMessage());
136  }
137  }
138 
139  @Override
140  public Class<PROTOBUF_TYPE> getType() {
141  return this.dataType;
142  }
143 
144  @Override
145  public String getTypeName() {
146  return this.ddsTypeSupport.getTypeName();
147  }
148 
150  return this.ddsTypeSupport;
151  }
152 
153  @Override
155  DomainParticipantImpl participant, String topicName, TopicQos qos,
157  Collection<Class<? extends Status>> statuses) {
158  return new TopicProtobuf<PROTOBUF_TYPE>(this.environment, participant,
159  topicName, this, qos, listener, statuses);
160  }
161 
162  @Override
166  Collection<Class<? extends Status>> statuses) {
168  this.environment, publisher,
169  (TopicProtobuf<PROTOBUF_TYPE>) topic, qos, listener, statuses);
170  }
171 
172  @Override
174  SubscriberImpl subscriber,
177  Collection<Class<? extends Status>> statuses){
179  this.environment, subscriber, topicDescription, qos, listener,
180  statuses);
181  }
182 
183  public byte[] getMetaDescriptor(){
184  return this.metaData.clone();
185  }
186 
187  public byte[] getMetaHash(){
188  return this.typeHash.clone();
189  }
190 
191  public byte[] getExtentions(){
192  return this.extentions.clone();
193  }
194 
195  protected static byte[] hexStringToByteArray(String s) {
196  int len = s.length();
197  byte[] data = new byte[len / 2];
198  for (int i = 0; i < len; i += 2) {
199  data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
200  + Character.digit(s.charAt(i+1), 16));
201  }
202  return data;
203  }
204 
205  public abstract PROTOBUF_TYPE ddsToProtobuf(DDS_TYPE ddsData);
206 
207  public abstract DDS_TYPE protobufToDds(PROTOBUF_TYPE protobufData);
208 
209  public abstract PROTOBUF_TYPE ddsKeyToProtobuf(DDS_TYPE ddsData);
210 }
Since a org.omg.dds.sub.DataReader is a kind of org.omg.dds.core.Entity, it has the ability to have a...
static< SOME_TYPE > org.omg.dds.type.TypeSupport< SOME_TYPE > getInstance(OsplServiceEnvironment environment, Class< SOME_TYPE > dataType, String registeredName)
AbstractDataWriter< PROTOBUF_TYPE > createDataWriter(PublisherImpl publisher, AbstractTopic< PROTOBUF_TYPE > topic, DataWriterQos qos, DataWriterListener< PROTOBUF_TYPE > listener, Collection< Class<? extends Status >> statuses)
AbstractDataReader< PROTOBUF_TYPE > createDataReader(SubscriberImpl subscriber, TopicDescriptionExt< PROTOBUF_TYPE > topicDescription, DataReaderQos qos, DataReaderListener< PROTOBUF_TYPE > listener, Collection< Class<? extends Status >> statuses)
abstract PROTOBUF_TYPE ddsToProtobuf(DDS_TYPE ddsData)
TypeSupportImpl< DDS_TYPE > getTypeSupportStandard()
TypeSupportProtobuf(OsplServiceEnvironment environment, Class< PROTOBUF_TYPE > dataType, TypeSupportImpl< DDS_TYPE > ddsTypeSupport, final byte[] metaData, final byte[] metaHash)
final TypeSupportImpl< DDS_TYPE > ddsTypeSupport
AbstractTopic< PROTOBUF_TYPE > createTopic(DomainParticipantImpl participant, String topicName, TopicQos qos, TopicListener< PROTOBUF_TYPE > listener, Collection< Class<? extends Status >> statuses)
Since org.omg.dds.topic.Topic is a kind of org.omg.dds.core.Entity, it has the ability to have an ass...
Since a org.omg.dds.pub.DataWriter is a kind of org.omg.dds.core.Entity, it has the ability to have a...
abstract PROTOBUF_TYPE ddsKeyToProtobuf(DDS_TYPE ddsData)
abstract DDS_TYPE protobufToDds(PROTOBUF_TYPE protobufData)
TypeSupport is an abstract interface that has to be specialized for each concrete type that will be u...
DDS implementations are rooted in this class, a concrete subclass of which can be instantiated based ...
Status is the abstract root class for all communication status objects.
Definition: Status.java:41