-- Copyright (C) 1987 by Xerox Corporation.  All rights reserved.
-- Stub file  was translated on April 2, 1987 6:04:02 pm PST by Lupine of January 22, 1987 5:37:32 pm PST
-- Source interface Agent came from file Agent.bcd, which was created on April 2, 1987 6:03:41 pm PST with version stamp 323#256#36742657033 from source of May 23, 1986 3:27:05 pm PDT.

-- 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,
  Pup,
  RPC,
  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, MarshalAtom, MarshalRope, NilHeader, ProtocolError,
      -- RopeHeader, RpcPktDoubleWord, RuntimeError, SequenceHeader, SHORT,
      -- StartNextPkt, StringHeader, StubPktDoubleWord, TranslationError,
      -- UnmarshalingError, UnmarshalingExprError, UnmarshalAtom, UnmarshalRope,
      -- 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~323#256#36742657033",
        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]];
      RoutingHostForHost => RETURN[
        RoutingHostForHostStub[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].
      [a, pktLength] ← Lupine.UnmarshalRope[pkt, pktLength, TRUE];
      END;  -- Unmarshal a.
    BEGIN  -- Unmarshal b: Principal from pkt.data[pktLength].
      [b, pktLength] ← Lupine.UnmarshalRope[pkt, pktLength, TRUE];
      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].
      pktLength ← MarshalFrob[an, pkt, pktLength];
      END;  -- Marshal an.
    BEGIN  -- Marshal ar: Frob to pkt.data[pktLength].
      pktLength ← MarshalFrob[ar, pkt, pktLength];
      END;  -- Marshal ar.
    RETURN[returnLength: pktLength];
    END;  -- AuthenticateStub.

  VitaeStub: --PROCEDURE [clientMachine: Machine, range: RPC.VersionRange,
      -- interfaceType: RPC.ShortROPE]
    -- RETURNS [serverMachine: 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):
        Machine, range (2): RPC.VersionRange];
    ResultOverlay: TYPE = MACHINE DEPENDENT RECORD [
        serverMachine (0): 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].
      [interfaceType, pktLength] ← Lupine.UnmarshalRope[pkt, pktLength,
          TRUE];
      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].
      pktLength ← Lupine.MarshalRope[clientRname, pkt, pktLength, TRUE];
      END;  -- Marshal clientRname.
    BEGIN  -- Marshal clientInstance: RPC.ShortROPE to pkt.data[pktLength].
      pktLength ← Lupine.MarshalRope[clientInstance, pkt, pktLength,
          TRUE];
      END;  -- Marshal clientInstance.
    BEGIN  -- Marshal serverInstance: RPC.ShortROPE to pkt.data[pktLength].
      pktLength ← Lupine.MarshalRope[serverInstance, pkt, pktLength,
          TRUE];
      END;  -- Marshal serverInstance.
    RETURN[returnLength: pktLength];
    END;  -- VitaeStub.

  RoutingHostForHostStub: --PROCEDURE [clientMachine: Machine, targetMachine:
      -- Machine] RETURNS [routingMachine: Machine]-- RpcPrivate.Dispatcher
      =
    BEGIN
    ArgumentOverlay: TYPE = MACHINE DEPENDENT RECORD [
        transferIndex (0): RpcControl.ProcedureIndex, clientMachine (1):
        Machine, targetMachine (2): Machine];
    ResultOverlay: TYPE = MACHINE DEPENDENT RECORD [
        routingMachine (0): Machine];
    argPkt: LONG POINTER TO ArgumentOverlay = @pkt.data[0];
    resPkt: LONG POINTER TO ResultOverlay = @pkt.data[0];
    pktLength: RpcPrivate.DataLength;
    Lupine.CheckPktLength[pkt: pkt, pktLength: 3];
    [resPkt.routingMachine] ← RoutingHostForHost[argPkt.clientMachine,
        argPkt.targetMachine];
    pktLength ← 1;
    RETURN[returnLength: pktLength];
    END;  -- RoutingHostForHostStub.


-- Marshall/Unmarshal procedures.
MarshalFrob: PROC[value: Agent.Frob, pkt: RpcPrivate.RPCPkt, pktLength0:
    RpcPrivate.DataLength]
  RETURNS[pktLength: RpcPrivate.DataLength] = BEGIN
  pktLength ← pktLength0; {
  BEGIN  -- Marshal value↑: Agent.FrobRecord to pkt.data[pktLength].
    pkt.data[pktLength] ← value=NIL;  pktLength ← pktLength+1;
    IF value # NIL THEN BEGIN
      -- Record has a sequence, put its length up front.
      Lupine.RpcPktDoubleWord[pkt, pktLength]↑ ← LENGTH[DESCRIPTOR[value↑]];
      pktLength ← pktLength + 2;
      pktLength ← Lupine.CopyToPkt[pkt: pkt, pktLength: pktLength,
          dataAdr: LOOPHOLE[value], dataLength: SIZE[Agent.FrobRecord[LENGTH[DESCRIPTOR[value↑]]]],
          alwaysOnePkt: TRUE];
      END;  -- IF value # NIL.
    END;  -- Marshal value↑.
  };END;



-- No module initialization.

END.  -- AgentRpcServerImpl.