/*
  Copyright (c) 2001 Karl-Petter Åkesson. All rights reserved.

  This software was developed by Karl-Petter Åkesson, karl-petter@home.se.

  Redistribution and use in source and binary forms, with or without 
  modification, are permitted provided that the following conditions are met:

   1.Redistributions of source code must retain the above copyright notice, 
     this list of conditions and the following disclaimer. 
   2.Redistributions in binary form must reproduce the above copyright notice, 
     this list of conditions and the following disclaimer in the documentation
     and/or other materials provided with the distribution. 
   3.The name of the author may not be used to endorse or promote products 
     derived from this software without specific prior written permission. 

   THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 
   IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
   IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 
   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
   BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 
   OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 
   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
   TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 
   USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

import java.io.DataInputStream;
import java.net.Socket;
import com.dalsemi.system.Clock;
import java.io.IOException;

/**
   Class: TiniTP
   Purpose: A small and simple class to that sets the clock od the Tini using 
            TimeProtocol TP, RFC-868.
*/
public class TiniTP implements Runnable {

    private String NTPServer;
    private Thread timeSetter;

    public TiniTP() {
	initTiniTP(new String("ntp.lth.se"));
    }
    
    public TiniTP(String ntpServer) {
	initTiniTP(ntpServer);
    }

    private void initTiniTP(String ntpServer) {
	this.NTPServer = ntpServer;

	System.out.println("Trying to contact ntp server '" + ntpServer 
			   + "' to set time.");
	setTime();
	Clock C = new Clock();
	System.out.println("Time set: " + 
			   new java.util.Date(C.getTickCount()));
	
	timeSetter = new Thread(this);
	timeSetter.setPriority(Thread.MIN_PRIORITY);
	timeSetter.start();
    }

    public void run() {
	while(true) {
	    try {
		// set time every second hour
		timeSetter.sleep(2*60*60*1000);
		// for debugging purposes every 10th second
		// timeSetter.sleep(10000);
		setTime();
		// Clock C = new Clock();
		// System.out.println("Time set: " + new java.util.Date(C.getTickCount()));
	    } catch(InterruptedException ie) {
	    }
	}
    }

    private void setTime() {
	
	Socket s=null;
	DataInputStream in=null;

	try {
	    s = new Socket(NTPServer,37);
	    s.setSoTimeout(10000);
	    
	    in = new DataInputStream(s.getInputStream());
	} catch(IOException ioe) {
	}
	if(in!=null)
	    try {
		long time = (long)in.readInt()&0xFFFFFFFFl;
		Clock tiniRTC = new Clock();
		tiniRTC.setTickCount((time-2208988800l)*1000);
	    } catch(IOException ioe) {
	    }
    }
}

