-- Stub file  was translated on January 13, 1985 12:57:26 pm PST by Lupine of July 22, 1984 2:36:32 pm PDT
-- Source interface ThSmarts came from file ThSmarts.bcd, which was created on January 13, 1985 12:57:12 pm PST with version stamp 202#333#27131134531 from source of January 13, 1985 12:53:55 pm PST.

-- The RPC stub modules for ThSmarts are:
--   ThSmartsRpcControl.mesa;
--   ThSmartsRpcClientImpl.mesa;
--   ThSmartsRpcBinderImpl.mesa;
--   ThSmartsRpcServerImpl.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
  Lark,
  LarkPlay,
  RPC,
  Thrush,
  ThSmarts,
  ThSmartsRpcControl 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]--;


ThSmartsRpcServerImpl: MONITOR
  IMPORTS ThSmarts, RpcPrivate: RPCLupine, Lupine: LupineRuntime, LupineRuntimeExtras:
      LupineRuntimeExtras, Atom, Rope
  EXPORTS ThSmartsRpcControl
  SHARES  ThSmarts, ThSmartsRpcControl, Rope
  = BEGIN OPEN ThSmarts, RpcControl: ThSmartsRpcControl, 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 "ThSmarts~202#333#27131134531",
        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
      Progress => RETURN[
        ProgressStub[pkt: pkt, callLength: callLength, lastPkt: lastPkt,
            localConversation: localConversation]];
      ENDCASE => RETURN[Lupine.DispatchingError[]];

    END;  -- ServerDispatcher


-- Public procedure dispatcher stubs.

  ProgressStub: --PROCEDURE [shh: SHHH, smartsID: Thrush.SmartsHandle,
      -- event: ConvEvent, yourParty: BOOL, latestEvent: BOOL, informationOnly:
      -- BOOL]
    -- RETURNS [d: Disposition]-- RpcPrivate.Dispatcher =
    BEGIN
    smartsID: Thrush.SmartsHandle;
    event: ConvEvent;
    yourParty: BOOL;
    latestEvent: BOOL;
    informationOnly: BOOL;
    ArgumentOverlay: TYPE = MACHINE DEPENDENT RECORD [
        transferIndex (0): RpcControl.ProcedureIndex, smartsID (1): Thrush.SmartsHandle,
        yourParty (3): BOOL, latestEvent (4): BOOL, informationOnly (5):
        BOOL];
    ResultOverlay: TYPE = MACHINE DEPENDENT RECORD [
        d (0): Disposition];
    argPkt: LONG POINTER TO ArgumentOverlay = @pkt.data[0];
    resPkt: LONG POINTER TO ResultOverlay;
    pktLength: RpcPrivate.DataLength ← 6;
    BEGIN  -- OnePkt.
    onePkt: BOOLEAN = lastPkt;
    IF ~onePkt THEN BEGIN  -- Must move statics from pkt now.
      [smartsID: smartsID, yourParty: yourParty, latestEvent: latestEvent,
          informationOnly: informationOnly] ← argPkt↑;
      END;
    BEGIN  -- Unmarshal event: ConvEvent from pkt.data[pktLength].
      [event, pktLength] ← UnmarshalConvEvent[pkt, pktLength];
      END;  -- Unmarshal event.
    Lupine.CheckPktLength[pkt: pkt, pktLength: pktLength];
    resPkt ← @pkt.data[0];
    IF onePkt
      THEN [resPkt.d] ← Progress[localConversation, argPkt.smartsID,
          event, argPkt.yourParty, argPkt.latestEvent, argPkt.informationOnly]
      ELSE [resPkt.d] ← Progress[localConversation, smartsID, event,
          yourParty, latestEvent, informationOnly];
    END;  -- OnePkt.
    pktLength ← 1;
    RETURN[returnLength: pktLength];
    END;  -- ProgressStub.


-- Marshall/Unmarshal procedures.
UnmarshalConvEvent: PROC[pkt: RpcPrivate.RPCPkt, pktLength0: RpcPrivate.DataLength]
  RETURNS[value: ThSmarts.ConvEvent, pktLength: RpcPrivate.DataLength]
      = BEGIN
  pktLength ← pktLength0; {
  isNIL: Lupine.NilHeader;
  IF pktLength+1 > RpcPrivate.maxDataLength
    THEN pktLength ← Lupine.FinishThisPkt[pkt: pkt, pktLength: pktLength];
  isNIL ← pkt.data[pktLength];  pktLength ← pktLength+1;
  IF isNIL
    THEN value ← NIL
    ELSE BEGIN
      value ← (paramZones.gc.NEW[Thrush.ConvEventBody]);
      BEGIN  -- Unmarshal value↑: Thrush.ConvEventBody from pkt.data[pktLength].
        pktLength ← Lupine.CopyFromPkt[pkt: pkt, pktLength: pktLength,
            dataAdr: LOOPHOLE[value], dataLength: SIZE[Thrush.ConvEventBody],
            alwaysOnePkt: FALSE];
        -- Restore garbled REFs to NIL following copy.
        BEGIN OPEN record: value↑;
        LOOPHOLE[record.spec, LONG POINTER] ← NIL;
        LOOPHOLE[record.keyTable, LONG POINTER] ← NIL;
        LOOPHOLE[record.intervalSpecs, LONG POINTER] ← NIL;
        LOOPHOLE[record.ringTune, LONG POINTER] ← NIL;
        LOOPHOLE[record.address, LONG POINTER] ← NIL;
        LOOPHOLE[record.comment, LONG POINTER] ← NIL;
        END;  -- OPEN record: value↑.
        BEGIN OPEN record: value↑;
        BEGIN  -- Unmarshal record.spec: Lark.ConnectionSpec from pkt.data[pktLength].
          [record.spec, pktLength] ← UnmarshalLarkDotConnectionSpec[pkt,
              pktLength];
          END;  -- Unmarshal record.spec.
        BEGIN  -- Unmarshal record.keyTable: Lark.KeyTable from pkt.data[pktLength].
          [record.keyTable, pktLength] ← UnmarshalLarkDotKeyTable[pkt,
              pktLength];
          END;  -- Unmarshal record.keyTable.
        BEGIN  -- Unmarshal record.intervalSpecs: Thrush.IntervalSpecs
            -- from pkt.data[pktLength].
          lastNode: Thrush.IntervalSpecs ← (record.intervalSpecs ←
              NIL);
          listLength: Lupine.ListHeader;
          IF pktLength+2 > RpcPrivate.maxDataLength
            THEN pktLength ← Lupine.FinishThisPkt[pkt: pkt, pktLength:
                pktLength];
          listLength ← Lupine.RpcPktDoubleWord[pkt, pktLength]↑;
          pktLength ← pktLength + 2;
          WHILE listLength > 0 DO
            thisNode3: Thrush.IntervalSpecs = paramZones.gc.CONS[--DefaultValue--,NIL];
            BEGIN  -- Unmarshal thisNode3.first: Thrush.IntervalSpec
                -- from pkt.data[pktLength].
              [thisNode3.first, pktLength] ← UnmarshalThrushDotIntervalSpec[pkt,
                  pktLength];
              END;  -- Unmarshal thisNode3.first.
            IF lastNode # NIL
              THEN lastNode ← (lastNode.rest ← thisNode3)
              ELSE lastNode ← (record.intervalSpecs ← thisNode3);
            listLength ← listLength - 1;
            ENDLOOP;  -- WHILE listLength > 0.
          END;  -- Unmarshal record.intervalSpecs.
        BEGIN  -- Unmarshal record.ringTune: LarkPlay.ToneSpec from
            -- pkt.data[pktLength].
          [record.ringTune, pktLength] ← UnmarshalLarkPlayDotToneSpec[pkt,
              pktLength];
          END;  -- Unmarshal record.ringTune.
        BEGIN  -- Unmarshal record.address: Thrush.ROPE from pkt.data[pktLength].
          [record.address, pktLength] ← LupineRuntimeExtras.UnmarshalRope[pkt,
              pktLength];
          END;  -- Unmarshal record.address.
        BEGIN  -- Unmarshal record.comment: Thrush.ROPE from pkt.data[pktLength].
          [record.comment, pktLength] ← LupineRuntimeExtras.UnmarshalRope[pkt,
              pktLength];
          END;  -- Unmarshal record.comment.
        END;  -- OPEN record: value↑.
        END;  -- Unmarshal value↑.
      END;  -- IF isNIL.
  };END;

