Skip to content

Commit b9e9fdd

Browse files
committed
Some code that compiles and doesn't break existing tests (osrf#10)
Signed-off-by: Ivan Santiago Paunovic <ivanpauno@ekumenlabs.com>
1 parent 093a101 commit b9e9fdd

File tree

6 files changed

+113
-2
lines changed

6 files changed

+113
-2
lines changed

rcljava/include/org_ros2_rcljava_executors_BaseExecutor.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,15 @@ JNIEXPORT void
134134
JNICALL Java_org_ros2_rcljava_executors_BaseExecutor_nativeWaitSetAddTimer(
135135
JNIEnv *, jclass, jlong, jlong);
136136

137+
/*
138+
* Class: org_ros2_rcljava_executors_BaseExecutor
139+
* Method: nativeWaitSetAddEvent
140+
* Signature: (JJ)V
141+
*/
142+
JNIEXPORT void
143+
JNICALL Java_org_ros2_rcljava_executors_BaseExecutor_nativeWaitSetAddEvent(
144+
JNIEnv *, jclass, jlong, jlong);
145+
137146
/*
138147
* Class: org_ros2_rcljava_executors_BaseExecutor
139148
* Method: nativeWaitSetSubscriptionIsReady
@@ -152,6 +161,15 @@ JNIEXPORT jboolean
152161
JNICALL Java_org_ros2_rcljava_executors_BaseExecutor_nativeWaitSetTimerIsReady(
153162
JNIEnv *, jclass, jlong, jlong);
154163

164+
/*
165+
* Class: org_ros2_rcljava_executors_BaseExecutor
166+
* Method: nativeWaitSetEventIsReady
167+
* Signature: (JJ)Z
168+
*/
169+
JNIEXPORT jboolean
170+
JNICALL Java_org_ros2_rcljava_executors_BaseExecutor_nativeWaitSetEventIsReady(
171+
JNIEnv *, jclass, jlong, jlong);
172+
155173
/*
156174
* Class: org_ros2_rcljava_executors_BaseExecutor
157175
* Method: nativeWaitSetServiceIsReady

rcljava/src/main/cpp/org_ros2_rcljava_executors_BaseExecutor.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,21 @@ Java_org_ros2_rcljava_executors_BaseExecutor_nativeWaitSetAddTimer(
276276
}
277277
}
278278

279+
JNIEXPORT void JNICALL
280+
Java_org_ros2_rcljava_executors_BaseExecutor_nativeWaitSetAddEvent(
281+
JNIEnv * env, jclass, jlong wait_set_handle, jlong event_handle)
282+
{
283+
auto * wait_set = reinterpret_cast<rcl_wait_set_t *>(wait_set_handle);
284+
auto * event = reinterpret_cast<rcl_event_t *>(event_handle);
285+
rcl_ret_t ret = rcl_wait_set_add_event(wait_set, event, nullptr);
286+
if (ret != RCL_RET_OK) {
287+
std::string msg =
288+
"Failed to add event to wait set: " + std::string(rcl_get_error_string().str);
289+
rcl_reset_error();
290+
rcljava_throw_rclexception(env, ret, msg);
291+
}
292+
}
293+
279294
JNIEXPORT jobject JNICALL
280295
Java_org_ros2_rcljava_executors_BaseExecutor_nativeTakeRequest(
281296
JNIEnv * env, jclass, jlong service_handle, jlong jrequest_from_java_converter_handle,
@@ -435,6 +450,14 @@ Java_org_ros2_rcljava_executors_BaseExecutor_nativeWaitSetTimerIsReady(
435450
return wait_set->timers[index] != nullptr;
436451
}
437452

453+
JNIEXPORT jboolean JNICALL
454+
Java_org_ros2_rcljava_executors_BaseExecutor_nativeWaitSetEventIsReady(
455+
JNIEnv *, jclass, jlong wait_set_handle, jlong index)
456+
{
457+
rcl_wait_set_t * wait_set = reinterpret_cast<rcl_wait_set_t *>(wait_set_handle);
458+
return wait_set->events[index] != nullptr;
459+
}
460+
438461
JNIEXPORT jboolean JNICALL
439462
Java_org_ros2_rcljava_executors_BaseExecutor_nativeWaitSetServiceIsReady(
440463
JNIEnv *, jclass, jlong wait_set_handle, jlong index)

rcljava/src/main/java/org/ros2/rcljava/executors/AnyExecutable.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.ros2.rcljava.executors;
1717

1818
import org.ros2.rcljava.client.Client;
19+
import org.ros2.rcljava.events.EventHandler;
1920
import org.ros2.rcljava.subscription.Subscription;
2021
import org.ros2.rcljava.service.Service;
2122
import org.ros2.rcljava.timer.Timer;
@@ -25,4 +26,5 @@ public class AnyExecutable {
2526
public Subscription subscription;
2627
public Service service;
2728
public Client client;
29+
public EventHandler eventHandler;
2830
}

rcljava/src/main/java/org/ros2/rcljava/executors/BaseExecutor.java

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.List;
2222
import java.util.Map;
2323

24+
import java.util.Collection;
2425
import java.util.concurrent.BlockingQueue;
2526
import java.util.concurrent.ConcurrentHashMap;
2627
import java.util.concurrent.LinkedBlockingQueue;
@@ -31,11 +32,13 @@
3132
import org.ros2.rcljava.RCLJava;
3233
import org.ros2.rcljava.client.Client;
3334
import org.ros2.rcljava.common.JNIUtils;
35+
import org.ros2.rcljava.events.EventHandler;
3436
import org.ros2.rcljava.executors.AnyExecutable;
3537
import org.ros2.rcljava.executors.Executor;
3638
import org.ros2.rcljava.interfaces.MessageDefinition;
3739
import org.ros2.rcljava.interfaces.ServiceDefinition;
3840
import org.ros2.rcljava.node.ComposableNode;
41+
import org.ros2.rcljava.publisher.Publisher;
3942
import org.ros2.rcljava.service.RMWRequestId;
4043
import org.ros2.rcljava.service.Service;
4144
import org.ros2.rcljava.subscription.Subscription;
@@ -64,6 +67,8 @@ public class BaseExecutor {
6467

6568
private List<Map.Entry<Long, Client>> clientHandles = new ArrayList<Map.Entry<Long, Client>>();
6669

70+
private List<Map.Entry<Long, EventHandler>> eventHandles = new ArrayList<Map.Entry<Long, EventHandler>>();
71+
6772
protected void addNode(ComposableNode node) {
6873
this.nodes.add(node);
6974
}
@@ -158,20 +163,34 @@ protected void executeAnyExecutable(AnyExecutable anyExecutable) {
158163
}
159164
clientHandles.remove(anyExecutable.client.getHandle());
160165
}
166+
167+
if (anyExecutable.eventHandler != null) {
168+
anyExecutable.eventHandler.executeCallback();
169+
eventHandles.remove(anyExecutable.eventHandler.getHandle());
170+
}
161171
}
162172

163173
protected void waitForWork(long timeout) {
164174
this.subscriptionHandles.clear();
165175
this.timerHandles.clear();
166176
this.serviceHandles.clear();
167177
this.clientHandles.clear();
178+
this.eventHandles.clear();
168179

169180
for (ComposableNode node : this.nodes) {
170181
for (Subscription<MessageDefinition> subscription : node.getNode().getSubscriptions()) {
171182
this.subscriptionHandles.add(new AbstractMap.SimpleEntry<Long, Subscription>(
172183
subscription.getHandle(), subscription));
173184
}
174185

186+
for (Publisher publisher : node.getNode().getPublishers()) {
187+
Collection<EventHandler> eventHandlers = publisher.getEventHandlers();
188+
for (EventHandler eventHandler : eventHandlers) {
189+
this.eventHandles.add(new AbstractMap.SimpleEntry<Long, EventHandler>(
190+
eventHandler.getHandle(), eventHandler));
191+
}
192+
}
193+
175194
for (Timer timer : node.getNode().getTimers()) {
176195
this.timerHandles.add(new AbstractMap.SimpleEntry<Long, Timer>(timer.getHandle(), timer));
177196
}
@@ -191,6 +210,7 @@ protected void waitForWork(long timeout) {
191210
int timersSize = 0;
192211
int clientsSize = 0;
193212
int servicesSize = 0;
213+
int eventsSize = this.eventHandles.size();
194214

195215
for (ComposableNode node : this.nodes) {
196216
subscriptionsSize += node.getNode().getSubscriptions().size();
@@ -205,7 +225,9 @@ protected void waitForWork(long timeout) {
205225

206226
long waitSetHandle = nativeGetZeroInitializedWaitSet();
207227
long contextHandle = RCLJava.getDefaultContext().getHandle();
208-
nativeWaitSetInit(waitSetHandle, contextHandle, subscriptionsSize, 0, timersSize, clientsSize, servicesSize, 0);
228+
nativeWaitSetInit(
229+
waitSetHandle, contextHandle, subscriptionsSize, 0,
230+
timersSize, clientsSize, servicesSize, eventsSize);
209231

210232
nativeWaitSetClear(waitSetHandle);
211233

@@ -225,6 +247,10 @@ protected void waitForWork(long timeout) {
225247
nativeWaitSetAddClient(waitSetHandle, entry.getKey());
226248
}
227249

250+
for (Map.Entry<Long, EventHandler> entry : this.eventHandles) {
251+
nativeWaitSetAddEvent(waitSetHandle, entry.getKey());
252+
}
253+
228254
nativeWait(waitSetHandle, timeout);
229255

230256
for (int i = 0; i < this.subscriptionHandles.size(); ++i) {
@@ -251,6 +277,12 @@ protected void waitForWork(long timeout) {
251277
}
252278
}
253279

280+
for (int i = 0; i < this.eventHandles.size(); ++i) {
281+
if (!nativeWaitSetEventIsReady(waitSetHandle, i)) {
282+
this.eventHandles.get(i).setValue(null);
283+
}
284+
}
285+
254286
Iterator<Map.Entry<Long, Subscription>> subscriptionIterator =
255287
this.subscriptionHandles.iterator();
256288
while (subscriptionIterator.hasNext()) {
@@ -284,6 +316,14 @@ protected void waitForWork(long timeout) {
284316
}
285317
}
286318

319+
Iterator<Map.Entry<Long, EventHandler>> eventIterator = this.eventHandles.iterator();
320+
while (eventIterator.hasNext()) {
321+
Map.Entry<Long, EventHandler> entry = eventIterator.next();
322+
if (entry.getValue() == null) {
323+
eventIterator.remove();
324+
}
325+
}
326+
287327
nativeDisposeWaitSet(waitSetHandle);
288328
}
289329

@@ -325,6 +365,14 @@ protected AnyExecutable getNextExecutable() {
325365
}
326366
}
327367

368+
for (Map.Entry<Long, EventHandler> entry : this.eventHandles) {
369+
if (entry.getValue() != null) {
370+
anyExecutable.eventHandler = entry.getValue();
371+
entry.setValue(null);
372+
return anyExecutable;
373+
}
374+
}
375+
328376
return null;
329377
}
330378

@@ -405,6 +453,8 @@ private static native MessageDefinition nativeTake(
405453

406454
private static native void nativeWaitSetAddTimer(long waitSetHandle, long timerHandle);
407455

456+
private static native void nativeWaitSetAddEvent(long waitSetHandle, long eventHandle);
457+
408458
private static native RMWRequestId nativeTakeRequest(long serviceHandle,
409459
long requestFromJavaConverterHandle, long requestToJavaConverterHandle,
410460
long requestDestructorHandle, MessageDefinition requestMessage);
@@ -421,6 +471,8 @@ private static native RMWRequestId nativeTakeResponse(long clientHandle,
421471

422472
private static native boolean nativeWaitSetTimerIsReady(long waitSetHandle, long index);
423473

474+
private static native boolean nativeWaitSetEventIsReady(long waitSetHandle, long index);
475+
424476
private static native boolean nativeWaitSetServiceIsReady(long waitSetHandle, long index);
425477

426478
private static native boolean nativeWaitSetClientIsReady(long waitSetHandle, long index);

rcljava/src/main/java/org/ros2/rcljava/publisher/Publisher.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.ros2.rcljava.publisher;
1717

1818
import java.lang.ref.WeakReference;
19+
import java.util.Collection;
1920
import java.util.function.Supplier;
2021

2122
import org.ros2.rcljava.consumers.Consumer;
@@ -64,4 +65,11 @@ <T extends PublisherEventStatus> EventHandler<T, Publisher> createEventHandler(
6465
*/
6566
<T extends PublisherEventStatus> void removeEventHandler(
6667
EventHandler<T, Publisher> eventHandler);
68+
69+
/**
70+
* Get the event handlers that were registered in this Publisher.
71+
*
72+
* @return The registered event handlers.
73+
*/
74+
Collection<EventHandler> getEventHandlers();
6775
}

rcljava/src/main/java/org/ros2/rcljava/publisher/PublisherImpl.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,15 @@ <T extends PublisherEventStatus> void removeEventHandler(
139139
}
140140

141141
/**
142-
* Create a publisher event (rcl_event_t).
142+
* {@inheritDoc}
143+
*/
144+
public final
145+
Collection<EventHandler> getEventHandlers() {
146+
return this.eventHandlers;
147+
}
148+
149+
/**
150+
* Create a publisher event (rcl_event_t)
143151
*
144152
* The ownership of the created event handle will immediately be transferred to an
145153
* @{link EventHandlerImpl}, that will be responsible of disposing it.

0 commit comments

Comments
 (0)