Skip to content

Commit 5670b3f

Browse files
committed
Update qos profile (osrf#20)
Signed-off-by: Ivan Santiago Paunovic <[email protected]>
1 parent ae5edaa commit 5670b3f

File tree

7 files changed

+274
-33
lines changed

7 files changed

+274
-33
lines changed

rcljava/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ set(${PROJECT_NAME}_sources
153153
"src/main/java/org/ros2/rcljava/publisher/PublisherImpl.java"
154154
"src/main/java/org/ros2/rcljava/qos/policies/Durability.java"
155155
"src/main/java/org/ros2/rcljava/qos/policies/History.java"
156+
"src/main/java/org/ros2/rcljava/qos/policies/Liveliness.java"
156157
"src/main/java/org/ros2/rcljava/qos/policies/QoSPolicy.java"
157158
"src/main/java/org/ros2/rcljava/qos/policies/Reliability.java"
158159
"src/main/java/org/ros2/rcljava/qos/QoSProfile.java"
@@ -237,6 +238,7 @@ if(BUILD_TESTING)
237238
# "src/test/java/org/ros2/rcljava/parameters/AsyncParametersClientTest.java"
238239
# "src/test/java/org/ros2/rcljava/parameters/SyncParametersClientTest.java"
239240
"src/test/java/org/ros2/rcljava/publisher/PublisherTest.java"
241+
"src/test/java/org/ros2/rcljava/qos/QoSProfileTest.java"
240242
"src/test/java/org/ros2/rcljava/subscription/SubscriptionTest.java"
241243
"src/test/java/org/ros2/rcljava/timer/TimerTest.java"
242244
)
@@ -252,6 +254,7 @@ if(BUILD_TESTING)
252254
"org.ros2.rcljava.node.NodeTest"
253255
# "org.ros2.rcljava.parameters.SyncParametersClientTest"
254256
"org.ros2.rcljava.publisher.PublisherTest"
257+
"org.ros2.rcljava.qos.QoSProfileTest"
255258
"org.ros2.rcljava.subscription.SubscriptionTest"
256259
"org.ros2.rcljava.timer.TimerTest"
257260
)

rcljava/include/org_ros2_rcljava_RCLJava.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ JNICALL Java_org_ros2_rcljava_RCLJava_nativeGetRMWIdentifier(JNIEnv *, jclass);
5252
*/
5353
JNIEXPORT jlong
5454
JNICALL Java_org_ros2_rcljava_RCLJava_nativeConvertQoSProfileToHandle(
55-
JNIEnv *, jclass, jint, jint, jint, jint, jboolean);
55+
JNIEnv *, jclass,
56+
jint, jint, jint, jint, jlong, jint, jlong, jint, jint, jlong, jint, jboolean);
5657

