-- Stub file  was translated on October 25, 1983 12:25 pm by Lupine of October 25, 1983 10:51 am
-- Source interface AlpineDebug came from file AlpineDebug.bcd, which was created on October 25, 1983 12:25 pm with version stamp 145B#143B#15732130232B from source of May 27, 1983 4:41 pm.

-- The RPC stub modules for AlpineDebug are:
--   AlpineDebugRpcControl.mesa;
--   AlpineDebugRpcClientImpl.mesa;
--   AlpineDebugRpcBinderImpl.mesa;
--   AlpineDebugRpcServerImpl.mesa.

-- The parameters for this translation are:
--   Target language = Cedar
--   Default parameter passing = VALUE
--   Deallocate server heap arguments = TRUE
--   Inline RpcServerImpl dispatcher stubs = FALSE
--   Declare signals = FALSE
--   Warn about short POINTER ("MDS") allocations = TRUE
--   Maximum number of dynamic heap NEWs = 50, MDS NEWs = 50
--   Acceptable parameter protocols = VersionRange[1..1].

-- NOTE: Discard this module unless you use dynamic client binding.


DIRECTORY
  AlpineDebug,
  AlpineDebugRpcControl USING [InterfaceRecord, InterfaceRecordObject],
  AlpineDebugRpcClientImpl,
  RPC USING [InterfaceName, Zones],
  SafeStorage USING [EnableFinalization, EstablishFinalization, FinalizationQueue,
      FQEmpty, FQNext, NewFQ];


AlpineDebugRpcBinderImpl: MONITOR
  IMPORTS AlpineDebug, ClientPrototype: AlpineDebugRpcClientImpl, RTT:
      SafeStorage
  EXPORTS AlpineDebugRpcControl
  SHARES  AlpineDebug, AlpineDebugRpcControl
  = BEGIN OPEN RpcControl: AlpineDebugRpcControl, RpcPublic: RPC;


-- Dynamic instantiation and binding routines.

  ImportNewInterface: PUBLIC SAFE PROCEDURE [
        interfaceName: RpcPublic.InterfaceName,
        parameterStorage: RpcPublic.Zones ]
      RETURNS [interfaceRecord: RpcControl.InterfaceRecord] =
    TRUSTED BEGIN
    interfaceRecord ← NewInterface[];
    LupineDetails[interfaceRecord].module.ImportInterface [
        interfaceName: interfaceName,
        parameterStorage: parameterStorage
      ! UNWIND => FreeInterface[interfaceRecord] ];
    END;

  UnimportNewInterface: SAFE PROCEDURE [
      interfaceRecord: RpcControl.InterfaceRecord ] =
    TRUSTED BEGIN
    LupineDetails[interfaceRecord].module.UnimportInterface[];
    FreeInterface[interfaceRecord];
    END;

-- Utility routines for interface instantiation and caching.

  ConcreteLupineDetails: TYPE = REF LupineDetailsObject;

  LupineDetailsObject:  PUBLIC TYPE = RECORD [
        module: ClientModule←NIL,
        list: RpcControl.InterfaceRecord←NIL, --package reference
        next: RpcControl.InterfaceRecord←NIL --free list -- ];

  LupineDetails: PROCEDURE [abstractInterface: RpcControl.InterfaceRecord]
      RETURNS [ConcreteLupineDetails] =
    INLINE {RETURN[abstractInterface.lupineDetails]};

  ClientModule: TYPE = POINTER TO FRAME[AlpineDebugRpcClientImpl];


  clientInterfaceCache: RpcControl.InterfaceRecord ← NIL; -- free interface
      -- records
  clientInterfaceList: RpcControl.InterfaceRecord ← NIL; -- all interface
      -- records

  NewInterfaceRecord: PUBLIC SAFE PROCEDURE
    RETURNS [interfaceRecord: RpcControl.InterfaceRecord] =
    TRUSTED BEGIN
    interfaceRecord ← NEW[RpcControl.InterfaceRecordObject];
    END;

  NewInterface: PROCEDURE RETURNS [interface: RpcControl.InterfaceRecord]=
    BEGIN
    GetCachedInterface: ENTRY PROCEDURE
        RETURNS [cachedIR: RpcControl.InterfaceRecord] =
      INLINE BEGIN ENABLE UNWIND => NULL;
      IF (cachedIR←clientInterfaceCache) # NIL
        THEN clientInterfaceCache ← LupineDetails[clientInterfaceCache].next;
      END;
    ReclaimInterfaces[];
    IF (interface ← GetCachedInterface[]) = NIL
      THEN BEGIN
        ChainNewInterface: ENTRY PROCEDURE =
          INLINE BEGIN ENABLE UNWIND => NULL;
          interface.lupineDetails ← NEW[
            LupineDetailsObject ← [module: module, list: clientInterfaceList]];
          clientInterfaceList ← interface;
          END; -- ChainNewInterface.
        module: ClientModule = NEW ClientPrototype;
        interface ← NewInterfaceRecord[];
        interface↑ ← [
            ReadOwnerFileHeader: module.ReadOwnerFileHeader, ReadNextOwnerRecord:
            module.ReadNextOwnerRecord, ChangeSpaceForOwner: module.ChangeSpaceForOwner,
            ReadDBUniversalFile: module.ReadDBUniversalFile, ReportAccessCacheStats:
            module.ReportAccessCacheStats, ReportAccessVolatileStats: module.ReportAccessVolatileStats,
            CrashSystem: module.CrashSystem, AccessFailed: AlpineDebug.AccessFailed,
            LockFailed: AlpineDebug.LockFailed, OperationFailed: AlpineDebug.OperationFailed,
            StaticallyInvalid: AlpineDebug.StaticallyInvalid, Unknown:
            AlpineDebug.Unknown];
        ChainNewInterface[];
        END;
    RTT.EnableFinalization[interface];
    END;

  FreeInterface: ENTRY PROCEDURE [interface: RpcControl.InterfaceRecord]=
    INLINE BEGIN ENABLE UNWIND => NULL;
    LupineDetails[interface].next ← clientInterfaceCache;
    clientInterfaceCache ← interface;
    END;


-- Finalization for dynamic interfaces.  Just cache and reuse for now.

  freedInterfaces: RTT.FinalizationQueue = RTT.NewFQ[20];

  ReclaimInterfaces: PROCEDURE =
    INLINE BEGIN
    WHILE ~RTT.FQEmpty[freedInterfaces] DO
      interface: RpcControl.InterfaceRecord =
        NARROW[RTT.FQNext[freedInterfaces]];
      IF interface.lupineDetails # NIL THEN
        UnimportNewInterface[interface];
      ENDLOOP;
    END;


-- Module initialization.

  RTT.EstablishFinalization[
    type: CODE[RpcControl.InterfaceRecordObject],
    npr: 1,  fq: freedInterfaces ];


END.  -- AlpineDebugRpcBinderImpl.