-- Copyright (C) 1983  by Xerox Corporation. All rights reserved. 
-- GateControlFinishNoDisk.mesa, HGM, 23-Sep-83 16:18:18

DIRECTORY
  Ascii USING [CR],
  Process USING [Detach, SecondsToTicks],
  Put USING [Text],
  String USING [AppendString, AppendChar],
  System USING [PowerOff],
  TemporaryBooting USING [BootButton],
  Time USING [AppendCurrent],

  GateControlDefs USING [],
  PupDefs USING [AppendHostName, PupBuffer, PupAddress];

GateControlFinishNoDisk: MONITOR
  IMPORTS
    Process, Put, String, System, TemporaryBooting,
    Time, PupDefs
  EXPORTS GateControlDefs =
  BEGIN

  verbose: BOOLEAN = TRUE;

  restarting: BOOLEAN ← FALSE;

  RestartGateway: PUBLIC ENTRY PROCEDURE [b: PupDefs.PupBuffer] =
    BEGIN
    IF restarting THEN RETURN;
    restarting ← TRUE;
    Process.Detach[FORK Restart[b.pup.source, TRUE]];
    END;

  HaltGateway: PUBLIC ENTRY PROCEDURE [b: PupDefs.PupBuffer] =
    BEGIN
    IF restarting THEN RETURN;
    restarting ← TRUE;
    Process.Detach[FORK Restart[b.pup.source, FALSE]];
    END;

  Restart: ENTRY PROCEDURE [who: PupDefs.PupAddress, restart: BOOLEAN] =
    BEGIN
    dally: CONDITION ← [timeout: Process.SecondsToTicks[1]];
    IF verbose THEN
      BEGIN
      text: STRING = [100];
      Time.AppendCurrent[text];
      String.AppendString[text, "  Gateway "L];
      String.AppendString[text, IF restart THEN "restarted"L ELSE "halted"L];
      String.AppendString[text, " by "L];
      PupDefs.AppendHostName[text, who];
      String.AppendChar[text, '.];
      String.AppendChar[text, Ascii.CR];
      LogString[text];
      END;
    FOR i: CARDINAL IN [0..5) DO WAIT dally; ENDLOOP;
    IF restart THEN TemporaryBooting.BootButton[]
    ELSE System.PowerOff[];
    END;

  LogString: PROCEDURE [text: STRING] =
    BEGIN
    Put.Text[NIL, text];
    END;

  END.