-- Copyright (C) 1987 by Xerox Corporation.  All rights reserved.
-- Stub file  was translated on April 2, 1987 6:03:59 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 [InterfaceRecord, InterMdsCallsOnly, LupineProtocolVersion,
      ProcedureIndex, SignalIndex],
  RPCLupine --USING SOME OF [Alloc, Call, DataLength, DeAlloc, Dispatcher,
      -- GetPkt, GetStubPkt, ImportHandle, ImportInterface, ImportInterfaceWithHost,
      -- maxDataLength, maxPrincipalLength, maxShortStringLength, noHost,
      -- pktOverhead, ReceiveExtraPkt, RPCHost, SendPrelimPkt, StartCall,
      -- StartSignal, StubPkt, UnimportInterface]--,
  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]--,
  PrincOpsUtils --USING SOME OF [Enter, Exit]--,
  Rope --USING SOME OF [InlineFlatten, Length, NewText, Text]--,
  VM --USING SOME OF [AddressForPageNumber, PageCount, PageNumber,
      -- PageNumberForAddress, PagesForWords]--;


AgentRpcClientImpl: MONITOR
  IMPORTS RpcPrivate: RPCLupine, Lupine: LupineRuntime, Atom, PrincOpsUtils,
      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,
        hostHint: RpcPrivate.RPCHost←RpcPrivate.noHost ] =
    TRUSTED BEGIN ENABLE UNWIND => NULL;
    IsNull: PROCEDURE [string: Rope.ROPE] RETURNS [BOOLEAN] =
      INLINE {RETURN[string.Length[] = 0]};
        myInterface ← RpcPrivate.ImportInterfaceWithHost [
      interface: [
        type: IF ~IsNull[interfaceName.type]
          THEN interfaceName.type ELSE "Agent~323#256#36742657033",
        instance: interfaceName.instance,
        version: interfaceName.version ],
      localOnly: RpcControl.InterMdsCallsOnly,
      stubProtocol: RpcControl.LupineProtocolVersion,
      hostHint: hostHint ];
    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];
    pkt: RpcPrivate.RPCPkt = RpcPrivate.GetPkt[space: RpcPrivate.Alloc[RpcPrivate.pktOverhead+136]];
    argPkt: LONG POINTER TO ArgumentOverlay = @pkt.data[0];
    pktLength: RpcPrivate.DataLength ← 3;
    lastPkt: BOOLEAN;
    BEGIN ENABLE UNWIND => RpcPrivate.DeAlloc[LOOPHOLE[pkt], RpcPrivate.pktOverhead+136];
      RpcPrivate.StartCall[callPkt: pkt, interface: myInterface];
      argPkt↑ ← [nonceId: nonceId];
      BEGIN  -- Marshal a: Principal to pkt.data[pktLength].
        pktLength ← Lupine.MarshalRope[a, pkt, pktLength, TRUE];
        END;  -- Marshal a.
      BEGIN  -- Marshal b: Principal to pkt.data[pktLength].
        pktLength ← Lupine.MarshalRope[b, pkt, pktLength, TRUE];
        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].
        [an, pktLength] ← UnmarshalFrob[pkt, pktLength];
        END;  -- Unmarshal an.
      BEGIN  -- Unmarshal ar: Frob from pkt.data[pktLength].
        [ar, pktLength] ← UnmarshalFrob[pkt, pktLength];
        END;  -- Unmarshal ar.
      Lupine.CheckPktLength[pkt: pkt, pktLength: pktLength];
      RpcPrivate.DeAlloc[LOOPHOLE[pkt], RpcPrivate.pktOverhead+136];
      RETURN[an, ar];
      END;  -- UNWIND.
    END;  -- Authenticate.

  Vitae: PUBLIC PROCEDURE [clientMachine: Machine, range: RPC.VersionRange,
      interfaceType: RPC.ShortROPE]
    RETURNS [serverMachine: Machine, clientRname: Principal, clientInstance:
        RPC.ShortROPE, serverInstance: RPC.ShortROPE] =
    BEGIN
    ArgumentOverlay: TYPE = MACHINE DEPENDENT RECORD [
        transferIndex (0): RpcControl.ProcedureIndex ← Vitae, clientMachine
        (1): Machine, range (2): RPC.VersionRange];
    ResultOverlay: TYPE = MACHINE DEPENDENT RECORD [
        serverMachine (0): Machine];
    pkt: RpcPrivate.RPCPkt = RpcPrivate.GetPkt[space: RpcPrivate.Alloc[RpcPrivate.pktOverhead+103]];
    argPkt: LONG POINTER TO ArgumentOverlay = @pkt.data[0];
    resPkt: LONG POINTER TO ResultOverlay = @pkt.data[0];
    pktLength: RpcPrivate.DataLength ← 4;
    lastPkt: BOOLEAN;
    BEGIN ENABLE UNWIND => RpcPrivate.DeAlloc[LOOPHOLE[pkt], RpcPrivate.pktOverhead+103];
      RpcPrivate.StartCall[callPkt: pkt, interface: myInterface];
      argPkt↑ ← [clientMachine: clientMachine, range: range];
      BEGIN  -- Marshal interfaceType: RPC.ShortROPE to pkt.data[pktLength].
        pktLength ← Lupine.MarshalRope[interfaceType, pkt, pktLength,
            TRUE];
        END;  -- Marshal interfaceType.
      [returnLength: , lastPkt: lastPkt] ←
        RpcPrivate.Call[ pkt: pkt, callLength: pktLength,
            maxReturnLength: 103, signalHandler: ClientDispatcher];
      pktLength ← 1;
      [serverMachine: serverMachine] ← resPkt↑;
      BEGIN  -- Unmarshal clientRname: Principal from pkt.data[pktLength].
        [clientRname, pktLength] ← Lupine.UnmarshalRope[pkt, pktLength,
            TRUE];
        END;  -- Unmarshal clientRname.
      BEGIN  -- Unmarshal clientInstance: RPC.ShortROPE from pkt.data[pktLength].
        [clientInstance, pktLength] ← Lupine.UnmarshalRope[pkt, pktLength,
            TRUE];
        END;  -- Unmarshal clientInstance.
      BEGIN  -- Unmarshal serverInstance: RPC.ShortROPE from pkt.data[pktLength].
        [serverInstance, pktLength] ← Lupine.UnmarshalRope[pkt, pktLength,
            TRUE];
        END;  -- Unmarshal serverInstance.
      Lupine.CheckPktLength[pkt: pkt, pktLength: pktLength];
      RpcPrivate.DeAlloc[LOOPHOLE[pkt], RpcPrivate.pktOverhead+103];
      RETURN[serverMachine, clientRname, clientInstance, serverInstance];
      END;  -- UNWIND.
    END;  -- Vitae.

  RoutingHostForHost: PUBLIC PROCEDURE [clientMachine: Machine, targetMachine:
      Machine] RETURNS [routingMachine: Machine] =
    BEGIN
    ArgumentOverlay: TYPE = MACHINE DEPENDENT RECORD [
        transferIndex (0): RpcControl.ProcedureIndex ← RoutingHostForHost,
        clientMachine (1): Machine, targetMachine (2): Machine];
    ResultOverlay: TYPE = MACHINE DEPENDENT RECORD [
        routingMachine (0): Machine];
    pktBuffer: ARRAY [1..RpcPrivate.pktOverhead+3] OF WORD;
    pkt: RpcPrivate.StubPkt = RpcPrivate.GetStubPkt[space: @pktBuffer];
    argPkt: LONG POINTER TO ArgumentOverlay = @pkt.data[0];
    resPkt: LONG POINTER TO ResultOverlay = @pkt.data[0];
    pktLength: RpcPrivate.DataLength ← 3;
    lastPkt: BOOLEAN;
    RpcPrivate.StartCall[callPkt: pkt, interface: myInterface];
    argPkt↑ ← [clientMachine: clientMachine, targetMachine: targetMachine];
    [returnLength: , lastPkt: lastPkt] ←
      RpcPrivate.Call[ pkt: pkt, callLength: pktLength,
          maxReturnLength: 1, signalHandler: ClientDispatcher];
    [routingMachine: routingMachine] ← resPkt↑;
    Lupine.CheckPktLength[pkt: pkt, pktLength: 1];
    RETURN[routingMachine];
    END;  -- RoutingHostForHost.


