Skip to content

Autoremove EventHandler from parent entity #16

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
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
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,19 @@ public class EventHandlerImpl<
* @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<ParentT> parentReference,
final long handle,
final Supplier<T> eventStatusFactory,
final Consumer<T> callback) {
final Consumer<T> callback,
final Consumer<EventHandler> disposeCallback) {
this.parentReference = parentReference;
this.handle = handle;
this.eventStatusFactory = eventStatusFactory;
this.callback = callback;
this.disposeCallback = disposeCallback;
}

/**
Expand Down Expand Up @@ -102,9 +105,11 @@ public final long getHandle() {
* {@inheritDoc}
*/
public synchronized final void dispose() {
if (this.handle != 0) {
nativeDispose(this.handle);
if (this.handle == 0) {
return;
}
this.disposeCallback.accept(this);
nativeDispose(this.handle);
this.handle = 0;
}

Expand All @@ -126,11 +131,12 @@ public synchronized final void executeCallback() {
nativeTake(this.handle, nativeEventStatusHandle);
eventStatus.fromRCLEvent(nativeEventStatusHandle);
eventStatus.deallocateRCLStatusEvent(nativeEventStatusHandle);
callback.accept(eventStatus);
this.callback.accept(eventStatus);
}

private final Supplier<T> eventStatusFactory;
private final WeakReference<ParentT> parentReference;
private long handle;
private final Consumer<T> callback;
private final Consumer<EventHandler> disposeCallback;
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,20 @@ public final WeakReference<Node> getNodeReference() {
public final
<T extends PublisherEventStatus> EventHandler<T, Publisher>
createEventHandler(Supplier<T> factory, Consumer<T> callback) {
final WeakReference<Collection<EventHandler>> weakEventHandlers = new WeakReference(
this.eventHandlers);
Consumer<EventHandler> disposeCallback = new Consumer<EventHandler>() {
public void accept(EventHandler eventHandler) {
Collection<EventHandler> eventHandlers = weakEventHandlers.get();
if (eventHandlers != null) {
eventHandlers.remove(eventHandler);
}
}
};
T status = factory.get();
long eventHandle = nativeCreateEvent(this.handle, status.getPublisherEventType());
EventHandler<T, Publisher> eventHandler = new EventHandlerImpl(
new WeakReference<Publisher>(this), eventHandle, factory, callback);
new WeakReference<Publisher>(this), eventHandle, factory, callback, disposeCallback);
this.eventHandlers.add(eventHandler);
return eventHandler;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

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;
Expand Down Expand Up @@ -128,10 +129,20 @@ public final WeakReference<Node> getNodeReference() {
public final
<T extends SubscriptionEventStatus> EventHandler<T, Subscription>
createEventHandler(Supplier<T> factory, Consumer<T> callback) {
final WeakReference<Collection<EventHandler>> weakEventHandlers = new WeakReference(
this.eventHandlers);
Consumer<EventHandler> disposeCallback = new Consumer<EventHandler>() {
public void accept(EventHandler eventHandler) {
Collection<EventHandler> eventHandlers = weakEventHandlers.get();
if (eventHandlers != null) {
eventHandlers.remove(eventHandler);
}
}
};
T status = factory.get();
long eventHandle = nativeCreateEvent(this.handle, status.getSubscriptionEventType());
EventHandler<T, Subscription> eventHandler = new EventHandlerImpl(
new WeakReference<Subscription>(this), eventHandle, factory, callback);
new WeakReference<Subscription>(this), eventHandle, factory, callback, disposeCallback);
this.eventHandlers.add(eventHandler);
return eventHandler;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,12 @@ public void accept(final OfferedQosIncompatible status) {
}
);
assertNotEquals(0, eventHandler.getHandle());
assertEquals(1, publisher.getEventHandlers().size());
// force executing the callback, so we check that taking an event works
eventHandler.executeCallback();
RCLJava.shutdown();
eventHandler.dispose();
assertEquals(0, eventHandler.getHandle());
assertEquals(0, publisher.getEventHandlers().size());
RCLJava.shutdown();
}
}