-- Stub file  was translated on January 31, 1985 9:26:12 pm PST by Lupine of December 20, 1983 1:22:57 pm PST
-- Source interface Sage came from file Sage.bcd, which was created on January 31, 1985 9:26:02 pm PST with version stamp 14#20#25451317127 from source of January 31, 1985 8:22:30 pm PST.

-- The RPC stub modules for Sage are:
--   SageRpcControl.mesa;
--   SageRpcClientImpl.mesa;
--   SageRpcBinderImpl.mesa;
--   SageRpcServerImpl.mesa.

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


DIRECTORY
  Sage,
  SageRpcControl USING [InterMdsCallsOnly, LupineProtocolVersion, ProcedureIndex,
      SignalIndex],
  MesaRPC USING [EncryptionKey, InterfaceName, Principal, standardZones,
      Zones],
  MesaRPCLupine --USING SOME OF [Call, DataLength, Dispatcher, ExportHandle,
      -- ExportInterface, GetStubPkt, maxDataLength, maxPrincipalLength,
      -- maxShortStringLength, pktOverhead, ReceiveExtraPkt, SendPrelimPkt,
      -- StartCall, StartSignal, StubPkt, UnexportInterface]--,
  LupineRuntime --USING SOME OF [BindingError, CheckPktLength, CopyFromPkt,
      -- CopyFromMultiplePkts, CopyToPkt, CopyToMultiplePkts, defaultZones,
      -- DispatchingError, FinishThisPkt, ListHeader, MarshalingError,
      -- MarshalingExprError, NilHeader, ProtocolError, RopeHeader, RpcPktDoubleWord,
      -- RuntimeError, SequenceHeader, SHORT, StartNextPkt, StringHeader,
      -- StubPktDoubleWord, TranslationError, UnmarshalingError, UnmarshalingExprError,
      -- WordsForChars]--;


SageRpcServerImpl: MONITOR
  IMPORTS Sage, RpcPrivate: MesaRPCLupine, Lupine: LupineRuntime
  EXPORTS SageRpcControl
  SHARES  Sage, SageRpcControl
  = BEGIN OPEN Sage, RpcControl: SageRpcControl, RpcPublic: MesaRPC;


-- Standard remote binding routines.

  bound: BOOLEAN ← FALSE;
  myInterface: RpcPrivate.ExportHandle;
  paramZones: RpcPublic.Zones ← RpcPublic.standardZones;

  ExportInterface: PUBLIC ENTRY PROCEDURE [
        interfaceName: RpcPublic.InterfaceName,
        user: RpcPublic.Principal,
        password: RpcPublic.EncryptionKey,
        parameterStorage: RpcPublic.Zones ] =
    BEGIN ENABLE UNWIND => NULL;
    IsNull: PROCEDURE [string: LONG STRING] RETURNS [BOOLEAN] =
      INLINE {RETURN[string=NIL OR string.length=0]};
    IF bound THEN Lupine.BindingError;
    myInterface ← RpcPrivate.ExportInterface [
      interface: [
        type: IF ~IsNull[interfaceName.type]
          THEN interfaceName.type ELSE "Sage~14#20#25451317127",
        instance: interfaceName.instance,
        version: interfaceName.version ],
      user: user,  password: password,
      dispatcher: ServerDispatcher,
      localOnly: RpcControl.InterMdsCallsOnly,
      stubProtocol: RpcControl.LupineProtocolVersion ];
    paramZones ← [
      gc: IF parameterStorage.gc # NIL
        THEN parameterStorage.gc ELSE Lupine.defaultZones.gc,
      heap: IF parameterStorage.heap # NIL
        THEN parameterStorage.heap ELSE Lupine.defaultZones.heap,
      mds: IF parameterStorage.mds # NIL
        THEN parameterStorage.mds ELSE Lupine.defaultZones.mds ];
    bound ← TRUE;
    END;

  UnexportInterface: PUBLIC ENTRY PROCEDURE =
    BEGIN ENABLE UNWIND => NULL;
    IF ~bound THEN Lupine.BindingError;
    myInterface ← RpcPrivate.UnexportInterface[myInterface];
    paramZones ← RpcPublic.standardZones;
    bound ← FALSE;
    END;


