-- 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, 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]--;


AgentRpcServerImpl: MONITOR
  IMPORTS Agent, RpcPrivate: RPCLupine, Lupine: LupineRuntime, Atom,
      Rope
  EXPORTS AgentRpcControl
  SHARES  Agent, AgentRpcControl, Rope
  = BEGIN OPEN Agent, RpcControl: AgentRpcControl, 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 "Agent~63B#37B#37644543706B",
        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
      Authenticate => RETURN[
        AuthenticateStub[pkt: pkt, callLength: callLength, lastPkt:
            lastPkt, localConversation: localConversation]];
      Vitae => RETURN[
        VitaeStub[pkt: pkt, callLength: callLength, lastPkt: lastPkt,
            localConversation: localConversation]];
      ENDCASE => RETURN[Lupine.DispatchingError[]];

    END;  -- ServerDispatcher


-- Public procedure dispatcher stubs.

  AuthenticateStub: --PROCEDURE [nonceId: LONG INTEGER, a: Principal,
      -- b: Principal]
    -- RETURNS [an: Frob, ar: Frob]-- RpcPrivate.Dispatcher =
    BEGIN
    a: Principal;
    b: Principal;
    an: Frob;
    ar: Frob;
    ArgumentOverlay: TYPE = MACHINE DEPENDENT RECORD [
        transferIndex (0): RpcControl.ProcedureIndex, nonceId (1): LONG
        INTEGER];
    argPkt: LONG POINTER TO ArgumentOverlay = @pkt.data[0];
    pktLength: RpcPrivate.DataLength ← 3;
    BEGIN  -- Unmarshal a: Principal from pkt.data[pktLength].
      ropeIsNIL: Lupine.NilHeader;
      ropeIsNIL ← pkt.data[pktLength];  pktLength ← pktLength+1;
      IF ropeIsNIL
        THEN a ← NIL
        ELSE BEGIN
          ropeLength: Lupine.RopeHeader;
          textRope: Rope.Text;
          ropeLength ← pkt.data[pktLength];  pktLength ← pktLength+1;
          IF ropeLength > RpcPrivate.maxShortStringLength
            THEN Lupine.UnmarshalingError;
          a ← 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 a.
    BEGIN  -- Unmarshal b: Principal from pkt.data[pktLength].
      ropeIsNIL: Lupine.NilHeader;
      ropeIsNIL ← pkt.data[pktLength];  pktLength ← pktLength+1;
      IF ropeIsNIL
        THEN b ← NIL
        ELSE BEGIN
          ropeLength: Lupine.RopeHeader;
          textRope: Rope.Text;
          ropeLength ← pkt.data[pktLength];  pktLength ← pktLength+1;
          IF ropeLength > RpcPrivate.maxShortStringLength
            THEN Lupine.UnmarshalingError;
          b ← 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 b.
    Lupine.CheckPktLength[pkt: pkt, pktLength: pktLength];
    [an, ar] ←
      Authenticate[argPkt.nonceId, a, b];
    pktLength ← 0;
    BEGIN  -- Marshal an: Frob to pkt.data[pktLength].
      BEGIN  -- Marshal an↑: FrobRecord to pkt.data[pktLength].
        pkt.data[pktLength] ← an=NIL;  pktLength ← pktLength+1;
        IF an # NIL THEN BEGIN
          -- Record has a sequence, put its length up front.
          Lupine.RpcPktDoubleWord[pkt, pktLength]↑ ← LENGTH[DESCRIPTOR[an↑]];
          pktLength ← pktLength + 2;
          pktLength ← Lupine.CopyToPkt[pkt: pkt, pktLength: pktLength,
              dataAdr: LOOPHOLE[an], dataLength: SIZE[FrobRecord[LENGTH[DESCRIPTOR[an↑]]]],
              alwaysOnePkt: TRUE];
          END;  -- IF an # NIL.
        END;  -- Marshal an↑.
      END;  -- Marshal an.
    BEGIN  -- Marshal ar: Frob to pkt.data[pktLength].
      BEGIN  -- Marshal ar↑: FrobRecord to pkt.data[pktLength].
        pkt.data[pktLength] ← ar=NIL;  pktLength ← pktLength+1;
        IF ar # NIL THEN BEGIN
          -- Record has a sequence, put its length up front.
          Lupine.RpcPktDoubleWord[pkt, pktLength]↑ ← LENGTH[DESCRIPTOR[ar↑]];
          pktLength ← pktLength + 2;
          pktLength ← Lupine.CopyToPkt[pkt: pkt, pktLength: pktLength,
              dataAdr: LOOPHOLE[ar], dataLength: SIZE[FrobRecord[LENGTH[DESCRIPTOR[ar↑]]]],
              alwaysOnePkt: TRUE];
          END;  -- IF ar # NIL.
        END;  -- Marshal ar↑.
      END;  -- Marshal ar.
    RETURN[returnLength: pktLength];
    END;  -- AuthenticateStub.

  VitaeStub: --PROCEDURE [clientMachine: RPCPkt.Machine, range: RPC.VersionRange,
      -- interfaceType: RPC.ShortROPE]
    -- RETURNS [serverMachine: RPCPkt.Machine, clientRname: Principal,
        -- clientInstance: RPC.ShortROPE, serverInstance: RPC.ShortROPE]--
        RpcPrivate.Dispatcher =
    BEGIN
    interfaceType: RPC.ShortROPE;
    clientRname: Principal;
    clientInstance: RPC.ShortROPE;
    serverInstance: RPC.ShortROPE;
    ArgumentOverlay: TYPE = MACHINE DEPENDENT RECORD [
        transferIndex (0): RpcControl.ProcedureIndex, clientMachine (1):
        RPCPkt.Machine, range (2): RPC.VersionRange];
    ResultOverlay: TYPE = MACHINE DEPENDENT RECORD [
        serverMachine (0): RPCPkt.Machine];
    argPkt: LONG POINTER TO ArgumentOverlay = @pkt.data[0];
    resPkt: LONG POINTER TO ResultOverlay = @pkt.data[0];
    pktLength: RpcPrivate.DataLength ← 4;
    BEGIN  -- Unmarshal interfaceType: RPC.ShortROPE from pkt.data[pktLength].
      ropeIsNIL: Lupine.NilHeader;
      ropeIsNIL ← pkt.data[pktLength];  pktLength ← pktLength+1;
      IF ropeIsNIL
        THEN interfaceType ← NIL
        ELSE BEGIN
          ropeLength: Lupine.RopeHeader;
          textRope: Rope.Text;
          ropeLength ← pkt.data[pktLength];  pktLength ← pktLength+1;
          IF ropeLength > RpcPrivate.maxShortStringLength
            THEN Lupine.UnmarshalingError;
          interfaceType ← 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 interfaceType.
    Lupine.CheckPktLength[pkt: pkt, pktLength: pktLength];
    [resPkt.serverMachine, clientRname, clientInstance, serverInstance]
        ←
      Vitae[argPkt.clientMachine, argPkt.range, interfaceType];
    pktLength ← 1;
    BEGIN  -- Marshal clientRname: Principal to pkt.data[pktLength].
      pkt.data[pktLength] ← clientRname=NIL;  pktLength ← pktLength+1;
      IF clientRname # NIL
        THEN BEGIN
          textRope: Rope.Text = Rope.InlineFlatten[r: clientRname];
          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 clientRname # NIL.
      END;  -- Marshal clientRname.
    BEGIN  -- Marshal clientInstance: RPC.ShortROPE to pkt.data[pktLength].
      pkt.data[pktLength] ← clientInstance=NIL;  pktLength ← pktLength+1;
      IF clientInstance # NIL
        THEN BEGIN
          textRope: Rope.Text = Rope.InlineFlatten[r: clientInstance];
          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 clientInstance # NIL.
      END;  -- Marshal clientInstance.
    BEGIN  -- Marshal serverInstance: RPC.ShortROPE to pkt.data[pktLength].
      pkt.data[pktLength] ← serverInstance=NIL;  pktLength ← pktLength+1;
      IF serverInstance # NIL
        THEN BEGIN
          textRope: Rope.Text = Rope.InlineFlatten[r: serverInstance];
          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 serverInstance # NIL.
      END;  -- Marshal serverInstance.
    RETURN[returnLength: pktLength];
    END;  -- VitaeStub.


-- No module initialization.

END.  -- AgentRpcServerImpl.