-- Stub file  was translated on December 19, 1983 9:49 am by Lupine of October 25, 1983 10:51 am
-- Source interface Agent came from file Agent.bcd, which was created on December 19, 1983 9:39 am with version stamp 63B#37B#37644543706B from source of August 31, 1983 9:20 am.

-- The RPC stub modules for Agent are:
--   AgentRpcControl.mesa;
--   AgentRpcClientImpl.mesa;
--   AgentRpcBinderImpl.mesa;
--   AgentRpcServerImpl.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 = 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
  DESFace,
  RPC,
  RPCPkt,
  Agent,
  AgentRpcControl USING [InterMdsCallsOnly, LupineProtocolVersion,
      ProcedureIndex, SignalIndex],
  RPCLupine --USING SOME OF [Call, DataLength, Dispatcher, GetStubPkt,
      -- ImportHandle, ImportInterface, maxDataLength, maxPrincipalLength,
      -- maxShortStringLength, pktOverhead, ReceiveExtraPkt, SendPrelimPkt,
      -- StartCall, StartSignal, StubPkt, UnimportInterface]--,
  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]--;


AgentRpcClientImpl: MONITOR
  IMPORTS RpcPrivate: RPCLupine, Lupine: LupineRuntime, Atom, Rope
  EXPORTS Agent, AgentRpcControl
  SHARES  Agent, AgentRpcControl, Rope
  = BEGIN OPEN Agent, RpcControl: AgentRpcControl, RpcPublic: RPC;


