Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions rcljava/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ set(${PROJECT_NAME}_jni_sources
"src/main/cpp/org_ros2_rcljava_contexts_ContextImpl.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_events_publisher_statuses_LivelinessLost.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"
Expand Down Expand Up @@ -129,6 +130,9 @@ set(${PROJECT_NAME}_sources
"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/events/publisher_statuses/LivelinessLost.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"
Expand Down
3 changes: 2 additions & 1 deletion rcljava/include/org_ros2_rcljava_events_EventHandlerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ extern "C" {
* Signature: (J)V
*/
JNIEXPORT void
JNICALL Java_org_ros2_rcljava_events_EventHandlerImpl_nativeDispose(JNIEnv *, jclass, jlong event_handle);
JNICALL Java_org_ros2_rcljava_events_EventHandlerImpl_nativeDispose(
JNIEnv *, jclass, jlong event_handle);

/*
* Class: org_ros2_rcljava_events_EventHandlerImpl
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <jni.h>
/* Header for class org_ros2_rcljava_events_publisher_statuses_LivelinessLost */

#ifndef ORG_ROS2_RCLJAVA_EVENTS_PUBLISHER_STATUSES_LIVELINESSLOST_H_
#define ORG_ROS2_RCLJAVA_EVENTS_PUBLISHER_STATUSES_LIVELINESSLOST_H_
#ifdef __cplusplus
extern "C" {
#endif

/*
* Class: org_ros2_rcljava_events_publisher_statuses_LivelinessLost
* Method: nativeAllocateRCLStatusEvent
* Signature: ()J
*/
JNIEXPORT jlong JNICALL
Java_org_ros2_rcljava_events_publisher_1statuses_LivelinessLost_nativeAllocateRCLStatusEvent(
JNIEnv *, jclass);

/*
* Class: org_ros2_rcljava_events_publisher_statuses_LivelinessLost
* Method: nativeDeallocateRCLStatusEvent
* Signature: (J)V
*/
JNIEXPORT void JNICALL
Java_org_ros2_rcljava_events_publisher_1statuses_LivelinessLost_nativeDeallocateRCLStatusEvent(
JNIEnv *, jclass, jlong);

/*
* Class: org_ros2_rcljava_events_publisher_statuses_LivelinessLost
* Method: nativeFromRCLEvent
* Signature: (J)V
*/
JNIEXPORT void JNICALL
Java_org_ros2_rcljava_events_publisher_1statuses_LivelinessLost_nativeFromRCLEvent(
JNIEnv *, jobject, jlong);

/*
* Class: org_ros2_rcljava_events_publisher_statuses_LivelinessLost
* Method: nativeGetPublisherEventType
* Signature: ()I
*/
JNIEXPORT jint JNICALL
Java_org_ros2_rcljava_events_publisher_1statuses_LivelinessLost_nativeGetPublisherEventType(
JNIEnv *, jclass);

#ifdef __cplusplus
}
#endif
#endif // ORG_ROS2_RCLJAVA_EVENTS_PUBLISHER_STATUSES_LIVELINESSLOST_H_
9 changes: 9 additions & 0 deletions rcljava/include/org_ros2_rcljava_publisher_PublisherImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ Java_org_ros2_rcljava_events_EventHandlerImpl_nativeDispose(
}

JNIEXPORT void JNICALL
Java_org_ros2_rcljava_events_EventHandlerImpl_nativeTake(JNIEnv *env, jclass, jlong event_handle, jlong event_status_handle)
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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// 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_events_publisher_statuses_LivelinessLost.h"

#include <jni.h>
#include <stdlib.h>

#include "rmw/events_statuses/liveliness_lost.h"
#include "rcl/event.h"
#include "rcljava_common/exceptions.hpp"

using rcljava_common::exceptions::rcljava_throw_exception;

JNIEXPORT jlong JNICALL
Java_org_ros2_rcljava_events_publisher_1statuses_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<jlong>(p);
}

JNIEXPORT void JNICALL
Java_org_ros2_rcljava_events_publisher_1statuses_LivelinessLost_nativeDeallocateRCLStatusEvent(
JNIEnv *, jclass, jlong handle)
{
free(reinterpret_cast<void *>(handle));
}

JNIEXPORT void JNICALL
Java_org_ros2_rcljava_events_publisher_1statuses_LivelinessLost_nativeFromRCLEvent(
JNIEnv * env, jobject self, jlong handle)
{
auto * p = reinterpret_cast<rmw_liveliness_lost_status_t *>(handle);
if (!p) {
rcljava_throw_exception(
env, "java/lang/IllegalArgumentException", "passed rmw object handle is NULL");
}
// TODO(ivanpauno): class and field lookup could be done at startup time
jclass clazz = env->GetObjectClass(self);
jfieldID total_count_fid = env->GetFieldID(clazz, "total_count", "I");
if (env->ExceptionCheck()) {
return;
}
jfieldID total_count_change_fid = env->GetFieldID(clazz, "total_count_change", "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_events_publisher_1statuses_LivelinessLost_nativeGetPublisherEventType(
JNIEnv *, jclass)
{
return RCL_PUBLISHER_LIVELINESS_LOST;
}
30 changes: 30 additions & 0 deletions rcljava/src/main/cpp/org_ros2_rcljava_publisher_PublisherImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <string>

#include "rcl/error_handling.h"
#include "rcl/event.h"
#include "rcl/node.h"
#include "rcl/rcl.h"
#include "rmw/rmw.h"
Expand All @@ -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;
Expand Down Expand Up @@ -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<rcl_publisher_t *>(publisher_handle);
if (!publisher) {
rcljava_throw_exception(
env, "java/lang/IllegalArgumentException", "passed rcl_publisher_t handle is NULL");
return 0;
}
auto * event = static_cast<rcl_event_t *>(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<rcl_publisher_event_type_t>(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<jlong>(event);
}
10 changes: 0 additions & 10 deletions rcljava/src/main/java/org/ros2/rcljava/events/EventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,6 @@
* @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.
*/
Expand Down
55 changes: 13 additions & 42 deletions rcljava/src/main/java/org/ros2/rcljava/events/EventHandlerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

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;
Expand Down Expand Up @@ -53,48 +54,28 @@ public class EventHandlerImpl<
/**
* 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.
* 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.
*/
public EventHandlerImpl(
final Class<ParentT> parentType,
final WeakReference<ParentT> parentReference,
final long handle,
final Class<T> eventStatusType,
final Supplier<T> eventStatusFactory,
final Consumer<T> callback) {
this.parentType = parentType;
this.parentReference = parentReference;
this.handle = handle;
this.eventStatusType = eventStatusType;
this.eventStatusFactory = eventStatusFactory;
this.callback = callback;
}

/**
* {@inheritDoc}
*/
public final Class<T> getEventStatusType() {
return this.eventStatusType;
}

/**
* {@inheritDoc}
*/
public final Class<ParentT> getParentType() {
return this.parentType;
}

/**
* {@inheritDoc}
*/
Expand All @@ -120,8 +101,10 @@ public final long getHandle() {
/**
* {@inheritDoc}
*/
public final void dispose() {
nativeDispose(this.handle);
public synchronized final void dispose() {
if (this.handle != 0) {
nativeDispose(this.handle);
}
this.handle = 0;
}

Expand All @@ -137,28 +120,16 @@ public final void dispose() {
/**
* {@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();
}
public synchronized final void executeCallback() {
T eventStatus = eventStatusFactory.get();
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 Supplier<T> eventStatusFactory;
private final WeakReference<ParentT> parentReference;
private long handle;
private final Consumer<T> callback;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ public interface PublisherEventStatus extends EventStatus {
/**
* @return The status event type.
*/
int get_publisher_event_type();
int getPublisherEventType();
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ public interface SubscriptionEventStatus extends EventStatus {
/**
* @return The status event type.
*/
int get_subscription_event_type();
int getSubscriptionEventType();
}
Loading