-- Copyright (C) 1983  by Xerox Corporation. All rights reserved. 
-- TimeServersLogTajo.mesa, HGM, 19-Nov-83 13:25:31

DIRECTORY
  Ascii USING [CR],
  Format USING [NetworkAddress],
  MStream USING [ReadWrite, Error, GetLength],
  Put USING [Text],
  Stream USING [Delete, Handle, PutString, SetPosition],
  String USING [AppendChar, AppendLongDecimal, AppendString],
  System USING [broadcastHostNumber, GreenwichMeanTime, NetworkAddress, nullNetworkNumber],
  Time USING [Append, AppendCurrent, Unpack],
  
  NSConstants USING [timeServerSocket],
  TimeServerLog USING [];

TimeServersLogTajo: PROGRAM
  IMPORTS Format, MStream, Put, Stream, String, Time
  EXPORTS TimeServerLog =
  BEGIN

  StartLogging: PUBLIC PROCEDURE = BEGIN END;

  StopLogging: PUBLIC PROCEDURE = BEGIN END;
  
  LogInconsistancy: PUBLIC PROCEDURE [
    whoWith: System.NetworkAddress,
    myTime: System.GreenwichMeanTime, myError: LONG CARDINAL,
    theirTime: System.GreenwichMeanTime, theirError: LONG CARDINAL] =
    BEGIN
    text: STRING = [200];
    Time.AppendCurrent[text];
    String.AppendString[text, "  TimeServer: Inconsistancy noted with "L];
    AppendNetworkAddress[text, whoWith];
    String.AppendChar[text, '.];
    String.AppendChar[text, Ascii.CR];
    String.AppendString[text, "Local: "L];
    Time.Append[text, Time.Unpack[myTime], TRUE];
    String.AppendString[text, " +/- "L];
    String.AppendLongDecimal[text, myError];
    String.AppendString[text, ", remote: "L];
    Time.Append[text, Time.Unpack[theirTime], TRUE];
    String.AppendString[text, " +/- "L];
    String.AppendLongDecimal[text, theirError];
    LogString[text];
    AppendToLogFile[text];
    END;
    
  LogClockChange: PUBLIC PROCEDURE [
    time: System.GreenwichMeanTime,
    delta: LONG INTEGER,
    whoFrom: System.NetworkAddress,
    oldError: LONG CARDINAL,
    newError: LONG CARDINAL,
    newVersion: LONG CARDINAL,
    flightTime: LONG CARDINAL] =
    BEGIN
    text: STRING = [300];
    Time.AppendCurrent[text];
    String.AppendString[text, "  TimeServer: Time reset from "L];
    AppendNetworkAddress[text, whoFrom];
    String.AppendString[text, ", adjustment was "L];
    String.AppendLongDecimal[text, delta];
    IF oldError # LAST[LONG CARDINAL] THEN
      BEGIN
      String.AppendChar[text, '.];
      String.AppendChar[text, Ascii.CR];
      String.AppendString[text, "The Clock error was "L];
      String.AppendLongDecimal[text, oldError];
      String.AppendString[text, " ms"L];
      END;
    IF newError # LAST[LONG CARDINAL] THEN
      BEGIN
      String.AppendChar[text, '.];
      String.AppendChar[text, Ascii.CR];
      String.AppendString[text, "The Clock error is "L];
      String.AppendLongDecimal[text, newError];
      String.AppendString[text, " ms"L];
      String.AppendString[text, ".  The flight time was "L];
      String.AppendLongDecimal[text, flightTime];
      String.AppendString[text, " ms"L];
      IF newVersion # 0 THEN
        BEGIN
        String.AppendString[text, ".  The new version is "L];
        String.AppendLongDecimal[text, newVersion];
        END;
      END;
    LogString[text];
    AppendToLogFile[text];
    END;
    
  LogReset: PUBLIC PROCEDURE [whoSays, from: System.NetworkAddress] =
    BEGIN
    anywhere: System.NetworkAddress = [
      net: System.nullNetworkNumber,
      host: System.broadcastHostNumber,
      socket: NSConstants.timeServerSocket];
    text: STRING = [200];
    Time.AppendCurrent[text];
    String.AppendString[text, "  TimeServer: "L];
    AppendNetworkAddress[text, whoSays];
    String.AppendString[text, " told us to reset from "L];
    IF from = anywhere THEN String.AppendString[text, "the local net(s)"L]
    ELSE AppendNetworkAddress[text, from];
    LogString[text];
    AppendToLogFile[text];
    END;

  LogStart: PUBLIC PROCEDURE [whoSays: System.NetworkAddress] =
    BEGIN
    text: STRING = [200];
    Time.AppendCurrent[text];
    String.AppendString[text, "  TimeServer: Started by "L];
    AppendNetworkAddress[text, whoSays];
    LogString[text];
    AppendToLogFile[text];
    END;
  
  LogStop: PUBLIC PROCEDURE [whoSays: System.NetworkAddress] =
    BEGIN
    text: STRING = [200];
    Time.AppendCurrent[text];
    String.AppendString[text, "  TimeServer: Stopped by "L];
    AppendNetworkAddress[text, whoSays];
    LogString[text];
    AppendToLogFile[text];
    END;
  
  LogString: PROCEDURE [text: LONG STRING] =
    BEGIN
    String.AppendChar[text, '.];
    String.AppendChar[text, Ascii.CR];
    Put.Text[NIL, text];
    END;

  AppendToLogFile: PROCEDURE [s: LONG STRING] =
    BEGIN
    sh: Stream.Handle ← NIL;
    sh ← MStream.ReadWrite["TimeServer.log"L, [], text ! MStream.Error => CONTINUE ];
    IF sh = NIL THEN RETURN;
    Stream.SetPosition[sh, MStream.GetLength[sh]];
    Stream.PutString[sh, s];
    Stream.Delete[sh];
    END;
  
  AppendNetworkAddress: PROCEDURE [string: LONG STRING, address: System.NetworkAddress] =
    BEGIN
    Append: PROCEDURE [s: LONG STRING, clientData: LONG POINTER] =
      BEGIN String.AppendString[string, s]; END;
    Format.NetworkAddress[Append, address, productSoftware];
    END;
    
  END.