-- Standard remote binding routines.

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

  ImportInterface: PUBLIC ENTRY SAFE PROCEDURE [
        interfaceName: RpcPublic.InterfaceName,
        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.ImportInterface [
      interface: [
        type: IF ~IsNull[interfaceName.type]
          THEN interfaceName.type ELSE "Agent~63B#37B#37644543706B",
        instance: interfaceName.instance,
        version: interfaceName.version ],
      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;

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



-- Remote public procedure stubs.

  Authenticate: PUBLIC PROCEDURE [nonceId: LONG INTEGER, a: Principal,
      b: Principal]
    RETURNS [an: Frob, ar: Frob] =
    BEGIN
    ArgumentOverlay: TYPE = MACHINE DEPENDENT RECORD [
        transferIndex (0): RpcControl.ProcedureIndex ← Authenticate, nonceId
        (1): LONG INTEGER];
    pktBuffer: ARRAY [1..RpcPrivate.pktOverhead+136] OF WORD;
    pkt: RpcPrivate.StubPkt = RpcPrivate.GetStubPkt[space: @pktBuffer];
    argPkt: POINTER TO ArgumentOverlay = @pkt.data[0];
    pktLength: RpcPrivate.DataLength ← 3;
    lastPkt: BOOLEAN;
    RpcPrivate.StartCall[callPkt: pkt, interface: myInterface];
    argPkt↑ ← [nonceId: nonceId];
    BEGIN  -- Marshal a: Principal to pkt.data[pktLength].
      pkt.data[pktLength] ← a=NIL;  pktLength ← pktLength+1;
      IF a # NIL
        THEN BEGIN
          textRope: Rope.Text = Rope.InlineFlatten[r: a];
          IF textRope.length > RpcPrivate.maxShortStringLength
            THEN Lupine.MarshalingError;
          pkt.data[pktLength] ← textRope.length;  pktLength ← pktLength+1;
          pktLength ← Lupine.CopyToPkt[pkt: pkt, pktLength: pktLength,
              dataAdr: BASE[DESCRIPTOR[textRope.text]], dataLength: Lupine.WordsForChars[textRope.length],
              alwaysOnePkt: TRUE];
          END;  -- IF a # NIL.
      END;  -- Marshal a.
    BEGIN  -- Marshal b: Principal to pkt.data[pktLength].
      pkt.data[pktLength] ← b=NIL;  pktLength ← pktLength+1;
      IF b # NIL
        THEN BEGIN
          textRope: Rope.Text = Rope.InlineFlatten[r: b];
          IF textRope.length > RpcPrivate.maxShortStringLength
            THEN Lupine.MarshalingError;
          pkt.data[pktLength] ← textRope.length;  pktLength ← pktLength+1;
          pktLength ← Lupine.CopyToPkt[pkt: pkt, pktLength: pktLength,
              dataAdr: BASE[DESCRIPTOR[textRope.text]], dataLength: Lupine.WordsForChars[textRope.length],
              alwaysOnePkt: TRUE];
          END;  -- IF b # NIL.
      END;  -- Marshal b.
    [returnLength: , lastPkt: lastPkt] ←
      RpcPrivate.Call[ pkt: pkt, callLength: pktLength,
          maxReturnLength: 136, signalHandler: ClientDispatcher];
    pktLength ← 0;
    BEGIN  -- Unmarshal an: Frob from pkt.data[pktLength].
      BEGIN  -- Unmarshal an↑: FrobRecord from pkt.data[pktLength].
        recordIsNIL: Lupine.NilHeader;
        recordIsNIL ← pkt.data[pktLength];  pktLength ← pktLength+1;
        IF recordIsNIL
          THEN an ← NIL
          ELSE BEGIN
            seqLength: Lupine.SequenceHeader;
            seqLength ← Lupine.StubPktDoubleWord[pkt, pktLength]↑;
            pktLength ← pktLength + 2;
            an ← (paramZones.gc.NEW[FrobRecord[Lupine.SHORT[seqLength]]]);
            pktLength ← Lupine.CopyFromPkt[pkt: pkt, pktLength: pktLength,
                dataAdr: LOOPHOLE[an], dataLength: SIZE[FrobRecord[LENGTH[DESCRIPTOR[an↑]]]],
                alwaysOnePkt: TRUE];
            END;  -- IF recordIsNIL.
        END;  -- Unmarshal an↑.
      END;  -- Unmarshal an.
    BEGIN  -- Unmarshal ar: Frob from pkt.data[pktLength].
      BEGIN  -- Unmarshal ar↑: FrobRecord from pkt.data[pktLength].
        recordIsNIL: Lupine.NilHeader;
        recordIsNIL ← pkt.data[pktLength];  pktLength ← pktLength+1;
        IF recordIsNIL
          THEN ar ← NIL
          ELSE BEGIN
            seqLength: Lupine.SequenceHeader;
            seqLength ← Lupine.StubPktDoubleWord[pkt, pktLength]↑;
            pktLength ← pktLength + 2;
            ar ← (paramZones.gc.NEW[FrobRecord[Lupine.SHORT[seqLength]]]);
            pktLength ← Lupine.CopyFromPkt[pkt: pkt, pktLength: pktLength,
                dataAdr: LOOPHOLE[ar], dataLength: SIZE[FrobRecord[LENGTH[DESCRIPTOR[ar↑]]]],
                alwaysOnePkt: TRUE];
            END;  -- IF recordIsNIL.
        END;  -- Unmarshal ar↑.
      END;  -- Unmarshal ar.
    Lupine.CheckPktLength[pkt: pkt, pktLength: pktLength];
    RETURN[an, ar];
    END;  -- Authenticate.

  Vitae: PUBLIC PROCEDURE [clientMachine: RPCPkt.Machine, range: RPC.VersionRange,
      interfaceType: RPC.ShortROPE]
    RETURNS [--serverMachine:-- RPCPkt.Machine, --clientRname:-- Principal,
        --clientInstance:-- RPC.ShortROPE, --serverInstance:-- RPC.ShortROPE]
        =
    BEGIN
    clientRname: Principal;
    clientInstance: RPC.ShortROPE;
    serverInstance: RPC.ShortROPE;
    ArgumentOverlay: TYPE = MACHINE DEPENDENT RECORD [
        transferIndex (0): RpcControl.ProcedureIndex ← Vitae, clientMachine
        (1): RPCPkt.Machine, range (2): RPC.VersionRange];
    ResultOverlay: TYPE = MACHINE DEPENDENT RECORD [
        serverMachine (0): RPCPkt.Machine];
    pktBuffer: ARRAY [1..RpcPrivate.pktOverhead+103] OF WORD;
    pkt: RpcPrivate.StubPkt = RpcPrivate.GetStubPkt[space: @pktBuffer];
    argPkt: POINTER TO ArgumentOverlay = @pkt.data[0];
    resPkt: POINTER TO ResultOverlay = @pkt.data[0];
    pktLength: RpcPrivate.DataLength ← 4;
    lastPkt: BOOLEAN;
    RpcPrivate.StartCall[callPkt: pkt, interface: myInterface];
    argPkt↑ ← [clientMachine: clientMachine, range: range];
    BEGIN  -- Marshal interfaceType: RPC.ShortROPE to pkt.data[pktLength].
      pkt.data[pktLength] ← interfaceType=NIL;  pktLength ← pktLength+1;
      IF interfaceType # NIL
        THEN BEGIN
          textRope: Rope.Text = Rope.InlineFlatten[r: interfaceType];
          IF textRope.length > RpcPrivate.maxShortStringLength
            THEN Lupine.MarshalingError;
          pkt.data[pktLength] ← textRope.length;  pktLength ← pktLength+1;
          pktLength ← Lupine.CopyToPkt[pkt: pkt, pktLength: pktLength,
              dataAdr: BASE[DESCRIPTOR[textRope.text]], dataLength: Lupine.WordsForChars[textRope.length],
              alwaysOnePkt: TRUE];
          END;  -- IF interfaceType # NIL.
      END;  -- Marshal interfaceType.
    [returnLength: , lastPkt: lastPkt] ←
      RpcPrivate.Call[ pkt: pkt, callLength: pktLength,
          maxReturnLength: 103, signalHandler: ClientDispatcher];
    pktLength ← 1;
    BEGIN  -- Unmarshal clientRname: Principal from pkt.data[pktLength].
      ropeIsNIL: Lupine.NilHeader;
      ropeIsNIL ← pkt.data[pktLength];  pktLength ← pktLength+1;
      IF ropeIsNIL
        THEN clientRname ← NIL
        ELSE BEGIN
          ropeLength: Lupine.RopeHeader;
          textRope: Rope.Text;
          ropeLength ← pkt.data[pktLength];  pktLength ← pktLength+1;
          IF ropeLength > RpcPrivate.maxShortStringLength
            THEN Lupine.UnmarshalingError;
          clientRname ← textRope ← Rope.NewText[size: ropeLength];
          pktLength ← Lupine.CopyFromPkt[pkt: pkt, pktLength: pktLength,
              dataAdr: BASE[DESCRIPTOR[textRope.text]], dataLength: Lupine.WordsForChars[ropeLength],
              alwaysOnePkt: TRUE];
          END;  -- IF ropeIsNIL.
      END;  -- Unmarshal clientRname.
    BEGIN  -- Unmarshal clientInstance: RPC.ShortROPE from pkt.data[pktLength].
      ropeIsNIL: Lupine.NilHeader;
      ropeIsNIL ← pkt.data[pktLength];  pktLength ← pktLength+1;
      IF ropeIsNIL
        THEN clientInstance ← NIL
        ELSE BEGIN
          ropeLength: Lupine.RopeHeader;
          textRope: Rope.Text;
          ropeLength ← pkt.data[pktLength];  pktLength ← pktLength+1;
          IF ropeLength > RpcPrivate.maxShortStringLength
            THEN Lupine.UnmarshalingError;
          clientInstance ← textRope ← Rope.NewText[size: ropeLength];
          pktLength ← Lupine.CopyFromPkt[pkt: pkt, pktLength: pktLength,
              dataAdr: BASE[DESCRIPTOR[textRope.text]], dataLength: Lupine.WordsForChars[ropeLength],
              alwaysOnePkt: TRUE];
          END;  -- IF ropeIsNIL.
      END;  -- Unmarshal clientInstance.
    BEGIN  -- Unmarshal serverInstance: RPC.ShortROPE from pkt.data[pktLength].
      ropeIsNIL: Lupine.NilHeader;
      ropeIsNIL ← pkt.data[pktLength];  pktLength ← pktLength+1;
      IF ropeIsNIL
        THEN serverInstance ← NIL
        ELSE BEGIN
          ropeLength: Lupine.RopeHeader;
          textRope: Rope.Text;
          ropeLength ← pkt.data[pktLength];  pktLength ← pktLength+1;
          IF ropeLength > RpcPrivate.maxShortStringLength
            THEN Lupine.UnmarshalingError;
          serverInstance ← textRope ← Rope.NewText[size: ropeLength];
          pktLength ← Lupine.CopyFromPkt[pkt: pkt, pktLength: pktLength,
              dataAdr: BASE[DESCRIPTOR[textRope.text]], dataLength: Lupine.WordsForChars[ropeLength],
              alwaysOnePkt: TRUE];
          END;  -- IF ropeIsNIL.
      END;  -- Unmarshal serverInstance.
    Lupine.CheckPktLength[pkt: pkt, pktLength: pktLength];
    RETURN[resPkt.serverMachine, clientRname, clientInstance, serverInstance];
    END;  -- Vitae.


-- Remote public signals and errors.


-- Public signal and error dispatcher.

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

    SELECT LOOPHOLE[pkt.data[0], RpcControl.SignalIndex] FROM
      ENDCASE => RETURN[Lupine.DispatchingError[]];

    END;  -- ClientDispatcher


-- Public signal and error dispatcher stubs.


-- No module initialization.

END.  -- AgentRpcClientImpl.