Skip to content

Commit e80c5df

Browse files
committed
Add offered qos incompatible event and event executor test (osrf#11)
Signed-off-by: Ivan Santiago Paunovic <ivanpauno@ekumenlabs.com>
1 parent b9e9fdd commit e80c5df

File tree

8 files changed

+368
-6
lines changed

8 files changed

+368
-6
lines changed

rcljava/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ set(${PROJECT_NAME}_jni_sources
6161
"src/main/cpp/org_ros2_rcljava_executors_BaseExecutor.cpp"
6262
"src/main/cpp/org_ros2_rcljava_events_EventHandlerImpl.cpp"
6363
"src/main/cpp/org_ros2_rcljava_events_publisher_statuses_LivelinessLost.cpp"
64+
"src/main/cpp/org_ros2_rcljava_events_publisher_statuses_OfferedQosIncompatible.cpp"
6465
"src/main/cpp/org_ros2_rcljava_node_NodeImpl.cpp"
6566
"src/main/cpp/org_ros2_rcljava_publisher_PublisherImpl.cpp"
6667
"src/main/cpp/org_ros2_rcljava_service_ServiceImpl.cpp"
@@ -133,6 +134,7 @@ set(${PROJECT_NAME}_sources
133134
"src/main/java/org/ros2/rcljava/events/PublisherEventStatus.java"
134135
"src/main/java/org/ros2/rcljava/events/SubscriptionEventStatus.java"
135136
"src/main/java/org/ros2/rcljava/events/publisher_statuses/LivelinessLost.java"
137+
"src/main/java/org/ros2/rcljava/events/publisher_statuses/OfferedQosIncompatible.java"
136138
"src/main/java/org/ros2/rcljava/executors/AnyExecutable.java"
137139
"src/main/java/org/ros2/rcljava/executors/BaseExecutor.java"
138140
"src/main/java/org/ros2/rcljava/executors/Executor.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_events_publisher_statuses_OfferedQosIncompatible */
17+
18+
#ifndef ORG_ROS2_RCLJAVA_EVENTS_PUBLISHER_STATUSES_OFFEREDQOSINCOMPATIBLE_H_
19+
#define ORG_ROS2_RCLJAVA_EVENTS_PUBLISHER_STATUSES_OFFEREDQOSINCOMPATIBLE_H_
20+
#ifdef __cplusplus
21+
extern "C" {
22+
#endif
23+
24+
/*
25+
* Class: org_ros2_rcljava_events_publisher_statuses_OfferedQosIncompatible
26+
* Method: nAllocateRCLStatusEvent
27+
* Signature: ()J
28+
*/
29+
JNIEXPORT jlong JNICALL
30+
Java_org_ros2_rcljava_events_publisher_1statuses_OfferedQosIncompatible_nAllocateRCLStatusEvent(
31+
JNIEnv *, jclass);
32+
33+
/*
34+
* Class: org_ros2_rcljava_events_publisher_statuses_OfferedQosIncompatible
35+
* Method: nDeallocateRCLStatusEvent
36+
* Signature: (J)V
37+
*/
38+
JNIEXPORT void JNICALL
39+
Java_org_ros2_rcljava_events_publisher_1statuses_OfferedQosIncompatible_nDeallocateRCLStatusEvent(
40+
JNIEnv *, jclass, jlong);
41+
42+
/*
43+
* Class: org_ros2_rcljava_events_publisher_statuses_OfferedQosIncompatible
44+
* Method: nFromRCLEvent
45+
* Signature: (J)V
46+
*/
47+
JNIEXPORT void JNICALL
48+
Java_org_ros2_rcljava_events_publisher_1statuses_OfferedQosIncompatible_nFromRCLEvent(
49+
JNIEnv *, jobject, jlong);
50+
51+
/*
52+
* Class: org_ros2_rcljava_events_publisher_statuses_OfferedQosIncompatible
53+
* Method: nGetPublisherEventType
54+
* Signature: ()I
55+
*/
56+
JNIEXPORT jint JNICALL
57+
Java_org_ros2_rcljava_events_publisher_1statuses_OfferedQosIncompatible_nGetPublisherEventType(
58+
JNIEnv *, jclass);
59+
60+
#ifdef __cplusplus
61+
}
62+
#endif
63+
#endif // ORG_ROS2_RCLJAVA_EVENTS_PUBLISHER_STATUSES_OFFEREDQOSINCOMPATIBLE_H_

rcljava/src/main/cpp/org_ros2_rcljava_events_publisher_statuses_LivelinessLost.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include <jni.h>
1818
#include <stdlib.h>
1919

20-
#include "rmw/events_statuses/liveliness_lost.h"
20+
#include "rmw/types.h"
2121
#include "rcl/event.h"
2222
#include "rcljava_common/exceptions.hpp"
2323

@@ -53,11 +53,11 @@ Java_org_ros2_rcljava_events_publisher_1statuses_LivelinessLost_nativeFromRCLEve
5353
}
5454
// TODO(ivanpauno): class and field lookup could be done at startup time
5555
jclass clazz = env->GetObjectClass(self);
56-
jfieldID total_count_fid = env->GetFieldID(clazz, "total_count", "I");
56+
jfieldID total_count_fid = env->GetFieldID(clazz, "totalCount", "I");
5757
if (env->ExceptionCheck()) {
5858
return;
5959
}
60-
jfieldID total_count_change_fid = env->GetFieldID(clazz, "total_count_change", "I");
60+
jfieldID total_count_change_fid = env->GetFieldID(clazz, "totalCountChange", "I");
6161
if (env->ExceptionCheck()) {
6262
return;
6363
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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_events_publisher_statuses_OfferedQosIncompatible.h"
16+
17+
#include <jni.h>
18+
#include <stdlib.h>
19+
20+
#include "rmw/incompatible_qos_events_statuses.h"
21+
#include "rmw/types.h"
22+
#include "rcl/event.h"
23+
#include "rcljava_common/exceptions.hpp"
24+
25+
using rcljava_common::exceptions::rcljava_throw_exception;
26+
27+
JNIEXPORT jlong JNICALL
28+
Java_org_ros2_rcljava_events_publisher_1statuses_OfferedQosIncompatible_nAllocateRCLStatusEvent(
29+
JNIEnv * env, jclass)
30+
{
31+
void * p = malloc(sizeof(rmw_offered_qos_incompatible_event_status_t));
32+
if (!p) {
33+
rcljava_throw_exception(
34+
env, "java/lang/OutOfMemoryError", "failed to allocate offered qos incompatible status");
35+
}
36+
return reinterpret_cast<jlong>(p);
37+
}
38+
39+
JNIEXPORT void JNICALL
40+
Java_org_ros2_rcljava_events_publisher_1statuses_OfferedQosIncompatible_nDeallocateRCLStatusEvent(
41+
JNIEnv *, jclass, jlong handle)
42+
{
43+
free(reinterpret_cast<void *>(handle));
44+
}
45+
46+
JNIEXPORT void JNICALL
47+
Java_org_ros2_rcljava_events_publisher_1statuses_OfferedQosIncompatible_nFromRCLEvent(
48+
JNIEnv * env, jobject self, jlong handle)
49+
{
50+
auto * p = reinterpret_cast<rmw_offered_qos_incompatible_event_status_t *>(handle);
51+
if (!p) {
52+
rcljava_throw_exception(
53+
env, "java/lang/IllegalArgumentException", "passed rmw object handle is NULL");
54+
}
55+
// TODO(ivanpauno): class and field lookup could be done at startup time
56+
jclass clazz = env->GetObjectClass(self);
57+
jclass qos_kind_clazz = env->FindClass(
58+
"org/ros2/rcljava/events/publisher_statuses/OfferedQosIncompatible$PolicyKind");
59+
if (env->ExceptionCheck()) {
60+
return;
61+
}
62+
jfieldID total_count_fid = env->GetFieldID(clazz, "totalCount", "I");
63+
if (env->ExceptionCheck()) {
64+
return;
65+
}
66+
jfieldID total_count_change_fid = env->GetFieldID(clazz, "totalCountChange", "I");
67+
if (env->ExceptionCheck()) {
68+
return;
69+
}
70+
const char * enum_class_path =
71+
"Lorg/ros2/rcljava/events/publisher_statuses/OfferedQosIncompatible$PolicyKind;";
72+
jfieldID policy_kind_fid = env->GetFieldID(clazz, "lastPolicyKind", enum_class_path);
73+
if (env->ExceptionCheck()) {
74+
return;
75+
}
76+
77+
jfieldID enum_value_fid;
78+
switch (p->last_policy_kind) {
79+
case RMW_QOS_POLICY_INVALID:
80+
enum_value_fid = env->GetStaticFieldID(qos_kind_clazz, "INVALID", enum_class_path);
81+
break;
82+
case RMW_QOS_POLICY_DURABILITY:
83+
enum_value_fid = env->GetStaticFieldID(qos_kind_clazz, "DURABILITY", enum_class_path);
84+
break;
85+
case RMW_QOS_POLICY_DEADLINE:
86+
enum_value_fid = env->GetStaticFieldID(qos_kind_clazz, "DEADLINE", enum_class_path);
87+
break;
88+
case RMW_QOS_POLICY_LIVELINESS:
89+
enum_value_fid = env->GetStaticFieldID(qos_kind_clazz, "LIVELINESS", enum_class_path);
90+
break;
91+
case RMW_QOS_POLICY_RELIABILITY:
92+
enum_value_fid = env->GetStaticFieldID(qos_kind_clazz, "RELIABILITY", enum_class_path);
93+
break;
94+
case RMW_QOS_POLICY_HISTORY:
95+
enum_value_fid = env->GetStaticFieldID(qos_kind_clazz, "HISTORY", enum_class_path);
96+
break;
97+
case RMW_QOS_POLICY_LIFESPAN:
98+
enum_value_fid = env->GetStaticFieldID(qos_kind_clazz, "LIFESPAN", enum_class_path);
99+
break;
100+
default:
101+
rcljava_throw_exception(
102+
env, "java/lang/IllegalStateException", "unknown rmw qos policy kind");
103+
break;
104+
}
105+
if (env->ExceptionCheck()) {
106+
return;
107+
}
108+
jobject enum_value = env->GetStaticObjectField(qos_kind_clazz, enum_value_fid);
109+
110+
env->SetIntField(self, total_count_fid, p->total_count);
111+
env->SetIntField(self, total_count_change_fid, p->total_count_change);
112+
env->SetObjectField(self, policy_kind_fid, enum_value);
113+
}
114+
115+
JNIEXPORT jint JNICALL
116+
Java_org_ros2_rcljava_events_publisher_1statuses_OfferedQosIncompatible_nGetPublisherEventType(
117+
JNIEnv *, jclass)
118+
{
119+
return RCL_PUBLISHER_OFFERED_INCOMPATIBLE_QOS;
120+
}

rcljava/src/main/java/org/ros2/rcljava/events/publisher_statuses/LivelinessLost.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
* This class serves as a bridge between a rmw_liveliness_lost_status_t and RCLJava.
2727
*/
2828
public class LivelinessLost implements PublisherEventStatus {
29-
int total_count;
30-
int total_count_change;
29+
public int totalCount;
30+
public int totalCountChange;
3131

3232
public final long allocateRCLStatusEvent() {
3333
return nativeAllocateRCLStatusEvent();
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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.events.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_qos_incompatible_event_status_t and RCLJava.
27+
*/
28+
public class OfferedQosIncompatible implements PublisherEventStatus {
29+
public int totalCount;
30+
public int totalCountChange;
31+
public PolicyKind lastPolicyKind;
32+
33+
public enum PolicyKind {
34+
INVALID,
35+
DURABILITY,
36+
DEADLINE,
37+
LIVELINESS,
38+
RELIABILITY,
39+
HISTORY,
40+
LIFESPAN;
41+
}
42+
43+
public final long allocateRCLStatusEvent() {
44+
return nAllocateRCLStatusEvent();
45+
}
46+
public final void deallocateRCLStatusEvent(long handle) {
47+
nDeallocateRCLStatusEvent(handle);
48+
}
49+
public final void fromRCLEvent(long handle) {
50+
nFromRCLEvent(handle);
51+
}
52+
public final int getPublisherEventType() {
53+
return nGetPublisherEventType();
54+
}
55+
// TODO(ivanpauno): Remove this when -source 8 can be used (method references for the win)
56+
public static final Supplier<OfferedQosIncompatible> factory = new Supplier<OfferedQosIncompatible>() {
57+
public OfferedQosIncompatible get() {
58+
return new OfferedQosIncompatible();
59+
}
60+
};
61+
62+
private static final Logger logger = LoggerFactory.getLogger(OfferedQosIncompatible.class);
63+
static {
64+
try {
65+
JNIUtils.loadImplementation(OfferedQosIncompatible.class);
66+
} catch (UnsatisfiedLinkError ule) {
67+
logger.error("Native code library failed to load.\n" + ule);
68+
System.exit(1);
69+
}
70+
}
71+
72+
private static native long nAllocateRCLStatusEvent();
73+
private static native void nDeallocateRCLStatusEvent(long handle);
74+
private native void nFromRCLEvent(long handle);
75+
private static native int nGetPublisherEventType();
76+
}

0 commit comments

Comments
 (0)