-- Copyright (C) 1987 by Xerox Corporation.  All rights reserved.
-- Stub file  was translated on April 4, 1987 3:22:23 pm PST by Lupine of January 22, 1987 5:37:32 pm PST
-- Source interface ThSmarts came from file ThSmarts.bcd, which was created on April 4, 1987 3:22:17 pm PST with version stamp 206#302#402567077 from source of May 30, 1986 11:40:35 am PDT.

-- 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~206#302#402567077",
        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]];
      ReportAction => RETURN[
        ReportActionStub[pkt: pkt, callLength: callLength, lastPkt:
            lastPkt, localConversation: localConversation]];
      Substitution => RETURN[
        SubstitutionStub[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.

  ReportActionStub: --PROCEDURE [shh: Thrush.SHHH, report: Thrush.ActionReport]--
      RpcPrivate.Dispatcher =
    INLINE BEGIN
    report: Thrush.ActionReport;
    pktLength: RpcPrivate.DataLength ← 1;
    BEGIN  -- Unmarshal report: Thrush.ActionReport from pkt.data[pktLength].
      pktLength ← Lupine.CopyFromPkt[pkt: pkt, pktLength: pktLength,
          dataAdr: @report, dataLength: SIZE[Thrush.ActionReport], alwaysOnePkt:
          FALSE];
      -- Restore garbled REFs to NIL following copy.
      BEGIN OPEN record: report;
      LOOPHOLE[record.actionClass, LONG POINTER] ← NIL;
      LOOPHOLE[record.actionType, LONG POINTER] ← NIL;
      LOOPHOLE[record.actionInfo, LONG POINTER] ← NIL;
      END;  -- OPEN record: report.
      BEGIN OPEN record: report;
      BEGIN  -- Unmarshal record.actionClass: ActionClass from pkt.data[pktLength].
        [record.actionClass, pktLength] ← Lupine.UnmarshalAtom[pkt,
            pktLength];
        END;  -- Unmarshal record.actionClass.
      BEGIN  -- Unmarshal record.actionType: ActionType from pkt.data[pktLength].
        [record.actionType, pktLength] ← Lupine.UnmarshalAtom[pkt,
            pktLength];
        END;  -- Unmarshal record.actionType.
      BEGIN  -- Unmarshal record.actionInfo: ROPE from pkt.data[pktLength].
        [record.actionInfo, pktLength] ← Lupine.UnmarshalRope[pkt,
            pktLength, FALSE];
        END;  -- Unmarshal record.actionInfo.
      END;  -- OPEN record: report.
      END;  -- Unmarshal report.
    Lupine.CheckPktLength[pkt: pkt, pktLength: pktLength];
    ReportAction[localConversation, report];
    pktLength ← 0;
    RETURN[returnLength: pktLength];
    END;  -- ReportActionStub.

  SubstitutionStub: --PROCEDURE [shh: Thrush.SHHH, convEvent: Thrush.ConvEvent,
      -- oldPartyID: Thrush.PartyID, newPartyID: Thrush.PartyID]-- RpcPrivate.Dispatcher
      =
    INLINE BEGIN
    convEvent: Thrush.ConvEvent;
    oldPartyID: Thrush.PartyID;
    newPartyID: Thrush.PartyID;
    ArgumentOverlay: TYPE = MACHINE DEPENDENT RECORD [
        transferIndex (0): RpcControl.ProcedureIndex, oldPartyID (1): Thrush.PartyID,
        newPartyID (3): Thrush.PartyID];
    argPkt: LONG POINTER TO ArgumentOverlay = @pkt.data[0];
    pktLength: RpcPrivate.DataLength ← 5;
    BEGIN  -- OnePkt.
    onePkt: BOOLEAN = lastPkt;
    IF ~onePkt THEN BEGIN  -- Must move statics from pkt now.
      [oldPartyID: oldPartyID, newPartyID: newPartyID] ← argPkt↑;
      END;
    BEGIN  -- Unmarshal convEvent: Thrush.ConvEvent from pkt.data[pktLength].
      [convEvent, pktLength] ← UnmarshalThrushDotConvEvent[pkt, pktLength];
      END;  -- Unmarshal convEvent.
    Lupine.CheckPktLength[pkt: pkt, pktLength: pktLength];
    IF onePkt
      THEN Substitution[localConversation, convEvent, argPkt.oldPartyID,
          argPkt.newPartyID]
      ELSE Substitution[localConversation, convEvent, oldPartyID, newPartyID];
    END;  -- OnePkt.
    pktLength ← 0;
    RETURN[returnLength: pktLength];
    END;  -- SubstitutionStub.


-- 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: Thrush.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.