-- File: TranslationServer.mesa - last edit:
-- AOF                  3-Feb-88 15:06:03
-- HGM                 25-Jun-85  2:21:33
-- Copyright (C) 1983, 1985, 1988 by Xerox Corporation. All rights reserved. 

-- Provide a Pup address for machines that have a 48 bit host number.

DIRECTORY
  NSBuffer USING [Buffer],
  NameServerDefs USING [NSNameServer],
  Socket USING [
    ChannelHandle, Create, defaultWaitTime, Delete, GetPacket,
    ReturnBuffer, SetWaitTime, SwapSourceAndDestination,
    TimeOut],
  NSConstants USING [pupAddressTranslation];

TranslationServer: PROGRAM IMPORTS Socket, NameServerDefs =
  BEGIN

  -- These should move to NSTypes
  translationRequest: CARDINAL = 1;
  translationResponse: CARDINAL = 2;
  translationError: CARDINAL = 3;

  pleaseStop: BOOLEAN;
  translationFork: PROCESS;

  CreateServer: PUBLIC PROCEDURE =
    BEGIN
    pleaseStop ← FALSE;
    translationFork ← FORK TranslationServer[];
    END;

  TranslationServer: PROCEDURE =
    BEGIN
    cH: Socket.ChannelHandle;
    b: NSBuffer.Buffer;
    cH ← Socket.Create[NSConstants.pupAddressTranslation, 0, 2];
    Socket.SetWaitTime[cH, Socket.defaultWaitTime];
    UNTIL pleaseStop DO
      b ← NIL;
      b ← Socket.GetPacket[cH ! Socket.TimeOut => CONTINUE];
      IF b = NIL THEN LOOP;
      IF b.ns.nsWords[2] = translationRequest THEN
        BEGIN
        Socket.SwapSourceAndDestination[b];
        NameServerDefs.NSNameServer[cH, b];
        LOOP;
        END;
      Socket.ReturnBuffer[b];
      ENDLOOP;
    Socket.Delete[cH];
    END;

  CreateServer[];
  END.