/* * [TimeServers.java] * * Summary: Maintain a list of TimeServers and present as an HTML table. * * Copyright: (c) 2010-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 2010-03-25 initial version */ package com.mindprod.repair; import com.mindprod.common18.BigDate; import com.mindprod.common18.EIO; import com.mindprod.csv.CSVReader; import com.mindprod.csv.CSVWriter; import com.mindprod.htmlmacros.macro.Global; import com.mindprod.htmlmacros.support.ConfigurationForMindprod; import com.mindprod.hunkio.HunkIO; import com.mindprod.setclock.MiniSNTP; import java.io.EOFException; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.HashSet; import static java.lang.System.*; /** * Maintain a list of TimeServers and present as an HTML table. * * @author Roedy Green, Canadian Mind Products * @version 1.0 2010-03-25 initial version * @since 2010-03-25 */ public class TimeServers { private static final String DO_NOT_EDIT = "\n"; /** * root directory of website */ private static String webrootDirname; private static File sourceDir; /** * this takes a while to run because it probes every server to see if * it is working. * * @param args * * @throws IOException */ public static void main( String[] args ) throws IOException { Global.installConfiguration( new ConfigurationForMindprod() ); // combine dirsWithMacros and dirsWithIncludes into dirsToProcess; webrootDirname = Global.configuration.getLocalWebrootWithSlashes(); sourceDir = new File( Global.configuration.getSourceDirWithSlashes() ); final File fileBeingProcessed = new File( sourceDir, "repair/timeservers.csv" ); final CSVReader r = new CSVReader( new FileReader( fileBeingProcessed ) ); final PrintWriter p = EIO.getPrintWriter( new File( webrootDirname + "/jgloss/include/timeservers.htmlfrag" ), 4 * 1024, EIO.UTF8 ); final File tempFile = HunkIO.createTempFile( "temp_", ".tmp", fileBeingProcessed ); // writer, quoteLevel, separatorChar, quoteChar, commentChar, trim final CSVWriter w = new CSVWriter( EIO.getPrintWriter( tempFile, 4 * 1024, EIO.UTF8 ), 0 /* minimal */, ',', '\"', '#', true ); final HashSet alreadyDone = new HashSet<>( 1024 ); String prev = null; try { p.print( DO_NOT_EDIT ); while ( true ) { // where, url, notify? final String where = r.get(); final String hostString = r.get(); if ( hostString.equalsIgnoreCase( prev ) ) { err.println( " Duplicate host: " + hostString ); } prev = hostString; String prevIP = r.get(); // ignore previous value String ip; final boolean notify = r.getBoolean(); r.skipToNextLine(); try { // Inet6Address is for pure ipV6 . Does not work. Shaw does not support ipV6. ip = InetAddress.getByName( hostString ).getHostAddress(); if ( !alreadyDone.add( ip ) ) { err.println( " Duplicate IP: " + hostString + " " + ip ); // we still leave them both is the csv and html lists. // It is up to you to remove one or the other if you want. } // do an nntp probe if ( MiniSNTP.correction( ip ) == Long.MAX_VALUE ) { // find out how differnt our clocks are. // something went wrong with accessing server. err.println( " Server failed: " + hostString ); ip = ""; } } catch ( UnknownHostException e ) { ip = prevIP; } out.println( hostString ); out.println( " " + ip ); // we update our CSV table. w.put( where ); w.put( hostString ); w.put( ip ); w.put( notify ); w.nl(); if ( ip.length() > 0 ) { // we alse output table for live sites // only sites we can lookup ip for do we present in html // we keep duds in CSV in case they come back to life p.print( "" ); p.print( where ); p.print( "" ); p.print( hostString ); p.print( "" ); p.print( ip ); p.print( "" ); p.print( "\n" ); } } } catch ( EOFException e ) { } r.close(); p.close(); w.close(); HunkIO.deleteAndRename( tempFile, fileBeingProcessed ); } }