-- OISCPControl.mesa
-- HGM: January 17, 1981  4:11 PM
-- BLyon: January 16, 1981  3:08 PM
-- Function: The ALTO control module for Pilot's Communication configuration.

DIRECTORY
  Process USING [Detach, SetPriority],
  CommunicationInternal USING [
    EchoServerImpl, PacketExchangeImpl, ChecksumsImpl, RouterImpl,
    RoutingTableImpl, SocketImpl, NetworkStreamMgr, PacketStreamMgr],
  DriverDefs USING [CommPackageGo, CommPackageOff],
  Echo USING [CreateServer, DeleteServer],
  OISCP USING [],
  PupDefs USING [PupPackageDestroy, PupPackageMake],
  Router USING [OisRouterOff, OisRouterOn],
  SocketInternal USING [SocketOn],
  SpecialSystem USING [NetworkAddress, nullNetworkAddress],
  System USING [];

OISCPControl: MONITOR
  IMPORTS
    Process, CommunicationInternal, DriverDefs, Echo, PupDefs, Router,
    SocketInternal
  EXPORTS OISCP, System =
  BEGIN

  NetworkAddress: PUBLIC TYPE = SpecialSystem.NetworkAddress;
  nullNetworkAddress: PUBLIC NetworkAddress ← SpecialSystem.nullNetworkAddress;

  -- monitor data
  useCount: CARDINAL;

  InitializeCommunication: PUBLIC PROCEDURE =
    -- module initialization:
    BEGIN
    useCount ← 0;
    PupDefs.PupPackageMake[]; -- So GetProcessorID will work on Altos
    START CommunicationInternal.ChecksumsImpl;
    START CommunicationInternal.RouterImpl;
    START CommunicationInternal.RoutingTableImpl;
    START CommunicationInternal.SocketImpl;
    START CommunicationInternal.PacketExchangeImpl;
    START CommunicationInternal.EchoServerImpl;
    OiscpPackageMake[];
    START CommunicationInternal.PacketStreamMgr;
    START CommunicationInternal.NetworkStreamMgr;
    PupDefs.PupPackageDestroy[];
    END;


  -- The procedure that a normal client calls to start things either before or
  -- after the MakeImage.

  OiscpPackageMake: PUBLIC ENTRY PROCEDURE =
    BEGIN
    IF (useCount ← useCount + 1) > 1 THEN RETURN;
    DriverDefs.CommPackageGo[];
    Router.OisRouterOn[];
    SocketInternal.SocketOn[];
    Process.Detach[FORK FixupEchoPriority[]];
    END;

  FixupEchoPriority: PROCEDURE =
    BEGIN Process.SetPriority[1]; Echo.CreateServer[]; END;

  -- The procedure that a normal client calls to stop things

  OiscpPackageDestroy: PUBLIC ENTRY PROCEDURE =
    BEGIN
    IF (useCount ← useCount - 1) > 0 THEN RETURN;
    Echo.DeleteServer[];
    Router.OisRouterOff[];
    DriverDefs.CommPackageOff[];
    END;

  InitializeCommunication[];

  END..