-- Public procedure dispatcher and public signal and error catcher.

  ServerDispatcher: --PROCEDURE [pkt: RPCPkt, callLength: DataLength,
      -- lastPkt: BOOLEAN, localConversation: Conversation] RETURNS [returnLength:
      -- DataLength]-- RpcPrivate.Dispatcher =
    BEGIN

    -- Catch public signals.

      ENABLE BEGIN

      Error --ERROR [why: ErrorReason]-- =>
        BEGIN
        ArgumentOverlay: TYPE = MACHINE DEPENDENT RECORD [
            transferIndex (0): RpcControl.SignalIndex ← Error, why (1):
            ErrorReason];
        argPkt: LONG POINTER TO ArgumentOverlay = @pkt.data[0];
        pktLength: RpcPrivate.DataLength ← 2;
        lastPkt: BOOLEAN;
        RpcPrivate.StartSignal[signalPkt: pkt];
        argPkt↑ ← [why: why];
        [returnLength: , lastPkt: lastPkt] ←
          RpcPrivate.Call[ pkt: pkt, callLength: pktLength,
              maxReturnLength: 0];
        Lupine.RuntimeError;  -- Impossible to RESUME an ERROR.
        END;  -- Error.

      END;  -- Catch public signals.


    -- Call public procedures (still in dispatcher).

    SELECT LOOPHOLE[pkt.data[0], RpcControl.ProcedureIndex] FROM
      InitializeTester => RETURN[
        InitializeTesterStub[pkt: pkt, callLength: callLength, lastPkt:
            lastPkt, localConversation: localConversation]];
      FinalizeTester => RETURN[
        FinalizeTesterStub[pkt: pkt, callLength: callLength, lastPkt:
            lastPkt, localConversation: localConversation]];
      Run => RETURN[
        RunStub[pkt: pkt, callLength: callLength, lastPkt: lastPkt,
            localConversation: localConversation]];
      ENDCASE => RETURN[Lupine.DispatchingError[]];

    END;  -- ServerDispatcher


