-- Stub file  was translated on June 19, 1984 12:42:55 pm PDT by Lupine of June 15, 1984 1:13:45 pm PDT
-- Source interface ThSmarts came from file ThSmarts.bcd, which was created on June 19, 1984 12:42:17 pm PDT with version stamp 355#46#14036406517 from source of December 19, 1983 2:07:24 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,
  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~355#46#14036406517",
        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
    event: ConvEvent;
    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 = @pkt.data[0];
    pktLength: RpcPrivate.DataLength ← 6;
    BEGIN  -- Unmarshal event: ConvEvent from pkt.data[pktLength].
      isNIL: Lupine.NilHeader;
      isNIL ← pkt.data[pktLength];  pktLength ← pktLength+1;
      IF isNIL
        THEN event ← NIL
        ELSE BEGIN
          event ← (paramZones.gc.NEW[Thrush.ConvEventBody]);
          BEGIN  -- Unmarshal event↑: Thrush.ConvEventBody from pkt.data[pktLength].
            pktLength ← Lupine.CopyFromPkt[pkt: pkt, pktLength: pktLength,
                dataAdr: LOOPHOLE[event], dataLength: SIZE[Thrush.ConvEventBody],
                alwaysOnePkt: TRUE];
            -- Restore garbled REFs to NIL following copy.
            BEGIN OPEN record: event↑;
            LOOPHOLE[record.spec, LONG POINTER] ← NIL;
            LOOPHOLE[record.keyTable, LONG POINTER] ← NIL;
            LOOPHOLE[record.intervalSpec, LONG POINTER] ← NIL;
            LOOPHOLE[record.address, LONG POINTER] ← NIL;
            LOOPHOLE[record.comment, LONG POINTER] ← NIL;
            END;  -- OPEN record: event↑.
            BEGIN OPEN record: event↑;
            BEGIN  -- Unmarshal record.spec: Lark.ConnectionSpec from
                -- pkt.data[pktLength].
              isNIL: Lupine.NilHeader;
              isNIL ← pkt.data[pktLength];  pktLength ← pktLength+1;
              IF isNIL
                THEN record.spec ← NIL
                ELSE BEGIN
                  record.spec ← (paramZones.gc.NEW[Lark.ConnectionSpecRec]);
                  BEGIN  -- Unmarshal record.spec↑: Lark.ConnectionSpecRec
                      -- from pkt.data[pktLength].
                    pktLength ← Lupine.CopyFromPkt[pkt: pkt, pktLength:
                        pktLength, dataAdr: LOOPHOLE[record.spec], dataLength:
                        SIZE[Lark.ConnectionSpecRec], alwaysOnePkt: TRUE];
                    END;  -- Unmarshal record.spec↑.
                  END;  -- IF isNIL.
              END;  -- Unmarshal record.spec.
            BEGIN  -- Unmarshal record.keyTable: Lark.KeyTable from
                -- pkt.data[pktLength].
              BEGIN  -- Unmarshal record.keyTable↑: Lark.KeyTableBody
                  -- from pkt.data[pktLength].
                recordIsNIL: Lupine.NilHeader;
                recordIsNIL ← pkt.data[pktLength];  pktLength ← pktLength+1;
                IF recordIsNIL
                  THEN record.keyTable ← NIL
                  ELSE BEGIN
                    seqLength: Lupine.SequenceHeader;
                    seqLength ← Lupine.RpcPktDoubleWord[pkt, pktLength]↑;
                    pktLength ← pktLength + 2;
                    record.keyTable ← (paramZones.gc.NEW[Lark.KeyTableBody[Lupine.SHORT[seqLength]]]);
                    pktLength ← Lupine.CopyFromPkt[pkt: pkt, pktLength:
                        pktLength, dataAdr: LOOPHOLE[record.keyTable],
                        dataLength: SIZE[Lark.KeyTableBody[LENGTH[DESCRIPTOR[record.keyTable↑]]]],
                        alwaysOnePkt: TRUE];
                    END;  -- IF recordIsNIL.
                END;  -- Unmarshal record.keyTable↑.
              END;  -- Unmarshal record.keyTable.
            BEGIN  -- Unmarshal record.intervalSpec: IntervalSpec from
                -- pkt.data[pktLength].
              isNIL: Lupine.NilHeader;
              isNIL ← pkt.data[pktLength];  pktLength ← pktLength+1;
              IF isNIL
                THEN record.intervalSpec ← NIL
                ELSE BEGIN
                  record.intervalSpec ← (paramZones.gc.NEW[IntervalSpecBody]);
                  BEGIN  -- Unmarshal record.intervalSpec↑: IntervalSpecBody
                      -- from pkt.data[pktLength].
                    pktLength ← Lupine.CopyFromPkt[pkt: pkt, pktLength:
                        pktLength, dataAdr: LOOPHOLE[record.intervalSpec],
                        dataLength: SIZE[IntervalSpecBody], alwaysOnePkt:
                        TRUE];
                    END;  -- Unmarshal record.intervalSpec↑.
                  END;  -- IF isNIL.
              END;  -- Unmarshal record.intervalSpec.
            BEGIN  -- Unmarshal record.address: ROPE from pkt.data[pktLength].
              [record.address, pktLength] ← LupineRuntimeExtras.UnmarshalRope[pkt,
                  pktLength];
              END;  -- Unmarshal record.address.
            BEGIN  -- Unmarshal record.comment: ROPE from pkt.data[pktLength].
              [record.comment, pktLength] ← LupineRuntimeExtras.UnmarshalRope[pkt,
                  pktLength];
              END;  -- Unmarshal record.comment.
            END;  -- OPEN record: event↑.
            END;  -- Unmarshal event↑.
          END;  -- IF isNIL.
      END;  -- Unmarshal event.
    Lupine.CheckPktLength[pkt: pkt, pktLength: pktLength];
    [resPkt.d] ← Progress[localConversation, argPkt.smartsID, event,
        argPkt.yourParty, argPkt.latestEvent, argPkt.informationOnly];
    pktLength ← 1;
    RETURN[returnLength: pktLength];
    END;  -- ProgressStub.


-- No module initialization.

END.  -- ThSmartsRpcServerImpl.