ClockTopicTimeProvider.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2012 Google Inc.
00003  * 
00004  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00005  * use this file except in compliance with the License. You may obtain a copy of
00006  * the License at
00007  * 
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00013  * License for the specific language governing permissions and limitations under
00014  * the License.
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 }


rosjava_core
Author(s):
autogenerated on Wed Aug 26 2015 16:06:49