Example usage for org.joda.time DateTimeUtils setCurrentMillisOffset

List of usage examples for org.joda.time DateTimeUtils setCurrentMillisOffset

Introduction

In this page you can find the example usage for org.joda.time DateTimeUtils setCurrentMillisOffset.

Prototype

public static final void setCurrentMillisOffset(long offsetMillis) throws SecurityException 

Source Link

Document

Sets the current time to return the system time plus an offset.

Usage

From source file:com.DSC.client.SecureChannel.java

License:Open Source License

/**
 * @param args//w w  w  . j a va 2  s  .  c o  m
 * @throws InterruptedException 
 * @throws IOException 
 */
public static void main(String[] args) throws InterruptedException, IOException {
    /* Create their private & public keys */
    ECKey key = new ECKey();
    key.init();
    ProgramState.publicKey = (ECPublicKeyParameters) key.getPublic();
    ProgramState.privateKey = (ECPrivateKeyParameters) key.getPrivate();

    /* Create the IV engine */
    byte[] seed = new byte[64]; // 512 bit seed 
    SecureRandom random = new SecureRandom();
    random.nextBytes(seed);
    ProgramState.IVEngine = new ISAACRandomGenerator(new ISAACEngine());
    ProgramState.IVEngine.init(seed);

    /* Create the blacklist and trusted contacts */
    ProgramState.blacklist = ConcurrentHashMultiset.create();
    ProgramState.trustedKeys = new ConcurrentHashMap<String, Address>();

    /* Set the time for the client accurately using a NTP server */
    DateTimeUtils.setCurrentMillisOffset(getTimeOffset());
    ProgramState.fmt = DateTimeFormat.forPattern("HH:mm:ss");

    /* Set the default nick as anonymous */
    ProgramState.nick = "anonymous";

    /* Initialize ISAACRandomGenerator, set ProgramState.IVEngine */
    receiveController = new ReceiveController();
    sendController = new SendController();

    /* Start input event handler loop */
    eventLoop();
}

From source file:gui.LoginGUI.java

/**
 * Clock sync./*from  ww  w .  ja va2 s. c  o  m*/
 *
 * @throws Exception
 *             the exception
 */
private static void clockSync() throws Exception {
    String serverIP = tfServerIP.getText();

    // Send request
    DatagramSocket socket = new DatagramSocket();
    InetAddress address = InetAddress.getByName(serverIP);
    byte[] buf = new NtpMessage().toByteArray();
    DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 123);

    // Set the transmit timestamp *just* before sending the packet
    // ToDo: Does this actually improve performance or not?
    NtpMessage.encodeTimestamp(packet.getData(), 40, (System.currentTimeMillis() / 1000.0) + 2208988800.0);

    socket.send(packet);

    // Get response
    System.out.println("NTP request sent, waiting for response...\n");
    packet = new DatagramPacket(buf, buf.length);
    socket.receive(packet);

    // Immediately record the incoming timestamp
    double destinationTimestamp = (System.currentTimeMillis() / 1000.0) + 2208988800.0;

    // Process response
    NtpMessage msg = new NtpMessage(packet.getData());

    // Corrected, according to RFC2030 errata
    double roundTripDelay = (destinationTimestamp - msg.originateTimestamp)
            - (msg.transmitTimestamp - msg.receiveTimestamp);

    double localClockOffset = ((msg.receiveTimestamp - msg.originateTimestamp)
            + (msg.transmitTimestamp - destinationTimestamp)) / 2;

    // Display response
    System.out.println("NTP server: " + serverIP);
    System.out.println(msg.toString());

    System.out.println("Dest. timestamp:     " + NtpMessage.timestampToString(destinationTimestamp));

    System.out.println("Round-trip delay: " + new DecimalFormat("0.00").format(roundTripDelay * 1000) + " ms");

    System.out.println(
            "Local clock offset: " + new DecimalFormat("0.00").format(localClockOffset * 1000) + " ms");
    System.out.println("Current time " + DateTimeUtils.currentTimeMillis());
    DateTimeUtils.setCurrentMillisOffset((long) (localClockOffset * 1000));
    System.out.println("Current time " + DateTimeUtils.currentTimeMillis());

    socket.close();
}

From source file:mystore.TheFirstScreenClient.java

private static void clockSync() throws Exception {
    String serverIP = tf_IPServer.getText();
    try (//from  w w  w  .  jav a 2  s . c o m
            // Send request
            DatagramSocket socket = new DatagramSocket()) {
        InetAddress address = InetAddress.getByName(serverIP);
        byte[] buf = new NtpMessage().toByteArray();
        DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 123);

        // Set the transmit timestamp *just* before sending the packet
        // ToDo: Does this actually improve performance or not?
        NtpMessage.encodeTimestamp(packet.getData(), 40, (System.currentTimeMillis() / 1000.0) + 2208988800.0);

        socket.send(packet);

        // Get response
        System.out.println("NTP request sent, waiting for response...\n");
        packet = new DatagramPacket(buf, buf.length);
        socket.receive(packet);

        // Immediately record the incoming timestamp
        double destinationTimestamp = (System.currentTimeMillis() / 1000.0) + 2208988800.0;

        // Process response
        NtpMessage msg = new NtpMessage(packet.getData());

        // Corrected, according to RFC2030 errata
        double roundTripDelay = (destinationTimestamp - msg.originateTimestamp)
                - (msg.transmitTimestamp - msg.receiveTimestamp);

        double localClockOffset = ((msg.receiveTimestamp - msg.originateTimestamp)
                + (msg.transmitTimestamp - destinationTimestamp)) / 2;

        // Display response
        System.out.println("NTP server: " + serverIP);
        System.out.println(msg.toString());

        System.out.println("Dest. timestamp:     " + NtpMessage.timestampToString(destinationTimestamp));

        System.out.println(
                "Round-trip delay: " + new DecimalFormat("0.00").format(roundTripDelay * 1000) + " ms");

        System.out.println(
                "Local clock offset: " + new DecimalFormat("0.00").format(localClockOffset * 1000) + " ms");
        System.out.println("Current time " + DateTimeUtils.currentTimeMillis());
        DateTimeUtils.setCurrentMillisOffset((long) (localClockOffset * 1000));
        System.out.println("Current time " + DateTimeUtils.currentTimeMillis());
    }
}

From source file:org.mifos.core.StandardTestDateUtilityService.java

License:Open Source License

public void setCurrentDateTime(DateTime newTime) {
    Duration durToNewTime = new Duration(new DateTime(), newTime);
    DateTimeUtils.setCurrentMillisOffset(durToNewTime.getMillis());
}

From source file:org.mifos.framework.util.DateTimeService.java

License:Open Source License

public void setCurrentDateTime(DateTime someDateTime) {
    resetToCurrentSystemDateTime();
    DateTimeUtils.setCurrentMillisOffset(someDateTime.getMillis() - new DateTime().getMillis());
}