-- Public procedure dispatcher stubs.

  InitializeTesterStub: --PROCEDURE [pulseTaps: PulseLineTaps, channelControl:
      -- ChannelControlData, initialData: ChannelVector, feedBack15: BOOL]
    -- RETURNS [anonP1id760835: HANDLETester]-- RpcPrivate.Dispatcher
        =
    BEGIN
    pulseTaps: PulseLineTaps;
    channelControl: ChannelControlData;
    initialData: ChannelVector;
    feedBack15: BOOL;
    ArgumentOverlay: TYPE = MACHINE DEPENDENT RECORD [
        transferIndex (0): RpcControl.ProcedureIndex, feedBack15 (1): BOOL];
    ResultOverlay: TYPE = MACHINE DEPENDENT RECORD [
        anonP1id760835 (0): HANDLETester];
    argPkt: LONG POINTER TO ArgumentOverlay = @pkt.data[0];
    resPkt: LONG POINTER TO ResultOverlay;
    pktLength: RpcPrivate.DataLength ← 2;
    MaxHeapAllocs: CARDINAL = 3;
    heapAllocVector: ARRAY [1..MaxHeapAllocs] OF LONG POINTER←ALL[NIL];
    BEGIN ENABLE UNWIND => BEGIN  -- Free storage.
        FOR ptr: CARDINAL IN [1..LENGTH[heapAllocVector]] DO
          IF heapAllocVector[ptr] = NIL
            THEN EXIT
            ELSE paramZones.heap.FREE[@heapAllocVector[ptr]];
          ENDLOOP;
        END;  -- Free storage.
      BEGIN  -- OnePkt.
      onePkt: BOOLEAN = lastPkt;
      IF ~onePkt THEN BEGIN  -- Must move statics from pkt now.
        [feedBack15: feedBack15] ← argPkt↑;
        END;
      BEGIN  -- Unmarshal pulseTaps: PulseLineTaps from pkt.data[pktLength].
        isNIL: Lupine.NilHeader;
        IF pktLength+1 > RpcPrivate.maxDataLength
          THEN pktLength ← Lupine.FinishThisPkt[pkt: pkt, pktLength:
              pktLength];
        isNIL ← pkt.data[pktLength];  pktLength ← pktLength+1;
        IF isNIL
          THEN pulseTaps ← NIL
          ELSE BEGIN
            pulseTaps ← (heapAllocVector[1] ← paramZones.heap.NEW[PulseLineTapRec]);
            BEGIN  -- Unmarshal pulseTaps↑: PulseLineTapRec from pkt.data[pktLength].
              pktLength ← Lupine.CopyFromPkt[pkt: pkt, pktLength: pktLength,
                  dataAdr: pulseTaps, dataLength: SIZE[PulseLineTapRec],
                  alwaysOnePkt: FALSE];
              END;  -- Unmarshal pulseTaps↑.
            END;  -- IF isNIL.
        END;  -- Unmarshal pulseTaps.
      BEGIN  -- Unmarshal channelControl: ChannelControlData from pkt.data[pktLength].
        isNIL: Lupine.NilHeader;
        IF pktLength+1 > RpcPrivate.maxDataLength
          THEN pktLength ← Lupine.FinishThisPkt[pkt: pkt, pktLength:
              pktLength];
        isNIL ← pkt.data[pktLength];  pktLength ← pktLength+1;
        IF isNIL
          THEN channelControl ← NIL
          ELSE BEGIN
            channelControl ← (heapAllocVector[2] ← paramZones.heap.NEW[ChannelControlDataRec]);
            BEGIN  -- Unmarshal channelControl↑: ChannelControlDataRec
                -- from pkt.data[pktLength].
              pktLength ← Lupine.CopyFromPkt[pkt: pkt, pktLength: pktLength,
                  dataAdr: channelControl, dataLength: SIZE[ChannelControlDataRec],
                  alwaysOnePkt: FALSE];
              END;  -- Unmarshal channelControl↑.
            END;  -- IF isNIL.
        END;  -- Unmarshal channelControl.
      BEGIN  -- Unmarshal initialData: ChannelVector from pkt.data[pktLength].
        isNIL: Lupine.NilHeader;
        IF pktLength+1 > RpcPrivate.maxDataLength
          THEN pktLength ← Lupine.FinishThisPkt[pkt: pkt, pktLength:
              pktLength];
        isNIL ← pkt.data[pktLength];  pktLength ← pktLength+1;
        IF isNIL
          THEN initialData ← NIL
          ELSE BEGIN
            initialData ← (heapAllocVector[3] ← paramZones.heap.NEW[ChannelVectorRec]);
            BEGIN  -- Unmarshal initialData↑: ChannelVectorRec from
                -- pkt.data[pktLength].
              pktLength ← Lupine.CopyFromPkt[pkt: pkt, pktLength: pktLength,
                  dataAdr: initialData, dataLength: SIZE[ChannelVectorRec],
                  alwaysOnePkt: FALSE];
              END;  -- Unmarshal initialData↑.
            END;  -- IF isNIL.
        END;  -- Unmarshal initialData.
      Lupine.CheckPktLength[pkt: pkt, pktLength: pktLength];
      resPkt ← @pkt.data[0];
      IF onePkt
        THEN [resPkt.anonP1id760835] ← InitializeTester[pulseTaps,
            channelControl, initialData, argPkt.feedBack15]
        ELSE [resPkt.anonP1id760835] ← InitializeTester[pulseTaps,
            channelControl, initialData, feedBack15];
      END;  -- OnePkt.
      pktLength ← 2;
      END;  -- ENABLE UNWIND => Free storage.
    FOR ptr: CARDINAL IN [1..LENGTH[heapAllocVector]] DO
      IF heapAllocVector[ptr] = NIL
        THEN EXIT
        ELSE paramZones.heap.FREE[@heapAllocVector[ptr]];
      ENDLOOP;
    RETURN[returnLength: pktLength];
    END;  -- InitializeTesterStub.

  FinalizeTesterStub: --PROCEDURE [t: HANDLETester]-- RpcPrivate.Dispatcher
      =
    BEGIN
    ArgumentOverlay: TYPE = MACHINE DEPENDENT RECORD [
        transferIndex (0): RpcControl.ProcedureIndex, t (1): HANDLETester];
    argPkt: LONG POINTER TO ArgumentOverlay = @pkt.data[0];
    pktLength: RpcPrivate.DataLength;
    Lupine.CheckPktLength[pkt: pkt, pktLength: 3];
    FinalizeTester[argPkt.t];
    pktLength ← 0;
    RETURN[returnLength: pktLength];
    END;  -- FinalizeTesterStub.

  RunStub: --PROCEDURE [t: HANDLETester, runData: VARChannelSequence]--
      RpcPrivate.Dispatcher =
    BEGIN
    t: HANDLETester;
    runData: VARChannelSequence;
    ArgumentOverlay: TYPE = MACHINE DEPENDENT RECORD [
        transferIndex (0): RpcControl.ProcedureIndex, t (1): HANDLETester];
    argPkt: LONG POINTER TO ArgumentOverlay = @pkt.data[0];
    pktLength: RpcPrivate.DataLength ← 3;
    MaxHeapAllocs: CARDINAL = 1;
    heapAllocVector: ARRAY [1..MaxHeapAllocs] OF LONG POINTER←ALL[NIL];
    BEGIN ENABLE UNWIND => BEGIN  -- Free storage.
        FOR ptr: CARDINAL IN [1..LENGTH[heapAllocVector]] DO
          IF heapAllocVector[ptr] = NIL
            THEN EXIT
            ELSE paramZones.heap.FREE[@heapAllocVector[ptr]];
          ENDLOOP;
        END;  -- Free storage.
      BEGIN  -- OnePkt.
      onePkt: BOOLEAN = lastPkt;
      IF ~onePkt THEN BEGIN  -- Must move statics from pkt now.
        [t: t] ← argPkt↑;
        END;
      BEGIN  -- Unmarshal runData: VARChannelSequence from pkt.data[pktLength].
        BEGIN  -- Unmarshal runData↑: ChannelSequenceRec from pkt.data[pktLength].
          recordIsNIL: Lupine.NilHeader;
          IF pktLength+3 > RpcPrivate.maxDataLength
            THEN pktLength ← Lupine.FinishThisPkt[pkt: pkt, pktLength:
                pktLength];
          recordIsNIL ← pkt.data[pktLength];  pktLength ← pktLength+1;
          IF recordIsNIL
            THEN runData ← NIL
            ELSE BEGIN
              seqLength: Lupine.SequenceHeader;
              seqLength ← Lupine.RpcPktDoubleWord[pkt, pktLength]↑;
              pktLength ← pktLength + 2;
              runData ← (heapAllocVector[1] ← paramZones.heap.NEW[ChannelSequenceRec[Lupine.SHORT[seqLength]]]);
              pktLength ← Lupine.CopyFromPkt[pkt: pkt, pktLength: pktLength,
                  dataAdr: runData, dataLength: SIZE[ChannelSequenceRec[LENGTH[DESCRIPTOR[runData↑]]]],
                  alwaysOnePkt: FALSE];
              END;  -- IF recordIsNIL.
          END;  -- Unmarshal runData↑.
        END;  -- Unmarshal runData.
      Lupine.CheckPktLength[pkt: pkt, pktLength: pktLength];
      IF onePkt
        THEN Run[argPkt.t, runData]
        ELSE Run[t, runData];
      END;  -- OnePkt.
      pktLength ← 0;
      BEGIN  -- Marshal runData: VARChannelSequence to pkt.data[pktLength].
        BEGIN  -- Marshal runData↑: ChannelSequenceRec to pkt.data[pktLength].
          IF pktLength+3 > RpcPrivate.maxDataLength
            THEN pktLength ← Lupine.StartNextPkt[pkt: pkt, pktLength:
                pktLength];
          pkt.data[pktLength] ← runData=NIL;  pktLength ← pktLength+1;
          IF runData # NIL THEN BEGIN
            -- Record has a sequence, put its length up front.
            Lupine.RpcPktDoubleWord[pkt, pktLength]↑ ← LENGTH[DESCRIPTOR[runData↑]];
            pktLength ← pktLength + 2;
            pktLength ← Lupine.CopyToPkt[pkt: pkt, pktLength: pktLength,
                dataAdr: runData, dataLength: SIZE[ChannelSequenceRec[LENGTH[DESCRIPTOR[runData↑]]]],
                alwaysOnePkt: FALSE];
            END;  -- IF runData # NIL.
          END;  -- Marshal runData↑.
        END;  -- Marshal runData.
      END;  -- ENABLE UNWIND => Free storage.
    FOR ptr: CARDINAL IN [1..LENGTH[heapAllocVector]] DO
      IF heapAllocVector[ptr] = NIL
        THEN EXIT
        ELSE paramZones.heap.FREE[@heapAllocVector[ptr]];
      ENDLOOP;
    RETURN[returnLength: pktLength];
    END;  -- RunStub.


-- No module initialization.

END.  -- SageRpcServerImpl.