Skip to content
Snippets Groups Projects
Commit 61879c47 authored by Juan Ignacio Ubeira's avatar Juan Ignacio Ubeira
Browse files

NativeNodeMain upgraded to upstream error codes to the application.

parent 37d365b2
Branches
No related tags found
No related merge requests found
/*
* Copyright (C) 2017 Ekumen, 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.ros.node; package org.ros.node;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
...@@ -7,11 +23,13 @@ import org.apache.commons.logging.LogFactory; ...@@ -7,11 +23,13 @@ import org.apache.commons.logging.LogFactory;
* A java wrapper to load and run a native-code ROS node. * A java wrapper to load and run a native-code ROS node.
* *
* Note: there are no actual native methods declared in this class. We only define an interface. The native methods should be declared in the child class. * Note: there are no actual native methods declared in this class. We only define an interface. The native methods should be declared in the child class.
* Native methods' return codes can be handled by the application using {@link #onError(Node, Throwable)}.
* *
* @author ecorbellini@creativa77.com.ar (Ernesto Corbellini) * @author ecorbellini@creativa77.com.ar (Ernesto Corbellini)
*/ */
public abstract class NativeNodeMain extends AbstractNodeMain { public abstract class NativeNodeMain extends AbstractNodeMain {
public static final int SUCCESS = 0;
private Log log = LogFactory.getLog(NativeNodeMain.class); private Log log = LogFactory.getLog(NativeNodeMain.class);
private String libName; private String libName;
private String masterUri = null; private String masterUri = null;
...@@ -19,6 +37,8 @@ public abstract class NativeNodeMain extends AbstractNodeMain { ...@@ -19,6 +37,8 @@ public abstract class NativeNodeMain extends AbstractNodeMain {
private String nodeName = null; private String nodeName = null;
private String[] remappingArguments; private String[] remappingArguments;
private boolean shuttingDown = false; private boolean shuttingDown = false;
protected int executeReturnCode = SUCCESS;
protected int shutdownReturnCode = SUCCESS;
/** /**
* @param libName * @param libName
...@@ -36,20 +56,16 @@ public abstract class NativeNodeMain extends AbstractNodeMain { ...@@ -36,20 +56,16 @@ public abstract class NativeNodeMain extends AbstractNodeMain {
} }
log.info("Trying to load native library '" + libName + "'..."); log.info("Trying to load native library '" + libName + "'...");
try try {
{
System.loadLibrary(libName); System.loadLibrary(libName);
} }
catch (SecurityException e) catch (SecurityException e) {
{
log.info("Error loading library! SecurityException"); log.info("Error loading library! SecurityException");
} }
catch (UnsatisfiedLinkError e) catch (UnsatisfiedLinkError e) {
{
log.info("Error loading library! UnsatisfiedLinkError"); log.info("Error loading library! UnsatisfiedLinkError");
} }
catch (NullPointerException e) catch (NullPointerException e) {
{
log.info("Error loading library! NullPointerException"); log.info("Error loading library! NullPointerException");
} }
} }
...@@ -62,7 +78,7 @@ public abstract class NativeNodeMain extends AbstractNodeMain { ...@@ -62,7 +78,7 @@ public abstract class NativeNodeMain extends AbstractNodeMain {
this(libName, null); this(libName, null);
} }
// These methods define the execution model interface for this node. Return values are error codes (not used by default). // These methods define the execution model interface for this node.
protected abstract int execute(String rosMasterUri, String rosHostName, String rosNodeName, String[] remappingArguments); protected abstract int execute(String rosMasterUri, String rosHostName, String rosNodeName, String[] remappingArguments);
protected abstract int shutdown(); protected abstract int shutdown();
...@@ -77,7 +93,11 @@ public abstract class NativeNodeMain extends AbstractNodeMain { ...@@ -77,7 +93,11 @@ public abstract class NativeNodeMain extends AbstractNodeMain {
new Thread() { new Thread() {
@Override @Override
public void run() { public void run() {
execute(masterUri, hostName, nodeName, remappingArguments); executeReturnCode = execute(masterUri, hostName, nodeName, remappingArguments);
if (executeReturnCode != SUCCESS) {
onError(connectedNode, new Throwable(nodeName + " execution error code " + executeReturnCode));
}
// node execution has finished so we propagate the shutdown sequence only if we aren't already shutting down for other reasons // node execution has finished so we propagate the shutdown sequence only if we aren't already shutting down for other reasons
if (!shuttingDown) { if (!shuttingDown) {
...@@ -90,6 +110,10 @@ public abstract class NativeNodeMain extends AbstractNodeMain { ...@@ -90,6 +110,10 @@ public abstract class NativeNodeMain extends AbstractNodeMain {
@Override @Override
public void onShutdown(Node node) { public void onShutdown(Node node) {
shuttingDown = true; shuttingDown = true;
shutdown(); shutdownReturnCode = shutdown();
if (shutdownReturnCode != SUCCESS) {
onError(node, new Throwable(nodeName + " shutdown error code " + shutdownReturnCode));
}
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment