-- Copyright (C) 1987 by Xerox Corporation.  All rights reserved.
-- Stub file  was translated on April 4, 1987 3:33:00 pm PST by Lupine of January 22, 1987 5:37:32 pm PST
-- Source interface LarkSmarts came from file LarkSmarts.bcd, which was created on April 4, 1987 3:32:53 pm PST with version stamp 317#365#23112662400 from source of June 10, 1986 9:03:37 am PDT.

-- The RPC stub modules for LarkSmarts are:
--   LarkSmartsRpcControl.mesa;
--   LarkSmartsRpcClientImpl.mesa;
--   LarkSmartsRpcBinderImpl.mesa;
--   LarkSmartsRpcServerImpl.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
  BasicTime,
  Lark,
  RPC,
  LarkSmarts,
  LarkSmartsRpcControl 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]--;


LarkSmartsRpcServerImpl: MONITOR
  IMPORTS LarkSmarts, RpcPrivate: RPCLupine, Lupine: LupineRuntime,
      Atom, Rope
  EXPORTS LarkSmartsRpcControl
  SHARES  LarkSmarts, LarkSmartsRpcControl, Rope
  = BEGIN OPEN LarkSmarts, RpcControl: LarkSmartsRpcControl, 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 "LarkSmarts~317#365#23112662400",
        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
      Register => RETURN[
        RegisterStub[pkt: pkt, callLength: callLength, lastPkt: lastPkt,
            localConversation: localConversation]];
      RecordEvent => RETURN[
        RecordEventStub[pkt: pkt, callLength: callLength, lastPkt:
            lastPkt, localConversation: localConversation]];
      EventRope => RETURN[
        EventRopeStub[pkt: pkt, callLength: callLength, lastPkt: lastPkt,
            localConversation: localConversation]];
      Login => RETURN[
        LoginStub[pkt: pkt, callLength: callLength, lastPkt: lastPkt,
            localConversation: localConversation]];
      ENDCASE => RETURN[Lupine.DispatchingError[]];

    END;  -- ServerDispatcher