-- 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.


-- Marshall/Unmarshal procedures.
UnmarshalFrob: PROC[pkt: RpcPrivate.RPCPkt, pktLength0: RpcPrivate.DataLength]
  RETURNS[value: Agent.Frob, pktLength: RpcPrivate.DataLength] = BEGIN
  pktLength ← pktLength0; {
  BEGIN  -- Unmarshal value↑: Agent.FrobRecord from pkt.data[pktLength].
    recordIsNIL: Lupine.NilHeader;
    recordIsNIL ← pkt.data[pktLength];  pktLength ← pktLength+1;
    IF recordIsNIL
      THEN value ← NIL
      ELSE BEGIN
        seqLength: Lupine.SequenceHeader;
        seqLength ← Lupine.RpcPktDoubleWord[pkt, pktLength]↑;
        pktLength ← pktLength + 2;
        value ← (paramZones.gc.NEW[Agent.FrobRecord[Lupine.SHORT[seqLength]]]);
        pktLength ← Lupine.CopyFromPkt[pkt: pkt, pktLength: pktLength,
            dataAdr: LOOPHOLE[value], dataLength: SIZE[Agent.FrobRecord[LENGTH[DESCRIPTOR[value↑]]]],
            alwaysOnePkt: TRUE];
        END;  -- IF recordIsNIL.
    END;  -- Unmarshal value↑.
  };END;



-- No module initialization.

END.  -- AgentRpcClientImpl.