-- Stub file  was translated on July 12, 1984 11:26:59 am PDT by Lupine of July 11, 1984 11:51:36 pm PDT
-- Source interface LarkSmarts came from file LarkSmarts.bcd, which was created on March 22, 1984 5:24:06 pm PST with version stamp 307#212#1634254640 from source of December 19, 1983 1:37:07 pm PST.

-- 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 = 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
  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, NilHeader, ProtocolError, RopeHeader, RpcPktDoubleWord,
      -- RuntimeError, SequenceHeader, SHORT, StartNextPkt, StringHeader,
      -- StubPktDoubleWord, TranslationError, UnmarshalingError, UnmarshalingExprError,
      -- WordsForChars]--,
  LupineRuntimeExtras --USING SOME OF [MarshalRope, MarshalAtom, UnmarshalRope,
      -- UnmarshalAtom]--,
  Atom --USING SOME OF [GetPName, MakeAtom]--,
  Rope --USING SOME OF [InlineFlatten, Length, NewText, Text]--;


LarkSmartsRpcServerImpl: MONITOR
  IMPORTS LarkSmarts, RpcPrivate: RPCLupine, Lupine: LupineRuntime,
      LupineRuntimeExtras: LupineRuntimeExtras, 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~307#212#1634254640",
        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: SmartsHandle,
      -- oldEpoch: Epoch, netAddress: NetAddress, model: Lark.LarkModel,
      -- authenticated: BOOL, clientInstance: ROPE]
    -- RETURNS [smartsID: SmartsHandle, epoch: Epoch]-- RpcPrivate.Dispatcher
        =
    BEGIN
    clientInstance: ROPE;
    ArgumentOverlay: TYPE = MACHINE DEPENDENT RECORD [
        transferIndex (0): RpcControl.ProcedureIndex, oldSmartsID (1):
        SmartsHandle, oldEpoch (3): Epoch, netAddress (5): NetAddress,
        model (6): Lark.LarkModel, authenticated (8): BOOL];
    ResultOverlay: TYPE = MACHINE DEPENDENT RECORD [
        smartsID (0): SmartsHandle, 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] ← LupineRuntimeExtras.UnmarshalRope[pkt,
          pktLength];
      END;  -- Unmarshal clientInstance.
    Lupine.CheckPktLength[pkt: pkt, pktLength: pktLength];
    [resPkt.smartsID, resPkt.epoch] ←
      Register[localConversation, argPkt.oldSmartsID, argPkt.oldEpoch,
          argPkt.netAddress, argPkt.model, argPkt.authenticated, clientInstance];
    pktLength ← 4;
    RETURN[returnLength: pktLength];
    END;  -- RegisterStub.

  RecordEventStub: --PROCEDURE [shh: SHHH, smartsID: SmartsHandle,
      -- whatHappened: Lark.StatusEvents]
    -- RETURNS [success: BOOL]-- RpcPrivate.Dispatcher =
    BEGIN
    whatHappened: Lark.StatusEvents;
    ArgumentOverlay: TYPE = MACHINE DEPENDENT RECORD [
        transferIndex (0): RpcControl.ProcedureIndex, smartsID (1): SmartsHandle];
    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: SmartsHandle, time:
      -- CARDINAL, device: Lark.Device, events: ROPE]
    -- RETURNS [success: BOOL]-- RpcPrivate.Dispatcher =
    BEGIN
    events: ROPE;
    ArgumentOverlay: TYPE = MACHINE DEPENDENT RECORD [
        transferIndex (0): RpcControl.ProcedureIndex, smartsID (1): SmartsHandle,
        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] ← LupineRuntimeExtras.UnmarshalRope[pkt,
          pktLength];
      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: SmartsHandle, authenticated:
      -- BOOL]-- RpcPrivate.Dispatcher =
    BEGIN
    ArgumentOverlay: TYPE = MACHINE DEPENDENT RECORD [
        transferIndex (0): RpcControl.ProcedureIndex, smartsID (1): SmartsHandle,
        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, pktLength:
    RpcPrivate.DataLength]
  RETURNS[value: Lark.StatusEvents, newLength: RpcPrivate.DataLength]
      = BEGIN
  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.