|
| 1 | +package org.ros.node; |
| 2 | + |
| 3 | +import org.ros.node.AbstractNodeMain; |
| 4 | +import org.ros.node.ConnectedNode; |
| 5 | +import org.ros.node.Node; |
| 6 | +import org.ros.namespace.GraphName; |
| 7 | + |
| 8 | +import org.apache.commons.logging.Log; |
| 9 | +import org.apache.commons.logging.LogFactory; |
| 10 | + |
| 11 | +import java.util.List; |
| 12 | +import java.util.Map; |
| 13 | + |
| 14 | +/** |
| 15 | + * A java wrapper to load and run a native-code ROS node. |
| 16 | + * |
| 17 | + * 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. |
| 18 | + * |
| 19 | + * @author [email protected] (Ernesto Corbellini) |
| 20 | + */ |
| 21 | +public abstract class NativeNodeMain extends AbstractNodeMain { |
| 22 | + |
| 23 | + private Log log = LogFactory.getLog(NativeNodeMain.class); |
| 24 | + private String libName; |
| 25 | + private String masterUri = null; |
| 26 | + private String hostName = null; |
| 27 | + private String nodeName = null; |
| 28 | + private String[] remappingArguments; |
| 29 | + private boolean shuttingDown = false; |
| 30 | + |
| 31 | + /** |
| 32 | + * @param libName |
| 33 | + * The name of the library to load. |
| 34 | + * |
| 35 | + * @param remappings |
| 36 | + * A string array with ROS argument remapping pairs in each element. |
| 37 | + **/ |
| 38 | + public NativeNodeMain(String libName, String[] remappings) { |
| 39 | + this.libName = libName; |
| 40 | + |
| 41 | + // if no remapping is needed, create an empty array |
| 42 | + if (remappings == null) { |
| 43 | + remappingArguments = new String[0]; |
| 44 | + } |
| 45 | + |
| 46 | + log.info("Trying to load native library '" + libName + "'..."); |
| 47 | + try |
| 48 | + { |
| 49 | + System.loadLibrary(libName); |
| 50 | + } |
| 51 | + catch (SecurityException e) |
| 52 | + { |
| 53 | + log.info("Error loading library! SecurityException"); |
| 54 | + } |
| 55 | + catch (UnsatisfiedLinkError e) |
| 56 | + { |
| 57 | + log.info("Error loading library! UnsatisfiedLinkError"); |
| 58 | + } |
| 59 | + catch (NullPointerException e) |
| 60 | + { |
| 61 | + log.info("Error loading library! NullPointerException"); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @param libName |
| 67 | + * The name of the library to load. |
| 68 | + **/ |
| 69 | + public NativeNodeMain(String libName) { |
| 70 | + this(libName, null); |
| 71 | + } |
| 72 | + |
| 73 | + // These methods define the execution model interface for this node. |
| 74 | + protected abstract void execute(String rosMasterUri, String rosHostName, String rosNodeName, String[] remappingArguments); |
| 75 | + protected abstract void shutdown(); |
| 76 | + |
| 77 | + @Override |
| 78 | + public void onStart(final ConnectedNode connectedNode) { |
| 79 | + // retain important ROS info |
| 80 | + masterUri = connectedNode.getMasterUri().toString(); |
| 81 | + hostName = connectedNode.getUri().getHost(); |
| 82 | + nodeName = this.libName; |
| 83 | + |
| 84 | + // create a new thread to execute the native code. |
| 85 | + new Thread() { |
| 86 | + @Override |
| 87 | + public void run() { |
| 88 | + execute(masterUri, hostName, nodeName, remappingArguments); |
| 89 | + |
| 90 | + // node execution has finished so we propagate the shutdown sequence only if we aren't already shutting down for other reasons |
| 91 | + if(!shuttingDown) { |
| 92 | + connectedNode.shutdown(); |
| 93 | + } |
| 94 | + } |
| 95 | + }.start(); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public void onShutdown(Node node) { |
| 100 | + shuttingDown = true; |
| 101 | + shutdown(); |
| 102 | + } |
| 103 | +} |
0 commit comments