5758
/*
5859
* Class: org_ros2_rcljava_RCLJava

rcljava/src/main/cpp/org_ros2_rcljava_RCLJava.cpp

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
#include "org_ros2_rcljava_RCLJava.h"
3333

34+
using rcljava_common::exceptions::rcljava_throw_exception;
3435
using rcljava_common::exceptions::rcljava_throw_rclexception;
3536
using rcljava_common::signatures::convert_from_java_signature;
3637
using rcljava_common::signatures::convert_to_java_signature;
@@ -152,9 +153,31 @@ Java_org_ros2_rcljava_RCLJava_nativeGetRMWIdentifier(JNIEnv * env, jclass)
152153
return env->NewStringUTF(rmw_implementation_identifier);
153154
}
154155

156+
#define RCLJAVA_QOS_SET_RMW_TIME(qos_profile, policy_name, seconds, nanos) \
157+
do { \
158+
if (seconds < 0) { \
159+
rcljava_throw_exception( \
160+
env, "java/lang/IllegalArgumentException", \
161+
"seconds must not be negative for " #policy_name); \
162+
free(qos_profile); \
163+
return 0; \
164+
} \
165+
if (nanos < 0) { \
166+
rcljava_throw_exception( \
167+
env, "java/lang/IllegalArgumentException", \
168+
"nanoseconds must not be negative for " #policy_name); \
169+
free(qos_profile); \
170+
return 0; \
171+
} \
172+
qos_profile->policy_name.sec = static_cast<uint64_t>(seconds); \
173+
qos_profile->policy_name.nsec = static_cast<uint64_t>(nanos); \
174+
} while (0)
175+
155176
JNIEXPORT jlong JNICALL
156177
Java_org_ros2_rcljava_RCLJava_nativeConvertQoSProfileToHandle(
157-
JNIEnv *, jclass, jint history, jint depth, jint reliability, jint durability,
178+
JNIEnv * env, jclass, jint history, jint depth, jint reliability, jint durability,
179+
jlong deadline_sec, jint deadline_nanos, jlong lifespan_sec, jint lifespan_nanos,
180+
jint liveliness, jlong liveliness_lease_sec, jint liveliness_lease_nanos,
158181
jboolean avoidROSNamespaceConventions)
159182
{
160183
rmw_qos_profile_t * qos_profile =
@@ -163,11 +186,11 @@ Java_org_ros2_rcljava_RCLJava_nativeConvertQoSProfileToHandle(
163186
qos_profile->depth = depth;
164187
qos_profile->reliability = static_cast<rmw_qos_reliability_policy_t>(reliability);
165188
qos_profile->durability = static_cast<rmw_qos_durability_policy_t>(durability);
166-
// TODO(jacobperron): Expose deadline, lifespan, and liveliness settings as parameters
167-
qos_profile->deadline = rmw_qos_profile_default.deadline;
168-
qos_profile->lifespan = rmw_qos_profile_default.lifespan;
169-
qos_profile->liveliness = rmw_qos_profile_default.liveliness;
170-
qos_profile->liveliness_lease_duration = rmw_qos_profile_default.liveliness_lease_duration;
189+
RCLJAVA_QOS_SET_RMW_TIME(qos_profile, deadline, deadline_sec, deadline_nanos);
190+
RCLJAVA_QOS_SET_RMW_TIME(qos_profile, lifespan, lifespan_sec, lifespan_nanos);
191+
qos_profile->liveliness = static_cast<rmw_qos_liveliness_policy_t>(liveliness);
192+
RCLJAVA_QOS_SET_RMW_TIME(
193+
qos_profile, liveliness_lease_duration, liveliness_lease_sec, liveliness_lease_nanos);
171194
qos_profile->avoid_ros_namespace_conventions = avoidROSNamespaceConventions;
172195
return reinterpret_cast<jlong>(qos_profile);
173196
}

rcljava/src/main/java/org/ros2/rcljava/RCLJava.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -301,18 +301,22 @@ public static synchronized void shutdown() {
301301
}
302302

303303
public static long convertQoSProfileToHandle(final QoSProfile qosProfile) {
304-
int history = qosProfile.getHistory().getValue();
305-
int depth = qosProfile.getDepth();
306-
int reliability = qosProfile.getReliability().getValue();
307-
int durability = qosProfile.getDurability().getValue();
308-
boolean avoidROSNamespaceConventions = qosProfile.getAvoidROSNamespaceConventions();
309-
310304
return nativeConvertQoSProfileToHandle(
311-
history, depth, reliability, durability, avoidROSNamespaceConventions);
305+
qosProfile.getHistory().getValue(),
306+
qosProfile.getDepth(),
307+
qosProfile.getReliability().getValue(),
308+
qosProfile.getDurability().getValue(),
309+
qosProfile.getDeadline().getSeconds(), qosProfile.getDeadline().getNano(),
310+
qosProfile.getLifespan().getSeconds(), qosProfile.getLifespan().getNano(),
311+
qosProfile.getLiveliness().getValue(), qosProfile.getLivelinessLeaseDuration().getSeconds(),
312+
qosProfile.getLivelinessLeaseDuration().getNano(),
313+
qosProfile.getAvoidROSNamespaceConventions());
312314
}
313315

314-
private static native long nativeConvertQoSProfileToHandle(int history, int depth,
315-
int reliability, int durability, boolean avoidROSNamespaceConventions);
316+
private static native long nativeConvertQoSProfileToHandle(
317+
int history, int depth, int reliability, int durability, long deadlineSec, int deadlineNanos,
318+
long lifespanSec, int lifespanNanos, int liveliness, long livelinessLeaseSec,
319+
int livelinessLeaseNanos, boolean avoidROSNamespaceConventions);
316320

317321
public static void disposeQoSProfile(final long qosProfileHandle) {
318322
nativeDisposeQoSProfile(qosProfileHandle);

rcljava/src/main/java/org/ros2/rcljava/qos/QoSProfile.java

Lines changed: 122 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,38 @@
1515

1616
package org.ros2.rcljava.qos;
1717

18+
import java.time.Duration;
19+
1820
import org.ros2.rcljava.qos.policies.Durability;
1921
import org.ros2.rcljava.qos.policies.History;
22+
import org.ros2.rcljava.qos.policies.Liveliness;
2023
import org.ros2.rcljava.qos.policies.QoSPolicy;
2124
import org.ros2.rcljava.qos.policies.Reliability;
2225

2326
public class QoSProfile {
24-
private final History history;
27+
// TODO(ivanpauno): Update all qos policies in a way that the objects are created from RCL,
28+
// to avoid depending on the enum values.
29+
private History history;
30+
31+
private int depth;
32+
33+
private Reliability reliability;
34+
35+
private Durability durability;
2536

26-
private final int depth;
37+
private Duration deadline = Duration.ofSeconds(0, 0);
2738

28-
private final Reliability reliability;
39+
private Duration lifespan = Duration.ofSeconds(0, 0);
2940

30-
private final Durability durability;
41+
private Liveliness liveliness = Liveliness.SYSTEM_DEFAULT;
3142

32-
private final boolean avoidROSNamespaceConventions;
43+
private Duration livelinessLeaseDuration = Duration.ofSeconds(0, 0);
44+
45+
private boolean avoidROSNamespaceConventions;
46+
47+
public QoSProfile(int depth) {
48+
this(History.KEEP_LAST, depth, Reliability.RELIABLE, Durability.VOLATILE, false);
49+
}
3350

3451
public QoSProfile(History history, int depth, Reliability reliability, Durability durability,
3552
boolean avoidROSNamespaceConventions) {
@@ -44,39 +61,127 @@ public final History getHistory() {
4461
return this.history;
4562
}
4663

64+
public final QoSProfile setHistory(final History history) {
65+
this.history = history;
66+
return this;
67+
}
68+
4769
public final int getDepth() {
4870
return this.depth;
4971
}
5072

73+
public final QoSProfile setDepth(final int depth) {
74+
this.depth = depth;
75+
return this;
76+
}
77+
5178
public final Reliability getReliability() {
5279
return this.reliability;
5380
}
5481

82+
public final QoSProfile setReliability(final Reliability reliability) {
83+
this.reliability = reliability;
84+
return this;
85+
}
86+
5587
public final Durability getDurability() {
5688
return this.durability;
5789
}
5890

91+
public final QoSProfile setDurability(final Durability durability) {
92+
this.durability = durability;
93+
return this;
94+
}
95+
96+
public final Duration getDeadline() {
97+
return this.deadline;
98+
}
99+
100+
public final QoSProfile setDeadline(final Duration deadline) {
101+
this.deadline = deadline;
102+
return this;
103+
}
104+
105+
public final Duration getLifespan() {
106+
return this.lifespan;
107+
}
108+
109+
public final QoSProfile setLifespan(final Duration lifespan) {
110+
this.lifespan = lifespan;
111+
return this;
112+
}
113+
114+
public final Liveliness getLiveliness() {
115+
return this.liveliness;
116+
}
117+
118+
public final QoSProfile setLiveliness(final Liveliness liveliness) {
119+
this.liveliness = liveliness;
120+
return this;
121+
}
122+
123+
public final Duration getLivelinessLeaseDuration() {
124+
return this.livelinessLeaseDuration;
125+
}
126+
127+
public final QoSProfile setLivelinessLeaseDuration(final Duration livelinessLeaseDuration) {
128+
this.livelinessLeaseDuration = livelinessLeaseDuration;
129+
return this;
130+
}
131+
59132
public final boolean getAvoidROSNamespaceConventions() {
60133
return this.avoidROSNamespaceConventions;
61134
}
62135

136+
public final QoSProfile setAvoidROSNamespaceConventions(final boolean avoidROSConventions) {
137+
this.avoidROSNamespaceConventions = avoidROSConventions;
138+
return this;
139+
}
140+
141+
// TODO(ivanpauno): refactor all static default profiles methods,
142+
// so that the return value is get from the rmw definition directly
143+
// (leveraging a native function).
144+
public static final QoSProfile defaultProfile() {
145+
return new QoSProfile(10);
146+
}
147+
148+
public static final QoSProfile systemDefault() {
149+
return new QoSProfile(
150+
History.SYSTEM_DEFAULT, QoSProfile.DEPTH_SYSTEM_DEFAULT,
151+
Reliability.SYSTEM_DEFAULT, Durability.SYSTEM_DEFAULT, false);
152+
}
153+
154+
public static final QoSProfile sensorData() {
155+
return new QoSProfile(
156+
History.KEEP_LAST, 5, Reliability.BEST_EFFORT, Durability.VOLATILE, false);
157+
}
158+
159+
public static final QoSProfile parametersDefault() {
160+
return new QoSProfile(
161+
History.KEEP_LAST, 1000, Reliability.RELIABLE, Durability.VOLATILE, false);
162+
}
163+
164+
public static final QoSProfile servicesDefault() {
165+
return new QoSProfile(
166+
History.KEEP_LAST, 10, Reliability.RELIABLE, Durability.VOLATILE, false);
167+
}
168+
169+
public static final QoSProfile parameterEventsDefault() {
170+
return new QoSProfile(
171+
History.KEEP_ALL, 1000, Reliability.RELIABLE, Durability.VOLATILE, false);
172+
}
173+
63174
public static final int DEPTH_SYSTEM_DEFAULT = 0;
64175

65-
public static final QoSProfile SENSOR_DATA =
66-
new QoSProfile(History.KEEP_LAST, 5, Reliability.BEST_EFFORT, Durability.VOLATILE, false);
176+
public static final QoSProfile SENSOR_DATA = sensorData();
67177

68-
public static final QoSProfile PARAMETERS =
69-
new QoSProfile(History.KEEP_LAST, 1000, Reliability.RELIABLE, Durability.VOLATILE, false);
178+
public static final QoSProfile PARAMETERS = parametersDefault();
70179

71-
public static final QoSProfile DEFAULT =
72-
new QoSProfile(History.KEEP_LAST, 10, Reliability.RELIABLE, Durability.VOLATILE, false);
180+
public static final QoSProfile DEFAULT = defaultProfile();
73181

74-
public static final QoSProfile SERVICES_DEFAULT =
75-
new QoSProfile(History.KEEP_LAST, 10, Reliability.RELIABLE, Durability.VOLATILE, false);
182+
public static final QoSProfile SERVICES_DEFAULT = servicesDefault();
76183

77-
public static final QoSProfile PARAMETER_EVENTS =
78-
new QoSProfile(History.KEEP_ALL, 1000, Reliability.RELIABLE, Durability.VOLATILE, false);
184+
public static final QoSProfile PARAMETER_EVENTS = parameterEventsDefault();
79185

80-
public static final QoSProfile SYSTEM_DEFAULT = new QoSProfile(History.SYSTEM_DEFAULT,
81-
DEPTH_SYSTEM_DEFAULT, Reliability.SYSTEM_DEFAULT, Durability.SYSTEM_DEFAULT, false);
186+
public static final QoSProfile SYSTEM_DEFAULT = systemDefault();
82187
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/* Copyright 2020 Open Source Robotics Foundation, Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
package org.ros2.rcljava.qos.policies;
17+
18+
public enum Liveliness implements QoSPolicy {
19+
SYSTEM_DEFAULT(0),
20+
AUTOMATIC(1),
21+
MANUAL_BY_TOPIC(3);
22+
23+
private final int value;
24+
25+
Liveliness(final int value) {
26+
this.value = value;
27+
}
28+
29+
public int getValue() {
30+
return value;
31+
}
32+
}

0 commit comments

Comments
 (0)