UnmarshalLarkPlayDotToneSpec: PROC[pkt: RpcPrivate.RPCPkt, pktLength0:
    RpcPrivate.DataLength]
  RETURNS[value: LarkPlay.ToneSpec, pktLength: RpcPrivate.DataLength]
      = BEGIN
  pktLength ← pktLength0; {
  isNIL: Lupine.NilHeader;
  IF pktLength+1 > RpcPrivate.maxDataLength
    THEN pktLength ← Lupine.FinishThisPkt[pkt: pkt, pktLength: pktLength];
  isNIL ← pkt.data[pktLength];  pktLength ← pktLength+1;
  IF isNIL
    THEN value ← NIL
    ELSE BEGIN
      value ← (paramZones.gc.NEW[LarkPlay.ToneSpecRec]);
      BEGIN  -- Unmarshal value↑: LarkPlay.ToneSpecRec from pkt.data[pktLength].
        pktLength ← Lupine.CopyFromPkt[pkt: pkt, pktLength: pktLength,
            dataAdr: LOOPHOLE[value], dataLength: SIZE[LarkPlay.ToneSpecRec],
            alwaysOnePkt: FALSE];
        -- Restore garbled REFs to NIL following copy.
        BEGIN OPEN record: value↑;
        LOOPHOLE[record.tones, LONG POINTER] ← NIL;
        END;  -- OPEN record: value↑.
        BEGIN OPEN record: value↑;
        BEGIN  -- Unmarshal record.tones: LIST OF LarkPlay.ToneList
            -- from pkt.data[pktLength].
          lastNode: LIST OF LarkPlay.ToneList ← (record.tones ← NIL);
          listLength: Lupine.ListHeader;
          IF pktLength+2 > RpcPrivate.maxDataLength
            THEN pktLength ← Lupine.FinishThisPkt[pkt: pkt, pktLength:
                pktLength];
          listLength ← Lupine.RpcPktDoubleWord[pkt, pktLength]↑;
          pktLength ← pktLength + 2;
          WHILE listLength > 0 DO
            thisNode5: LIST OF LarkPlay.ToneList = paramZones.gc.CONS[--DefaultValue--,NIL];
            BEGIN  -- Unmarshal thisNode5.first: LarkPlay.ToneList
                -- from pkt.data[pktLength].
              lastNode: LarkPlay.ToneList ← (thisNode5.first ← NIL);
              listLength: Lupine.ListHeader;
              IF pktLength+2 > RpcPrivate.maxDataLength
                THEN pktLength ← Lupine.FinishThisPkt[pkt: pkt, pktLength:
                    pktLength];
              listLength ← Lupine.RpcPktDoubleWord[pkt, pktLength]↑;
              pktLength ← pktLength + 2;
              WHILE listLength > 0 DO
                thisNode6: LarkPlay.ToneList = paramZones.gc.CONS[--DefaultValue--,NIL];
                BEGIN  -- Unmarshal thisNode6.first: LarkPlay.Tone
                    -- from pkt.data[pktLength].
                  pktLength ← Lupine.CopyFromPkt[pkt: pkt, pktLength:
                      pktLength, dataAdr: @thisNode6.first, dataLength:
                      SIZE[LarkPlay.Tone], alwaysOnePkt: FALSE];
                  END;  -- Unmarshal thisNode6.first.
                IF lastNode # NIL
                  THEN lastNode ← (lastNode.rest ← thisNode6)
                  ELSE lastNode ← (thisNode5.first ← thisNode6);
                listLength ← listLength - 1;
                ENDLOOP;  -- WHILE listLength > 0.
              END;  -- Unmarshal thisNode5.first.
            IF lastNode # NIL
              THEN lastNode ← (lastNode.rest ← thisNode5)
              ELSE lastNode ← (record.tones ← thisNode5);
            listLength ← listLength - 1;
            ENDLOOP;  -- WHILE listLength > 0.
          END;  -- Unmarshal record.tones.
        END;  -- OPEN record: value↑.
        END;  -- Unmarshal value↑.
      END;  -- IF isNIL.
  };END;

UnmarshalThrushDotIntervalSpec: PROC[pkt: RpcPrivate.RPCPkt, pktLength0:
    RpcPrivate.DataLength]
  RETURNS[value: Thrush.IntervalSpec, pktLength: RpcPrivate.DataLength]
      = BEGIN
  pktLength ← pktLength0; {
  isNIL: Lupine.NilHeader;
  IF pktLength+1 > RpcPrivate.maxDataLength
    THEN pktLength ← Lupine.FinishThisPkt[pkt: pkt, pktLength: pktLength];
  isNIL ← pkt.data[pktLength];  pktLength ← pktLength+1;
  IF isNIL
    THEN value ← NIL
    ELSE BEGIN
      value ← (paramZones.gc.NEW[Thrush.IntervalSpecBody]);
      BEGIN  -- Unmarshal value↑: Thrush.IntervalSpecBody from pkt.data[pktLength].
        pktLength ← Lupine.CopyFromPkt[pkt: pkt, pktLength: pktLength,
            dataAdr: LOOPHOLE[value], dataLength: SIZE[Thrush.IntervalSpecBody],
            alwaysOnePkt: FALSE];
        END;  -- Unmarshal value↑.
      END;  -- IF isNIL.
  };END;

UnmarshalLarkDotKeyTable: PROC[pkt: RpcPrivate.RPCPkt, pktLength0:
    RpcPrivate.DataLength]
  RETURNS[value: Lark.KeyTable, pktLength: RpcPrivate.DataLength] =
      BEGIN
  pktLength ← pktLength0; {
  BEGIN  -- Unmarshal value↑: Lark.KeyTableBody from pkt.data[pktLength].
    recordIsNIL: Lupine.NilHeader;
    IF pktLength+3 > RpcPrivate.maxDataLength
      THEN pktLength ← Lupine.FinishThisPkt[pkt: pkt, pktLength: pktLength];
    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.KeyTableBody[Lupine.SHORT[seqLength]]]);
        pktLength ← Lupine.CopyFromPkt[pkt: pkt, pktLength: pktLength,
            dataAdr: LOOPHOLE[value], dataLength: SIZE[Lark.KeyTableBody[LENGTH[DESCRIPTOR[value↑]]]],
            alwaysOnePkt: FALSE];
        END;  -- IF recordIsNIL.
    END;  -- Unmarshal value↑.
  };END;

UnmarshalLarkDotConnectionSpec: PROC[pkt: RpcPrivate.RPCPkt, pktLength0:
    RpcPrivate.DataLength]
  RETURNS[value: Lark.ConnectionSpec, pktLength: RpcPrivate.DataLength]
      = BEGIN
  pktLength ← pktLength0; {
  isNIL: Lupine.NilHeader;
  IF pktLength+1 > RpcPrivate.maxDataLength
    THEN pktLength ← Lupine.FinishThisPkt[pkt: pkt, pktLength: pktLength];
  isNIL ← pkt.data[pktLength];  pktLength ← pktLength+1;
  IF isNIL
    THEN value ← NIL
    ELSE BEGIN
      value ← (paramZones.gc.NEW[Lark.ConnectionSpecRec]);
      BEGIN  -- Unmarshal value↑: Lark.ConnectionSpecRec from pkt.data[pktLength].
        pktLength ← Lupine.CopyFromPkt[pkt: pkt, pktLength: pktLength,
            dataAdr: LOOPHOLE[value], dataLength: SIZE[Lark.ConnectionSpecRec],
            alwaysOnePkt: FALSE];
        END;  -- Unmarshal value↑.
      END;  -- IF isNIL.
  };END;



-- No module initialization.

END.  -- ThSmartsRpcServerImpl.