forked from rosjava/rosjava_core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added NtpTimeProvider and renamed WallclockProvider to WallTimeProvider.
Improved error messages from MessageFactory. Added fromMillis/Nanos to Duration to support NtpTimeProvider.
- Loading branch information
1 parent
5e6199d
commit ba59add
Showing
8 changed files
with
125 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,11 +16,11 @@ | |
|
||
package org.ros.internal.message.old_style; | ||
|
||
import com.google.common.base.Preconditions; | ||
|
||
import org.ros.exception.RosRuntimeException; | ||
import org.ros.message.Message; | ||
|
||
import com.google.common.base.Preconditions; | ||
|
||
/** | ||
* @author [email protected] (Damon Kohler) | ||
*/ | ||
|
@@ -72,14 +72,12 @@ public <T> T newServiceRequest(String serviceType) { | |
* Get the class for a given message type. | ||
* | ||
* @param messageType | ||
* The string giving "ros package"/"message name", e.g. std_msgs/Time | ||
* the string giving "ros package"/"message name", e.g. std_msgs/Time | ||
* @param packagePath | ||
* Path to the package which contain messages. | ||
* | ||
* @return The class. | ||
* | ||
* path to the package which contain messages | ||
* @return the message class | ||
* @throws RosRuntimeException | ||
* No class representing that name or the class is not accessible. | ||
* no class representing that name or the class is not accessible | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
private static Class<Message> loadMessageClass(String messageType, String packagePath) { | ||
|
@@ -88,7 +86,7 @@ private static Class<Message> loadMessageClass(String messageType, String packag | |
return (Class<Message>) MessageFactory.class.getClassLoader().loadClass( | ||
packagePath + "." + messageType.replace('/', '.')); | ||
} catch (Exception e) { | ||
throw new RosRuntimeException(e); | ||
throw new RosRuntimeException("Failed to load message type: \"" + messageType + "\"", e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright (C) 2011 Google 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.time; | ||
|
||
import org.apache.commons.net.ntp.NTPUDPClient; | ||
import org.apache.commons.net.ntp.TimeInfo; | ||
import org.ros.exception.RosRuntimeException; | ||
import org.ros.message.Duration; | ||
import org.ros.message.Time; | ||
|
||
import java.io.IOException; | ||
import java.net.InetAddress; | ||
|
||
/** | ||
* @author [email protected] (Damon Kohler) | ||
*/ | ||
public class NtpTimeProvider implements TimeProvider { | ||
|
||
private final InetAddress host; | ||
private final NTPUDPClient ntpClient; | ||
private final WallTimeProvider wallTimeProvider; | ||
|
||
private TimeInfo time; | ||
|
||
public NtpTimeProvider(InetAddress host) { | ||
this.host = host; | ||
this.wallTimeProvider = new WallTimeProvider(); | ||
ntpClient = new NTPUDPClient(); | ||
} | ||
|
||
public void updateTime() { | ||
try { | ||
time = ntpClient.getTime(host); | ||
} catch (IOException e) { | ||
throw new RosRuntimeException("Failed to read time from NTP server " + host.getHostName(), e); | ||
} | ||
time.computeDetails(); | ||
} | ||
|
||
@Override | ||
public Time getCurrentTime() { | ||
Time currentTime = wallTimeProvider.getCurrentTime(); | ||
long offset = time.getOffset(); | ||
return currentTime.add(Duration.fromMillis(offset)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,9 +14,8 @@ | |
* the License. | ||
*/ | ||
|
||
package org.ros.internal.time; | ||
package org.ros.time; | ||
|
||
import org.ros.time.TimeProvider; | ||
|
||
import org.ros.message.Time; | ||
|
||
|
@@ -25,7 +24,7 @@ | |
* | ||
* @author [email protected] (Ken Conley) | ||
*/ | ||
public class WallclockProvider implements TimeProvider { | ||
public class WallTimeProvider implements TimeProvider { | ||
|
||
@Override | ||
public Time getCurrentTime() { | ||
|
36 changes: 36 additions & 0 deletions
36
rosjava/src/test/java/org/ros/time/NtpTimeProviderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright (C) 2011 Google 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.time; | ||
|
||
import org.junit.Test; | ||
import org.ros.address.InetAddressFactory; | ||
|
||
/** | ||
* @author [email protected] (Damon Kohler) | ||
*/ | ||
public class NtpTimeProviderTest { | ||
|
||
@Test | ||
public void testNtpTime() { | ||
// TODO(damonkohler): This is only a simple sanity check. | ||
NtpTimeProvider ntpTimeProvider = | ||
new NtpTimeProvider(InetAddressFactory.newFromHostString("ntps1-0.cs.tu-berlin.de")); | ||
ntpTimeProvider.updateTime(); | ||
ntpTimeProvider.getCurrentTime(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters