Skip to content

Commit d72d1cd

Browse files
committed
Add RequestedDeadlineMissed subscription event (osrf#19)
Signed-off-by: Ivan Santiago Paunovic <ivanpauno@ekumenlabs.com>
1 parent 48d2be8 commit d72d1cd

File tree

5 files changed

+228
-0
lines changed

5 files changed

+228
-0
lines changed

rcljava/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ set(${PROJECT_NAME}_jni_sources
6868
"src/main/cpp/org_ros2_rcljava_publisher_PublisherImpl.cpp"
6969
"src/main/cpp/org_ros2_rcljava_service_ServiceImpl.cpp"
7070
"src/main/cpp/org_ros2_rcljava_subscription_SubscriptionImpl.cpp"
71+
"src/main/cpp/org_ros2_rcljava_subscription_statuses_RequestedDeadlineMissed.cpp"
7172
"src/main/cpp/org_ros2_rcljava_subscription_statuses_RequestedQosIncompatible.cpp"
7273
"src/main/cpp/org_ros2_rcljava_time_Clock.cpp"
7374
"src/main/cpp/org_ros2_rcljava_timer_WallTimerImpl.cpp"
@@ -177,6 +178,7 @@ set(${PROJECT_NAME}_sources
177178
"src/main/java/org/ros2/rcljava/service/ServiceImpl.java"
178179
"src/main/java/org/ros2/rcljava/subscription/Subscription.java"
179180
"src/main/java/org/ros2/rcljava/subscription/SubscriptionImpl.java"
181+
"src/main/java/org/ros2/rcljava/subscription/statuses/RequestedDeadlineMissed.java"
180182
"src/main/java/org/ros2/rcljava/subscription/statuses/RequestedQosIncompatible.java"
181183
"src/main/java/org/ros2/rcljava/time/Clock.java"
182184
"src/main/java/org/ros2/rcljava/time/ClockType.java"
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
#include <jni.h>
16+
/* Header for class org_ros2_rcljava_subscription_statuses_RequestedDeadlineMissed */
17+
18+
#ifndef ORG_ROS2_RCLJAVA_SUBSCRIPTION_STATUSES_REQUESTEDDEADLINEMISSED_H_
19+
#define ORG_ROS2_RCLJAVA_SUBSCRIPTION_STATUSES_REQUESTEDDEADLINEMISSED_H_
20+
#ifdef __cplusplus
21+
extern "C" {
22+
#endif
23+
24+
/*
25+
* Class: org_ros2_rcljava_subscription_statuses_RequestedDeadlineMissed
26+
* Method: nativeAllocateRCLStatusEvent
27+
* Signature: ()J
28+
*/
29+
JNIEXPORT jlong JNICALL
30+
Java_org_ros2_rcljava_subscription_statuses_RequestedDeadlineMissed_nativeAllocateRCL(
31+
JNIEnv *, jclass);
32+
33+
/*
34+
* Class: org_ros2_rcljava_subscription_statuses_RequestedDeadlineMissed
35+
* Method: nativeDeallocateRCLStatusEvent
36+
* Signature: (J)V
37+
*/
38+
JNIEXPORT void JNICALL
39+
Java_org_ros2_rcljava_subscription_statuses_RequestedDeadlineMissed_nativeDeallocateRCL(
40+
JNIEnv *, jclass, jlong);
41+
42+
/*
43+
* Class: org_ros2_rcljava_subscription_statuses_RequestedDeadlineMissed
44+
* Method: nativeFromRCLEvent
45+
* Signature: (J)V
46+
*/
47+
JNIEXPORT void JNICALL
48+
Java_org_ros2_rcljava_subscription_statuses_RequestedDeadlineMissed_nativeFromRCL(
49+
JNIEnv *, jobject, jlong);
50+
51+
/*
52+
* Class: org_ros2_rcljava_subscription_statuses_RequestedDeadlineMissed
53+
* Method: nativeGetSubscriptionEventType
54+
* Signature: ()I
55+
*/
56+
JNIEXPORT jint JNICALL
57+
Java_org_ros2_rcljava_subscription_statuses_RequestedDeadlineMissed_nativeGetEventType(
58+
JNIEnv *, jclass);
59+
60+
#ifdef __cplusplus
61+
}
62+
#endif
63+
#endif // ORG_ROS2_RCLJAVA_SUBSCRIPTION_STATUSES_REQUESTEDDEADLINEMISSED_H_
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
#include "org_ros2_rcljava_subscription_statuses_RequestedDeadlineMissed.h"
16+
17+
#include <jni.h>
18+
#include <stdlib.h>
19+
20+
#include "rmw/types.h"
21+
#include "rcl/event.h"
22+
#include "rcljava_common/exceptions.hpp"
23+
24+
using rcljava_common::exceptions::rcljava_throw_exception;
25+
26+
JNIEXPORT jlong JNICALL
27+
Java_org_ros2_rcljava_subscription_statuses_RequestedDeadlineMissed_nativeAllocateRCL(
28+
JNIEnv * env, jclass)
29+
{
30+
void * p = malloc(sizeof(rmw_requested_deadline_missed_status_t));
31+
if (!p) {
32+
rcljava_throw_exception(
33+
env, "java/lang/OutOfMemoryError", "failed to allocate missed deadline status");
34+
}
35+
return reinterpret_cast<jlong>(p);
36+
}
37+
38+
JNIEXPORT void JNICALL
39+
Java_org_ros2_rcljava_subscription_statuses_RequestedDeadlineMissed_nativeDeallocateRCL(
40+
JNIEnv *, jclass, jlong handle)
41+
{
42+
free(reinterpret_cast<void *>(handle));
43+
}
44+
45+
JNIEXPORT void JNICALL
46+
Java_org_ros2_rcljava_subscription_statuses_RequestedDeadlineMissed_nativeFromRCL(
47+
JNIEnv * env, jobject self, jlong handle)
48+
{
49+
auto * p = reinterpret_cast<rmw_requested_deadline_missed_status_t *>(handle);
50+
if (!p) {
51+
rcljava_throw_exception(
52+
env, "java/lang/IllegalArgumentException", "passed rmw object handle is NULL");
53+
}
54+
// TODO(ivanpauno): class and field lookup could be done at startup time
55+
jclass clazz = env->GetObjectClass(self);
56+
jfieldID total_count_fid = env->GetFieldID(clazz, "totalCount", "I");
57+
if (env->ExceptionCheck()) {
58+
return;
59+
}
60+
jfieldID total_count_change_fid = env->GetFieldID(clazz, "totalCountChange", "I");
61+
if (env->ExceptionCheck()) {
62+
return;
63+
}
64+
env->SetIntField(self, total_count_fid, p->total_count);
65+
env->SetIntField(self, total_count_change_fid, p->total_count_change);
66+
}
67+
68+
JNIEXPORT jint JNICALL
69+
Java_org_ros2_rcljava_subscription_statuses_RequestedDeadlineMissed_nativeGetEventType(
70+
JNIEnv *, jclass)
71+
{
72+
return RCL_SUBSCRIPTION_REQUESTED_DEADLINE_MISSED;
73+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
package org.ros2.rcljava.subscription.statuses;
16+
17+
import java.util.function.Supplier;
18+
19+
import org.ros2.rcljava.common.JNIUtils;
20+
import org.ros2.rcljava.events.SubscriptionEventStatus;
21+
22+
import org.slf4j.Logger;
23+
import org.slf4j.LoggerFactory;
24+
25+
/**
26+
* This class serves as a bridge between a rmw_requested_deadline_missed_status_t and RCLJava.
27+
*/
28+
public class RequestedDeadlineMissed implements SubscriptionEventStatus {
29+
public int totalCount;
30+
public int totalCountChange;
31+
32+
public final long allocateRCLStatusEvent() {
33+
return nativeAllocateRCL();
34+
}
35+
public final void deallocateRCLStatusEvent(long handle) {
36+
nativeDeallocateRCL(handle);
37+
}
38+
public final void fromRCLEvent(long handle) {
39+
nativeFromRCL(handle);
40+
}
41+
public final int getSubscriptionEventType() {
42+
return nativeGetEventType();
43+
}
44+
// TODO(ivanpauno): Remove this when -source 8 can be used (method references for the win)
45+
public static final Supplier<RequestedDeadlineMissed> factory = new Supplier<RequestedDeadlineMissed>() {
46+
public RequestedDeadlineMissed get() {
47+
return new RequestedDeadlineMissed();
48+
}
49+
};
50+
51+
private static final Logger logger = LoggerFactory.getLogger(RequestedDeadlineMissed.class);
52+
static {
53+
try {
54+
JNIUtils.loadImplementation(RequestedDeadlineMissed.class);
55+
} catch (UnsatisfiedLinkError ule) {
56+
logger.error("Native code library failed to load.\n" + ule);
57+
System.exit(1);
58+
}
59+
}
60+
61+
private static native long nativeAllocateRCL();
62+
private static native void nativeDeallocateRCL(long handle);
63+
private native void nativeFromRCL(long handle);
64+
private static native int nativeGetEventType();
65+
}

rcljava/src/test/java/org/ros2/rcljava/subscription/SubscriptionTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.ros2.rcljava.consumers.Consumer;
2828
import org.ros2.rcljava.events.EventHandler;
2929
import org.ros2.rcljava.node.Node;
30+
import org.ros2.rcljava.subscription.statuses.RequestedDeadlineMissed;
3031
import org.ros2.rcljava.subscription.statuses.RequestedQosIncompatible;
3132

3233
public class SubscriptionTest {
@@ -69,6 +70,30 @@ public void accept(final std_msgs.msg.String msg) {}
6970
RCLJava.shutdown();
7071
}
7172

73+
@Test
74+
public final void testCreateRequestedDeadlineMissedEvent() {
75+
String identifier = RCLJava.getRMWIdentifier();
76+
RCLJava.rclJavaInit();
77+
Node node = RCLJava.createNode("test_node");
78+
Subscription<std_msgs.msg.String> subscription = node.<std_msgs.msg.String>createSubscription(
79+
std_msgs.msg.String.class, "test_topic", new Consumer<std_msgs.msg.String>() {
80+
public void accept(final std_msgs.msg.String msg) {}
81+
});
82+
EventHandler eventHandler = subscription.createEventHandler(
83+
RequestedDeadlineMissed.factory, new Consumer<RequestedDeadlineMissed>() {
84+
public void accept(final RequestedDeadlineMissed status) {
85+
assertEquals(status.totalCount, 0);
86+
assertEquals(status.totalCountChange, 0);
87+
}
88+
}
89+
);
90+
assertNotEquals(0, eventHandler.getHandle());
91+
// force executing the callback, so we check that taking an event works
92+
eventHandler.executeCallback();
93+
RCLJava.shutdown();
94+
assertEquals(0, eventHandler.getHandle());
95+
}
96+
7297
@Test
7398
public final void testCreateRequestedQosIncompatibleEvent() {
7499
String identifier = RCLJava.getRMWIdentifier();

0 commit comments

Comments
 (0)