Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 package org.ros.time;
00018
00019 import com.google.common.base.Preconditions;
00020
00021 import org.ros.Topics;
00022 import org.ros.internal.node.DefaultNode;
00023 import org.ros.message.MessageListener;
00024 import org.ros.message.Time;
00025 import org.ros.node.topic.Subscriber;
00026 import rosgraph_msgs.Clock;
00027
00034 public class ClockTopicTimeProvider implements TimeProvider {
00035
00036 private final Subscriber<rosgraph_msgs.Clock> subscriber;
00037
00038 private Object mutex;
00039 private rosgraph_msgs.Clock clock;
00040
00041 public ClockTopicTimeProvider(DefaultNode defaultNode) {
00042 subscriber = defaultNode.newSubscriber(Topics.CLOCK, rosgraph_msgs.Clock._TYPE);
00043 mutex = new Object();
00044 subscriber.addMessageListener(new MessageListener<Clock>() {
00045 @Override
00046 public void onNewMessage(Clock message) {
00047 synchronized (mutex) {
00048 clock = message;
00049 }
00050 }
00051 });
00052 }
00053
00054 public Subscriber<rosgraph_msgs.Clock> getSubscriber() {
00055 return subscriber;
00056 }
00057
00058 @Override
00059 public Time getCurrentTime() {
00060 Preconditions.checkNotNull(clock);
00061 synchronized (mutex) {
00062 return new Time(clock.getClock());
00063 }
00064 }
00065 }