OpenSplice Java FACE API  v6.x
OpenSplice Future Airborne Capability Environment (FACE) Java API
Connection.java
Go to the documentation of this file.
1 /*
2  * Vortex OpenSplice
3  *
4  * This software and documentation are Copyright 2006 to 2021 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 package org.vortex.FACE;
21 
22 import java.util.ArrayList;
23 import java.util.concurrent.locks.Lock;
24 import java.util.concurrent.locks.ReadWriteLock;
25 import java.util.concurrent.locks.ReentrantReadWriteLock;
26 
27 import org.omg.dds.core.AlreadyClosedException;
28 import org.omg.dds.core.status.Status;
29 import org.omg.dds.domain.DomainParticipant;
30 import org.omg.dds.domain.DomainParticipantFactory;
31 import org.omg.dds.domain.DomainParticipantQos;
32 import org.omg.dds.topic.Topic;
33 import org.omg.dds.topic.TopicQos;
34 
35 import FACE.CONNECTION_DIRECTION_TYPE;
36 import FACE.TRANSPORT_CONNECTION_STATUS_TYPE;
37 import FACE.VALIDITY_TYPE;
38 
39 public abstract class Connection<TYPE> {
40  private final ConnectionDescription description;
41  private final Class<TYPE> dataType;
42  private DomainParticipant participant;
43  private Topic<TYPE> topic;
44  private TRANSPORT_CONNECTION_STATUS_TYPE status;
45  private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
46  private final Lock readLock = readWriteLock.readLock();
47  private final Lock writeLock = readWriteLock.writeLock();
48 
49  public Connection(ConnectionDescription description, Class<TYPE> dataType) {
50  this.description = description;
51  this.dataType = dataType;
52  this.setupParticipant();
53  this.setupTopic();
54  this.status = new TRANSPORT_CONNECTION_STATUS_TYPE(0, 1, 0,
55  this.getDirection(), 0, this.description.getRefreshPeriod(),
56  VALIDITY_TYPE.INVALID);
57  }
58 
59  public TRANSPORT_CONNECTION_STATUS_TYPE getStatus() {
60  return new TRANSPORT_CONNECTION_STATUS_TYPE(this.status.MAX_MESSAGE,
61  this.status.MAX_MESSAGE_SIZE, this.status.MESSAGE,
62  this.status.CONNECTION_DIRECTION,
63  this.status.WAITING_PROCESSES_OR_MESSAGES,
64  this.status.REFRESH_PERIOD, this.status.LAST_MSG_VALIDITY);
65  }
66 
67  protected void setLastMessageValidity(VALIDITY_TYPE lastMessageValidity) {
68  synchronized(this) {
69  this.status.LAST_MSG_VALIDITY = lastMessageValidity;
70  }
71  }
72 
73  public abstract CONNECTION_DIRECTION_TYPE getDirection();
74 
76  return this.description;
77  }
78 
79  public DomainParticipant getParticipant() {
80  return this.participant;
81  }
82 
83  public Topic<TYPE> getTopic() {
84  return this.topic;
85  }
86 
87  private void setupParticipant() {
88  DomainParticipantFactory df = this.description.getEnvironment()
89  .getSPI().getParticipantFactory();
90 
91  DomainParticipantQos qos = this.description.getDomainParticipantQos();
92 
93  if (qos == null) {
94  qos = df.getDefaultParticipantQos();
95  }
96  this.participant = df.createParticipant(this.description.getDomainId(),
97  qos, null, new ArrayList<Class<? extends Status>>());
98  }
99 
100  private void setupTopic() {
101  TopicQos qos = this.description.getTopicQos();
102 
103  if (qos == null) {
104  qos = this.participant.getDefaultTopicQos();
105  }
106  this.topic = this.participant.createTopic(
107  this.description.getTopicName(), this.dataType, qos, null,
108  new ArrayList<Class<? extends Status>>());
109  }
110 
111  public static <TYPE> Connection<TYPE> getConnection(
112  ConnectionDescription description, Class<TYPE> dataType) {
113 
114  switch (description.getDirection()) {
115  case SOURCE:
116  return new SourceConnection<TYPE>(description, dataType);
117  case DESTINATION:
118  return new DestinationConnection<TYPE>(description, dataType);
119  default:
120  return null;
121  }
122  }
123 
124  public void close() {
125  try {
126  this.participant.close();
127  } catch(AlreadyClosedException e) {
128  // ignore
129  }
130  this.setLastMessageValidity(VALIDITY_TYPE.INVALID);
131  }
132 
133  @SuppressWarnings("unchecked")
134  public <OTHER> Connection<OTHER> cast() {
135  Connection<OTHER> other;
136 
137  try {
138  other = (Connection<OTHER>) this;
139  } catch (ClassCastException c) {
140  other = null;
141  }
142  return other;
143  }
144 
146  if (this.getDirection() != CONNECTION_DIRECTION_TYPE.DESTINATION) {
147  return null;
148  }
149  return (DestinationConnection<TYPE>) this;
150  }
151 
153  if (this.getDirection() != CONNECTION_DIRECTION_TYPE.SOURCE) {
154  return null;
155  }
156  return (SourceConnection<TYPE>) this;
157  }
158 
159  protected void readLock() {
160  this.readLock.lock();
161  }
162 
163  protected void readUnlock() {
164  this.readLock.unlock();
165  }
166 
167  protected void writeLock() {
168  this.writeLock.lock();
169  }
170 
171  protected void writeUnlock() {
172  this.writeLock.unlock();
173  }
174 }
DomainParticipant getParticipant()
Definition: Connection.java:79
DestinationConnection< TYPE > asDestination()
abstract CONNECTION_DIRECTION_TYPE getDirection()
Topic< TYPE > getTopic()
Definition: Connection.java:83
TRANSPORT_CONNECTION_STATUS_TYPE getStatus()
Definition: Connection.java:59
static< TYPE > Connection< TYPE > getConnection(ConnectionDescription description, Class< TYPE > dataType)
SourceConnection< TYPE > asSource()
void setLastMessageValidity(VALIDITY_TYPE lastMessageValidity)
Definition: Connection.java:67
This is a typed class which will be generated by idlpp.
Definition: TS.java:25
Connection(ConnectionDescription description, Class< TYPE > dataType)
Definition: Connection.java:49
ConnectionDescription getDescription()
Definition: Connection.java:75