-- Stub file  was translated on January 20, 1984 6:06 pm by Lupine of December 20, 1983 1:22 pm
-- Source interface ChipTestRemote came from file ChipTestRemote.bcd, which was created on January 20, 1984 6:06 pm with version stamp 264B#344B#21174652556B from source of January 20, 1984 1:13 pm.

-- The RPC stub modules for ChipTestRemote are:
--   ChipTestRemoteRpcControl.mesa;
--   ChipTestRemoteRpcClientImpl.mesa;
--   ChipTestRemoteRpcBinderImpl.mesa;
--   ChipTestRemoteRpcServerImpl.mesa.

-- The parameters for this translation are:
--   Target language = Cedar
--   Default parameter passing = VALUE
--   Deallocate server heap arguments = TRUE
--   Inline RpcServerImpl dispatcher stubs = TRUE
--   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
  ChipTest,
  ChipTestRemote,
  ChipTestRemoteRpcControl USING [InterMdsCallsOnly, LupineProtocolVersion,
      ProcedureIndex, SignalIndex],
  RPC USING [EncryptionKey, InterfaceName, Principal, standardZones,
      Zones],
  RPCLupine --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]--,
  Atom --USING SOME OF [GetPName, MakeAtom]--,
  Rope --USING SOME OF [InlineFlatten, Length, NewText, Text]--;


ChipTestRemoteRpcServerImpl: MONITOR
  IMPORTS ChipTestRemote, RpcPrivate: RPCLupine, Lupine: LupineRuntime,
      Atom, Rope
  EXPORTS ChipTestRemoteRpcControl
  SHARES  ChipTestRemote, ChipTestRemoteRpcControl, Rope
  = BEGIN OPEN ChipTestRemote, RpcControl: ChipTestRemoteRpcControl,
      RpcPublic: RPC;


