From 0a7b88ac8f3f764f76f68a787c821e772ab15af2 Mon Sep 17 00:00:00 2001 From: Ivan Santiago Paunovic Date: Wed, 2 Sep 2020 14:55:55 -0300 Subject: [PATCH 01/11] Add MessageLost subscription event status Signed-off-by: Ivan Santiago Paunovic --- rcljava/CMakeLists.txt | 2 + ...cljava_subscription_statuses_MessageLost.h | 63 ++++++++++++++++ ...java_subscription_statuses_MessageLost.cpp | 74 +++++++++++++++++++ .../subscription/statuses/MessageLost.java | 65 ++++++++++++++++ .../subscription/SubscriptionTest.java | 29 ++++++++ 5 files changed, 233 insertions(+) create mode 100644 rcljava/include/org_ros2_rcljava_subscription_statuses_MessageLost.h create mode 100644 rcljava/src/main/cpp/org_ros2_rcljava_subscription_statuses_MessageLost.cpp create mode 100644 rcljava/src/main/java/org/ros2/rcljava/subscription/statuses/MessageLost.java diff --git a/rcljava/CMakeLists.txt b/rcljava/CMakeLists.txt index dbd3d649..c454fab7 100644 --- a/rcljava/CMakeLists.txt +++ b/rcljava/CMakeLists.txt @@ -69,6 +69,7 @@ set(${PROJECT_NAME}_jni_sources "src/main/cpp/org_ros2_rcljava_service_ServiceImpl.cpp" "src/main/cpp/org_ros2_rcljava_subscription_SubscriptionImpl.cpp" "src/main/cpp/org_ros2_rcljava_subscription_statuses_LivelinessChanged.cpp" + "src/main/cpp/org_ros2_rcljava_subscription_statuses_MessageLost.cpp" "src/main/cpp/org_ros2_rcljava_subscription_statuses_RequestedDeadlineMissed.cpp" "src/main/cpp/org_ros2_rcljava_subscription_statuses_RequestedQosIncompatible.cpp" "src/main/cpp/org_ros2_rcljava_time_Clock.cpp" @@ -180,6 +181,7 @@ set(${PROJECT_NAME}_sources "src/main/java/org/ros2/rcljava/subscription/Subscription.java" "src/main/java/org/ros2/rcljava/subscription/SubscriptionImpl.java" "src/main/java/org/ros2/rcljava/subscription/statuses/LivelinessChanged.java" + "src/main/java/org/ros2/rcljava/subscription/statuses/MessageLost.java" "src/main/java/org/ros2/rcljava/subscription/statuses/RequestedDeadlineMissed.java" "src/main/java/org/ros2/rcljava/subscription/statuses/RequestedQosIncompatible.java" "src/main/java/org/ros2/rcljava/time/Clock.java" diff --git a/rcljava/include/org_ros2_rcljava_subscription_statuses_MessageLost.h b/rcljava/include/org_ros2_rcljava_subscription_statuses_MessageLost.h new file mode 100644 index 00000000..3139afa9 --- /dev/null +++ b/rcljava/include/org_ros2_rcljava_subscription_statuses_MessageLost.h @@ -0,0 +1,63 @@ +// Copyright 2020 Open Source Robotics Foundation, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +/* Header for class org_ros2_rcljava_subscription_statuses_MessageLost */ + +#ifndef ORG_ROS2_RCLJAVA_SUBSCRIPTION_STATUSES_MESSAGELOST_H_ +#define ORG_ROS2_RCLJAVA_SUBSCRIPTION_STATUSES_MESSAGELOST_H_ +#ifdef __cplusplus +extern "C" { +#endif + +/* + * Class: org_ros2_rcljava_subscription_statuses_MessageLost + * Method: nativeAllocateRCLStatusEvent + * Signature: ()J + */ +JNIEXPORT jlong JNICALL +Java_org_ros2_rcljava_subscription_statuses_MessageLost_nativeAllocateRCLStatusEvent( + JNIEnv *, jclass); + +/* + * Class: org_ros2_rcljava_subscription_statuses_MessageLost + * Method: nativeDeallocateRCLStatusEvent + * Signature: (J)V + */ +JNIEXPORT void JNICALL +Java_org_ros2_rcljava_subscription_statuses_MessageLost_nativeDeallocateRCLStatusEvent( + JNIEnv *, jclass, jlong); + +/* + * Class: org_ros2_rcljava_subscription_statuses_MessageLost + * Method: nativeFromRCLEvent + * Signature: (J)V + */ +JNIEXPORT void JNICALL +Java_org_ros2_rcljava_subscription_statuses_MessageLost_nativeFromRCLEvent( + JNIEnv *, jobject, jlong); + +/* + * Class: org_ros2_rcljava_subscription_statuses_MessageLost + * Method: nativeGetsubscriptionEventType + * Signature: ()I + */ +JNIEXPORT jint JNICALL +Java_org_ros2_rcljava_subscription_statuses_MessageLost_nativeGetSubscriptionEventType( + JNIEnv *, jclass); + +#ifdef __cplusplus +} +#endif +#endif // ORG_ROS2_RCLJAVA_SUBSCRIPTION_STATUSES_MESSAGELOST_H_ diff --git a/rcljava/src/main/cpp/org_ros2_rcljava_subscription_statuses_MessageLost.cpp b/rcljava/src/main/cpp/org_ros2_rcljava_subscription_statuses_MessageLost.cpp new file mode 100644 index 00000000..d2f935d5 --- /dev/null +++ b/rcljava/src/main/cpp/org_ros2_rcljava_subscription_statuses_MessageLost.cpp @@ -0,0 +1,74 @@ +// Copyright 2020 Open Source Robotics Foundation, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "org_ros2_rcljava_subscription_statuses_MessageLost.h" + +#include +#include + +#include "rmw/types.h" +#include "rcl/event.h" +#include "rcljava_common/exceptions.hpp" + +using rcljava_common::exceptions::rcljava_throw_exception; + +JNIEXPORT jlong JNICALL +Java_org_ros2_rcljava_subscription_statuses_MessageLost_nativeAllocateRCL( + JNIEnv * env, jclass) +{ + void * p = malloc(sizeof(rmw_message_lost_status_t)); + if (!p) { + rcljava_throw_exception( + env, "java/lang/OutOfMemoryError", "failed to allocate missed deadline status"); + } + return reinterpret_cast(p); +} + +JNIEXPORT void JNICALL +Java_org_ros2_rcljava_subscription_statuses_MessageLost_nativeDeallocateRCL( + JNIEnv *, jclass, jlong handle) +{ + free(reinterpret_cast(handle)); +} + +JNIEXPORT void JNICALL +Java_org_ros2_rcljava_subscription_statuses_MessageLost_nativeFromRCL( + JNIEnv * env, jobject self, jlong handle) +{ + auto * p = reinterpret_cast(handle); + if (!p) { + rcljava_throw_exception( + env, "java/lang/IllegalArgumentException", "passed rmw object handle is NULL"); + return; + } + // TODO(ivanpauno): class and field lookup could be done at startup time + jclass clazz = env->GetObjectClass(self); + jfieldID total_count_fid = env->GetFieldID(clazz, "totalCount", "I"); + if (env->ExceptionCheck()) { + return; + } + jfieldID total_count_change_fid = env->GetFieldID(clazz, "totalCountChange", "I"); + if (env->ExceptionCheck()) { + return; + } + env->SetIntField(self, total_count_fid, p->total_count); + env->SetIntField(self, total_count_change_fid, p->total_count_change); +} + +JNIEXPORT jint JNICALL +Java_org_ros2_rcljava_subscription_statuses_MessageLost_nativeGetEventType( + JNIEnv *, jclass) +{ + return RCL_SUBSCRIPTION_MESSAGE_LOST; +} diff --git a/rcljava/src/main/java/org/ros2/rcljava/subscription/statuses/MessageLost.java b/rcljava/src/main/java/org/ros2/rcljava/subscription/statuses/MessageLost.java new file mode 100644 index 00000000..d136b4df --- /dev/null +++ b/rcljava/src/main/java/org/ros2/rcljava/subscription/statuses/MessageLost.java @@ -0,0 +1,65 @@ +// Copyright 2020 Open Source Robotics Foundation, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package org.ros2.rcljava.subscription.statuses; + +import java.util.function.Supplier; + +import org.ros2.rcljava.common.JNIUtils; +import org.ros2.rcljava.events.SubscriptionEventStatus; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class serves as a bridge between a rmw_message_lost_status_t and RCLJava. + */ +public class MessageLost implements SubscriptionEventStatus { + public int totalCount; + public int totalCountChange; + + public final long allocateRCLStatusEvent() { + return nativeAllocateRCL(); + } + public final void deallocateRCLStatusEvent(long handle) { + nativeDeallocateRCL(handle); + } + public final void fromRCLEvent(long handle) { + nativeFromRCL(handle); + } + public final int getSubscriptionEventType() { + return nativeGetEventType(); + } + // TODO(ivanpauno): Remove this when -source 8 can be used (method references for the win) + public static final Supplier factory = new Supplier() { + public MessageLost get() { + return new MessageLost(); + } + }; + + private static final Logger logger = LoggerFactory.getLogger(MessageLost.class); + static { + try { + JNIUtils.loadImplementation(MessageLost.class); + } catch (UnsatisfiedLinkError ule) { + logger.error("Native code library failed to load.\n" + ule); + System.exit(1); + } + } + + private static native long nativeAllocateRCL(); + private static native void nativeDeallocateRCL(long handle); + private native void nativeFromRCL(long handle); + private static native int nativeGetEventType(); +} diff --git a/rcljava/src/test/java/org/ros2/rcljava/subscription/SubscriptionTest.java b/rcljava/src/test/java/org/ros2/rcljava/subscription/SubscriptionTest.java index 21299024..59976add 100644 --- a/rcljava/src/test/java/org/ros2/rcljava/subscription/SubscriptionTest.java +++ b/rcljava/src/test/java/org/ros2/rcljava/subscription/SubscriptionTest.java @@ -28,6 +28,7 @@ import org.ros2.rcljava.events.EventHandler; import org.ros2.rcljava.node.Node; import org.ros2.rcljava.subscription.statuses.LivelinessChanged; +import org.ros2.rcljava.subscription.statuses.MessageLost; import org.ros2.rcljava.subscription.statuses.RequestedDeadlineMissed; import org.ros2.rcljava.subscription.statuses.RequestedQosIncompatible; @@ -149,4 +150,32 @@ public void accept(final RequestedQosIncompatible status) { RCLJava.shutdown(); assertEquals(0, eventHandler.getHandle()); } + + @Test + public final void testCreateMessageLost() { + String identifier = RCLJava.getRMWIdentifier(); + if (identifier.equals("rmw_fastrtps_cpp") || identifier.equals("rmw_fastrtps_dynamic_cpp")) { + // event not supported in these implementations + return; + } + RCLJava.rclJavaInit(); + Node node = RCLJava.createNode("test_node"); + Subscription subscription = node.createSubscription( + std_msgs.msg.String.class, "test_topic", new Consumer() { + public void accept(final std_msgs.msg.String msg) {} + }); + EventHandler eventHandler = subscription.createEventHandler( + MessageLost.factory, new Consumer() { + public void accept(final MessageLost status) { + assertEquals(status.totalCount, 0); + assertEquals(status.totalCountChange, 0); + } + } + ); + assertNotEquals(0, eventHandler.getHandle()); + // force executing the callback, so we check that taking an event works + eventHandler.executeCallback(); + RCLJava.shutdown(); + assertEquals(0, eventHandler.getHandle()); + } } From 758051d884124f7638809da0dd3f396d57c770e0 Mon Sep 17 00:00:00 2001 From: Ivan Santiago Paunovic Date: Thu, 3 Sep 2020 13:48:17 -0300 Subject: [PATCH 02/11] Add macros to handle java exceptions Signed-off-by: Ivan Santiago Paunovic --- .../include/rcljava_common/exceptions.hpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/rcljava_common/include/rcljava_common/exceptions.hpp b/rcljava_common/include/rcljava_common/exceptions.hpp index 8a148611..788d4ba3 100644 --- a/rcljava_common/include/rcljava_common/exceptions.hpp +++ b/rcljava_common/include/rcljava_common/exceptions.hpp @@ -19,6 +19,24 @@ #include "rcljava_common/visibility_control.hpp" +/// Execute \a error_statement if an exception has happened. +/** + * \param env a JNIEnv pointer, used to check for exceptions. + * \param error_statement statement executed if an exception has happened. + */ +#define RCLJAVA_COMMON_EXCEPTION_CHECK_X(env, error_statement) \ + do { \ + if (env->ExceptionCheck()) { \ + error_statement; \ + } \ + } while (0) + +/// Return from the current function if a java exception has happened. +/** + * \param env a JNIEnv pointer, used to check for exceptions. + */ +#define RCLJAVA_COMMON_EXCEPTION_CHECK(env) RCLJAVA_COMMON_EXCEPTION_CHECK_X(env, return ) + namespace rcljava_common { namespace exceptions From 2130315408c29fc4f71e8fa935a8756cf5e305e3 Mon Sep 17 00:00:00 2001 From: Ivan Santiago Paunovic Date: Thu, 3 Sep 2020 13:57:03 -0300 Subject: [PATCH 03/11] Add macros to throw a java exception from an rcl error Signed-off-by: Ivan Santiago Paunovic --- .../include/rcljava_common/exceptions.hpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/rcljava_common/include/rcljava_common/exceptions.hpp b/rcljava_common/include/rcljava_common/exceptions.hpp index 788d4ba3..2b7b15a4 100644 --- a/rcljava_common/include/rcljava_common/exceptions.hpp +++ b/rcljava_common/include/rcljava_common/exceptions.hpp @@ -37,6 +37,31 @@ */ #define RCLJAVA_COMMON_EXCEPTION_CHECK(env) RCLJAVA_COMMON_EXCEPTION_CHECK_X(env, return ) +/// Call \ref rcljava_throw_rclexception if \a ret is not RCL_RET_OK, +/// and execute \a error_statement in that case. +/** + * \param env a JNIEnv pointer, used to throw a java exception from the rcl error. + * \param ret rcl_ret_t error that will be checked. + * \param message error message that will be passed to the thrown exception. + * \param error_statement statement executed if ret was not RCL_RET_OK. + */ +#define RCLJAVA_COMMON_THROW_FROM_RCL_X(env, ret, message, error_statement) \ + do { \ + if (RCL_RET_OK != ret) { \ + rcljava_common::exception::rcljava_throw_rclexception(env, ret, message); \ + error_statement; \ + } \ + } while (0) + +/// Call \ref rcljava_throw_rclexception if \a ret is not RCL_RET_OK and return. +/** + * \param env a JNIEnv pointer, used to check for exceptions. + * \param ret rcl_ret_t error that will be checked. + * \param message error message that will be passed to the thrown exception. + */ +#define RCLJAVA_COMMON_THROW_FROM_RCL(env, ret, message) \ + RCLJAVA_COMMON_THROW_FROM_RCL_X(env, ret, message, return ) + namespace rcljava_common { namespace exceptions From fa66914804b6034d50b21612b4a5d0ef23a12334 Mon Sep 17 00:00:00 2001 From: Ivan Santiago Paunovic Date: Thu, 3 Sep 2020 14:52:27 -0300 Subject: [PATCH 04/11] Improve the macro Signed-off-by: Ivan Santiago Paunovic --- rcljava_common/include/rcljava_common/exceptions.hpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/rcljava_common/include/rcljava_common/exceptions.hpp b/rcljava_common/include/rcljava_common/exceptions.hpp index 2b7b15a4..307cc08a 100644 --- a/rcljava_common/include/rcljava_common/exceptions.hpp +++ b/rcljava_common/include/rcljava_common/exceptions.hpp @@ -40,15 +40,21 @@ /// Call \ref rcljava_throw_rclexception if \a ret is not RCL_RET_OK, /// and execute \a error_statement in that case. /** + * The rcl error message will be added to \a base_message, and the rcl error state will be reset. + * * \param env a JNIEnv pointer, used to throw a java exception from the rcl error. * \param ret rcl_ret_t error that will be checked. * \param message error message that will be passed to the thrown exception. + * The complete error will be "${message}: ${rcl_error_string}". + * \a message can be either a `const char *` or as `std::string`. * \param error_statement statement executed if ret was not RCL_RET_OK. */ -#define RCLJAVA_COMMON_THROW_FROM_RCL_X(env, ret, message, error_statement) \ +#define RCLJAVA_COMMON_THROW_FROM_RCL_X(env, ret, base_message, error_statement) \ do { \ if (RCL_RET_OK != ret) { \ - rcljava_common::exception::rcljava_throw_rclexception(env, ret, message); \ + rcljava_common::exception::rcljava_throw_rclexception( \ + env, ret, static_cast(message) + ": " + rcl_get_error_string().str); \ + rcl_reset_error(); error_statement; \ } \ } while (0) From 00daf006fdd9d246a74adc6341d55a6cea9474ad Mon Sep 17 00:00:00 2001 From: Ivan Santiago Paunovic Date: Thu, 3 Sep 2020 15:02:47 -0300 Subject: [PATCH 05/11] fixup Signed-off-by: Ivan Santiago Paunovic --- rcljava_common/include/rcljava_common/exceptions.hpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/rcljava_common/include/rcljava_common/exceptions.hpp b/rcljava_common/include/rcljava_common/exceptions.hpp index 307cc08a..b61ba668 100644 --- a/rcljava_common/include/rcljava_common/exceptions.hpp +++ b/rcljava_common/include/rcljava_common/exceptions.hpp @@ -41,7 +41,7 @@ /// and execute \a error_statement in that case. /** * The rcl error message will be added to \a base_message, and the rcl error state will be reset. - * + * * \param env a JNIEnv pointer, used to throw a java exception from the rcl error. * \param ret rcl_ret_t error that will be checked. * \param message error message that will be passed to the thrown exception. @@ -52,9 +52,9 @@ #define RCLJAVA_COMMON_THROW_FROM_RCL_X(env, ret, base_message, error_statement) \ do { \ if (RCL_RET_OK != ret) { \ - rcljava_common::exception::rcljava_throw_rclexception( \ - env, ret, static_cast(message) + ": " + rcl_get_error_string().str); \ - rcl_reset_error(); + rcljava_common::exceptions::rcljava_throw_rclexception( \ + env, ret, static_cast(base_message) + ": " + rcl_get_error_string().str); \ + rcl_reset_error(); \ error_statement; \ } \ } while (0) @@ -65,8 +65,8 @@ * \param ret rcl_ret_t error that will be checked. * \param message error message that will be passed to the thrown exception. */ -#define RCLJAVA_COMMON_THROW_FROM_RCL(env, ret, message) \ - RCLJAVA_COMMON_THROW_FROM_RCL_X(env, ret, message, return ) +#define RCLJAVA_COMMON_THROW_FROM_RCL(env, ret, base_message) \ + RCLJAVA_COMMON_THROW_FROM_RCL_X(env, ret, base_message, return ) namespace rcljava_common { From f960ce7a70518b4a5b756a24ad40b59f779de1dc Mon Sep 17 00:00:00 2001 From: Ivan Santiago Paunovic Date: Tue, 8 Sep 2020 16:27:31 -0300 Subject: [PATCH 06/11] Always use `base_message` instead of `message` in docblocks Signed-off-by: Ivan Santiago Paunovic Co-authored-by: Jacob Perron --- rcljava_common/include/rcljava_common/exceptions.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rcljava_common/include/rcljava_common/exceptions.hpp b/rcljava_common/include/rcljava_common/exceptions.hpp index b61ba668..408019fc 100644 --- a/rcljava_common/include/rcljava_common/exceptions.hpp +++ b/rcljava_common/include/rcljava_common/exceptions.hpp @@ -63,7 +63,7 @@ /** * \param env a JNIEnv pointer, used to check for exceptions. * \param ret rcl_ret_t error that will be checked. - * \param message error message that will be passed to the thrown exception. + * \param base_message error message that will be passed to the thrown exception. */ #define RCLJAVA_COMMON_THROW_FROM_RCL(env, ret, base_message) \ RCLJAVA_COMMON_THROW_FROM_RCL_X(env, ret, base_message, return ) From 6dddaeecfb1c5b9bde5ef939cf933db6d4c3f563 Mon Sep 17 00:00:00 2001 From: Ivan Santiago Paunovic Date: Tue, 8 Sep 2020 16:26:59 -0300 Subject: [PATCH 07/11] Always use `base_message` instead of `message` in docblocks Signed-off-by: Ivan Santiago Paunovic Co-authored-by: Jacob Perron --- rcljava_common/include/rcljava_common/exceptions.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rcljava_common/include/rcljava_common/exceptions.hpp b/rcljava_common/include/rcljava_common/exceptions.hpp index 408019fc..73d4c03b 100644 --- a/rcljava_common/include/rcljava_common/exceptions.hpp +++ b/rcljava_common/include/rcljava_common/exceptions.hpp @@ -44,9 +44,9 @@ * * \param env a JNIEnv pointer, used to throw a java exception from the rcl error. * \param ret rcl_ret_t error that will be checked. - * \param message error message that will be passed to the thrown exception. - * The complete error will be "${message}: ${rcl_error_string}". - * \a message can be either a `const char *` or as `std::string`. + * \param base_message error message that will be passed to the thrown exception. + * The complete error will be "${base_message}: ${rcl_error_string}". + * \a base_message can be either a `const char *` or as `std::string`. * \param error_statement statement executed if ret was not RCL_RET_OK. */ #define RCLJAVA_COMMON_THROW_FROM_RCL_X(env, ret, base_message, error_statement) \ From 8218285e96c1dc6c6fd743867343b7461720f054 Mon Sep 17 00:00:00 2001 From: Ivan Santiago Paunovic Date: Tue, 8 Sep 2020 16:41:41 -0300 Subject: [PATCH 08/11] rename macros Signed-off-by: Ivan Santiago Paunovic --- rcljava_common/include/rcljava_common/exceptions.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/rcljava_common/include/rcljava_common/exceptions.hpp b/rcljava_common/include/rcljava_common/exceptions.hpp index 73d4c03b..992a509d 100644 --- a/rcljava_common/include/rcljava_common/exceptions.hpp +++ b/rcljava_common/include/rcljava_common/exceptions.hpp @@ -24,7 +24,7 @@ * \param env a JNIEnv pointer, used to check for exceptions. * \param error_statement statement executed if an exception has happened. */ -#define RCLJAVA_COMMON_EXCEPTION_CHECK_X(env, error_statement) \ +#define RCLJAVA_COMMON_CHECK_FOR_EXCEPTION_WITH_ERROR_STATEMENT(env, error_statement) \ do { \ if (env->ExceptionCheck()) { \ error_statement; \ @@ -35,7 +35,7 @@ /** * \param env a JNIEnv pointer, used to check for exceptions. */ -#define RCLJAVA_COMMON_EXCEPTION_CHECK(env) RCLJAVA_COMMON_EXCEPTION_CHECK_X(env, return ) +#define RCLJAVA_COMMON_CHECK_FOR_EXCEPTION(env) RCLJAVA_COMMON_CHECK_FOR_EXCEPTION_WITH_STATEMENT(env, return ) /// Call \ref rcljava_throw_rclexception if \a ret is not RCL_RET_OK, /// and execute \a error_statement in that case. @@ -49,7 +49,7 @@ * \a base_message can be either a `const char *` or as `std::string`. * \param error_statement statement executed if ret was not RCL_RET_OK. */ -#define RCLJAVA_COMMON_THROW_FROM_RCL_X(env, ret, base_message, error_statement) \ +#define RCLJAVA_COMMON_THROW_FROM_RCL_WITH_ERROR_STATEMENT(env, ret, base_message, error_statement) \ do { \ if (RCL_RET_OK != ret) { \ rcljava_common::exceptions::rcljava_throw_rclexception( \ @@ -66,7 +66,7 @@ * \param base_message error message that will be passed to the thrown exception. */ #define RCLJAVA_COMMON_THROW_FROM_RCL(env, ret, base_message) \ - RCLJAVA_COMMON_THROW_FROM_RCL_X(env, ret, base_message, return ) + RCLJAVA_COMMON_THROW_FROM_RCL_WITH_ERROR_STATEMENT(env, ret, base_message, return ) namespace rcljava_common { From e93189d7b03958966d02ef20e1f03ba9948106c1 Mon Sep 17 00:00:00 2001 From: Ivan Santiago Paunovic Date: Tue, 8 Sep 2020 17:58:37 -0300 Subject: [PATCH 09/11] Please linters Signed-off-by: Ivan Santiago Paunovic --- rcljava_common/include/rcljava_common/exceptions.hpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/rcljava_common/include/rcljava_common/exceptions.hpp b/rcljava_common/include/rcljava_common/exceptions.hpp index 992a509d..11b426aa 100644 --- a/rcljava_common/include/rcljava_common/exceptions.hpp +++ b/rcljava_common/include/rcljava_common/exceptions.hpp @@ -35,7 +35,8 @@ /** * \param env a JNIEnv pointer, used to check for exceptions. */ -#define RCLJAVA_COMMON_CHECK_FOR_EXCEPTION(env) RCLJAVA_COMMON_CHECK_FOR_EXCEPTION_WITH_STATEMENT(env, return ) +#define RCLJAVA_COMMON_CHECK_FOR_EXCEPTION(env) \ + RCLJAVA_COMMON_CHECK_FOR_EXCEPTION_WITH_STATEMENT(env, return ) /// Call \ref rcljava_throw_rclexception if \a ret is not RCL_RET_OK, /// and execute \a error_statement in that case. @@ -49,7 +50,8 @@ * \a base_message can be either a `const char *` or as `std::string`. * \param error_statement statement executed if ret was not RCL_RET_OK. */ -#define RCLJAVA_COMMON_THROW_FROM_RCL_WITH_ERROR_STATEMENT(env, ret, base_message, error_statement) \ +#define RCLJAVA_COMMON_THROW_FROM_RCL_WITH_ERROR_STATEMENT( \ + env, ret, base_message, error_statement) \ do { \ if (RCL_RET_OK != ret) { \ rcljava_common::exceptions::rcljava_throw_rclexception( \ From bd16c19d01228e8273d39fdd34664bea16cb00c2 Mon Sep 17 00:00:00 2001 From: Ivan Santiago Paunovic Date: Tue, 8 Sep 2020 18:06:26 -0300 Subject: [PATCH 10/11] fix after using the macros Signed-off-by: Ivan Santiago Paunovic --- rcljava_common/include/rcljava_common/exceptions.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rcljava_common/include/rcljava_common/exceptions.hpp b/rcljava_common/include/rcljava_common/exceptions.hpp index 11b426aa..b7f2d3c5 100644 --- a/rcljava_common/include/rcljava_common/exceptions.hpp +++ b/rcljava_common/include/rcljava_common/exceptions.hpp @@ -36,7 +36,7 @@ * \param env a JNIEnv pointer, used to check for exceptions. */ #define RCLJAVA_COMMON_CHECK_FOR_EXCEPTION(env) \ - RCLJAVA_COMMON_CHECK_FOR_EXCEPTION_WITH_STATEMENT(env, return ) + RCLJAVA_COMMON_CHECK_FOR_EXCEPTION_WITH_ERROR_STATEMENT(env, return ) /// Call \ref rcljava_throw_rclexception if \a ret is not RCL_RET_OK, /// and execute \a error_statement in that case. From 56fd92ccc2a2425e941a2de1083fdf22b0cc328d Mon Sep 17 00:00:00 2001 From: Ivan Santiago Paunovic Date: Thu, 13 May 2021 19:59:10 -0300 Subject: [PATCH 11/11] Fix MessageLost native methods names causing loading issues (#70) Signed-off-by: Ivan Santiago Paunovic --- ...ljava_subscription_statuses_MessageLost.cpp | 8 ++++---- .../subscription/statuses/MessageLost.java | 18 +++++++++--------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/rcljava/src/main/cpp/org_ros2_rcljava_subscription_statuses_MessageLost.cpp b/rcljava/src/main/cpp/org_ros2_rcljava_subscription_statuses_MessageLost.cpp index d2f935d5..9fdee7ab 100644 --- a/rcljava/src/main/cpp/org_ros2_rcljava_subscription_statuses_MessageLost.cpp +++ b/rcljava/src/main/cpp/org_ros2_rcljava_subscription_statuses_MessageLost.cpp @@ -24,7 +24,7 @@ using rcljava_common::exceptions::rcljava_throw_exception; JNIEXPORT jlong JNICALL -Java_org_ros2_rcljava_subscription_statuses_MessageLost_nativeAllocateRCL( +Java_org_ros2_rcljava_subscription_statuses_MessageLost_nativeAllocateRCLStatusEvent( JNIEnv * env, jclass) { void * p = malloc(sizeof(rmw_message_lost_status_t)); @@ -36,14 +36,14 @@ Java_org_ros2_rcljava_subscription_statuses_MessageLost_nativeAllocateRCL( } JNIEXPORT void JNICALL -Java_org_ros2_rcljava_subscription_statuses_MessageLost_nativeDeallocateRCL( +Java_org_ros2_rcljava_subscription_statuses_MessageLost_nativeDeallocateRCLStatusEvent( JNIEnv *, jclass, jlong handle) { free(reinterpret_cast(handle)); } JNIEXPORT void JNICALL -Java_org_ros2_rcljava_subscription_statuses_MessageLost_nativeFromRCL( +Java_org_ros2_rcljava_subscription_statuses_MessageLost_nativeFromRCLEvent( JNIEnv * env, jobject self, jlong handle) { auto * p = reinterpret_cast(handle); @@ -67,7 +67,7 @@ Java_org_ros2_rcljava_subscription_statuses_MessageLost_nativeFromRCL( } JNIEXPORT jint JNICALL -Java_org_ros2_rcljava_subscription_statuses_MessageLost_nativeGetEventType( +Java_org_ros2_rcljava_subscription_statuses_MessageLost_nativeGetSubscriptionEventType( JNIEnv *, jclass) { return RCL_SUBSCRIPTION_MESSAGE_LOST; diff --git a/rcljava/src/main/java/org/ros2/rcljava/subscription/statuses/MessageLost.java b/rcljava/src/main/java/org/ros2/rcljava/subscription/statuses/MessageLost.java index d136b4df..eb789b4c 100644 --- a/rcljava/src/main/java/org/ros2/rcljava/subscription/statuses/MessageLost.java +++ b/rcljava/src/main/java/org/ros2/rcljava/subscription/statuses/MessageLost.java @@ -30,16 +30,16 @@ public class MessageLost implements SubscriptionEventStatus { public int totalCountChange; public final long allocateRCLStatusEvent() { - return nativeAllocateRCL(); + return nativeAllocateRCLStatusEvent(); } public final void deallocateRCLStatusEvent(long handle) { - nativeDeallocateRCL(handle); + nativeDeallocateRCLStatusEvent(handle); } public final void fromRCLEvent(long handle) { - nativeFromRCL(handle); + nativeFromRCLEvent(handle); } public final int getSubscriptionEventType() { - return nativeGetEventType(); + return nativeGetSubscriptionEventType(); } // TODO(ivanpauno): Remove this when -source 8 can be used (method references for the win) public static final Supplier factory = new Supplier() { @@ -48,6 +48,11 @@ public MessageLost get() { } }; + private static native long nativeAllocateRCLStatusEvent(); + private static native void nativeDeallocateRCLStatusEvent(long handle); + private native void nativeFromRCLEvent(long handle); + private static native int nativeGetSubscriptionEventType(); + private static final Logger logger = LoggerFactory.getLogger(MessageLost.class); static { try { @@ -57,9 +62,4 @@ public MessageLost get() { System.exit(1); } } - - private static native long nativeAllocateRCL(); - private static native void nativeDeallocateRCL(long handle); - private native void nativeFromRCL(long handle); - private static native int nativeGetEventType(); }