-- DiagnosticsImplC.mesa  -  edited by:
-- Poskanzer	27-Apr-83 10:31:52

DIRECTORY
  Address USING [AddressTrouble, StringsToNetworkAddress],
  DiagnosticsOps USING [
    CheckForAbort, PutCR, PutChar, PutLine, PutLongNumber, PutText],
  OISCPConstants USING [echoerSocket],
  OnlineDiagnostics USING [
    EchoResults, EchoUserTest, EchoUserTestFeedbackType, PrintFeedbackProc],
  System USING [NetworkAddress, nullNetworkAddress];

DiagnosticsImplC: PROGRAM
  IMPORTS Address, DiagnosticsOps, OnlineDiagnostics
  EXPORTS DiagnosticsOps =
  BEGIN

  EchoUserTest: PUBLIC PROCEDURE [net: LONG STRING, host: LONG STRING] =
    BEGIN

    destAddr: System.NetworkAddress;
    errFlag: BOOLEAN ← FALSE;
    echoResults: OnlineDiagnostics.EchoResults;

    destAddr ← Address.StringsToNetworkAddress[
      host, OISCPConstants.echoerSocket, net !
      Address.AddressTrouble =>
        BEGIN
        DiagnosticsOps.PutText["Address translation troubles: "L];
        DiagnosticsOps.PutLine[reason];
        errFlag ← TRUE;
        CONTINUE;
        END];
    IF errFlag THEN RETURN;

    DiagnosticsOps.PutCR[];
    DiagnosticsOps.PutLine["               Ethernet Diagnostics"L];
    DiagnosticsOps.PutLine["Each '!' represents a successful echo operation."L];
    DiagnosticsOps.PutLine[
      "Each '?' represents no packet echoed back during waiting period."L];
    DiagnosticsOps.PutLine["Each '#' represents a packet echoed back late."L];
    DiagnosticsOps.PutLine[
      "Each '~' represents a packet which was echoed with bad data but a good CRC."L];
    DiagnosticsOps.PutLine[
      "Each ':' marks largest size packet has just been transmitted."L];

    echoResults ← OnlineDiagnostics.EchoUserTest[
      destAddr: destAddr, printFeedback: PrintFeedback,
      sourceAddr: System.nullNetworkAddress, echoParams: []];

    DiagnosticsOps.PutCR[];
    DiagnosticsOps.PutText["Total packets to be echoed: "L];
    DiagnosticsOps.PutLongNumber[echoResults.totalAttempts, 10];
    DiagnosticsOps.PutCR[];
    DiagnosticsOps.PutText["Packets echoed successfully: "L];
    DiagnosticsOps.PutLongNumber[echoResults.subtotals[goodEcho], 10];
    DiagnosticsOps.PutCR[];
    DiagnosticsOps.PutText["Percentage of packets echoed successfully: "L];
    DiagnosticsOps.PutLongNumber[
      IF echoResults.totalAttempts = 0 THEN 0
      ELSE (echoResults.subtotals[goodEcho]*100)/echoResults.totalAttempts, 10];
    DiagnosticsOps.PutLine["%"L];
    DiagnosticsOps.PutCR[];

    END;  -- EchoUserTest --
    
    PrintFeedback: OnlineDiagnostics.PrintFeedbackProc =
      BEGIN
      DiagnosticsOps.PutChar[
        SELECT feedbackType FROM
          goodEcho => '!,
          noEcho => '?,
          lateEcho => '#,
          badDataGoodCRC => '~,
          bigPacket => ':,
          ENDCASE => '@];
      DiagnosticsOps.CheckForAbort[ ! ABORTED => GOTO aborted];
      RETURN[TRUE];
      EXITS aborted => RETURN[FALSE];
      END;


  END.