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
99 changes: 64 additions & 35 deletions rcljava/src/main/java/org/ros2/rcljava/action/ActionServerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.ros2.rcljava.action;

import java.lang.ref.WeakReference;
import java.lang.SuppressWarnings;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
Expand Down Expand Up @@ -171,6 +172,8 @@ public synchronized void abort(ResultDefinition<T> result) {
/**
* {@inheritDoc}
*/
// TODO(ivanpauno): Improve generated code API so we don't need this.
@SuppressWarnings("unchecked")
public synchronized void publishFeedback(FeedbackDefinition<T> feedback) {
Class<? extends FeedbackMessageDefinition> feedbackMessageType =
ActionServerImpl.this.actionTypeInstance.getFeedbackMessageType();
Expand Down Expand Up @@ -202,7 +205,7 @@ public final long getHandle() {

private synchronized final void toTerminalState(byte status, ResultDefinition<T> result) {
nativeUpdateGoalState(this.handle, status);
ResultResponseDefinition<T> resultResponse = ActionServerImpl.this.createResultResponse();
ResultResponseDefinition<T> resultResponse = ActionServerImpl.this.createResultResponseUnchecked();
resultResponse.setGoalStatus(status);
resultResponse.setResult(result);
ActionServerImpl.this.sendResult(goalInfo.getGoalId().getUuidAsList(), resultResponse);
Expand Down Expand Up @@ -337,6 +340,12 @@ public boolean isReady(long waitSetHandle) {
return false;
}

@SuppressWarnings("unchecked")
private GoalCallback.GoalResponse
handleGoalUnchecked(GoalRequestDefinition<T> requestMessage) {
return this.goalCallback.handleGoal(requestMessage);
}

private ActionServerGoalHandle<T> executeGoalRequest(
RMWRequestId rmwRequestId,
GoalRequestDefinition<T> requestMessage,
Expand All @@ -358,7 +367,7 @@ private ActionServerGoalHandle<T> executeGoalRequest(
}

// Call user callback
GoalCallback.GoalResponse response = this.goalCallback.handleGoal(requestMessage);
GoalCallback.GoalResponse response = this.handleGoalUnchecked(requestMessage);

boolean accepted = GoalCallback.GoalResponse.ACCEPT_AND_DEFER == response
|| GoalCallback.GoalResponse.ACCEPT_AND_EXECUTE == response;
Expand Down Expand Up @@ -542,17 +551,6 @@ private action_msgs.msg.GoalInfo createGoalInfo(List<Byte> goalUuid) {
return goalInfo;
}

private ResultResponseDefinition<T> createResultResponse() {
ResultResponseDefinition<T> resultResponse;
try {
resultResponse =
this.actionTypeInstance.getGetResultResponseType().getDeclaredConstructor().newInstance();
} catch (ReflectiveOperationException ex) {
throw new IllegalStateException("Failed to instantiate provided action type: ", ex);
}
return resultResponse;
}

// This will store the result, so it can be sent to future result requests, and
// will also send a result response to all requests that were already made.
private void sendResult(List<Byte> goalUuid, ResultResponseDefinition<T> resultResponse) {
Expand All @@ -571,23 +569,61 @@ private void sendResult(List<Byte> goalUuid, ResultResponseDefinition<T> resultR
}
}

// TODO(ivanpauno): Improve generated code API so we don't need this.
@SuppressWarnings("unchecked")
private ResultRequestDefinition<T> createResultRequestUnchecked() {
ResultRequestDefinition<T> resultRequest;
try {
resultRequest =
this.actionTypeInstance.getGetResultRequestType().getDeclaredConstructor().newInstance();
} catch (ReflectiveOperationException ex) {
throw new IllegalStateException("Failed to instantiate provided action type: ", ex);
}
return resultRequest;
}

// TODO(ivanpauno): Improve generated code API so we don't need this.
@SuppressWarnings("unchecked")
private ResultResponseDefinition<T> createResultResponseUnchecked() {
ResultResponseDefinition<T> resultResponse;
try {
resultResponse =
this.actionTypeInstance.getGetResultResponseType().getDeclaredConstructor().newInstance();
} catch (ReflectiveOperationException ex) {
throw new IllegalStateException("Failed to instantiate provided action type: ", ex);
}
return resultResponse;
}

// TODO(ivanpauno): Improve generated code API so we don't need this.
@SuppressWarnings("unchecked")
private GoalRequestDefinition<T> newRequestUnchecked() {
Class<? extends GoalRequestDefinition> requestType = this.actionTypeInstance.getSendGoalRequestType();
try {
return requestType.getDeclaredConstructor().newInstance();
} catch (ReflectiveOperationException ex) {
throw new IllegalStateException("Failed to instantiate request: ", ex);
}
}

// TODO(ivanpauno): Improve generated code API so we don't need this.
@SuppressWarnings("unchecked")
private GoalResponseDefinition<T> newResponseUnchecked() {
Class<? extends GoalResponseDefinition> responseType = this.actionTypeInstance.getSendGoalResponseType();
try {
return responseType.getDeclaredConstructor().newInstance();
} catch (ReflectiveOperationException ex) {
throw new IllegalStateException("Failed to instantiate responce: ", ex);
}
}

/**
* {@inheritDoc}
*/
public void execute() {
if (this.isGoalRequestReady()) {
Class<? extends GoalRequestDefinition> requestType = this.actionTypeInstance.getSendGoalRequestType();
Class<? extends GoalResponseDefinition> responseType = this.actionTypeInstance.getSendGoalResponseType();

GoalRequestDefinition<T> requestMessage = null;
GoalResponseDefinition<T> responseMessage = null;

try {
requestMessage = requestType.getDeclaredConstructor().newInstance();
responseMessage = responseType.getDeclaredConstructor().newInstance();
} catch (ReflectiveOperationException ex) {
throw new IllegalStateException("Failed to instantiate request or responce: ", ex);
}
GoalRequestDefinition<T> requestMessage = newRequestUnchecked();
GoalResponseDefinition<T> responseMessage = newResponseUnchecked();

if (requestMessage != null && responseMessage != null) {
long requestFromJavaConverterHandle = requestMessage.getFromJavaConverterInstance();
Expand Down Expand Up @@ -643,14 +679,7 @@ public void execute() {
}

if (this.isResultRequestReady()) {
Class<? extends ResultRequestDefinition> requestType = this.actionTypeInstance.getGetResultRequestType();

ResultRequestDefinition<T> requestMessage = null;
try {
requestMessage = requestType.getDeclaredConstructor().newInstance();
} catch (ReflectiveOperationException ex) {
throw new IllegalArgumentException("Failed to instantiate action result request: ", ex);
}
ResultRequestDefinition<T> requestMessage = createResultRequestUnchecked();

if (requestMessage != null) {
long requestFromJavaConverterHandle = requestMessage.getFromJavaConverterInstance();
Expand All @@ -672,7 +701,7 @@ public void execute() {

ResultResponseDefinition<T> resultResponse = null;
if (!goalExists) {
resultResponse = this.createResultResponse();
resultResponse = this.createResultResponseUnchecked();
resultResponse.setGoalStatus(action_msgs.msg.GoalStatus.STATUS_UNKNOWN);
} else {
resultResponse = this.goalResults.get(goalUuid);
Expand All @@ -682,7 +711,7 @@ public void execute() {
List<RMWRequestId> requestIds = null;
requestIds = this.goalRequests.get(goalUuid);
if (requestIds == null) {
requestIds = new ArrayList();
requestIds = new ArrayList<RMWRequestId>();
this.goalRequests.put(goalUuid, requestIds);
}
requestIds.add(rmwRequestId);
Expand Down
18 changes: 9 additions & 9 deletions rcljava/src/main/java/org/ros2/rcljava/node/NodeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -879,15 +879,15 @@ public rcl_interfaces.msg.ListParametersResult listParameters(
}

public final Collection<NodeNameInfo> getNodeNames() {
ArrayList<NodeNameInfo> nodeNames = new ArrayList();
ArrayList<NodeNameInfo> nodeNames = new ArrayList<NodeNameInfo>();
nativeGetNodeNames(this.handle, nodeNames);
return nodeNames;
}

private native static final void nativeGetNodeNames(long handle, ArrayList<NodeNameInfo> nodeNames);

public final Collection<NameAndTypes> getTopicNamesAndTypes() {
Collection<NameAndTypes> namesAndTypes = new ArrayList();
Collection<NameAndTypes> namesAndTypes = new ArrayList<NameAndTypes>();
nativeGetTopicNamesAndTypes(this.handle, namesAndTypes);
return namesAndTypes;
}
Expand All @@ -896,7 +896,7 @@ private static native final void nativeGetTopicNamesAndTypes(
long handle, Collection<NameAndTypes> namesAndTypes);

public final Collection<NameAndTypes> getServiceNamesAndTypes() {
Collection<NameAndTypes> namesAndTypes = new ArrayList();
Collection<NameAndTypes> namesAndTypes = new ArrayList<NameAndTypes>();
nativeGetServiceNamesAndTypes(this.handle, namesAndTypes);
return namesAndTypes;
}
Expand All @@ -905,7 +905,7 @@ private static native final void nativeGetServiceNamesAndTypes(
long handle, Collection<NameAndTypes> namesAndTypes);

public final Collection<EndpointInfo> getPublishersInfo(final String topicName) {
ArrayList<EndpointInfo> returnValue = new ArrayList();
ArrayList<EndpointInfo> returnValue = new ArrayList<EndpointInfo>();
nativeGetPublishersInfo(this.handle, topicName, returnValue);
return returnValue;
}
Expand All @@ -914,7 +914,7 @@ private native static final void nativeGetPublishersInfo(
final long handle, final String topicName, ArrayList<EndpointInfo> endpointInfo);

public final Collection<EndpointInfo> getSubscriptionsInfo(final String topicName) {
ArrayList<EndpointInfo> returnValue = new ArrayList();
ArrayList<EndpointInfo> returnValue = new ArrayList<EndpointInfo>();
nativeGetSubscriptionsInfo(this.handle, topicName, returnValue);
return returnValue;
}
Expand All @@ -925,7 +925,7 @@ private native static final void nativeGetSubscriptionsInfo(
public final Collection<NameAndTypes> getPublisherNamesAndTypesByNode(
String nodeName, String nodeNamespace)
{
Collection<NameAndTypes> namesAndTypes = new ArrayList();
Collection<NameAndTypes> namesAndTypes = new ArrayList<NameAndTypes>();
nativeGetPublisherNamesAndTypesByNode(this.handle, nodeName, nodeNamespace, namesAndTypes);
return namesAndTypes;
}
Expand All @@ -936,7 +936,7 @@ private static native final Collection<NameAndTypes> nativeGetPublisherNamesAndT
public final Collection<NameAndTypes> getSubscriptionNamesAndTypesByNode(
String nodeName, String nodeNamespace)
{
Collection<NameAndTypes> namesAndTypes = new ArrayList();
Collection<NameAndTypes> namesAndTypes = new ArrayList<NameAndTypes>();
nativeGetSubscriptionNamesAndTypesByNode(this.handle, nodeName, nodeNamespace, namesAndTypes);
return namesAndTypes;
}
Expand All @@ -947,7 +947,7 @@ private static native final Collection<NameAndTypes> nativeGetSubscriptionNamesA
public final Collection<NameAndTypes> getServiceNamesAndTypesByNode(
String nodeName, String nodeNamespace)
{
Collection<NameAndTypes> namesAndTypes = new ArrayList();
Collection<NameAndTypes> namesAndTypes = new ArrayList<NameAndTypes>();
nativeGetServiceNamesAndTypesByNode(this.handle, nodeName, nodeNamespace, namesAndTypes);
return namesAndTypes;
}
Expand All @@ -958,7 +958,7 @@ private static native final Collection<NameAndTypes> nativeGetServiceNamesAndTyp
public final Collection<NameAndTypes> getClientNamesAndTypesByNode(
String nodeName, String nodeNamespace)
{
Collection<NameAndTypes> namesAndTypes = new ArrayList();
Collection<NameAndTypes> namesAndTypes = new ArrayList<NameAndTypes>();
nativeGetClientNamesAndTypesByNode(this.handle, nodeName, nodeNamespace, namesAndTypes);
return namesAndTypes;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ 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);
final WeakReference<Collection<EventHandler>> weakEventHandlers =
new WeakReference<Collection<EventHandler>>(this.eventHandlers);
Consumer<EventHandler> disposeCallback = new Consumer<EventHandler>() {
public void accept(EventHandler eventHandler) {
Collection<EventHandler> eventHandlers = weakEventHandlers.get();
Expand All @@ -129,7 +129,7 @@ public void accept(EventHandler eventHandler) {
};
T status = factory.get();
long eventHandle = nativeCreateEvent(this.handle, status.getPublisherEventType());
EventHandler<T, Publisher> eventHandler = new EventHandlerImpl(
EventHandler<T, Publisher> eventHandler = new EventHandlerImpl<T, Publisher>(
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 @@ -129,8 +129,8 @@ 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);
final WeakReference<Collection<EventHandler>> weakEventHandlers =
new WeakReference<Collection<EventHandler>>(this.eventHandlers);
Consumer<EventHandler> disposeCallback = new Consumer<EventHandler>() {
public void accept(EventHandler eventHandler) {
Collection<EventHandler> eventHandlers = weakEventHandlers.get();
Expand All @@ -141,8 +141,9 @@ public void accept(EventHandler 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, disposeCallback);
EventHandler<T, Subscription> eventHandler =
new EventHandlerImpl<T, Subscription>(
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 @@ -54,7 +54,7 @@ public static void setupOnce() throws Exception {
{
// Configure log4j. Doing this dynamically so that Android does not complain about missing
// the log4j JARs, SLF4J uses Android's native logging mechanism instead.
Class c = Class.forName("org.apache.log4j.BasicConfigurator");
Class<?> c = Class.forName("org.apache.log4j.BasicConfigurator");
Method m = c.getDeclaredMethod("configure", (Class<?>[]) null);
Object o = m.invoke(null, (Object[]) null);
}
Expand Down
Loading