cgl.narada.service.time.ntptime
Class NtpTimeService

java.lang.Object
  extended by java.lang.Thread
      extended by cgl.narada.service.time.ntptime.NtpTimeService
All Implemented Interfaces:
Runnable

public class NtpTimeService
extends Thread

This is the thread that computes time according to NTP and adjust the time in NtpTimeserviceImpl class.


Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
Thread.State, Thread.UncaughtExceptionHandler
 
Field Summary
protected  String propertiesFile
           
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Method Summary
 void addNtpInfo(NtpInfo ntpInfo)
          Adds NtpInfo object to the queue.
 void computeOffset()
          Computes the offset from the NtpInfo obtained from servers available in the list.
static NtpTimeService getInstance()
          Gets the only instance of NtpTimeService.
 void getNtpSamples()
          Gets samples from Ntp Servers.
 int getNumberOfServers()
          gets the number of servers obtained from properties file.
 HashMap getServerList()
          Gets server list.
 boolean initialize()
          This method is called inside run before the loop starts.
static boolean isCheckLastUTC()
          Returns true if checkLastUTC is true, otherwise returns false.
 boolean isRunning()
          Returns true if running is true, otherwise returns false.
 void loadDefaults()
          Reads Ntp server IPs, interval time and other required params from DefaultParams class.
 void loadProperties()
          Reads Ntp server IPs, interval time and other required params from properties pointer obtained from QosServiceImpl.
 void run()
          This method calls getNtpSamples() and computeOffset() in a while loop, looping on running variable, which can be set to false by setRunning(false) to break the loop.
 void setQosServiceImpl(QosServiceImpl qosServiceImpl)
          Sets QosServiceImpl pointer to get a reference to the properties loaded at initialization.
 void setRunning(boolean running)
          Sets the value of running to the new value.
 void terminateService()
           
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

propertiesFile

protected String propertiesFile
Method Detail

isCheckLastUTC

public static boolean isCheckLastUTC()
Returns true if checkLastUTC is true, otherwise returns false. This enables OffsetHandler to check the previous time provided before making any modifications to the offset.

Returns:
boolean - value of checkLastUTC

getInstance

public static NtpTimeService getInstance()
Gets the only instance of NtpTimeService.

Returns:
NtpTimeService - this object

getNtpSamples

public void getNtpSamples()
Gets samples from Ntp Servers. This method excahnges NtpDatagramPacket with Ntp servers and sets NtpInfo object from the received NtpDatagramPackets. Then NtpInfo objects are put into a queue to be processed by computeOffset() to compute the offset.


getNumberOfServers

public int getNumberOfServers()
gets the number of servers obtained from properties file.

Returns:
int - number of servers

setQosServiceImpl

public void setQosServiceImpl(QosServiceImpl qosServiceImpl)
Sets QosServiceImpl pointer to get a reference to the properties loaded at initialization.

Parameters:
qosServiceImpl - QosServiceImpl

setRunning

public void setRunning(boolean running)
Sets the value of running to the new value.

Parameters:
running - - new value of running. Setting this vlaue to false causes thread to stop running

isRunning

public boolean isRunning()
Returns true if running is true, otherwise returns false.

Returns:
boolean - value of running

getServerList

public HashMap getServerList()
Gets server list. serverList is a HashMap that maps server IP to an integer. This integer is peer id.

Returns:
HashMap - server list

addNtpInfo

public void addNtpInfo(NtpInfo ntpInfo)
Adds NtpInfo object to the queue.

Parameters:
ntpInfo - NtpInfo - NtpInfo object to be added to the queue.

computeOffset

public void computeOffset()
Computes the offset from the NtpInfo obtained from servers available in the list. It filters th eNtpInfo objects in the queue and calls dts() and select() methods which are implemented in NTP class. It then computes the new offset.


initialize

public boolean initialize()
This method is called inside run before the loop starts. It tries to set the offset to a predetermined range. It attempts it until the accuracy is reached or the counter is reached. accuracy and the counter values are set in the properties file with the other information, i.e. interval, serverIPs, etc. The default value for accuracy is 5 ms and for the counter is 5. It waits 1000 ms between each iteration until the the offset is reached.

Returns:
boolean - it returns true if success, otherwise returns false;

loadDefaults

public void loadDefaults()
Reads Ntp server IPs, interval time and other required params from DefaultParams class.


loadProperties

public void loadProperties()
Reads Ntp server IPs, interval time and other required params from properties pointer obtained from QosServiceImpl.


run

public void run()
This method calls getNtpSamples() and computeOffset() in a while loop, looping on running variable, which can be set to false by setRunning(false) to break the loop.

Specified by:
run in interface Runnable
Overrides:
run in class Thread

terminateService

public void terminateService()


For comments and suggestions please send e-mail to The NaradaBrokering Project