-- Copyright (C) 1985 by Xerox Corporation.  All rights reserved.
-- Stub file  was translated on December 10, 1985 9:27:26 am PST by Lupine of May 23, 1985 8:38:08 am PDT
-- Source interface ThSmarts came from file ThSmarts.bcd, which was created on December 10, 1985 9:27:19 am PST with version stamp 17#354#4357056060 from source of November 25, 1985 12:45:05 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 = 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
  Thrush,
  ThSmarts,
  ThSmartsRpcControl USING [InterMdsCallsOnly, LupineProtocolVersion,
      ProcedureIndex, SignalIndex],
  RPC USING [EncryptionKey, InterfaceName, Principal, standardZones,
      Zones],
  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]--;


ThSmartsRpcServerImpl: MONITOR
  IMPORTS ThSmarts, RpcPrivate: RPCLupine, Lupine: LupineRuntime, 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~17#354#4357056060",
        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: Thrush.SHHH, convEvent: Thrush.ConvEvent]--
      RpcPrivate.Dispatcher =
    INLINE BEGIN
    convEvent: Thrush.ConvEvent;
    pktLength: RpcPrivate.DataLength ← 1;
    BEGIN  -- Unmarshal convEvent: Thrush.ConvEvent from pkt.data[pktLength].
      [convEvent, pktLength] ← UnmarshalThrushDotConvEvent[pkt, pktLength];
      END;  -- Unmarshal convEvent.
    Lupine.CheckPktLength[pkt: pkt, pktLength: pktLength];
    Progress[localConversation, convEvent];
    pktLength ← 0;
    RETURN[returnLength: pktLength];
    END;  -- ProgressStub.


-- Marshall/Unmarshal procedures.
UnmarshalThrushDotConvEvent: PROC[pkt: RpcPrivate.RPCPkt, pktLength0:
    RpcPrivate.DataLength]
  RETURNS[value: Thrush.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.reason, LONG POINTER] ← NIL;
        LOOPHOLE[record.comment, LONG POINTER] ← NIL;
        END;  -- OPEN record: value↑.
        BEGIN OPEN record: value↑;
        BEGIN  -- Unmarshal record.reason: Thrush.Reason from pkt.data[pktLength].
          [record.reason, pktLength] ← Lupine.UnmarshalAtom[pkt, pktLength];
          END;  -- Unmarshal record.reason.
        BEGIN  -- Unmarshal record.comment: Rope.ROPE from pkt.data[pktLength].
          [record.comment, pktLength] ← Lupine.UnmarshalRope[pkt, pktLength,
              FALSE];
          END;  -- Unmarshal record.comment.
        END;  -- OPEN record: value↑.
        END;  -- Unmarshal value↑.
      END;  -- IF isNIL.
  };END;



-- No module initialization.

END.  -- ThSmartsRpcServerImpl.