OpenSplice Java 5 DCPS  v6.x
OpenSplice Java 5 OpenSplice Data Distribution Service Data-Centric Publish-Subscribe API
WaitSetImpl.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.core;
22 
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.concurrent.ConcurrentHashMap;
26 import java.util.concurrent.TimeUnit;
27 import java.util.concurrent.TimeoutException;
28 
29 import org.omg.dds.core.Condition;
30 import org.omg.dds.core.Duration;
32 import org.omg.dds.core.WaitSet;
33 
34 public class WaitSetImpl extends WaitSet {
35  private final OsplServiceEnvironment environment;
36  private final DDS.WaitSet oldWaitSet;
37  private ConcurrentHashMap<DDS.Condition, org.omg.dds.core.Condition> conditions;
38 
39  public WaitSetImpl(OsplServiceEnvironment environment) {
40  this.environment = environment;
41  this.oldWaitSet = new DDS.WaitSet();
42  this.conditions = new ConcurrentHashMap<DDS.Condition, org.omg.dds.core.Condition>();
43  }
44 
45  @Override
47  return this.environment;
48  }
49 
50  @Override
51  public void waitForConditions() {
52  DDS.ConditionSeqHolder holder = new DDS.ConditionSeqHolder();
53  int rc = this.oldWaitSet._wait(holder, DDS.DURATION_INFINITE.value);
54 
55  Utilities.checkReturnCode(rc, this.environment,
56  "Waitset.waitForConditions() failed.");
57 
58  return;
59  }
60 
61  @Override
62  public void waitForConditions(Collection<Condition> activeConditions) {
63  if (activeConditions == null) {
64  throw new IllegalArgumentExceptionImpl(this.environment,
65  "Illegal Collection<Condition> (null) provided.");
66  }
67 
68  DDS.ConditionSeqHolder holder = new DDS.ConditionSeqHolder();
69  int rc = this.oldWaitSet._wait(holder, DDS.DURATION_INFINITE.value);
70 
71  Utilities.checkReturnCode(rc, this.environment,
72  "Waitset.waitForConditions() failed.");
73 
74  activeConditions.clear();
75 
76  for (DDS.Condition cond : holder.value) {
77  activeConditions.add(this.conditions.get(cond));
78  }
79  }
80 
81  @Override
82  public void waitForConditions(Duration timeout) throws TimeoutException {
83  if (timeout == null) {
84  throw new IllegalArgumentExceptionImpl(this.environment,
85  "Illegal Duration (null) provided.");
86  }
87  DDS.ConditionSeqHolder holder = new DDS.ConditionSeqHolder();
88  DDS.Duration_t oldTimeout = Utilities
89  .convert(this.environment, timeout);
90 
91  int rc = this.oldWaitSet._wait(holder, oldTimeout);
92 
93  Utilities.checkReturnCodeWithTimeout(rc, this.environment,
94  "Waitset.waitForConditions() failed.");
95  }
96 
97  @Override
98  public void waitForConditions(long timeout, TimeUnit unit)
99  throws TimeoutException {
100  this.waitForConditions(this.environment.getSPI().newDuration(timeout,
101  unit));
102  }
103 
104  @Override
105  public void waitForConditions(Collection<Condition> activeConditions,
106  Duration timeout) throws TimeoutException {
107 
108  if (activeConditions == null) {
109  throw new IllegalArgumentExceptionImpl(this.environment,
110  "Illegal Collection<Condition> (null) provided.");
111 
112  }
113  DDS.ConditionSeqHolder holder = new DDS.ConditionSeqHolder();
114  DDS.Duration_t oldTimeout = Utilities
115  .convert(this.environment, timeout);
116 
117  int rc = this.oldWaitSet._wait(holder, oldTimeout);
118 
119  Utilities.checkReturnCode(rc, this.environment,
120  "Waitset.waitForConditions() failed.");
121 
122  activeConditions.clear();
123 
124  for (DDS.Condition cond : holder.value) {
125  activeConditions.add(this.conditions.get(cond));
126  }
127 
128  }
129 
130  @Override
131  public void waitForConditions(Collection<Condition> activeConditions,
132  long timeout, TimeUnit unit) throws TimeoutException {
133  this.waitForConditions(activeConditions, this.environment.getSPI()
134  .newDuration(timeout, unit));
135 
136  }
137 
138  @Override
139  public void attachCondition(Condition cond) {
141 
142  if (cond == null) {
143  throw new IllegalArgumentExceptionImpl(this.environment,
144  "Illegal Condition (null) provided.");
145  }
146  try {
147  c = (org.opensplice.dds.core.Condition<?>) cond;
148  } catch (ClassCastException cce) {
149  throw new IllegalArgumentExceptionImpl(this.environment,
150  "Attaching non-OpenSplice Condition implementation is not supported.");
151  }
152  DDS.Condition old = c.getOldCondition();
153  int rc = this.oldWaitSet.attach_condition(old);
154  Utilities.checkReturnCode(rc, this.environment,
155  "Attaching condition failed.");
156 
157  this.conditions.put(old, cond);
158 
159  }
160 
161  @Override
162  public void detachCondition(Condition cond) {
164 
165  if (cond == null) {
166  throw new IllegalArgumentExceptionImpl(this.environment,
167  "Illegal Condition (null) provided.");
168  }
169  try {
170  c = (org.opensplice.dds.core.Condition<?>) cond;
171  } catch (ClassCastException cce) {
172  throw new IllegalArgumentExceptionImpl(this.environment,
173  "Detaching non-OpenSplice Condition implementation is not supported.");
174  }
175  DDS.Condition old = c.getOldCondition();
176  int rc = this.oldWaitSet.detach_condition(old);
177  Utilities.checkReturnCode(rc, this.environment,
178  "Detaching condition failed.");
179 
180  this.conditions.remove(old);
181 
182  }
183 
184  @Override
185  public Collection<Condition> getConditions() {
186  return Collections.unmodifiableCollection(conditions.values());
187  }
188 
189  @Override
190  public String toString() {
191  return "WaitSet (" + Integer.toHexString(this.hashCode()) + ")";
192  }
193 }
static void checkReturnCodeWithTimeout(int retCode, OsplServiceEnvironment environment, String message)
Definition: Utilities.java:176
WaitSetImpl(OsplServiceEnvironment environment)
A WaitSet object allows an application to wait until one or more of the attached org.omg.dds.core.Condition objects has a triggerValue of true or else until the timeout expires.
Definition: WaitSet.java:117
static DDS.Duration_t convert(OsplServiceEnvironment environment, Duration d)
Definition: Utilities.java:232
ServiceEnvironment getEnvironment()
void waitForConditions(Duration timeout)
A Condition is a root interface for all the conditions that may be attached to a org.omg.dds.core.WaitSet.
Definition: Condition.java:31
void attachCondition(Condition cond)
static void checkReturnCode(int retCode, OsplServiceEnvironment environment, String message)
Definition: Utilities.java:33
void waitForConditions(Collection< Condition > activeConditions, Duration timeout)
void waitForConditions(Collection< Condition > activeConditions)
Duration newDuration(long duration, TimeUnit unit)
Construct a org.omg.dds.core.Duration of the given magnitude.
A span of elapsed time expressed with nanosecond precision.
Definition: Duration.java:35
DDS implementations are rooted in this class, a concrete subclass of which can be instantiated based ...
Collection< Condition > getConditions()
void detachCondition(Condition cond)
void waitForConditions(Collection< Condition > activeConditions, long timeout, TimeUnit unit)
void waitForConditions(long timeout, TimeUnit unit)