Skip to content

Commit 085e469

Browse files
committed
Add OfferedDeadlineMissed QoS event (osrf#17)
Signed-off-by: Ivan Santiago Paunovic <ivanpauno@ekumenlabs.com>
1 parent a50c76a commit 085e469

File tree

5 files changed

+225
-0
lines changed

5 files changed

+225
-0
lines changed

rcljava/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ set(${PROJECT_NAME}_jni_sources
6262
"src/main/cpp/org_ros2_rcljava_executors_BaseExecutor.cpp"
6363
"src/main/cpp/org_ros2_rcljava_events_EventHandlerImpl.cpp"
6464
"src/main/cpp/org_ros2_rcljava_publisher_statuses_LivelinessLost.cpp"
65+
"src/main/cpp/org_ros2_rcljava_publisher_statuses_OfferedDeadlineMissed.cpp"
6566
"src/main/cpp/org_ros2_rcljava_publisher_statuses_OfferedQosIncompatible.cpp"
6667
"src/main/cpp/org_ros2_rcljava_node_NodeImpl.cpp"
6768
"src/main/cpp/org_ros2_rcljava_publisher_PublisherImpl.cpp"
@@ -137,6 +138,7 @@ set(${PROJECT_NAME}_sources
137138
"src/main/java/org/ros2/rcljava/events/PublisherEventStatus.java"
138139
"src/main/java/org/ros2/rcljava/events/SubscriptionEventStatus.java"
139140
"src/main/java/org/ros2/rcljava/publisher/statuses/LivelinessLost.java"
141+
"src/main/java/org/ros2/rcljava/publisher/statuses/OfferedDeadlineMissed.java"
140142
"src/main/java/org/ros2/rcljava/publisher/statuses/OfferedQosIncompatible.java"
141143
"src/main/java/org/ros2/rcljava/executors/AnyExecutable.java"
142144
"src/main/java/org/ros2/rcljava/executors/BaseExecutor.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_publisher_statuses_OfferedDeadlineMissed */
17+
18+
#ifndef ORG_ROS2_RCLJAVA_PUBLISHER_STATUSES_OFFEREDDEADLINEMISSED_H_
19+
#define ORG_ROS2_RCLJAVA_PUBLISHER_STATUSES_OFFEREDDEADLINEMISSED_H_
20+
#ifdef __cplusplus
21+
extern "C" {
22+
#endif
23+
24+
/*
25+
* Class: org_ros2_rcljava_publisher_statuses_OfferedDeadlineMissed
26+
* Method: nativeAllocateRCLStatusEvent
27+
* Signature: ()J
28+
*/
29+
JNIEXPORT jlong JNICALL
30+
Java_org_ros2_rcljava_publisher_statuses_OfferedDeadlineMissed_nativeAllocateRCLStatusEvent(
31+
JNIEnv *, jclass);
32+
33+
/*
34+
* Class: org_ros2_rcljava_publisher_statuses_OfferedDeadlineMissed
35+
* Method: nativeDeallocateRCLStatusEvent
36+
* Signature: (J)V
37+
*/
38+
JNIEXPORT void JNICALL
39+
Java_org_ros2_rcljava_publisher_statuses_OfferedDeadlineMissed_nativeDeallocateRCLStatusEvent(
40+
JNIEnv *, jclass, jlong);
41+
42+
/*
43+
* Class: org_ros2_rcljava_publisher_statuses_OfferedDeadlineMissed
44+
* Method: nativeFromRCLEvent
45+
* Signature: (J)V
46+
*/
47+
JNIEXPORT void JNICALL
48+
Java_org_ros2_rcljava_publisher_statuses_OfferedDeadlineMissed_nativeFromRCLEvent(
49+
JNIEnv *, jobject, jlong);
50+
51+
/*
52+
* Class: org_ros2_rcljava_publisher_statuses_OfferedDeadlineMissed
53+
* Method: nativeGetPublisherEventType
54+
* Signature: ()I
55+
*/
56+
JNIEXPORT jint JNICALL
57+
Java_org_ros2_rcljava_publisher_statuses_OfferedDeadlineMissed_nativeGetPublisherEventType(
58+
JNIEnv *, jclass);
59+
60+
#ifdef __cplusplus
61+
}
62+
#endif
63+
#endif // ORG_ROS2_RCLJAVA_PUBLISHER_STATUSES_OFFEREDDEADLINEMISSED_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_publisher_statuses_OfferedDeadlineMissed.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_publisher_statuses_OfferedDeadlineMissed_nativeAllocateRCLStatusEvent(
28+
JNIEnv * env, jclass)
29+
{
30+
void * p = malloc(sizeof(rmw_offered_deadline_missed_status_t));
31+
if (!p) {
32+
rcljava_throw_exception(
33+
env, "java/lang/OutOfMemoryError", "failed to allocate deadline missed status");
34+
}
35+
return reinterpret_cast<jlong>(p);
36+
}
37+
38+
JNIEXPORT void JNICALL
39+
Java_org_ros2_rcljava_publisher_statuses_OfferedDeadlineMissed_nativeDeallocateRCLStatusEvent(
40+
JNIEnv *, jclass, jlong handle)
41+
{
42+
free(reinterpret_cast<void *>(handle));
43+
}
44+
45+
JNIEXPORT void JNICALL
46+
Java_org_ros2_rcljava_publisher_statuses_OfferedDeadlineMissed_nativeFromRCLEvent(
47+
JNIEnv * env, jobject self, jlong handle)
48+
{
49+
auto * p = reinterpret_cast<rmw_offered_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_publisher_statuses_OfferedDeadlineMissed_nativeGetPublisherEventType(
70+
JNIEnv *, jclass)
71+
{
72+
return RCL_PUBLISHER_OFFERED_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.publisher.statuses;
16+
17+
import java.util.function.Supplier;
18+
19+
import org.ros2.rcljava.common.JNIUtils;
20+
import org.ros2.rcljava.events.PublisherEventStatus;
21+
22+
import org.slf4j.Logger;
23+
import org.slf4j.LoggerFactory;
24+
25+
/**
26+
* This class serves as a bridge between a rmw_offered_deadline_missed_status_t and RCLJava.
27+
*/
28+
public class OfferedDeadlineMissed implements PublisherEventStatus {
29+
public int totalCount;
30+
public int totalCountChange;
31+
32+
public final long allocateRCLStatusEvent() {
33+
return nativeAllocateRCLStatusEvent();
34+
}
35+
public final void deallocateRCLStatusEvent(long handle) {
36+
nativeDeallocateRCLStatusEvent(handle);
37+
}
38+
public final void fromRCLEvent(long handle) {
39+
nativeFromRCLEvent(handle);
40+
}
41+
public final int getPublisherEventType() {
42+
return nativeGetPublisherEventType();
43+
}
44+
// TODO(ivanpauno): Remove this when -source 8 can be used (method references for the win)
45+
public static final Supplier<OfferedDeadlineMissed> factory = new Supplier<OfferedDeadlineMissed>() {
46+
public OfferedDeadlineMissed get() {
47+
return new OfferedDeadlineMissed();
48+
}
49+
};
50+
51+
private static final Logger logger = LoggerFactory.getLogger(OfferedDeadlineMissed.class);
52+
static {
53+
try {
54+
JNIUtils.loadImplementation(OfferedDeadlineMissed.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 nativeAllocateRCLStatusEvent();
62+
private static native void nativeDeallocateRCLStatusEvent(long handle);
63+
private native void nativeFromRCLEvent(long handle);
64+
private static native int nativeGetPublisherEventType();
65+
}

rcljava/src/test/java/org/ros2/rcljava/publisher/PublisherTest.java

Lines changed: 22 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.publisher.statuses.LivelinessLost;
30+
import org.ros2.rcljava.publisher.statuses.OfferedDeadlineMissed;
3031
import org.ros2.rcljava.publisher.statuses.OfferedQosIncompatible;
3132
import org.ros2.rcljava.exceptions.RCLException;
3233
import org.ros2.rcljava.node.Node;
@@ -116,4 +117,25 @@ public void accept(final OfferedQosIncompatible status) {
116117
RCLJava.shutdown();
117118
assertEquals(0, eventHandler.getHandle());
118119
}
120+
121+
@Test
122+
public final void testCreateOfferedDeadlineMissedEvent() {
123+
RCLJava.rclJavaInit();
124+
Node node = RCLJava.createNode("test_node");
125+
Publisher<std_msgs.msg.String> publisher =
126+
node.<std_msgs.msg.String>createPublisher(std_msgs.msg.String.class, "test_topic");
127+
EventHandler eventHandler = publisher.createEventHandler(
128+
OfferedDeadlineMissed.factory, new Consumer<OfferedDeadlineMissed>() {
129+
public void accept(final OfferedDeadlineMissed status) {
130+
assertEquals(status.totalCount, 0);
131+
assertEquals(status.totalCountChange, 0);
132+
}
133+
}
134+
);
135+
assertNotEquals(0, eventHandler.getHandle());
136+
// force executing the callback, so we check that taking an event works
137+
eventHandler.executeCallback();
138+
RCLJava.shutdown();
139+
assertEquals(0, eventHandler.getHandle());
140+
}
119141
}

0 commit comments

Comments
 (0)