diff --git a/src/rosjava_actionlib/rosjava_actionlib/src/main/java/com/github/ekumen/rosjava_actionlib/GoalIDGenerator.java b/src/rosjava_actionlib/rosjava_actionlib/src/main/java/com/github/ekumen/rosjava_actionlib/GoalIDGenerator.java new file mode 100644 index 0000000000000000000000000000000000000000..35654c6f483dad257592b36690be380cb322126b --- /dev/null +++ b/src/rosjava_actionlib/rosjava_actionlib/src/main/java/com/github/ekumen/rosjava_actionlib/GoalIDGenerator.java @@ -0,0 +1,82 @@ +/* + * Copyright (C) 2011 Alexander Perzylo, Technische Universität München + * + * 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 com.github.ekumen.rosjava_actionlib; + +import org.ros.message.Time; +import actionlib_msgs.GoalID; +import org.ros.node.ConnectedNode; +import org.ros.node.NodeConfiguration; +import org.ros.message.MessageFactory; + +import java.util.concurrent.atomic.AtomicLong; + +/** + * The GoalIDGenerator may be used to create unique GoalIDs. + * + * <p> + * The node's nodeName will be used and the time on the node. + * + * @author Alexander C. Perzylo, perzylo@cs.tum.edu + */ +public class GoalIDGenerator { + /** + * A global ID which provide a count for each goal id. + */ + private static AtomicLong goalCount = new AtomicLong(0); + + /** + * Unique nodeName to prepend to the goal id. This will generally be a fully + * qualified node nodeName. + */ + private ConnectedNode node; + + /** + * Constructor to create a GoalIDGenerator using a unique nodeName to prepend to + * the goal id. This will generally be a fully qualified node nodeName. + * + * @param node + * The node used to generate IDs. The node's full nodeName should be + * unique in the system. + */ + public GoalIDGenerator(ConnectedNode node) { + this.node = node; + } + + /** + * Creates a GoalID object with an unique id and a timestamp of the current + * time. + * + * @return GoalID object + */ + public String generateID(ConnectedNode node, GoalID goal) { + String id; + Time t = node.getCurrentTime(); + //NodeConfiguration nc = NodeConfiguration.newPrivate(); + //MessageFactory mf = nc.getTopicMessageFactory(); + //GoalID id = mf.newFromType(GoalID._TYPE); + + //StringBuilder sb = new StringBuilder(node.getName().toString()); + //sb.append("-").append(goalCount.incrementAndGet()).append("-").append(t.secs).append(".").append(t.nsecs); + id = node.getName().toString() + "-" + goalCount.incrementAndGet() + + "-" + t.secs + "." + t.nsecs; + + goal.setId(id); + goal.setStamp(t); + + return id; + } +}