diff --git a/rcljava/CMakeLists.txt b/rcljava/CMakeLists.txt index dda754a0..e3532b62 100644 --- a/rcljava/CMakeLists.txt +++ b/rcljava/CMakeLists.txt @@ -58,11 +58,19 @@ set(${PROJECT_NAME}_jni_sources "src/main/cpp/org_ros2_rcljava_Time.cpp" "src/main/cpp/org_ros2_rcljava_client_ClientImpl.cpp" "src/main/cpp/org_ros2_rcljava_contexts_ContextImpl.cpp" + "src/main/cpp/org_ros2_rcljava_detail_QosIncompatibleStatus.cpp" "src/main/cpp/org_ros2_rcljava_executors_BaseExecutor.cpp" + "src/main/cpp/org_ros2_rcljava_events_EventHandlerImpl.cpp" + "src/main/cpp/org_ros2_rcljava_publisher_statuses_LivelinessLost.cpp" + "src/main/cpp/org_ros2_rcljava_publisher_statuses_OfferedDeadlineMissed.cpp" + "src/main/cpp/org_ros2_rcljava_publisher_statuses_OfferedQosIncompatible.cpp" "src/main/cpp/org_ros2_rcljava_node_NodeImpl.cpp" "src/main/cpp/org_ros2_rcljava_publisher_PublisherImpl.cpp" "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_RequestedDeadlineMissed.cpp" + "src/main/cpp/org_ros2_rcljava_subscription_statuses_RequestedQosIncompatible.cpp" "src/main/cpp/org_ros2_rcljava_time_Clock.cpp" "src/main/cpp/org_ros2_rcljava_timer_WallTimerImpl.cpp" ) @@ -125,6 +133,15 @@ set(${PROJECT_NAME}_sources "src/main/java/org/ros2/rcljava/consumers/BiConsumer.java" "src/main/java/org/ros2/rcljava/consumers/Consumer.java" "src/main/java/org/ros2/rcljava/consumers/TriConsumer.java" + "src/main/java/org/ros2/rcljava/detail/QosIncompatibleStatus.java" + "src/main/java/org/ros2/rcljava/events/EventHandler.java" + "src/main/java/org/ros2/rcljava/events/EventHandlerImpl.java" + "src/main/java/org/ros2/rcljava/events/EventStatus.java" + "src/main/java/org/ros2/rcljava/events/PublisherEventStatus.java" + "src/main/java/org/ros2/rcljava/events/SubscriptionEventStatus.java" + "src/main/java/org/ros2/rcljava/publisher/statuses/LivelinessLost.java" + "src/main/java/org/ros2/rcljava/publisher/statuses/OfferedDeadlineMissed.java" + "src/main/java/org/ros2/rcljava/publisher/statuses/OfferedQosIncompatible.java" "src/main/java/org/ros2/rcljava/executors/AnyExecutable.java" "src/main/java/org/ros2/rcljava/executors/BaseExecutor.java" "src/main/java/org/ros2/rcljava/executors/Executor.java" @@ -161,6 +178,9 @@ set(${PROJECT_NAME}_sources "src/main/java/org/ros2/rcljava/service/ServiceImpl.java" "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/RequestedDeadlineMissed.java" + "src/main/java/org/ros2/rcljava/subscription/statuses/RequestedQosIncompatible.java" "src/main/java/org/ros2/rcljava/time/Clock.java" "src/main/java/org/ros2/rcljava/time/ClockType.java" "src/main/java/org/ros2/rcljava/timer/Timer.java" diff --git a/rcljava/include/org_ros2_rcljava_detail_QosIncompatibleStatus.h b/rcljava/include/org_ros2_rcljava_detail_QosIncompatibleStatus.h new file mode 100644 index 00000000..af8f7319 --- /dev/null +++ b/rcljava/include/org_ros2_rcljava_detail_QosIncompatibleStatus.h @@ -0,0 +1,54 @@ +// 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_detail_QosIncompatibleStatus */ + +#ifndef ORG_ROS2_RCLJAVA_DETAIL_QOSINCOMPATIBLESTATUS_H_ +#define ORG_ROS2_RCLJAVA_DETAIL_QOSINCOMPATIBLESTATUS_H_ +#ifdef __cplusplus +extern "C" { +#endif + +/* + * Class: org_ros2_rcljava_detail_QosIncompatibleStatus + * Method: nativeAllocateRCLStatusEvent + * Signature: ()J + */ +JNIEXPORT jlong JNICALL +Java_org_ros2_rcljava_detail_QosIncompatibleStatus_nativeAllocateRCLStatusEvent( + JNIEnv *, jclass); + +/* + * Class: org_ros2_rcljava_detail_QosIncompatibleStatus + * Method: nativeDeallocateRCLStatusEvent + * Signature: (J)V + */ +JNIEXPORT void JNICALL +Java_org_ros2_rcljava_detail_QosIncompatibleStatus_nativeDeallocateRCLStatusEvent( + JNIEnv *, jclass, jlong); + +/* + * Class: org_ros2_rcljava_detail_QosIncompatibleStatus + * Method: nativeFromRCLEvent + * Signature: (J)V + */ +JNIEXPORT void JNICALL +Java_org_ros2_rcljava_detail_QosIncompatibleStatus_nativeFromRCLEvent( + JNIEnv *, jobject, jlong); + +#ifdef __cplusplus +} +#endif +#endif // ORG_ROS2_RCLJAVA_DETAIL_QOSINCOMPATIBLESTATUS_H_ diff --git a/rcljava/include/org_ros2_rcljava_events_EventHandlerImpl.h b/rcljava/include/org_ros2_rcljava_events_EventHandlerImpl.h new file mode 100644 index 00000000..8dbf6ce2 --- /dev/null +++ b/rcljava/include/org_ros2_rcljava_events_EventHandlerImpl.h @@ -0,0 +1,45 @@ +// 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_events_EventHandlerImpl */ + +#ifndef ORG_ROS2_RCLJAVA_EVENTS_EVENTHANDLERIMPL_H_ +#define ORG_ROS2_RCLJAVA_EVENTS_EVENTHANDLERIMPL_H_ +#ifdef __cplusplus +extern "C" { +#endif + +/* + * Class: org_ros2_rcljava_events_EventHandlerImpl + * Method: nativeDispose + * Signature: (J)V + */ +JNIEXPORT void +JNICALL Java_org_ros2_rcljava_events_EventHandlerImpl_nativeDispose( + JNIEnv *, jclass, jlong event_handle); + +/* + * Class: org_ros2_rcljava_events_EventHandlerImpl + * Method: nativeTake + * Signature: (JJ)V + */ +JNIEXPORT void +JNICALL Java_org_ros2_rcljava_events_EventHandlerImpl_nativeTake( + JNIEnv *, jclass, jlong event_handle, jlong event_status_handle); + +#ifdef __cplusplus +} +#endif +#endif // ORG_ROS2_RCLJAVA_EVENTS_EVENTHANDLERIMPL_H_ diff --git a/rcljava/include/org_ros2_rcljava_executors_BaseExecutor.h b/rcljava/include/org_ros2_rcljava_executors_BaseExecutor.h index c94a7959..5522ac8d 100644 --- a/rcljava/include/org_ros2_rcljava_executors_BaseExecutor.h +++ b/rcljava/include/org_ros2_rcljava_executors_BaseExecutor.h @@ -134,6 +134,15 @@ JNIEXPORT void JNICALL Java_org_ros2_rcljava_executors_BaseExecutor_nativeWaitSetAddTimer( JNIEnv *, jclass, jlong, jlong); +/* + * Class: org_ros2_rcljava_executors_BaseExecutor + * Method: nativeWaitSetAddEvent + * Signature: (JJ)V + */ +JNIEXPORT void +JNICALL Java_org_ros2_rcljava_executors_BaseExecutor_nativeWaitSetAddEvent( + JNIEnv *, jclass, jlong, jlong); + /* * Class: org_ros2_rcljava_executors_BaseExecutor * Method: nativeWaitSetSubscriptionIsReady @@ -152,6 +161,15 @@ JNIEXPORT jboolean JNICALL Java_org_ros2_rcljava_executors_BaseExecutor_nativeWaitSetTimerIsReady( JNIEnv *, jclass, jlong, jlong); +/* + * Class: org_ros2_rcljava_executors_BaseExecutor + * Method: nativeWaitSetEventIsReady + * Signature: (JJ)Z + */ +JNIEXPORT jboolean +JNICALL Java_org_ros2_rcljava_executors_BaseExecutor_nativeWaitSetEventIsReady( + JNIEnv *, jclass, jlong, jlong); + /* * Class: org_ros2_rcljava_executors_BaseExecutor * Method: nativeWaitSetServiceIsReady diff --git a/rcljava/include/org_ros2_rcljava_publisher_PublisherImpl.h b/rcljava/include/org_ros2_rcljava_publisher_PublisherImpl.h index befe7447..9236fdda 100644 --- a/rcljava/include/org_ros2_rcljava_publisher_PublisherImpl.h +++ b/rcljava/include/org_ros2_rcljava_publisher_PublisherImpl.h @@ -38,6 +38,15 @@ JNIEXPORT void JNICALL Java_org_ros2_rcljava_publisher_PublisherImpl_nativeDispose( JNIEnv *, jclass, jlong, jlong); +/* + * Class: org_ros2_rcljava_publisher_PublisherImpl + * Method: nativeCreateEvent + * Signature: (JJ)J + */ +JNIEXPORT jlong +JNICALL Java_org_ros2_rcljava_publisher_PublisherImpl_nativeCreateEvent( + JNIEnv *, jclass, jlong, jint); + #ifdef __cplusplus } #endif diff --git a/rcljava/include/org_ros2_rcljava_publisher_statuses_LivelinessLost.h b/rcljava/include/org_ros2_rcljava_publisher_statuses_LivelinessLost.h new file mode 100644 index 00000000..b8af4373 --- /dev/null +++ b/rcljava/include/org_ros2_rcljava_publisher_statuses_LivelinessLost.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_publisher_statuses_LivelinessLost */ + +#ifndef ORG_ROS2_RCLJAVA_PUBLISHER_STATUSES_LIVELINESSLOST_H_ +#define ORG_ROS2_RCLJAVA_PUBLISHER_STATUSES_LIVELINESSLOST_H_ +#ifdef __cplusplus +extern "C" { +#endif + +/* + * Class: org_ros2_rcljava_publisher_statuses_LivelinessLost + * Method: nativeAllocateRCLStatusEvent + * Signature: ()J + */ +JNIEXPORT jlong JNICALL +Java_org_ros2_rcljava_publisher_statuses_LivelinessLost_nativeAllocateRCLStatusEvent( + JNIEnv *, jclass); + +/* + * Class: org_ros2_rcljava_publisher_statuses_LivelinessLost + * Method: nativeDeallocateRCLStatusEvent + * Signature: (J)V + */ +JNIEXPORT void JNICALL +Java_org_ros2_rcljava_publisher_statuses_LivelinessLost_nativeDeallocateRCLStatusEvent( + JNIEnv *, jclass, jlong); + +/* + * Class: org_ros2_rcljava_publisher_statuses_LivelinessLost + * Method: nativeFromRCLEvent + * Signature: (J)V + */ +JNIEXPORT void JNICALL +Java_org_ros2_rcljava_publisher_statuses_LivelinessLost_nativeFromRCLEvent( + JNIEnv *, jobject, jlong); + +/* + * Class: org_ros2_rcljava_publisher_statuses_LivelinessLost + * Method: nativeGetPublisherEventType + * Signature: ()I + */ +JNIEXPORT jint JNICALL +Java_org_ros2_rcljava_publisher_statuses_LivelinessLost_nativeGetPublisherEventType( + JNIEnv *, jclass); + +#ifdef __cplusplus +} +#endif +#endif // ORG_ROS2_RCLJAVA_PUBLISHER_STATUSES_LIVELINESSLOST_H_ diff --git a/rcljava/include/org_ros2_rcljava_publisher_statuses_OfferedDeadlineMissed.h b/rcljava/include/org_ros2_rcljava_publisher_statuses_OfferedDeadlineMissed.h new file mode 100644 index 00000000..2f0575c9 --- /dev/null +++ b/rcljava/include/org_ros2_rcljava_publisher_statuses_OfferedDeadlineMissed.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_publisher_statuses_OfferedDeadlineMissed */ + +#ifndef ORG_ROS2_RCLJAVA_PUBLISHER_STATUSES_OFFEREDDEADLINEMISSED_H_ +#define ORG_ROS2_RCLJAVA_PUBLISHER_STATUSES_OFFEREDDEADLINEMISSED_H_ +#ifdef __cplusplus +extern "C" { +#endif + +/* + * Class: org_ros2_rcljava_publisher_statuses_OfferedDeadlineMissed + * Method: nativeAllocateRCLStatusEvent + * Signature: ()J + */ +JNIEXPORT jlong JNICALL +Java_org_ros2_rcljava_publisher_statuses_OfferedDeadlineMissed_nativeAllocateRCLStatusEvent( + JNIEnv *, jclass); + +/* + * Class: org_ros2_rcljava_publisher_statuses_OfferedDeadlineMissed + * Method: nativeDeallocateRCLStatusEvent + * Signature: (J)V + */ +JNIEXPORT void JNICALL +Java_org_ros2_rcljava_publisher_statuses_OfferedDeadlineMissed_nativeDeallocateRCLStatusEvent( + JNIEnv *, jclass, jlong); + +/* + * Class: org_ros2_rcljava_publisher_statuses_OfferedDeadlineMissed + * Method: nativeFromRCLEvent + * Signature: (J)V + */ +JNIEXPORT void JNICALL +Java_org_ros2_rcljava_publisher_statuses_OfferedDeadlineMissed_nativeFromRCLEvent( + JNIEnv *, jobject, jlong); + +/* + * Class: org_ros2_rcljava_publisher_statuses_OfferedDeadlineMissed + * Method: nativeGetPublisherEventType + * Signature: ()I + */ +JNIEXPORT jint JNICALL +Java_org_ros2_rcljava_publisher_statuses_OfferedDeadlineMissed_nativeGetPublisherEventType( + JNIEnv *, jclass); + +#ifdef __cplusplus +} +#endif +#endif // ORG_ROS2_RCLJAVA_PUBLISHER_STATUSES_OFFEREDDEADLINEMISSED_H_ diff --git a/rcljava/include/org_ros2_rcljava_publisher_statuses_OfferedQosIncompatible.h b/rcljava/include/org_ros2_rcljava_publisher_statuses_OfferedQosIncompatible.h new file mode 100644 index 00000000..15860176 --- /dev/null +++ b/rcljava/include/org_ros2_rcljava_publisher_statuses_OfferedQosIncompatible.h @@ -0,0 +1,36 @@ +// 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_publisher_statuses_OfferedQosIncompatible */ + +#ifndef ORG_ROS2_RCLJAVA_PUBLISHER_STATUSES_OFFEREDQOSINCOMPATIBLE_H_ +#define ORG_ROS2_RCLJAVA_PUBLISHER_STATUSES_OFFEREDQOSINCOMPATIBLE_H_ +#ifdef __cplusplus +extern "C" { +#endif + +/* + * Class: org_ros2_rcljava_publisher_statuses_OfferedQosIncompatible + * Method: nativeGetEventType + * Signature: ()I + */ +JNIEXPORT jint JNICALL +Java_org_ros2_rcljava_publisher_statuses_OfferedQosIncompatible_nativeGetEventType( + JNIEnv *, jclass); + +#ifdef __cplusplus +} +#endif +#endif // ORG_ROS2_RCLJAVA_PUBLISHER_STATUSES_OFFEREDQOSINCOMPATIBLE_H_ diff --git a/rcljava/include/org_ros2_rcljava_subscription_SubscriptionImpl.h b/rcljava/include/org_ros2_rcljava_subscription_SubscriptionImpl.h index afb3456c..1f788aef 100644 --- a/rcljava/include/org_ros2_rcljava_subscription_SubscriptionImpl.h +++ b/rcljava/include/org_ros2_rcljava_subscription_SubscriptionImpl.h @@ -29,6 +29,15 @@ JNIEXPORT void JNICALL Java_org_ros2_rcljava_subscription_SubscriptionImpl_nativeDispose( JNIEnv *, jclass, jlong, jlong); +/* + * Class: org_ros2_rcljava_subscription_SubscriptionImpl + * Method: nativeCreateEvent + * Signature: (JJ)J + */ +JNIEXPORT jlong +JNICALL Java_org_ros2_rcljava_subscription_SubscriptionImpl_nativeCreateEvent( + JNIEnv *, jclass, jlong, jint); + #ifdef __cplusplus } #endif diff --git a/rcljava/include/org_ros2_rcljava_subscription_statuses_LivelinessChanged.h b/rcljava/include/org_ros2_rcljava_subscription_statuses_LivelinessChanged.h new file mode 100644 index 00000000..d34198f9 --- /dev/null +++ b/rcljava/include/org_ros2_rcljava_subscription_statuses_LivelinessChanged.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_LivelinessChanged */ + +#ifndef ORG_ROS2_RCLJAVA_SUBSCRIPTION_STATUSES_LIVELINESSCHANGED_H_ +#define ORG_ROS2_RCLJAVA_SUBSCRIPTION_STATUSES_LIVELINESSCHANGED_H_ +#ifdef __cplusplus +extern "C" { +#endif + +/* + * Class: org_ros2_rcljava_subscription_statuses_LivelinessChanged + * Method: nativeAllocateRCLStatusEvent + * Signature: ()J + */ +JNIEXPORT jlong JNICALL +Java_org_ros2_rcljava_subscription_statuses_LivelinessChanged_nativeAllocateRCLStatusEvent( + JNIEnv *, jclass); + +/* + * Class: org_ros2_rcljava_subscription_statuses_LivelinessChanged + * Method: nativeDeallocateRCLStatusEvent + * Signature: (J)V + */ +JNIEXPORT void JNICALL +Java_org_ros2_rcljava_subscription_statuses_LivelinessChanged_nativeDeallocateRCLStatusEvent( + JNIEnv *, jclass, jlong); + +/* + * Class: org_ros2_rcljava_subscription_statuses_LivelinessChanged + * Method: nativeFromRCLEvent + * Signature: (J)V + */ +JNIEXPORT void JNICALL +Java_org_ros2_rcljava_subscription_statuses_LivelinessChanged_nativeFromRCLEvent( + JNIEnv *, jobject, jlong); + +/* + * Class: org_ros2_rcljava_subscription_statuses_LivelinessChanged + * Method: nativeGetsubscriptionEventType + * Signature: ()I + */ +JNIEXPORT jint JNICALL +Java_org_ros2_rcljava_subscription_statuses_LivelinessChanged_nativeGetSubscriptionEventType( + JNIEnv *, jclass); + +#ifdef __cplusplus +} +#endif +#endif // ORG_ROS2_RCLJAVA_SUBSCRIPTION_STATUSES_LIVELINESSCHANGED_H_ diff --git a/rcljava/include/org_ros2_rcljava_subscription_statuses_RequestedDeadlineMissed.h b/rcljava/include/org_ros2_rcljava_subscription_statuses_RequestedDeadlineMissed.h new file mode 100644 index 00000000..47c09fdd --- /dev/null +++ b/rcljava/include/org_ros2_rcljava_subscription_statuses_RequestedDeadlineMissed.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_RequestedDeadlineMissed */ + +#ifndef ORG_ROS2_RCLJAVA_SUBSCRIPTION_STATUSES_REQUESTEDDEADLINEMISSED_H_ +#define ORG_ROS2_RCLJAVA_SUBSCRIPTION_STATUSES_REQUESTEDDEADLINEMISSED_H_ +#ifdef __cplusplus +extern "C" { +#endif + +/* + * Class: org_ros2_rcljava_subscription_statuses_RequestedDeadlineMissed + * Method: nativeAllocateRCLStatusEvent + * Signature: ()J + */ +JNIEXPORT jlong JNICALL +Java_org_ros2_rcljava_subscription_statuses_RequestedDeadlineMissed_nativeAllocateRCL( + JNIEnv *, jclass); + +/* + * Class: org_ros2_rcljava_subscription_statuses_RequestedDeadlineMissed + * Method: nativeDeallocateRCLStatusEvent + * Signature: (J)V + */ +JNIEXPORT void JNICALL +Java_org_ros2_rcljava_subscription_statuses_RequestedDeadlineMissed_nativeDeallocateRCL( + JNIEnv *, jclass, jlong); + +/* + * Class: org_ros2_rcljava_subscription_statuses_RequestedDeadlineMissed + * Method: nativeFromRCLEvent + * Signature: (J)V + */ +JNIEXPORT void JNICALL +Java_org_ros2_rcljava_subscription_statuses_RequestedDeadlineMissed_nativeFromRCL( + JNIEnv *, jobject, jlong); + +/* + * Class: org_ros2_rcljava_subscription_statuses_RequestedDeadlineMissed + * Method: nativeGetSubscriptionEventType + * Signature: ()I + */ +JNIEXPORT jint JNICALL +Java_org_ros2_rcljava_subscription_statuses_RequestedDeadlineMissed_nativeGetEventType( + JNIEnv *, jclass); + +#ifdef __cplusplus +} +#endif +#endif // ORG_ROS2_RCLJAVA_SUBSCRIPTION_STATUSES_REQUESTEDDEADLINEMISSED_H_ diff --git a/rcljava/include/org_ros2_rcljava_subscription_statuses_RequestedQosIncompatible.h b/rcljava/include/org_ros2_rcljava_subscription_statuses_RequestedQosIncompatible.h new file mode 100644 index 00000000..ef7a0236 --- /dev/null +++ b/rcljava/include/org_ros2_rcljava_subscription_statuses_RequestedQosIncompatible.h @@ -0,0 +1,36 @@ +// 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_RequestedQosIncompatible */ + +#ifndef ORG_ROS2_RCLJAVA_SUBSCRIPTION_STATUSES_REQUESTEDQOSINCOMPATIBLE_H_ +#define ORG_ROS2_RCLJAVA_SUBSCRIPTION_STATUSES_REQUESTEDQOSINCOMPATIBLE_H_ +#ifdef __cplusplus +extern "C" { +#endif + +/* + * Class: org_ros2_rcljava_subscription_statuses_RequestedQosIncompatible + * Method: nativeGetEventType + * Signature: ()I + */ +JNIEXPORT jint JNICALL +Java_org_ros2_rcljava_subscription_statuses_RequestedQosIncompatible_nativeGetEventType( + JNIEnv *, jclass); + +#ifdef __cplusplus +} +#endif +#endif // ORG_ROS2_RCLJAVA_SUBSCRIPTION_STATUSES_REQUESTEDQOSINCOMPATIBLE_H_ diff --git a/rcljava/src/main/cpp/org_ros2_rcljava_detail_QosIncompatibleStatus.cpp b/rcljava/src/main/cpp/org_ros2_rcljava_detail_QosIncompatibleStatus.cpp new file mode 100644 index 00000000..149b90c6 --- /dev/null +++ b/rcljava/src/main/cpp/org_ros2_rcljava_detail_QosIncompatibleStatus.cpp @@ -0,0 +1,114 @@ +// 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_detail_QosIncompatibleStatus.h" + +#include +#include + +#include "rmw/incompatible_qos_events_statuses.h" +#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_detail_QosIncompatibleStatus_nativeAllocateRCLStatusEvent( + JNIEnv * env, jclass) +{ + void * p = malloc(sizeof(rmw_qos_incompatible_event_status_t)); + if (!p) { + rcljava_throw_exception( + env, "java/lang/OutOfMemoryError", "failed to allocate qos incompatible status"); + } + return reinterpret_cast(p); +} + +JNIEXPORT void JNICALL +Java_org_ros2_rcljava_detail_QosIncompatibleStatus_nativeDeallocateRCLStatusEvent( + JNIEnv *, jclass, jlong handle) +{ + free(reinterpret_cast(handle)); +} + +JNIEXPORT void JNICALL +Java_org_ros2_rcljava_detail_QosIncompatibleStatus_nativeFromRCLEvent( + 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); + jclass qos_kind_clazz = env->FindClass( + "org/ros2/rcljava/detail/QosIncompatibleStatus$PolicyKind"); + if (env->ExceptionCheck()) { + return; + } + 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; + } + const char * enum_class_path = + "Lorg/ros2/rcljava/detail/QosIncompatibleStatus$PolicyKind;"; + jfieldID policy_kind_fid = env->GetFieldID(clazz, "lastPolicyKind", enum_class_path); + if (env->ExceptionCheck()) { + return; + } + + jfieldID enum_value_fid; + switch (p->last_policy_kind) { + case RMW_QOS_POLICY_INVALID: + enum_value_fid = env->GetStaticFieldID(qos_kind_clazz, "INVALID", enum_class_path); + break; + case RMW_QOS_POLICY_DURABILITY: + enum_value_fid = env->GetStaticFieldID(qos_kind_clazz, "DURABILITY", enum_class_path); + break; + case RMW_QOS_POLICY_DEADLINE: + enum_value_fid = env->GetStaticFieldID(qos_kind_clazz, "DEADLINE", enum_class_path); + break; + case RMW_QOS_POLICY_LIVELINESS: + enum_value_fid = env->GetStaticFieldID(qos_kind_clazz, "LIVELINESS", enum_class_path); + break; + case RMW_QOS_POLICY_RELIABILITY: + enum_value_fid = env->GetStaticFieldID(qos_kind_clazz, "RELIABILITY", enum_class_path); + break; + case RMW_QOS_POLICY_HISTORY: + enum_value_fid = env->GetStaticFieldID(qos_kind_clazz, "HISTORY", enum_class_path); + break; + case RMW_QOS_POLICY_LIFESPAN: + enum_value_fid = env->GetStaticFieldID(qos_kind_clazz, "LIFESPAN", enum_class_path); + break; + default: + rcljava_throw_exception( + env, "java/lang/IllegalArgumentException", "unknown rmw qos policy kind"); + break; + } + if (env->ExceptionCheck()) { + return; + } + jobject enum_value = env->GetStaticObjectField(qos_kind_clazz, enum_value_fid); + + env->SetIntField(self, total_count_fid, p->total_count); + env->SetIntField(self, total_count_change_fid, p->total_count_change); + env->SetObjectField(self, policy_kind_fid, enum_value); +} diff --git a/rcljava/src/main/cpp/org_ros2_rcljava_events_EventHandlerImpl.cpp b/rcljava/src/main/cpp/org_ros2_rcljava_events_EventHandlerImpl.cpp new file mode 100644 index 00000000..89e2faba --- /dev/null +++ b/rcljava/src/main/cpp/org_ros2_rcljava_events_EventHandlerImpl.cpp @@ -0,0 +1,72 @@ +// 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 + +#include + +#include "rcl/error_handling.h" +#include "rcl/event.h" + +#include "rcljava_common/exceptions.hpp" + +#include "org_ros2_rcljava_events_EventHandlerImpl.h" + +using rcljava_common::exceptions::rcljava_throw_exception; +using rcljava_common::exceptions::rcljava_throw_rclexception; + +JNIEXPORT void JNICALL +Java_org_ros2_rcljava_events_EventHandlerImpl_nativeDispose( + JNIEnv * env, jclass, jlong event_handle) +{ + if (event_handle == 0) { + // everything is ok, already destroyed + return; + } + + auto * event = reinterpret_cast(event_handle); + + rcl_ret_t ret = rcl_event_fini(event); + + if (RCL_RET_OK != ret) { + std::string msg = "Failed to destroy event: " + std::string(rcl_get_error_string().str); + rcl_reset_error(); + rcljava_throw_exception(env, "java/lang/IllegalStateException", msg); + } +} + +JNIEXPORT void JNICALL +Java_org_ros2_rcljava_events_EventHandlerImpl_nativeTake( + JNIEnv * env, jclass, jlong event_handle, jlong event_status_handle) +{ + auto * event = reinterpret_cast(event_handle); + if (!event) { + rcljava_throw_exception( + env, + "java/lang/IllegalArgumentException", + "The underlying rcl_event_t has been already disposed"); + } + void * event_status = reinterpret_cast(event_status_handle); + if (!event_status) { + rcljava_throw_exception( + env, + "java/lang/IllegalArgumentException", + "The passed event status is NULL"); + } + rcl_ret_t ret = rcl_take_event(event, event_status); + if (RCL_RET_OK != ret) { + rcljava_throw_rclexception(env, ret, rcl_get_error_string().str); + rcl_reset_error(); + } +} diff --git a/rcljava/src/main/cpp/org_ros2_rcljava_executors_BaseExecutor.cpp b/rcljava/src/main/cpp/org_ros2_rcljava_executors_BaseExecutor.cpp index ad515de5..287a175f 100644 --- a/rcljava/src/main/cpp/org_ros2_rcljava_executors_BaseExecutor.cpp +++ b/rcljava/src/main/cpp/org_ros2_rcljava_executors_BaseExecutor.cpp @@ -276,6 +276,21 @@ Java_org_ros2_rcljava_executors_BaseExecutor_nativeWaitSetAddTimer( } } +JNIEXPORT void JNICALL +Java_org_ros2_rcljava_executors_BaseExecutor_nativeWaitSetAddEvent( + JNIEnv * env, jclass, jlong wait_set_handle, jlong event_handle) +{ + auto * wait_set = reinterpret_cast(wait_set_handle); + auto * event = reinterpret_cast(event_handle); + rcl_ret_t ret = rcl_wait_set_add_event(wait_set, event, nullptr); + if (ret != RCL_RET_OK) { + std::string msg = + "Failed to add event to wait set: " + std::string(rcl_get_error_string().str); + rcl_reset_error(); + rcljava_throw_rclexception(env, ret, msg); + } +} + JNIEXPORT jobject JNICALL Java_org_ros2_rcljava_executors_BaseExecutor_nativeTakeRequest( JNIEnv * env, jclass, jlong service_handle, jlong jrequest_from_java_converter_handle, @@ -435,6 +450,14 @@ Java_org_ros2_rcljava_executors_BaseExecutor_nativeWaitSetTimerIsReady( return wait_set->timers[index] != nullptr; } +JNIEXPORT jboolean JNICALL +Java_org_ros2_rcljava_executors_BaseExecutor_nativeWaitSetEventIsReady( + JNIEnv *, jclass, jlong wait_set_handle, jlong index) +{ + rcl_wait_set_t * wait_set = reinterpret_cast(wait_set_handle); + return wait_set->events[index] != nullptr; +} + JNIEXPORT jboolean JNICALL Java_org_ros2_rcljava_executors_BaseExecutor_nativeWaitSetServiceIsReady( JNIEnv *, jclass, jlong wait_set_handle, jlong index) diff --git a/rcljava/src/main/cpp/org_ros2_rcljava_publisher_PublisherImpl.cpp b/rcljava/src/main/cpp/org_ros2_rcljava_publisher_PublisherImpl.cpp index 6cef82c2..ea5d9feb 100644 --- a/rcljava/src/main/cpp/org_ros2_rcljava_publisher_PublisherImpl.cpp +++ b/rcljava/src/main/cpp/org_ros2_rcljava_publisher_PublisherImpl.cpp @@ -20,6 +20,7 @@ #include #include "rcl/error_handling.h" +#include "rcl/event.h" #include "rcl/node.h" #include "rcl/rcl.h" #include "rmw/rmw.h" @@ -29,6 +30,7 @@ #include "org_ros2_rcljava_publisher_PublisherImpl.h" +using rcljava_common::exceptions::rcljava_throw_exception; using rcljava_common::exceptions::rcljava_throw_rclexception; using rcljava_common::signatures::convert_from_java_signature; using rcljava_common::signatures::destroy_ros_message_signature; @@ -90,3 +92,31 @@ Java_org_ros2_rcljava_publisher_PublisherImpl_nativeDispose( rcljava_throw_rclexception(env, ret, msg); } } + +JNIEXPORT jlong JNICALL +Java_org_ros2_rcljava_publisher_PublisherImpl_nativeCreateEvent( + JNIEnv * env, jclass, jlong publisher_handle, jint event_type) +{ + auto * publisher = reinterpret_cast(publisher_handle); + if (!publisher) { + rcljava_throw_exception( + env, "java/lang/IllegalArgumentException", "passed rcl_publisher_t handle is NULL"); + return 0; + } + auto * event = static_cast(malloc(sizeof(rcl_event_t))); + if (!event) { + rcljava_throw_exception(env, "java/lang/OutOfMemoryError", "failed to allocate rcl_event_t"); + return 0; + } + *event = rcl_get_zero_initialized_event(); + rcl_ret_t ret = rcl_publisher_event_init( + event, publisher, static_cast(event_type)); + if (RCL_RET_OK != ret) { + std::string msg = "Failed to create event: " + std::string(rcl_get_error_string().str); + rcl_reset_error(); + rcljava_throw_rclexception(env, ret, msg); + free(event); + return 0; + } + return reinterpret_cast(event); +} diff --git a/rcljava/src/main/cpp/org_ros2_rcljava_publisher_statuses_LivelinessLost.cpp b/rcljava/src/main/cpp/org_ros2_rcljava_publisher_statuses_LivelinessLost.cpp new file mode 100644 index 00000000..8e1118fc --- /dev/null +++ b/rcljava/src/main/cpp/org_ros2_rcljava_publisher_statuses_LivelinessLost.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_publisher_statuses_LivelinessLost.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_publisher_statuses_LivelinessLost_nativeAllocateRCLStatusEvent( + JNIEnv * env, jclass) +{ + void * p = malloc(sizeof(rmw_liveliness_lost_status_t)); + if (!p) { + rcljava_throw_exception( + env, "java/lang/OutOfMemoryError", "failed to allocate liveliness lost status"); + } + return reinterpret_cast(p); +} + +JNIEXPORT void JNICALL +Java_org_ros2_rcljava_publisher_statuses_LivelinessLost_nativeDeallocateRCLStatusEvent( + JNIEnv *, jclass, jlong handle) +{ + free(reinterpret_cast(handle)); +} + +JNIEXPORT void JNICALL +Java_org_ros2_rcljava_publisher_statuses_LivelinessLost_nativeFromRCLEvent( + 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_publisher_statuses_LivelinessLost_nativeGetPublisherEventType( + JNIEnv *, jclass) +{ + return RCL_PUBLISHER_LIVELINESS_LOST; +} diff --git a/rcljava/src/main/cpp/org_ros2_rcljava_publisher_statuses_OfferedDeadlineMissed.cpp b/rcljava/src/main/cpp/org_ros2_rcljava_publisher_statuses_OfferedDeadlineMissed.cpp new file mode 100644 index 00000000..bce491af --- /dev/null +++ b/rcljava/src/main/cpp/org_ros2_rcljava_publisher_statuses_OfferedDeadlineMissed.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_publisher_statuses_OfferedDeadlineMissed.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_publisher_statuses_OfferedDeadlineMissed_nativeAllocateRCLStatusEvent( + JNIEnv * env, jclass) +{ + void * p = malloc(sizeof(rmw_offered_deadline_missed_status_t)); + if (!p) { + rcljava_throw_exception( + env, "java/lang/OutOfMemoryError", "failed to allocate deadline missed status"); + } + return reinterpret_cast(p); +} + +JNIEXPORT void JNICALL +Java_org_ros2_rcljava_publisher_statuses_OfferedDeadlineMissed_nativeDeallocateRCLStatusEvent( + JNIEnv *, jclass, jlong handle) +{ + free(reinterpret_cast(handle)); +} + +JNIEXPORT void JNICALL +Java_org_ros2_rcljava_publisher_statuses_OfferedDeadlineMissed_nativeFromRCLEvent( + 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_publisher_statuses_OfferedDeadlineMissed_nativeGetPublisherEventType( + JNIEnv *, jclass) +{ + return RCL_PUBLISHER_OFFERED_DEADLINE_MISSED; +} diff --git a/rcljava/src/main/cpp/org_ros2_rcljava_publisher_statuses_OfferedQosIncompatible.cpp b/rcljava/src/main/cpp/org_ros2_rcljava_publisher_statuses_OfferedQosIncompatible.cpp new file mode 100644 index 00000000..d2e681c3 --- /dev/null +++ b/rcljava/src/main/cpp/org_ros2_rcljava_publisher_statuses_OfferedQosIncompatible.cpp @@ -0,0 +1,26 @@ +// 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_publisher_statuses_OfferedQosIncompatible.h" + +#include + +#include "rcl/event.h" + +JNIEXPORT jint JNICALL +Java_org_ros2_rcljava_publisher_statuses_OfferedQosIncompatible_nativeGetEventType( + JNIEnv *, jclass) +{ + return RCL_PUBLISHER_OFFERED_INCOMPATIBLE_QOS; +} diff --git a/rcljava/src/main/cpp/org_ros2_rcljava_subscription_SubscriptionImpl.cpp b/rcljava/src/main/cpp/org_ros2_rcljava_subscription_SubscriptionImpl.cpp index 151bc0a0..5c995f26 100644 --- a/rcljava/src/main/cpp/org_ros2_rcljava_subscription_SubscriptionImpl.cpp +++ b/rcljava/src/main/cpp/org_ros2_rcljava_subscription_SubscriptionImpl.cpp @@ -19,6 +19,7 @@ #include #include "rcl/error_handling.h" +#include "rcl/event.h" #include "rcl/node.h" #include "rcl/rcl.h" #include "rmw/rmw.h" @@ -29,6 +30,7 @@ #include "org_ros2_rcljava_subscription_SubscriptionImpl.h" +using rcljava_common::exceptions::rcljava_throw_exception; using rcljava_common::exceptions::rcljava_throw_rclexception; JNIEXPORT void JNICALL @@ -61,3 +63,31 @@ Java_org_ros2_rcljava_subscription_SubscriptionImpl_nativeDispose( rcljava_throw_rclexception(env, ret, msg); } } + +JNIEXPORT jlong +JNICALL Java_org_ros2_rcljava_subscription_SubscriptionImpl_nativeCreateEvent( + JNIEnv * env, jclass, jlong subscription_handle, jint event_type) +{ + auto * subscription = reinterpret_cast(subscription_handle); + if (!subscription) { + rcljava_throw_exception( + env, "java/lang/IllegalArgumentException", "passed rcl_subscription_t handle is NULL"); + return 0; + } + auto * event = static_cast(malloc(sizeof(rcl_event_t))); + if (!event) { + rcljava_throw_exception(env, "java/lang/OutOfMemoryError", "failed to allocate rcl_event_t"); + return 0; + } + *event = rcl_get_zero_initialized_event(); + rcl_ret_t ret = rcl_subscription_event_init( + event, subscription, static_cast(event_type)); + if (RCL_RET_OK != ret) { + std::string msg = "Failed to create event: " + std::string(rcl_get_error_string().str); + rcl_reset_error(); + rcljava_throw_rclexception(env, ret, msg); + free(event); + return 0; + } + return reinterpret_cast(event); +} diff --git a/rcljava/src/main/cpp/org_ros2_rcljava_subscription_statuses_LivelinessChanged.cpp b/rcljava/src/main/cpp/org_ros2_rcljava_subscription_statuses_LivelinessChanged.cpp new file mode 100644 index 00000000..4c6102d5 --- /dev/null +++ b/rcljava/src/main/cpp/org_ros2_rcljava_subscription_statuses_LivelinessChanged.cpp @@ -0,0 +1,84 @@ +// 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_LivelinessChanged.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_LivelinessChanged_nativeAllocateRCLStatusEvent( + JNIEnv * env, jclass) +{ + void * p = malloc(sizeof(rmw_liveliness_changed_status_t)); + if (!p) { + rcljava_throw_exception( + env, "java/lang/OutOfMemoryError", "failed to allocate liveliness changed status"); + } + return reinterpret_cast(p); +} + +JNIEXPORT void JNICALL +Java_org_ros2_rcljava_subscription_statuses_LivelinessChanged_nativeDeallocateRCLStatusEvent( + JNIEnv *, jclass, jlong handle) +{ + free(reinterpret_cast(handle)); +} + +JNIEXPORT void JNICALL +Java_org_ros2_rcljava_subscription_statuses_LivelinessChanged_nativeFromRCLEvent( + 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 alive_count_fid = env->GetFieldID(clazz, "aliveCount", "I"); + if (env->ExceptionCheck()) { + return; + } + jfieldID not_alive_count_fid = env->GetFieldID(clazz, "notAliveCount", "I"); + if (env->ExceptionCheck()) { + return; + } + jfieldID alive_count_change_fid = env->GetFieldID(clazz, "aliveCountChange", "I"); + if (env->ExceptionCheck()) { + return; + } + jfieldID not_alive_count_change_fid = env->GetFieldID(clazz, "notAliveCountChange", "I"); + if (env->ExceptionCheck()) { + return; + } + env->SetIntField(self, alive_count_fid, p->alive_count); + env->SetIntField(self, not_alive_count_fid, p->not_alive_count); + env->SetIntField(self, alive_count_change_fid, p->alive_count_change); + env->SetIntField(self, not_alive_count_change_fid, p->not_alive_count_change); +} + +JNIEXPORT jint JNICALL +Java_org_ros2_rcljava_subscription_statuses_LivelinessChanged_nativeGetSubscriptionEventType( + JNIEnv *, jclass) +{ + return RCL_SUBSCRIPTION_LIVELINESS_CHANGED; +} diff --git a/rcljava/src/main/cpp/org_ros2_rcljava_subscription_statuses_RequestedDeadlineMissed.cpp b/rcljava/src/main/cpp/org_ros2_rcljava_subscription_statuses_RequestedDeadlineMissed.cpp new file mode 100644 index 00000000..b3f06cbc --- /dev/null +++ b/rcljava/src/main/cpp/org_ros2_rcljava_subscription_statuses_RequestedDeadlineMissed.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_RequestedDeadlineMissed.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_RequestedDeadlineMissed_nativeAllocateRCL( + JNIEnv * env, jclass) +{ + void * p = malloc(sizeof(rmw_requested_deadline_missed_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_RequestedDeadlineMissed_nativeDeallocateRCL( + JNIEnv *, jclass, jlong handle) +{ + free(reinterpret_cast(handle)); +} + +JNIEXPORT void JNICALL +Java_org_ros2_rcljava_subscription_statuses_RequestedDeadlineMissed_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_RequestedDeadlineMissed_nativeGetEventType( + JNIEnv *, jclass) +{ + return RCL_SUBSCRIPTION_REQUESTED_DEADLINE_MISSED; +} diff --git a/rcljava/src/main/cpp/org_ros2_rcljava_subscription_statuses_RequestedQosIncompatible.cpp b/rcljava/src/main/cpp/org_ros2_rcljava_subscription_statuses_RequestedQosIncompatible.cpp new file mode 100644 index 00000000..fc506e5e --- /dev/null +++ b/rcljava/src/main/cpp/org_ros2_rcljava_subscription_statuses_RequestedQosIncompatible.cpp @@ -0,0 +1,26 @@ +// 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_RequestedQosIncompatible.h" + +#include + +#include "rcl/event.h" + +JNIEXPORT jint JNICALL +Java_org_ros2_rcljava_subscription_statuses_RequestedQosIncompatible_nativeGetEventType( + JNIEnv *, jclass) +{ + return RCL_SUBSCRIPTION_REQUESTED_INCOMPATIBLE_QOS; +} diff --git a/rcljava/src/main/java/org/ros2/rcljava/detail/QosIncompatibleStatus.java b/rcljava/src/main/java/org/ros2/rcljava/detail/QosIncompatibleStatus.java new file mode 100644 index 00000000..91a3e9b4 --- /dev/null +++ b/rcljava/src/main/java/org/ros2/rcljava/detail/QosIncompatibleStatus.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.detail; + +import org.ros2.rcljava.common.JNIUtils; +import org.ros2.rcljava.events.EventStatus; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class serves as a bridge between rmw_qos_incompatible_event_status_t + * and RCLJava. + */ +public class QosIncompatibleStatus implements EventStatus { + public int totalCount; + public int totalCountChange; + public PolicyKind lastPolicyKind; + + public enum PolicyKind { + INVALID, + DURABILITY, + DEADLINE, + LIVELINESS, + RELIABILITY, + HISTORY, + LIFESPAN; + } + + public final long allocateRCLStatusEvent() { + return nativeAllocateRCLStatusEvent(); + } + public final void deallocateRCLStatusEvent(long handle) { + nativeDeallocateRCLStatusEvent(handle); + } + public final void fromRCLEvent(long handle) { + nativeFromRCLEvent(handle); + } + + private static final Logger logger = LoggerFactory.getLogger(QosIncompatibleStatus.class); + static { + try { + JNIUtils.loadImplementation(QosIncompatibleStatus.class); + } catch (UnsatisfiedLinkError ule) { + logger.error("Native code library failed to load.\n" + ule); + System.exit(1); + } + } + + private static native long nativeAllocateRCLStatusEvent(); + private static native void nativeDeallocateRCLStatusEvent(long handle); + private native void nativeFromRCLEvent(long handle); +} diff --git a/rcljava/src/main/java/org/ros2/rcljava/events/EventHandler.java b/rcljava/src/main/java/org/ros2/rcljava/events/EventHandler.java new file mode 100644 index 00000000..6f6c7985 --- /dev/null +++ b/rcljava/src/main/java/org/ros2/rcljava/events/EventHandler.java @@ -0,0 +1,41 @@ +// 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.events; + +import java.lang.ref.WeakReference; + +import org.ros2.rcljava.interfaces.Disposable; +import org.ros2.rcljava.events.EventStatus; + +/** + * This class serves as a bridge between a rcl_event_t and RCLJava. + * An EventHandler must be created via + * @{link Publisher#createEventHandler(Class<T>, Consumer<T>)} + * @{link Subscription#createEventHandler(Class<T>, Consumer<T>)} + * + * @param The event status type. + * @param The parent class type. + */ +public interface EventHandler extends Disposable { + /** + * @return A weak reference to the parent. + */ + WeakReference getParentReference(); + + /** + * @return Execute the registered event callback + */ + void executeCallback(); +} diff --git a/rcljava/src/main/java/org/ros2/rcljava/events/EventHandlerImpl.java b/rcljava/src/main/java/org/ros2/rcljava/events/EventHandlerImpl.java new file mode 100644 index 00000000..74e4e6dd --- /dev/null +++ b/rcljava/src/main/java/org/ros2/rcljava/events/EventHandlerImpl.java @@ -0,0 +1,142 @@ +// 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.events; + +import java.lang.ref.WeakReference; +import java.lang.reflect.InvocationTargetException; +import java.util.function.Supplier; + +import org.ros2.rcljava.common.JNIUtils; +import org.ros2.rcljava.consumers.Consumer; +import org.ros2.rcljava.events.EventHandler; +import org.ros2.rcljava.events.EventStatus; +import org.ros2.rcljava.interfaces.Disposable; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class serves as a bridge between a rcl_event_t and RCLJava. + * An EventHandler must be created via + * @{link Publisher#createEventHandler(Class<T>, Consumer<T>)} + * @{link Subscription#createEventHandler(Class<T>, Consumer<T>)} + * + * @param The status event type. + * @param The parent class type. + */ +public class EventHandlerImpl< + T extends EventStatus, + ParentT extends Disposable> +implements EventHandler { + private static final Logger logger = LoggerFactory.getLogger(EventHandlerImpl.class); + + static { + try { + JNIUtils.loadImplementation(EventHandlerImpl.class); + } catch (UnsatisfiedLinkError ule) { + logger.error("Native code library failed to load.\n" + ule); + System.exit(1); + } + } + + /** + * Constructor. + * + * @param parentReference A {@link java.lang.ref.WeakReference} to the + * @{link org.ros2.rcljava.Publisher} or @{link org.ros2.rcljava.Subscription} + * that created this event handler. + * @param handle A pointer to the underlying ROS 2 event structure, as an integer. + * Must not be zero. + * Ownership of the passed `handle` is taken, and this object is responsible + * of disposing it. That can be done using @{link EventHandler#dispose()}. + * @param eventStatusFactory Factory of an event status. + * @param callback The callback function that will be called when the event + * is triggered. + * @param disposeCallback Callback that will be called when this object is disposed. + */ + public EventHandlerImpl( + final WeakReference parentReference, + final long handle, + final Supplier eventStatusFactory, + final Consumer callback, + final Consumer disposeCallback) { + this.parentReference = parentReference; + this.handle = handle; + this.eventStatusFactory = eventStatusFactory; + this.callback = callback; + this.disposeCallback = disposeCallback; + } + + /** + * {@inheritDoc} + */ + public final WeakReference getParentReference() { + return this.parentReference; + } + + /** + * {@inheritDoc} + */ + public final long getHandle() { + return this.handle; + } + + /** + * Destroy a ROS 2 event (rcl_event_t). + * + * @param handle A pointer to the underlying ROS 2 event structure, + * as an integer. Must not be zero. + */ + private static native void nativeDispose(long handle); + + /** + * {@inheritDoc} + */ + public synchronized final void dispose() { + if (this.handle == 0) { + return; + } + this.disposeCallback.accept(this); + nativeDispose(this.handle); + this.handle = 0; + } + + /** + * Takes the RCL event status and returns a pointer to it. + * + * @param event_handle A pointer to the underlying ROS 2 event (rcl_event_t). + * @param event_status_handle A pointer to the underlying ROS 2 event status (void *). + * Must not be zero. + */ + private static native void nativeTake(long event_handle, long event_status_handle); + + /** + * {@inheritDoc} + */ + public synchronized final void executeCallback() { + T eventStatus = eventStatusFactory.get(); + long nativeEventStatusHandle = eventStatus.allocateRCLStatusEvent(); + nativeTake(this.handle, nativeEventStatusHandle); + eventStatus.fromRCLEvent(nativeEventStatusHandle); + eventStatus.deallocateRCLStatusEvent(nativeEventStatusHandle); + this.callback.accept(eventStatus); + } + + private final Supplier eventStatusFactory; + private final WeakReference parentReference; + private long handle; + private final Consumer callback; + private final Consumer disposeCallback; +} diff --git a/rcljava/src/main/java/org/ros2/rcljava/events/EventStatus.java b/rcljava/src/main/java/org/ros2/rcljava/events/EventStatus.java new file mode 100644 index 00000000..4b129826 --- /dev/null +++ b/rcljava/src/main/java/org/ros2/rcljava/events/EventStatus.java @@ -0,0 +1,43 @@ +// 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.events; + +import org.ros2.rcljava.interfaces.Disposable; + +/** + * This class serves as a bridge between ROS2's rcl event status types and RCLJava. + */ +public interface EventStatus { + /** + * Allocates an rcl event status. + * + * @return A pointer to the allocated status. + */ + long allocateRCLStatusEvent(); + + /** + * Deallocates an rcl event status. + * + * @param handle Pointer to a previously allocated event status. + */ + void deallocateRCLStatusEvent(long handle); + + /** + * Loads the event with the data of the rcl event status. + * + * @param handle A pointer to the underlying event status. + */ + void fromRCLEvent(long handle); +} diff --git a/rcljava/src/main/java/org/ros2/rcljava/events/PublisherEventStatus.java b/rcljava/src/main/java/org/ros2/rcljava/events/PublisherEventStatus.java new file mode 100644 index 00000000..a1eb3375 --- /dev/null +++ b/rcljava/src/main/java/org/ros2/rcljava/events/PublisherEventStatus.java @@ -0,0 +1,28 @@ +// 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.events; + +import org.ros2.rcljava.events.EventStatus; + +/** + * This class serves as a bridge between ROS2's rcl publisher event + * status types and RCLJava. + */ +public interface PublisherEventStatus extends EventStatus { + /** + * @return The status event type. + */ + int getPublisherEventType(); +} diff --git a/rcljava/src/main/java/org/ros2/rcljava/events/SubscriptionEventStatus.java b/rcljava/src/main/java/org/ros2/rcljava/events/SubscriptionEventStatus.java new file mode 100644 index 00000000..55d93372 --- /dev/null +++ b/rcljava/src/main/java/org/ros2/rcljava/events/SubscriptionEventStatus.java @@ -0,0 +1,28 @@ +// 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.events; + +import org.ros2.rcljava.events.EventStatus; + +/** + * This class serves as a bridge between ROS2's rcl subscription event + * status types and RCLJava. + */ +public interface SubscriptionEventStatus extends EventStatus { + /** + * @return The status event type. + */ + int getSubscriptionEventType(); +} diff --git a/rcljava/src/main/java/org/ros2/rcljava/executors/AnyExecutable.java b/rcljava/src/main/java/org/ros2/rcljava/executors/AnyExecutable.java index f6ce97b9..dd3d3039 100644 --- a/rcljava/src/main/java/org/ros2/rcljava/executors/AnyExecutable.java +++ b/rcljava/src/main/java/org/ros2/rcljava/executors/AnyExecutable.java @@ -16,6 +16,7 @@ package org.ros2.rcljava.executors; import org.ros2.rcljava.client.Client; +import org.ros2.rcljava.events.EventHandler; import org.ros2.rcljava.subscription.Subscription; import org.ros2.rcljava.service.Service; import org.ros2.rcljava.timer.Timer; @@ -25,4 +26,5 @@ public class AnyExecutable { public Subscription subscription; public Service service; public Client client; + public EventHandler eventHandler; } diff --git a/rcljava/src/main/java/org/ros2/rcljava/executors/BaseExecutor.java b/rcljava/src/main/java/org/ros2/rcljava/executors/BaseExecutor.java index f00ddf75..1ed9218e 100644 --- a/rcljava/src/main/java/org/ros2/rcljava/executors/BaseExecutor.java +++ b/rcljava/src/main/java/org/ros2/rcljava/executors/BaseExecutor.java @@ -21,6 +21,7 @@ import java.util.List; import java.util.Map; +import java.util.Collection; import java.util.concurrent.BlockingQueue; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.LinkedBlockingQueue; @@ -31,11 +32,13 @@ import org.ros2.rcljava.RCLJava; import org.ros2.rcljava.client.Client; import org.ros2.rcljava.common.JNIUtils; +import org.ros2.rcljava.events.EventHandler; import org.ros2.rcljava.executors.AnyExecutable; import org.ros2.rcljava.executors.Executor; import org.ros2.rcljava.interfaces.MessageDefinition; import org.ros2.rcljava.interfaces.ServiceDefinition; import org.ros2.rcljava.node.ComposableNode; +import org.ros2.rcljava.publisher.Publisher; import org.ros2.rcljava.service.RMWRequestId; import org.ros2.rcljava.service.Service; import org.ros2.rcljava.subscription.Subscription; @@ -64,6 +67,8 @@ public class BaseExecutor { private List> clientHandles = new ArrayList>(); + private List> eventHandles = new ArrayList>(); + protected void addNode(ComposableNode node) { this.nodes.add(node); } @@ -158,6 +163,11 @@ protected void executeAnyExecutable(AnyExecutable anyExecutable) { } clientHandles.remove(anyExecutable.client.getHandle()); } + + if (anyExecutable.eventHandler != null) { + anyExecutable.eventHandler.executeCallback(); + eventHandles.remove(anyExecutable.eventHandler.getHandle()); + } } protected void waitForWork(long timeout) { @@ -165,11 +175,25 @@ protected void waitForWork(long timeout) { this.timerHandles.clear(); this.serviceHandles.clear(); this.clientHandles.clear(); + this.eventHandles.clear(); for (ComposableNode node : this.nodes) { for (Subscription subscription : node.getNode().getSubscriptions()) { this.subscriptionHandles.add(new AbstractMap.SimpleEntry( subscription.getHandle(), subscription)); + Collection eventHandlers = subscription.getEventHandlers(); + for (EventHandler eventHandler : eventHandlers) { + this.eventHandles.add(new AbstractMap.SimpleEntry( + eventHandler.getHandle(), eventHandler)); + } + } + + for (Publisher publisher : node.getNode().getPublishers()) { + Collection eventHandlers = publisher.getEventHandlers(); + for (EventHandler eventHandler : eventHandlers) { + this.eventHandles.add(new AbstractMap.SimpleEntry( + eventHandler.getHandle(), eventHandler)); + } } for (Timer timer : node.getNode().getTimers()) { @@ -191,6 +215,7 @@ protected void waitForWork(long timeout) { int timersSize = 0; int clientsSize = 0; int servicesSize = 0; + int eventsSize = this.eventHandles.size(); for (ComposableNode node : this.nodes) { subscriptionsSize += node.getNode().getSubscriptions().size(); @@ -205,7 +230,9 @@ protected void waitForWork(long timeout) { long waitSetHandle = nativeGetZeroInitializedWaitSet(); long contextHandle = RCLJava.getDefaultContext().getHandle(); - nativeWaitSetInit(waitSetHandle, contextHandle, subscriptionsSize, 0, timersSize, clientsSize, servicesSize, 0); + nativeWaitSetInit( + waitSetHandle, contextHandle, subscriptionsSize, 0, + timersSize, clientsSize, servicesSize, eventsSize); nativeWaitSetClear(waitSetHandle); @@ -225,6 +252,10 @@ protected void waitForWork(long timeout) { nativeWaitSetAddClient(waitSetHandle, entry.getKey()); } + for (Map.Entry entry : this.eventHandles) { + nativeWaitSetAddEvent(waitSetHandle, entry.getKey()); + } + nativeWait(waitSetHandle, timeout); for (int i = 0; i < this.subscriptionHandles.size(); ++i) { @@ -251,6 +282,12 @@ protected void waitForWork(long timeout) { } } + for (int i = 0; i < this.eventHandles.size(); ++i) { + if (!nativeWaitSetEventIsReady(waitSetHandle, i)) { + this.eventHandles.get(i).setValue(null); + } + } + Iterator> subscriptionIterator = this.subscriptionHandles.iterator(); while (subscriptionIterator.hasNext()) { @@ -284,6 +321,14 @@ protected void waitForWork(long timeout) { } } + Iterator> eventIterator = this.eventHandles.iterator(); + while (eventIterator.hasNext()) { + Map.Entry entry = eventIterator.next(); + if (entry.getValue() == null) { + eventIterator.remove(); + } + } + nativeDisposeWaitSet(waitSetHandle); } @@ -325,6 +370,14 @@ protected AnyExecutable getNextExecutable() { } } + for (Map.Entry entry : this.eventHandles) { + if (entry.getValue() != null) { + anyExecutable.eventHandler = entry.getValue(); + entry.setValue(null); + return anyExecutable; + } + } + return null; } @@ -405,6 +458,8 @@ private static native MessageDefinition nativeTake( private static native void nativeWaitSetAddTimer(long waitSetHandle, long timerHandle); + private static native void nativeWaitSetAddEvent(long waitSetHandle, long eventHandle); + private static native RMWRequestId nativeTakeRequest(long serviceHandle, long requestFromJavaConverterHandle, long requestToJavaConverterHandle, long requestDestructorHandle, MessageDefinition requestMessage); @@ -421,6 +476,8 @@ private static native RMWRequestId nativeTakeResponse(long clientHandle, private static native boolean nativeWaitSetTimerIsReady(long waitSetHandle, long index); + private static native boolean nativeWaitSetEventIsReady(long waitSetHandle, long index); + private static native boolean nativeWaitSetServiceIsReady(long waitSetHandle, long index); private static native boolean nativeWaitSetClientIsReady(long waitSetHandle, long index); diff --git a/rcljava/src/main/java/org/ros2/rcljava/publisher/Publisher.java b/rcljava/src/main/java/org/ros2/rcljava/publisher/Publisher.java index 0d2427b7..47c857ff 100644 --- a/rcljava/src/main/java/org/ros2/rcljava/publisher/Publisher.java +++ b/rcljava/src/main/java/org/ros2/rcljava/publisher/Publisher.java @@ -16,9 +16,14 @@ package org.ros2.rcljava.publisher; import java.lang.ref.WeakReference; +import java.util.Collection; +import java.util.function.Supplier; +import org.ros2.rcljava.consumers.Consumer; import org.ros2.rcljava.interfaces.Disposable; import org.ros2.rcljava.interfaces.MessageDefinition; +import org.ros2.rcljava.events.EventHandler; +import org.ros2.rcljava.events.PublisherEventStatus; import org.ros2.rcljava.node.Node; /** @@ -41,4 +46,30 @@ public interface Publisher extends Disposable { * that created this publisher. */ WeakReference getNodeReference(); + + /** + * Create an event handler. + * + * @param A publisher event status type. + * @param factory A factory that can instantiate an event status of type T. + * @param callback Callback that will be called when the event is triggered. + */ + EventHandler createEventHandler( + Supplier factory, Consumer callback); + + /** + * Remove a previously registered event handler. + * + * @param A publisher event status type. + * @param eventHandler An event handler that was registered previously in this object. + */ + void removeEventHandler( + EventHandler eventHandler); + + /** + * Get the event handlers that were registered in this Publisher. + * + * @return The registered event handlers. + */ + Collection getEventHandlers(); } diff --git a/rcljava/src/main/java/org/ros2/rcljava/publisher/PublisherImpl.java b/rcljava/src/main/java/org/ros2/rcljava/publisher/PublisherImpl.java index f3fa717b..0e93531e 100644 --- a/rcljava/src/main/java/org/ros2/rcljava/publisher/PublisherImpl.java +++ b/rcljava/src/main/java/org/ros2/rcljava/publisher/PublisherImpl.java @@ -16,10 +16,17 @@ package org.ros2.rcljava.publisher; import java.lang.ref.WeakReference; +import java.util.Collection; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.function.Supplier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.ros2.rcljava.consumers.Consumer; +import org.ros2.rcljava.events.EventHandler; +import org.ros2.rcljava.events.EventHandlerImpl; +import org.ros2.rcljava.events.PublisherEventStatus; import org.ros2.rcljava.RCLJava; import org.ros2.rcljava.common.JNIUtils; import org.ros2.rcljava.interfaces.MessageDefinition; @@ -53,6 +60,8 @@ public class PublisherImpl implements Publisher */ private final String topic; + private final Collection eventHandlers; + /** * Constructor. * @@ -67,6 +76,7 @@ public PublisherImpl( this.nodeReference = nodeReference; this.handle = handle; this.topic = topic; + this.eventHandlers = new LinkedBlockingQueue(); } /** @@ -101,6 +111,63 @@ public final WeakReference getNodeReference() { return this.nodeReference; } + /** + * {@inheritDoc} + */ + public final + EventHandler + createEventHandler(Supplier factory, Consumer callback) { + final WeakReference> weakEventHandlers = new WeakReference( + this.eventHandlers); + Consumer disposeCallback = new Consumer() { + public void accept(EventHandler eventHandler) { + Collection eventHandlers = weakEventHandlers.get(); + if (eventHandlers != null) { + eventHandlers.remove(eventHandler); + } + } + }; + T status = factory.get(); + long eventHandle = nativeCreateEvent(this.handle, status.getPublisherEventType()); + EventHandler eventHandler = new EventHandlerImpl( + new WeakReference(this), eventHandle, factory, callback, disposeCallback); + this.eventHandlers.add(eventHandler); + return eventHandler; + } + + /** + * {@inheritDoc} + */ + public final + void removeEventHandler( + EventHandler eventHandler) + { + if (!this.eventHandlers.remove(eventHandler)) { + throw new IllegalArgumentException("The passed eventHandler wasn't created by this publisher"); + } + eventHandler.dispose(); + } + + /** + * {@inheritDoc} + */ + public final + Collection getEventHandlers() { + return this.eventHandlers; + } + + /** + * Create a publisher event (rcl_event_t) + * + * The ownership of the created event handle will immediately be transferred to an + * @{link EventHandlerImpl}, that will be responsible of disposing it. + * + * @param handle A pointer to the underlying ROS2 publisher structure. + * Must not be zero. + * @param eventType The rcl event type. + */ + private static native long nativeCreateEvent(long handle, int eventType); + /** * Destroy a ROS2 publisher (rcl_publisher_t). * @@ -115,6 +182,10 @@ public final WeakReference getNodeReference() { * {@inheritDoc} */ public final void dispose() { + for (EventHandler eventHandler : this.eventHandlers) { + eventHandler.dispose(); + } + this.eventHandlers.clear(); Node node = this.nodeReference.get(); if (node != null) { node.removePublisher(this); diff --git a/rcljava/src/main/java/org/ros2/rcljava/publisher/statuses/LivelinessLost.java b/rcljava/src/main/java/org/ros2/rcljava/publisher/statuses/LivelinessLost.java new file mode 100644 index 00000000..bf49a1f0 --- /dev/null +++ b/rcljava/src/main/java/org/ros2/rcljava/publisher/statuses/LivelinessLost.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.publisher.statuses; + +import java.util.function.Supplier; + +import org.ros2.rcljava.common.JNIUtils; +import org.ros2.rcljava.events.PublisherEventStatus; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class serves as a bridge between a rmw_liveliness_lost_status_t and RCLJava. + */ +public class LivelinessLost implements PublisherEventStatus { + public int totalCount; + public int totalCountChange; + + public final long allocateRCLStatusEvent() { + return nativeAllocateRCLStatusEvent(); + } + public final void deallocateRCLStatusEvent(long handle) { + nativeDeallocateRCLStatusEvent(handle); + } + public final void fromRCLEvent(long handle) { + nativeFromRCLEvent(handle); + } + public final int getPublisherEventType() { + return nativeGetPublisherEventType(); + } + // TODO(ivanpauno): Remove this when -source 8 can be used (method references for the win) + public static final Supplier factory = new Supplier() { + public LivelinessLost get() { + return new LivelinessLost(); + } + }; + + private static final Logger logger = LoggerFactory.getLogger(LivelinessLost.class); + static { + try { + JNIUtils.loadImplementation(LivelinessLost.class); + } catch (UnsatisfiedLinkError ule) { + logger.error("Native code library failed to load.\n" + ule); + System.exit(1); + } + } + + private static native long nativeAllocateRCLStatusEvent(); + private static native void nativeDeallocateRCLStatusEvent(long handle); + private native void nativeFromRCLEvent(long handle); + private static native int nativeGetPublisherEventType(); +} diff --git a/rcljava/src/main/java/org/ros2/rcljava/publisher/statuses/OfferedDeadlineMissed.java b/rcljava/src/main/java/org/ros2/rcljava/publisher/statuses/OfferedDeadlineMissed.java new file mode 100644 index 00000000..f4409806 --- /dev/null +++ b/rcljava/src/main/java/org/ros2/rcljava/publisher/statuses/OfferedDeadlineMissed.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.publisher.statuses; + +import java.util.function.Supplier; + +import org.ros2.rcljava.common.JNIUtils; +import org.ros2.rcljava.events.PublisherEventStatus; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class serves as a bridge between a rmw_offered_deadline_missed_status_t and RCLJava. + */ +public class OfferedDeadlineMissed implements PublisherEventStatus { + public int totalCount; + public int totalCountChange; + + public final long allocateRCLStatusEvent() { + return nativeAllocateRCLStatusEvent(); + } + public final void deallocateRCLStatusEvent(long handle) { + nativeDeallocateRCLStatusEvent(handle); + } + public final void fromRCLEvent(long handle) { + nativeFromRCLEvent(handle); + } + public final int getPublisherEventType() { + return nativeGetPublisherEventType(); + } + // TODO(ivanpauno): Remove this when -source 8 can be used (method references for the win) + public static final Supplier factory = new Supplier() { + public OfferedDeadlineMissed get() { + return new OfferedDeadlineMissed(); + } + }; + + private static final Logger logger = LoggerFactory.getLogger(OfferedDeadlineMissed.class); + static { + try { + JNIUtils.loadImplementation(OfferedDeadlineMissed.class); + } catch (UnsatisfiedLinkError ule) { + logger.error("Native code library failed to load.\n" + ule); + System.exit(1); + } + } + + private static native long nativeAllocateRCLStatusEvent(); + private static native void nativeDeallocateRCLStatusEvent(long handle); + private native void nativeFromRCLEvent(long handle); + private static native int nativeGetPublisherEventType(); +} diff --git a/rcljava/src/main/java/org/ros2/rcljava/publisher/statuses/OfferedQosIncompatible.java b/rcljava/src/main/java/org/ros2/rcljava/publisher/statuses/OfferedQosIncompatible.java new file mode 100644 index 00000000..ba633cd6 --- /dev/null +++ b/rcljava/src/main/java/org/ros2/rcljava/publisher/statuses/OfferedQosIncompatible.java @@ -0,0 +1,53 @@ +// 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.publisher.statuses; + +import java.util.function.Supplier; + +import org.ros2.rcljava.common.JNIUtils; +import org.ros2.rcljava.detail.QosIncompatibleStatus; +import org.ros2.rcljava.events.PublisherEventStatus; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class serves as a bridge between rmw_offered_qos_incompatible_event_status_t and RCLJava. + */ +public class OfferedQosIncompatible +extends QosIncompatibleStatus implements PublisherEventStatus { + public final int getPublisherEventType() { + 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 OfferedQosIncompatible get() { + return new OfferedQosIncompatible(); + } + }; + + private static final Logger logger = LoggerFactory.getLogger(OfferedQosIncompatible.class); + static { + try { + JNIUtils.loadImplementation(OfferedQosIncompatible.class); + } catch (UnsatisfiedLinkError ule) { + logger.error("Native code library failed to load.\n" + ule); + System.exit(1); + } + } + + private static native int nativeGetEventType(); +} diff --git a/rcljava/src/main/java/org/ros2/rcljava/subscription/Subscription.java b/rcljava/src/main/java/org/ros2/rcljava/subscription/Subscription.java index c74cc9bd..732d8bbb 100644 --- a/rcljava/src/main/java/org/ros2/rcljava/subscription/Subscription.java +++ b/rcljava/src/main/java/org/ros2/rcljava/subscription/Subscription.java @@ -16,8 +16,12 @@ package org.ros2.rcljava.subscription; import java.lang.ref.WeakReference; +import java.util.Collection; +import java.util.function.Supplier; import org.ros2.rcljava.consumers.Consumer; +import org.ros2.rcljava.events.EventHandler; +import org.ros2.rcljava.events.SubscriptionEventStatus; import org.ros2.rcljava.interfaces.Disposable; import org.ros2.rcljava.interfaces.MessageDefinition; import org.ros2.rcljava.node.Node; @@ -42,4 +46,30 @@ public interface Subscription extends Disposable { WeakReference getNodeReference(); void executeCallback(T message); + + /** + * Create an event handler. + * + * @param A subscription event status type. + * @param factory A factory that can instantiate an event status of type T. + * @param callback Callback that will be called when the event is triggered. + */ + EventHandler createEventHandler( + Supplier factory, Consumer callback); + + /** + * Remove a previously registered event handler. + * + * @param A subscription event status type. + * @param eventHandler An event handler that was registered previously in this object. + */ + void removeEventHandler( + EventHandler eventHandler); + + /** + * Get the event handlers that were registered in this Subscription. + * + * @return The registered event handlers. + */ + Collection getEventHandlers(); } diff --git a/rcljava/src/main/java/org/ros2/rcljava/subscription/SubscriptionImpl.java b/rcljava/src/main/java/org/ros2/rcljava/subscription/SubscriptionImpl.java index 47fce59a..8e20d81d 100644 --- a/rcljava/src/main/java/org/ros2/rcljava/subscription/SubscriptionImpl.java +++ b/rcljava/src/main/java/org/ros2/rcljava/subscription/SubscriptionImpl.java @@ -16,10 +16,17 @@ package org.ros2.rcljava.subscription; import java.lang.ref.WeakReference; +import java.util.Collection; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.function.Supplier; import org.ros2.rcljava.RCLJava; import org.ros2.rcljava.common.JNIUtils; +import org.ros2.rcljava.consumers.BiConsumer; import org.ros2.rcljava.consumers.Consumer; +import org.ros2.rcljava.events.EventHandler; +import org.ros2.rcljava.events.EventHandlerImpl; +import org.ros2.rcljava.events.SubscriptionEventStatus; import org.ros2.rcljava.interfaces.MessageDefinition; import org.ros2.rcljava.node.Node; @@ -68,6 +75,8 @@ public class SubscriptionImpl implements Subscripti */ private final Consumer callback; + private final Collection eventHandlers; + /** * Constructor. * @@ -90,6 +99,7 @@ public SubscriptionImpl(final WeakReference nodeReference, final long hand this.messageType = messageType; this.topic = topic; this.callback = callback; + this.eventHandlers = new LinkedBlockingQueue(); } /** @@ -113,6 +123,64 @@ public final WeakReference getNodeReference() { return this.nodeReference; } + /** + * {@inheritDoc} + */ + public final + EventHandler + createEventHandler(Supplier factory, Consumer callback) { + final WeakReference> weakEventHandlers = new WeakReference( + this.eventHandlers); + Consumer disposeCallback = new Consumer() { + public void accept(EventHandler eventHandler) { + Collection eventHandlers = weakEventHandlers.get(); + if (eventHandlers != null) { + eventHandlers.remove(eventHandler); + } + } + }; + T status = factory.get(); + long eventHandle = nativeCreateEvent(this.handle, status.getSubscriptionEventType()); + EventHandler eventHandler = new EventHandlerImpl( + new WeakReference(this), eventHandle, factory, callback, disposeCallback); + this.eventHandlers.add(eventHandler); + return eventHandler; + } + + /** + * {@inheritDoc} + */ + public final + void removeEventHandler( + EventHandler eventHandler) + { + if (!this.eventHandlers.remove(eventHandler)) { + throw new IllegalArgumentException( + "The passed eventHandler wasn't created by this subscription"); + } + eventHandler.dispose(); + } + + /** + * {@inheritDoc} + */ + public final + Collection getEventHandlers() { + return this.eventHandlers; + } + + /** + * Create a subscription event (rcl_event_t) + * + * The ownership of the created event handle will immediately be transferred to an + * @{link EventHandlerImpl}, that will be responsible of disposing it. + * + * @param handle A pointer to the underlying ROS 2 subscription structure. + * Must not be zero. + * @param eventType The rcl event type. + */ + private static native long nativeCreateEvent(long handle, int eventType); + /** * Destroy a ROS2 subscription (rcl_subscription_t). * @@ -127,6 +195,10 @@ public final WeakReference getNodeReference() { * {@inheritDoc} */ public final void dispose() { + for (EventHandler eventHandler : this.eventHandlers) { + eventHandler.dispose(); + } + this.eventHandlers.clear(); Node node = this.nodeReference.get(); if (node != null) { node.removeSubscription(this); diff --git a/rcljava/src/main/java/org/ros2/rcljava/subscription/statuses/LivelinessChanged.java b/rcljava/src/main/java/org/ros2/rcljava/subscription/statuses/LivelinessChanged.java new file mode 100644 index 00000000..f126508b --- /dev/null +++ b/rcljava/src/main/java/org/ros2/rcljava/subscription/statuses/LivelinessChanged.java @@ -0,0 +1,69 @@ +// 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 rmw_liveliness_changed_status_t + * and RCLJava. + */ +public class LivelinessChanged implements SubscriptionEventStatus { + public int aliveCount; + public int notAliveCount; + public int aliveCountChange; + public int notAliveCountChange; + + public final long allocateRCLStatusEvent() { + return nativeAllocateRCLStatusEvent(); + } + public final void deallocateRCLStatusEvent(long handle) { + nativeDeallocateRCLStatusEvent(handle); + } + public final void fromRCLEvent(long handle) { + nativeFromRCLEvent(handle); + } + public final int getSubscriptionEventType() { + return nativeGetSubscriptionEventType(); + } + // TODO(ivanpauno): Remove this when -source 8 can be used (method references for the win) + public static final Supplier + factory = new Supplier() { + public LivelinessChanged get() { + return new LivelinessChanged(); + } + }; + + private static final Logger logger = LoggerFactory.getLogger(LivelinessChanged.class); + static { + try { + JNIUtils.loadImplementation(LivelinessChanged.class); + } catch (UnsatisfiedLinkError ule) { + logger.error("Native code library failed to load.\n" + ule); + System.exit(1); + } + } + + private static native long nativeAllocateRCLStatusEvent(); + private static native void nativeDeallocateRCLStatusEvent(long handle); + private native void nativeFromRCLEvent(long handle); + private static native int nativeGetSubscriptionEventType(); +} diff --git a/rcljava/src/main/java/org/ros2/rcljava/subscription/statuses/RequestedDeadlineMissed.java b/rcljava/src/main/java/org/ros2/rcljava/subscription/statuses/RequestedDeadlineMissed.java new file mode 100644 index 00000000..fa56f36d --- /dev/null +++ b/rcljava/src/main/java/org/ros2/rcljava/subscription/statuses/RequestedDeadlineMissed.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_requested_deadline_missed_status_t and RCLJava. + */ +public class RequestedDeadlineMissed 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 RequestedDeadlineMissed get() { + return new RequestedDeadlineMissed(); + } + }; + + private static final Logger logger = LoggerFactory.getLogger(RequestedDeadlineMissed.class); + static { + try { + JNIUtils.loadImplementation(RequestedDeadlineMissed.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/main/java/org/ros2/rcljava/subscription/statuses/RequestedQosIncompatible.java b/rcljava/src/main/java/org/ros2/rcljava/subscription/statuses/RequestedQosIncompatible.java new file mode 100644 index 00000000..33c0ea38 --- /dev/null +++ b/rcljava/src/main/java/org/ros2/rcljava/subscription/statuses/RequestedQosIncompatible.java @@ -0,0 +1,54 @@ +// 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.detail.QosIncompatibleStatus; +import org.ros2.rcljava.events.SubscriptionEventStatus; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class serves as a bridge between rmw_requested_qos_incompatible_event_status_t + * and RCLJava. + */ +public class RequestedQosIncompatible +extends QosIncompatibleStatus implements SubscriptionEventStatus { + 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 RequestedQosIncompatible get() { + return new RequestedQosIncompatible(); + } + }; + + private static final Logger logger = LoggerFactory.getLogger(RequestedQosIncompatible.class); + static { + try { + JNIUtils.loadImplementation(RequestedQosIncompatible.class); + } catch (UnsatisfiedLinkError ule) { + logger.error("Native code library failed to load.\n" + ule); + System.exit(1); + } + } + + private static native int nativeGetEventType(); +} diff --git a/rcljava/src/test/java/org/ros2/rcljava/SpinTest.java b/rcljava/src/test/java/org/ros2/rcljava/SpinTest.java index f015d260..aa9b51ab 100644 --- a/rcljava/src/test/java/org/ros2/rcljava/SpinTest.java +++ b/rcljava/src/test/java/org/ros2/rcljava/SpinTest.java @@ -18,6 +18,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; +import java.lang.System; import java.util.concurrent.TimeUnit; import org.junit.AfterClass; @@ -26,11 +27,21 @@ import org.ros2.rcljava.RCLJava; import org.ros2.rcljava.concurrent.Callback; +import org.ros2.rcljava.consumers.Consumer; +import org.ros2.rcljava.events.EventHandler; +import org.ros2.rcljava.publisher.statuses.OfferedQosIncompatible; import org.ros2.rcljava.executors.Executor; import org.ros2.rcljava.executors.SingleThreadedExecutor; import org.ros2.rcljava.node.ComposableNode; import org.ros2.rcljava.node.Node; import org.ros2.rcljava.timer.WallTimer; +import org.ros2.rcljava.publisher.Publisher; +import org.ros2.rcljava.qos.policies.Durability; +import org.ros2.rcljava.qos.policies.History; +import org.ros2.rcljava.qos.policies.Reliability; +import org.ros2.rcljava.qos.QoSProfile; +import org.ros2.rcljava.subscription.Subscription; +import org.ros2.rcljava.subscription.statuses.RequestedQosIncompatible; public class SpinTest { public static class TimerCallback implements Callback { @@ -333,4 +344,119 @@ public Node getNode() { executor.spinAll(100*1000*1000); assertEquals(1, timerCallback.getCounter()); } + + // custom event consumer + public static class OfferedQosIncompatibleConsumer implements Consumer { + public boolean done = false; + + public void accept(final OfferedQosIncompatible status) { + assertEquals(status.totalCount, 1); + assertEquals(status.totalCountChange, 1); + assertEquals(status.lastPolicyKind, OfferedQosIncompatible.PolicyKind.RELIABILITY); + this.done = true; + } + } + + @Test + public final void testSpinPublisherEvent() { + String identifier = RCLJava.getRMWIdentifier(); + if (identifier.equals("rmw_fastrtps_cpp") || identifier.equals("rmw_fastrtps_dynamic_cpp")) { + // OfferedQosIncompatible events is not supported in these implementations. + return; + } + final Node node = RCLJava.createNode("test_node_spin_publisher_event"); + Publisher publisher = node.createPublisher( + std_msgs.msg.String.class, "test_topic_spin_publisher_event", new QoSProfile( + History.KEEP_LAST, 1, + Reliability.BEST_EFFORT, + Durability.VOLATILE, + false)); + // create a OfferedQoSIncompatible event handler with custom event consumer + OfferedQosIncompatibleConsumer eventConsumer = new OfferedQosIncompatibleConsumer(); + EventHandler eventHandler = publisher.createEventHandler( + OfferedQosIncompatible.factory, eventConsumer + ); + // create an incompatible subscription (reliable vs best effort publisher) + Subscription subscription = node.createSubscription( + std_msgs.msg.String.class, "test_topic_spin_publisher_event", + new Consumer() { + public void accept(final std_msgs.msg.String msg) {} + }, + new QoSProfile( + History.KEEP_LAST, 1, + Reliability.RELIABLE, + Durability.VOLATILE, + false)); + // set up executor + ComposableNode composableNode = new ComposableNode() { + public Node getNode() { + return node; + } + }; + Executor executor = new SingleThreadedExecutor(); + executor.addNode(composableNode); + long start = System.currentTimeMillis(); + do { + executor.spinAll((1000 + System.currentTimeMillis() - start) * 1000 * 1000); + } while (!eventConsumer.done && System.currentTimeMillis() < start + 1000); + assert(eventConsumer.done); + } + + public static class RequestedQosIncompatibleConsumer + implements Consumer { + public boolean done = false; + + public void accept(final RequestedQosIncompatible status) { + assertEquals(status.totalCount, 1); + assertEquals(status.totalCountChange, 1); + assertEquals(status.lastPolicyKind, RequestedQosIncompatible.PolicyKind.RELIABILITY); + this.done = true; + } + } + + @Test + public final void testSpinSubscriptionEvent() { + String identifier = RCLJava.getRMWIdentifier(); + if (identifier.equals("rmw_fastrtps_cpp") || identifier.equals("rmw_fastrtps_dynamic_cpp")) { + // RequestedQosIncompatible events is not supported in these implementations. + return; + } + final Node node = RCLJava.createNode("test_node_spin_subscription_event"); + // create an incompatible subscription (reliable vs best effort publisher) + Subscription subscription = node.createSubscription( + std_msgs.msg.String.class, "test_topic_spin_subscription_event", + new Consumer() { + public void accept(final std_msgs.msg.String msg) {} + }, + new QoSProfile( + History.KEEP_LAST, 1, + Reliability.RELIABLE, + Durability.VOLATILE, + false)); + // create a RequestedQoSIncompatible event handler with custom event consumer + RequestedQosIncompatibleConsumer eventConsumer = new RequestedQosIncompatibleConsumer(); + EventHandler eventHandler = subscription.createEventHandler( + RequestedQosIncompatible.factory, eventConsumer + ); + Publisher publisher = node.createPublisher( + std_msgs.msg.String.class, "test_topic_spin_subscription_event", new QoSProfile( + History.KEEP_LAST, 1, + Reliability.BEST_EFFORT, + Durability.VOLATILE, + false)); + + // set up executor + ComposableNode composableNode = new ComposableNode() { + public Node getNode() { + return node; + } + }; + Executor executor = new SingleThreadedExecutor(); + executor.addNode(composableNode); + long start = System.currentTimeMillis(); + do { + executor.spinAll((1000 + System.currentTimeMillis() - start) * 1000 * 1000); + } while (!eventConsumer.done && System.currentTimeMillis() < start + 1000); + assert(eventConsumer.done); + } } diff --git a/rcljava/src/test/java/org/ros2/rcljava/publisher/PublisherTest.java b/rcljava/src/test/java/org/ros2/rcljava/publisher/PublisherTest.java index bfacdd2a..1b1a4b98 100644 --- a/rcljava/src/test/java/org/ros2/rcljava/publisher/PublisherTest.java +++ b/rcljava/src/test/java/org/ros2/rcljava/publisher/PublisherTest.java @@ -22,6 +22,12 @@ import org.junit.Test; import org.ros2.rcljava.RCLJava; +import org.ros2.rcljava.consumers.Consumer; +import org.ros2.rcljava.events.EventHandler; +import org.ros2.rcljava.publisher.statuses.LivelinessLost; +import org.ros2.rcljava.publisher.statuses.OfferedDeadlineMissed; +import org.ros2.rcljava.publisher.statuses.OfferedQosIncompatible; +import org.ros2.rcljava.exceptions.RCLException; import org.ros2.rcljava.node.Node; public class PublisherTest { @@ -42,4 +48,76 @@ public final void testCreate() { assertNotEquals(0, publisher.getHandle()); RCLJava.shutdown(); } + + @Test + public final void testCreateLivelinessLostEvent() { + RCLJava.rclJavaInit(); + Node node = RCLJava.createNode("test_node"); + Publisher publisher = + node.createPublisher(std_msgs.msg.String.class, "test_topic"); + EventHandler eventHandler = publisher.createEventHandler( + LivelinessLost.factory, new Consumer() { + public void accept(final LivelinessLost 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()); + } + + @Test + public final void testCreateOfferedQosIncompatibleEvent() { + 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"); + Publisher publisher = + node.createPublisher(std_msgs.msg.String.class, "test_topic"); + EventHandler eventHandler = publisher.createEventHandler( + OfferedQosIncompatible.factory, new Consumer() { + public void accept(final OfferedQosIncompatible status) { + assertEquals(status.totalCount, 0); + assertEquals(status.totalCountChange, 0); + assertEquals(status.lastPolicyKind, OfferedQosIncompatible.PolicyKind.INVALID); + } + } + ); + assertNotEquals(0, eventHandler.getHandle()); + assertEquals(1, publisher.getEventHandlers().size()); + // force executing the callback, so we check that taking an event works + eventHandler.executeCallback(); + eventHandler.dispose(); + assertEquals(0, eventHandler.getHandle()); + assertEquals(0, publisher.getEventHandlers().size()); + RCLJava.shutdown(); + } + + @Test + public final void testCreateOfferedDeadlineMissedEvent() { + RCLJava.rclJavaInit(); + Node node = RCLJava.createNode("test_node"); + Publisher publisher = + node.createPublisher(std_msgs.msg.String.class, "test_topic"); + EventHandler eventHandler = publisher.createEventHandler( + OfferedDeadlineMissed.factory, new Consumer() { + public void accept(final OfferedDeadlineMissed 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()); + } } 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 6b01a634..4fcddffc 100644 --- a/rcljava/src/test/java/org/ros2/rcljava/subscription/SubscriptionTest.java +++ b/rcljava/src/test/java/org/ros2/rcljava/subscription/SubscriptionTest.java @@ -23,7 +23,11 @@ import org.ros2.rcljava.RCLJava; import org.ros2.rcljava.consumers.Consumer; +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.RequestedDeadlineMissed; +import org.ros2.rcljava.subscription.statuses.RequestedQosIncompatible; public class SubscriptionTest { @BeforeClass @@ -53,4 +57,83 @@ public void accept(final std_msgs.msg.String msg) {} RCLJava.shutdown(); } + + @Test + public final void testCreateLivelinessChangedEvent() { + String identifier = RCLJava.getRMWIdentifier(); + 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( + LivelinessChanged.factory, new Consumer() { + public void accept(final LivelinessChanged status) { + assertEquals(status.aliveCount, 0); + assertEquals(status.notAliveCount, 0); + assertEquals(status.aliveCountChange, 0); + assertEquals(status.notAliveCountChange, 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()); + } + + @Test + public final void testCreateRequestedDeadlineMissedEvent() { + String identifier = RCLJava.getRMWIdentifier(); + 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( + RequestedDeadlineMissed.factory, new Consumer() { + public void accept(final RequestedDeadlineMissed 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()); + } + + @Test + public final void testCreateRequestedQosIncompatibleEvent() { + 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( + RequestedQosIncompatible.factory, new Consumer() { + public void accept(final RequestedQosIncompatible status) { + assertEquals(status.totalCount, 0); + assertEquals(status.totalCountChange, 0); + assertEquals(status.lastPolicyKind, RequestedQosIncompatible.PolicyKind.INVALID); + } + } + ); + assertNotEquals(0, eventHandler.getHandle()); + // force executing the callback, so we check that taking an event works + eventHandler.executeCallback(); + RCLJava.shutdown(); + assertEquals(0, eventHandler.getHandle()); + } }