List of usage examples for org.apache.commons.net.ntp TimeStamp getTime
public long getTime()
From source file:flex.helpers.NTPHelper.java
/** * Process <code>TimeInfo</code> object and print its details. * //from w w w. ja va 2 s.c om * @param info <code>TimeInfo</code> object. * @return */ public static Date processResponse(TimeInfo info) { NtpV3Packet message = info.getMessage(); org.apache.commons.net.ntp.TimeStamp rcvNtpTime = message.getReceiveTimeStamp(); //Receive Time is time request received by server (t2) return new Date(rcvNtpTime.getTime()); //OJO... Para mejorar // TimeStamp refNtpTime = message.getReferenceTimeStamp(); // TimeStamp origNtpTime = message.getOriginateTimeStamp(); //Originate Time is time request sent by client (t1) // TimeStamp xmitNtpTime = message.getTransmitTimeStamp(); //Transmit time is time reply sent by server (t3) // long destTime = info.getReturnTime(); // TimeStamp destNtpTime = TimeStamp.getNtpTime(destTime); //Destination time is time reply received by client (t4) // int stratum = message.getStratum(); //Stratum should be 0..15... // String refType; // if (stratum <= 0) { // refType = "(Unspecified or Unavailable)"; // } else if (stratum == 1) { // refType = "(Primary Reference; e.g., GPS)"; //GPS, radio clock, etc. // } else { // refType = "(Secondary Reference; e.g. via NTP or SNTP)"; // } // // int version = message.getVersion(); // int li = message.getLeapIndicator(); // int poll = message.getPoll(); //poll value typically btwn MINPOLL (4) and MAXPOLL (14) // double disp = message.getRootDispersionInMillisDouble(); // int refId = message.getReferenceId(); // String refAddr = NtpUtils.getHostAddress(refId); // String refName = null; // if (refId != 0) { // if (refAddr.compareTo("127.127.1.0") == 0) { // refName = "LOCAL"; //This is the ref address for the Local Clock // } else if (stratum >= 2) { // //If reference id has 127.127 prefix then it uses its own reference clock // //defined in the form 127.127.clock-type.unit-num (e.g. 127.127.8.0 mode 5 // //for GENERIC DCF77 AM; see refclock.htm from the NTP software distribution. // if (!refAddr.startsWith("127.127")) { // try { // InetAddress addr = InetAddress.getByName(refAddr); // String name = addr.getHostName(); // if (name != null && !(name.compareTo(refAddr) == 0)) { // refName = name; // } // } catch (UnknownHostException e) { // //some stratum-2 servers sync to ref clock device but fudge stratum level higher... (e.g. 2) // //ref not valid host maybe it's a reference clock name? // //otherwise just show the ref IP address. // refName = NtpUtils.getReferenceClock(message); // } // } // } else if (version >= 3 && (stratum == 0 || stratum == 1)) { // refName = NtpUtils.getReferenceClock(message); // //refname usually have at least 3 characters (e.g. GPS, WWV, LCL, etc.) // } // //otherwise give up on naming the beast... // } // if (refName != null && refName.length() > 1) { // refAddr += " (" + refName + ")"; // } // // info.computeDetails(); // compute offset/delay if not already done // Long offsetValue = info.getOffset(); // Long delayValue = info.getDelay(); // String delay = (delayValue == null) ? "N/A" : delayValue.toString(); // String offset = (offsetValue == null) ? "N/A" : offsetValue.toString(); // final NumberFormat numberFormat = new java.text.DecimalFormat("0.00"); // // System.out.println(" Stratum: " + stratum + " " + refType); // System.out.println(" leap=" + li + ", version=" + version + ", precision=" + message.getPrecision()); // System.out.println(" mode: " + message.getModeName() + " (" + message.getMode() + ")"); // System.out.println(" poll: " + (poll <= 0 ? 1 : (int) Math.pow(2, poll)) + " seconds" + " (2 ** " + poll + ")"); // System.out.println(" rootdelay=" + numberFormat.format(message.getRootDelayInMillisDouble()) + ", rootdispersion(ms): " + numberFormat.format(disp)); // System.out.println(" Reference Identifier:\t" + refAddr); // System.out.println(" Reference Timestamp:\t" + refNtpTime + " " + refNtpTime.toDateString()); // System.out.println(" Originate Timestamp:\t" + origNtpTime + " " + origNtpTime.toDateString()); // System.out.println(" Receive Timestamp:\t" + rcvNtpTime + " " + rcvNtpTime.toDateString()); // System.out.println(" Transmit Timestamp:\t" + xmitNtpTime + " " + xmitNtpTime.toDateString()); // System.out.println(" Destination Timestamp:\t" + destNtpTime + " " + destNtpTime.toDateString()); // System.out.println(" Roundtrip delay(ms)=" + delay + ", clock offset(ms)=" + offset); //offset in ms }
From source file:com.eventsourcing.hlc.NTPServerTimeProviderTest.java
@Test(successPercentage = 99, dataProvider = "delays") public void secondsPassed(int delay) throws UnknownHostException, InterruptedException, TimeoutException { TimeStamp ts1 = provider.getTimestamp(); Thread.sleep(delay);/*from w w w. j a v a 2 s .c o m*/ TimeStamp ts2 = provider.getTimestamp(); long seconds = delay / 1000; // Verify that seconds passed were calculated properly // since the last time NTP timestamp was fetched. Measuring fractions // is pointless as there's a gap between sleeping and requesting the timestamp. assertEquals("Delay=" + delay + " time_diff=" + (ts2.getTime() - ts1.getTime()), seconds, (ts2.getTime() - ts1.getTime()) / 1000); }
From source file:org.mandar.analysis.recsys2014.recsysMain.java
public void writeAlgoTestResults(double nDCG, DB db) { /*//from w ww. j a va 2 s . com * Write the test configuration * parameters as well as test * results for nDCG @10 */ TimeStamp time1 = new TimeStamp(new Date()); algoConfig.append("timestamp", time1.getTime()); algoConfig.append("nDCG", nDCG); DBCollection eval_runs = db.getCollection(DBSettings.EVAL_COLLECTION); eval_runs.insert(algoConfig); }
From source file:org.vesalainen.nmea.processor.SNTPServer.java
@Override public void run() { try {/*from w w w . j a va2 s . c o m*/ socket = new DatagramSocket(NTP_PORT); ntpMessage = new NtpV4Impl(); DatagramPacket datagramPacket = ntpMessage.getDatagramPacket(); while (true) { socket.receive(datagramPacket); log.info("NTP: received %s from %s %d", ntpMessage, datagramPacket.getAddress(), datagramPacket.getPort()); TimeStamp transmitTimeStamp = ntpMessage.getTransmitTimeStamp(); ntpMessage.setLeapIndicator(LI_NO_WARNING); ntpMessage.setMode(MODE_SERVER); ntpMessage.setStratum(1); ntpMessage.setPrecision(-20); ntpMessage.setRootDelay(0); ntpMessage.setRootDispersion(0); ntpMessage.setReferenceId(ReferenceIdentifier.GPS); ntpMessage.setReferenceTime(TimeStamp.getNtpTime(clock.getZonedDateTime().toEpochSecond())); ntpMessage.setOriginateTimeStamp(transmitTimeStamp); long time = clock.millis(); TimeStamp timeStamp = TimeStamp.getNtpTime(time); ntpMessage.setReceiveTimeStamp(timeStamp); ntpMessage.setTransmitTime(timeStamp); socket.send(datagramPacket); log.info("NTP: sent %s to %s %d diff=%d", ntpMessage, datagramPacket.getAddress(), datagramPacket.getPort(), time - transmitTimeStamp.getTime()); } } catch (Exception ex) { log.log(Level.SEVERE, ex, ""); } }