forked from ros2-java/ros2_java
-
Notifications
You must be signed in to change notification settings - Fork 9
Add abstractions and minimal implementation of rcl events #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c914f4f
Add support for rcl events, first attempt
ivanpauno 55a5f8c
nit
ivanpauno 83296b9
nit
ivanpauno 3fa5de8
nit
ivanpauno 81ca9b1
nit
ivanpauno 41c5fb1
nit
ivanpauno 2348044
Actually execute the registered callback
ivanpauno b963362
Make everything compile again
ivanpauno 51c640f
Rename Event to EventHandler
ivanpauno File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
rcljava/include/org_ros2_rcljava_events_EventHandlerImpl.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| // 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 <jni.h> | ||
| /* 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_ |
71 changes: 71 additions & 0 deletions
71
rcljava/src/main/cpp/org_ros2_rcljava_events_EventHandlerImpl.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| // 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 <jni.h> | ||
|
|
||
| #include <string> | ||
|
|
||
| #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<rcl_event_t *>(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<rcl_event_t *>(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<void *>(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(); | ||
| } | ||
| } |
51 changes: 51 additions & 0 deletions
51
rcljava/src/main/java/org/ros2/rcljava/events/EventHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // 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 <T> The event status type. | ||
| * @param <ParentT> The parent class type. | ||
| */ | ||
| public interface EventHandler<T extends EventStatus, ParentT extends Disposable> extends Disposable { | ||
| /** | ||
| * @return The event status type. | ||
| */ | ||
| Class<T> getEventStatusType(); | ||
|
|
||
| /** | ||
| * @return The parent entity type. | ||
| */ | ||
| Class<ParentT> getParentType(); | ||
|
|
||
| /** | ||
| * @return A weak reference to the parent. | ||
| */ | ||
| WeakReference<ParentT> getParentReference(); | ||
|
|
||
| /** | ||
| * @return Execute the registered event callback | ||
| */ | ||
| void executeCallback(); | ||
| } |
165 changes: 165 additions & 0 deletions
165
rcljava/src/main/java/org/ros2/rcljava/events/EventHandlerImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| // 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 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 <T> The status event type. | ||
| * @param <ParentT> The parent class type. | ||
| */ | ||
| public class EventHandlerImpl< | ||
| T extends EventStatus, | ||
| ParentT extends Disposable> | ||
| implements EventHandler<T, ParentT> { | ||
| 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 parentType The <code>Class</code> type of the parent. | ||
| * It can be either a @{link org.ros2.rcljava.Publisher} or a | ||
| * @{link org.ros2.rcljava.Subscription} class. | ||
| * @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. | ||
| * @param eventStatusType The <code>Class</code> of the messages that this | ||
| * subscription will receive. We need this because of Java's type erasure, | ||
| * which doesn't allow us to use the generic parameter of | ||
| * @{link org.ros2.rcljava.Subscription} directly. | ||
| * @param callback The callback function that will be called when the event | ||
| * is triggered. | ||
| */ | ||
| public EventHandlerImpl( | ||
| final Class<ParentT> parentType, | ||
| final WeakReference<ParentT> parentReference, | ||
| final long handle, | ||
| final Class<T> eventStatusType, | ||
| final Consumer<T> callback) { | ||
| this.parentType = parentType; | ||
| this.parentReference = parentReference; | ||
| this.handle = handle; | ||
| this.eventStatusType = eventStatusType; | ||
| this.callback = callback; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| public final Class<T> getEventStatusType() { | ||
| return this.eventStatusType; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| public final Class<ParentT> getParentType() { | ||
| return this.parentType; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| public final WeakReference<ParentT> 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 final void dispose() { | ||
| 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 final void executeCallback() { | ||
| T eventStatus = null; | ||
| try { | ||
| eventStatus = this.eventStatusType.getDeclaredConstructor().newInstance(); | ||
| } catch (NoSuchMethodException nme) { | ||
| nme.printStackTrace(); | ||
| } catch (InvocationTargetException ite) { | ||
| ite.printStackTrace(); | ||
| } catch (InstantiationException ie) { | ||
| ie.printStackTrace(); | ||
| } catch (IllegalAccessException iae) { | ||
| iae.printStackTrace(); | ||
| } | ||
| long nativeEventStatusHandle = eventStatus.allocateRCLStatusEvent(); | ||
| nativeTake(this.handle, nativeEventStatusHandle); | ||
| eventStatus.fromRCLEvent(nativeEventStatusHandle); | ||
| eventStatus.deallocateRCLStatusEvent(nativeEventStatusHandle); | ||
| callback.accept(eventStatus); | ||
| } | ||
|
|
||
| private final Class<T> eventStatusType; | ||
| private final Class<ParentT> parentType; | ||
| private final WeakReference<ParentT> parentReference; | ||
| private long handle; | ||
| private final Consumer<T> callback; | ||
| } |
43 changes: 43 additions & 0 deletions
43
rcljava/src/main/java/org/ros2/rcljava/events/EventStatus.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it make sense to also have a "get event type" method as part of this interface?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's part of
PublisherEventStatusandSubscriptionEventStatus.