From 834fb7e317695088cd729940f805c4c28c5f6acf Mon Sep 17 00:00:00 2001
From: jrandom <jrandom>
Date: Fri, 23 Jul 2004 17:43:45 +0000
Subject: [PATCH] allow the timestamper to be controlled by env properties
 (and, in turn, safe to always run) if/when the property "timestamper.enabled"
 is set, the timestamper will query the sntp server(s) and update the clock
 accordingly if/when it is not set (or set to something other than "true"), it
 will pause with its standard delay before checking again in addition, it has
 a guard to help running the timestamper multiple times in the same JVM

---
 .../java/src/net/i2p/time/Timestamper.java    | 46 ++++++++++++-------
 1 file changed, 29 insertions(+), 17 deletions(-)

diff --git a/apps/time/java/src/net/i2p/time/Timestamper.java b/apps/time/java/src/net/i2p/time/Timestamper.java
index 57f3c540d5..d0bc4b508e 100644
--- a/apps/time/java/src/net/i2p/time/Timestamper.java
+++ b/apps/time/java/src/net/i2p/time/Timestamper.java
@@ -23,21 +23,25 @@ import net.i2p.util.Log;
  */
 public class Timestamper implements Runnable {
     private static Log _log = new Log(Timestamper.class);
-    private String _targetURL;
-    private String _serverList[];
+    private static String _targetURL;
+    private static String _serverList[];
     
     private int DELAY_MS = 5*60*1000;
     
-    public Timestamper(String url, String serverNames[]) {
-        if (_log.shouldLog(Log.INFO))
-            _log.info("Creating new timestamper pointing at " + url);
-        _targetURL = url;
-        _serverList = serverNames;
-    }
+    public Timestamper() {}
     
     public void startTimestamper() {
         if (_log.shouldLog(Log.INFO))
             _log.info("Starting timestamper pointing at " + _targetURL);
+        synchronized (Timestamper.class) {
+            String enabled = System.getProperty("timestamper.enabled");
+            if (enabled != null) {
+                _log.warn("Timestamper already running");
+                return;
+            } else {
+                System.setProperty("timestamper.enabled", "true");
+            }
+        }
         I2PThread t = new I2PThread(this, "Timestamper");
         t.setPriority(I2PThread.MIN_PRIORITY);
         t.start();
@@ -48,15 +52,21 @@ public class Timestamper implements Runnable {
             _log.info("Starting up timestamper");
         try {
             while (true) {
-                if (_log.shouldLog(Log.DEBUG))
-                    _log.debug("Querying servers " + _serverList);
-                try {
-                    long now = NtpClient.currentTime(_serverList);
+                String enabled = System.getProperty("timestamper.enabled");
+                if ( (enabled == null) || (!"true".equals(enabled)) ) {
+                    if (_log.shouldLog(Log.DEBUG))
+                        _log.debug("Not stamping the time");
+                } else {
                     if (_log.shouldLog(Log.DEBUG))
-                        _log.debug("Stamp time");
-                    stampTime(now);
-                } catch (IllegalArgumentException iae) {
-                    _log.log(Log.CRIT, "Unable to reach any of the NTP servers - network disconnected?");
+                        _log.debug("Querying servers " + _serverList);
+                    try {
+                        long now = NtpClient.currentTime(_serverList);
+                        if (_log.shouldLog(Log.DEBUG))
+                            _log.debug("Stamp time");
+                        stampTime(now);
+                    } catch (IllegalArgumentException iae) {
+                        _log.log(Log.CRIT, "Unable to reach any of the NTP servers - network disconnected?");
+                    }
                 }
                 try { Thread.sleep(DELAY_MS); } catch (InterruptedException ie) {}
             }
@@ -98,7 +108,9 @@ public class Timestamper implements Runnable {
         } 
         String servers[] = new String[args.length-1];
         System.arraycopy(args, 1, servers, 0, servers.length);
-        Timestamper ts = new Timestamper(args[0], servers);
+        _targetURL = args[0];
+        _serverList = servers;
+        Timestamper ts = new Timestamper();
         ts.startTimestamper();
     }
     
-- 
GitLab