-- Public procedure dispatcher stubs.

  RegisterStub: --PROCEDURE [shh: SHHH, oldSmartsID: SmartsID, oldEpoch:
      -- Epoch, machine: Machine, model: Lark.LarkModel, authenticated:
      -- BOOL, clientInstance: ROPE]
    -- RETURNS [smartsID: SmartsID, epoch: Epoch]-- RpcPrivate.Dispatcher
        =
    INLINE BEGIN
    clientInstance: ROPE;
    ArgumentOverlay: TYPE = MACHINE DEPENDENT RECORD [
        transferIndex (0): RpcControl.ProcedureIndex, oldSmartsID (1):
        SmartsID, oldEpoch (3): Epoch, machine (5): Machine, model (6):
        Lark.LarkModel, authenticated (8): BOOL];
    ResultOverlay: TYPE = MACHINE DEPENDENT RECORD [
        smartsID (0): SmartsID, epoch (2): Epoch];
    argPkt: LONG POINTER TO ArgumentOverlay = @pkt.data[0];
    resPkt: LONG POINTER TO ResultOverlay = @pkt.data[0];
    pktLength: RpcPrivate.DataLength ← 9;
    BEGIN  -- Unmarshal clientInstance: ROPE from pkt.data[pktLength].
      [clientInstance, pktLength] ← Lupine.UnmarshalRope[pkt, pktLength,
          TRUE];
      END;  -- Unmarshal clientInstance.
    Lupine.CheckPktLength[pkt: pkt, pktLength: pktLength];
    [resPkt.smartsID, resPkt.epoch] ←
      Register[localConversation, argPkt.oldSmartsID, argPkt.oldEpoch,
          argPkt.machine, argPkt.model, argPkt.authenticated, clientInstance];
    pktLength ← 4;
    RETURN[returnLength: pktLength];
    END;  -- RegisterStub.

  RecordEventStub: --PROCEDURE [shh: SHHH, smartsID: SmartsID, whatHappened:
      -- Lark.StatusEvents]
    -- RETURNS [success: BOOL]-- RpcPrivate.Dispatcher =
    INLINE BEGIN
    whatHappened: Lark.StatusEvents;
    ArgumentOverlay: TYPE = MACHINE DEPENDENT RECORD [
        transferIndex (0): RpcControl.ProcedureIndex, smartsID (1): SmartsID];
    ResultOverlay: TYPE = MACHINE DEPENDENT RECORD [
        success (0): BOOL];
    argPkt: LONG POINTER TO ArgumentOverlay = @pkt.data[0];
    resPkt: LONG POINTER TO ResultOverlay = @pkt.data[0];
    pktLength: RpcPrivate.DataLength ← 3;
    BEGIN  -- Unmarshal whatHappened: Lark.StatusEvents from pkt.data[pktLength].
      [whatHappened, pktLength] ← UnmarshalLarkDotStatusEvents[pkt,
          pktLength];
      END;  -- Unmarshal whatHappened.
    Lupine.CheckPktLength[pkt: pkt, pktLength: pktLength];
    [resPkt.success] ← RecordEvent[localConversation, argPkt.smartsID,
        whatHappened];
    pktLength ← 1;
    RETURN[returnLength: pktLength];
    END;  -- RecordEventStub.

  EventRopeStub: --PROCEDURE [shh: SHHH, smartsID: SmartsID, time:
      -- CARDINAL, device: Lark.Device, events: ROPE]
    -- RETURNS [success: BOOL]-- RpcPrivate.Dispatcher =
    INLINE BEGIN
    events: ROPE;
    ArgumentOverlay: TYPE = MACHINE DEPENDENT RECORD [
        transferIndex (0): RpcControl.ProcedureIndex, smartsID (1): SmartsID,
        time (3): CARDINAL, device (4): Lark.Device];
    ResultOverlay: TYPE = MACHINE DEPENDENT RECORD [
        success (0): BOOL];
    argPkt: LONG POINTER TO ArgumentOverlay = @pkt.data[0];
    resPkt: LONG POINTER TO ResultOverlay = @pkt.data[0];
    pktLength: RpcPrivate.DataLength ← 5;
    BEGIN  -- Unmarshal events: ROPE from pkt.data[pktLength].
      [events, pktLength] ← Lupine.UnmarshalRope[pkt, pktLength, TRUE];
      END;  -- Unmarshal events.
    Lupine.CheckPktLength[pkt: pkt, pktLength: pktLength];
    [resPkt.success] ← EventRope[localConversation, argPkt.smartsID,
        argPkt.time, argPkt.device, events];
    pktLength ← 1;
    RETURN[returnLength: pktLength];
    END;  -- EventRopeStub.

  LoginStub: --PROCEDURE [shh: SHHH, smartsID: SmartsID, authenticated:
      -- BOOL]-- RpcPrivate.Dispatcher =
    INLINE BEGIN
    ArgumentOverlay: TYPE = MACHINE DEPENDENT RECORD [
        transferIndex (0): RpcControl.ProcedureIndex, smartsID (1): SmartsID,
        authenticated (3): BOOL];
    argPkt: LONG POINTER TO ArgumentOverlay = @pkt.data[0];
    pktLength: RpcPrivate.DataLength ← 4;
    Lupine.CheckPktLength[pkt: pkt, pktLength: pktLength];
    Login[localConversation, argPkt.smartsID, argPkt.authenticated];
    pktLength ← 0;
    RETURN[returnLength: pktLength];
    END;  -- LoginStub.


-- Marshall/Unmarshal procedures.
UnmarshalLarkDotStatusEvents: PROC[pkt: RpcPrivate.RPCPkt, pktLength0:
    RpcPrivate.DataLength]
  RETURNS[value: Lark.StatusEvents, pktLength: RpcPrivate.DataLength]
      = BEGIN
  pktLength ← pktLength0; {
  BEGIN  -- Unmarshal value↑: Lark.StatusEventSequence 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[Lark.StatusEventSequence[Lupine.SHORT[seqLength]]]);
        pktLength ← Lupine.CopyFromPkt[pkt: pkt, pktLength: pktLength,
            dataAdr: LOOPHOLE[value], dataLength: SIZE[Lark.StatusEventSequence[LENGTH[DESCRIPTOR[value↑]]]],
            alwaysOnePkt: TRUE];
        END;  -- IF recordIsNIL.
    END;  -- Unmarshal value↑.
  };END;



-- No module initialization.

END.  -- LarkSmartsRpcServerImpl.