-- Standard remote binding routines.

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

  ExportInterface: PUBLIC ENTRY SAFE PROCEDURE [
        interfaceName: RpcPublic.InterfaceName,
        user: RpcPublic.Principal,
        password: RpcPublic.EncryptionKey,
        parameterStorage: RpcPublic.Zones ] =
    TRUSTED BEGIN ENABLE UNWIND => NULL;
    IsNull: PROCEDURE [string: Rope.ROPE] RETURNS [BOOLEAN] =
      INLINE {RETURN[string.Length[] = 0]};
    IF bound THEN Lupine.BindingError;
    myInterface ← RpcPrivate.ExportInterface [
      interface: [
        type: IF ~IsNull[interfaceName.type]
          THEN interfaceName.type ELSE "ChipTestRemote~264B#344B#21174652556B",
        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 SAFE PROCEDURE =
    TRUSTED 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

      END;  -- Catch public signals.


    -- Call public procedures (still in dispatcher).

    SELECT LOOPHOLE[pkt.data[0], RpcControl.ProcedureIndex] FROM
      WriteRegs => RETURN[
        WriteRegsStub[pkt: pkt, callLength: callLength, lastPkt: lastPkt,
            localConversation: localConversation]];
      ReadIBuf => RETURN[
        ReadIBufStub[pkt: pkt, callLength: callLength, lastPkt: lastPkt,
            localConversation: localConversation]];
      ExecuteBuffer => RETURN[
        ExecuteBufferStub[pkt: pkt, callLength: callLength, lastPkt:
            lastPkt, localConversation: localConversation]];
      ENDCASE => RETURN[Lupine.DispatchingError[]];

    END;  -- ServerDispatcher


-- Public procedure dispatcher stubs.

  WriteRegsStub: --PROCEDURE [s: RegStores]-- RpcPrivate.Dispatcher
      =
    INLINE BEGIN
    ArgumentOverlay: TYPE = MACHINE DEPENDENT RECORD [
        transferIndex (0): RpcControl.ProcedureIndex, s (1): RegStores];
    argPkt: LONG POINTER TO ArgumentOverlay = @pkt.data[0];
    pktLength: RpcPrivate.DataLength;
    Lupine.CheckPktLength[pkt: pkt, pktLength: 42];
    WriteRegs[argPkt.s];
    pktLength ← 0;
    RETURN[returnLength: pktLength];
    END;  -- WriteRegsStub.

  ReadIBufStub: --PROCEDURE RETURNS [anonP1id1933845: ChipTest.ChannelVec]--
      RpcPrivate.Dispatcher =
    INLINE BEGIN
    ResultOverlay: TYPE = MACHINE DEPENDENT RECORD [
        anonP1id1933845 (0): ChipTest.ChannelVec];
    resPkt: LONG POINTER TO ResultOverlay = @pkt.data[0];
    pktLength: RpcPrivate.DataLength;
    Lupine.CheckPktLength[pkt: pkt, pktLength: 1];
    [resPkt.anonP1id1933845] ← ReadIBuf[];
    pktLength ← 8;
    RETURN[returnLength: pktLength];
    END;  -- ReadIBufStub.

  ExecuteBufferStub: --PROCEDURE [stepCount: NAT, steps: REF WordSeq,
      -- testData: REF WordSeq]
    -- RETURNS [result: ChipTest.ChannelVec, stepsLeft: NAT]-- RpcPrivate.Dispatcher
        =
    INLINE BEGIN
    stepCount: NAT;
    steps: REF WordSeq;
    testData: REF WordSeq;
    ArgumentOverlay: TYPE = MACHINE DEPENDENT RECORD [
        transferIndex (0): RpcControl.ProcedureIndex, stepCount (1): NAT];
    ResultOverlay: TYPE = MACHINE DEPENDENT RECORD [
        result (0): ChipTest.ChannelVec, stepsLeft (8): NAT];
    argPkt: LONG POINTER TO ArgumentOverlay = @pkt.data[0];
    resPkt: LONG POINTER TO ResultOverlay;
    pktLength: RpcPrivate.DataLength ← 2;
    BEGIN  -- OnePkt.
    onePkt: BOOLEAN = lastPkt;
    IF ~onePkt THEN BEGIN  -- Must move statics from pkt now.
      [stepCount: stepCount] ← argPkt↑;
      END;
    BEGIN  -- Unmarshal steps: REF WordSeq from pkt.data[pktLength].
      BEGIN  -- Unmarshal steps↑: WordSeq 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 steps ← NIL
          ELSE BEGIN
            seqLength: Lupine.SequenceHeader;
            seqLength ← Lupine.RpcPktDoubleWord[pkt, pktLength]↑;
            pktLength ← pktLength + 2;
            steps ← (paramZones.gc.NEW[WordSeq[Lupine.SHORT[seqLength]]]);
            pktLength ← Lupine.CopyFromPkt[pkt: pkt, pktLength: pktLength,
                dataAdr: LOOPHOLE[steps], dataLength: SIZE[WordSeq[LENGTH[DESCRIPTOR[steps↑]]]],
                alwaysOnePkt: FALSE];
            END;  -- IF recordIsNIL.
        END;  -- Unmarshal steps↑.
      END;  -- Unmarshal steps.
    BEGIN  -- Unmarshal testData: REF WordSeq from pkt.data[pktLength].
      BEGIN  -- Unmarshal testData↑: WordSeq 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 testData ← NIL
          ELSE BEGIN
            seqLength: Lupine.SequenceHeader;
            seqLength ← Lupine.RpcPktDoubleWord[pkt, pktLength]↑;
            pktLength ← pktLength + 2;
            testData ← (paramZones.gc.NEW[WordSeq[Lupine.SHORT[seqLength]]]);
            pktLength ← Lupine.CopyFromPkt[pkt: pkt, pktLength: pktLength,
                dataAdr: LOOPHOLE[testData], dataLength: SIZE[WordSeq[LENGTH[DESCRIPTOR[testData↑]]]],
                alwaysOnePkt: FALSE];
            END;  -- IF recordIsNIL.
        END;  -- Unmarshal testData↑.
      END;  -- Unmarshal testData.
    Lupine.CheckPktLength[pkt: pkt, pktLength: pktLength];
    resPkt ← @pkt.data[0];
    IF onePkt
      THEN [resPkt.result, resPkt.stepsLeft] ←
          ExecuteBuffer[argPkt.stepCount, steps, testData]
      ELSE [resPkt.result, resPkt.stepsLeft] ←
          ExecuteBuffer[stepCount, steps, testData];
    END;  -- OnePkt.
    pktLength ← 9;
    RETURN[returnLength: pktLength];
    END;  -- ExecuteBufferStub.


-- No module initialization.

END.  -- ChipTestRemoteRpcServerImpl.