-- Copyright (C) 1984  by Xerox Corporation. All rights reserved. 
-- Stub file  was translated on July 19, 1984 1:58:15 am PDT by Lupine of December 20, 1983 1:22:57 pm PST
-- Source interface Multibus came from file Multibus.bcd, which was created on July 19, 1984 1:56:38 am PDT with version stamp 14#17#25044235147 from source of July 17, 1984 3:41:14 pm PDT.

-- The RPC stub modules for Multibus are:
--   MultibusRpcControl.mesa;
--   MultibusRpcClientImpl.mesa;
--   MultibusRpcBinderImpl.mesa;
--   MultibusRpcServerImpl.mesa.

-- The parameters for this translation are:
--   Target language = Mesa
--   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
  Multibus,
  MultibusRpcControl USING [InterMdsCallsOnly, LupineProtocolVersion,
      ProcedureIndex, SignalIndex],
  MesaRPC USING [InterfaceName, standardZones, Zones],
  MesaRPCLupine --USING SOME OF [Call, DataLength, Dispatcher, GetStubPkt,
      -- ImportHandle, ImportInterface, maxDataLength, maxPrincipalLength,
      -- maxShortStringLength, pktOverhead, ReceiveExtraPkt, SendPrelimPkt,
      -- StartCall, StartSignal, StubPkt, UnimportInterface]--,
  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]--;


MultibusRpcClientImpl: MONITOR
  IMPORTS RpcPrivate: MesaRPCLupine, Lupine: LupineRuntime
  EXPORTS Multibus, MultibusRpcControl
  SHARES  Multibus, MultibusRpcControl
  = BEGIN OPEN Multibus, RpcControl: MultibusRpcControl, RpcPublic:
      MesaRPC;


-- Standard remote binding routines.

  bound: BOOLEAN ← FALSE;
  myInterface: RpcPrivate.ImportHandle;
  paramZones: RpcPublic.Zones ← RpcPublic.standardZones;

  ImportInterface: PUBLIC ENTRY PROCEDURE [
        interfaceName: RpcPublic.InterfaceName,
        parameterStorage: RpcPublic.Zones ] =
    BEGIN ENABLE UNWIND => NULL;
    IsNull: PROCEDURE [string: LONG STRING] RETURNS [BOOLEAN] =
      INLINE {RETURN[string=NIL OR string.length=0]};
    IF bound THEN Lupine.BindingError;
    myInterface ← RpcPrivate.ImportInterface [
      interface: [
        type: IF ~IsNull[interfaceName.type]
          THEN interfaceName.type ELSE "Multibus~14#17#25044235147",
        instance: interfaceName.instance,
        version: interfaceName.version ],
      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;

  UnimportInterface: PUBLIC ENTRY PROCEDURE =
    BEGIN ENABLE UNWIND => NULL;
    IF ~bound THEN Lupine.BindingError;
    myInterface ← RpcPrivate.UnimportInterface[myInterface];
    paramZones ← RpcPublic.standardZones;
    bound ← FALSE;
    END;



-- Remote public procedure stubs.

  Input: PUBLIC PROCEDURE [address: HANDLEIOAddress] RETURNS [--word:--
      UNSPECIFIED] =
    BEGIN
    ArgumentOverlay: TYPE = MACHINE DEPENDENT RECORD [
        transferIndex (0): RpcControl.ProcedureIndex ← Input, address (1):
        HANDLEIOAddress];
    ResultOverlay: TYPE = MACHINE DEPENDENT RECORD [
        word (0): UNSPECIFIED];
    pktBuffer: ARRAY [1..RpcPrivate.pktOverhead+3] OF WORD;
    pkt: RpcPrivate.StubPkt = RpcPrivate.GetStubPkt[space: @pktBuffer];
    argPkt: POINTER TO ArgumentOverlay = @pkt.data[0];
    resPkt: POINTER TO ResultOverlay = @pkt.data[0];
    pktLength: RpcPrivate.DataLength ← 3;
    lastPkt: BOOLEAN;
    RpcPrivate.StartCall[callPkt: pkt, interface: myInterface];
    argPkt↑ ← [address: address];
    [returnLength: , lastPkt: lastPkt] ←
      RpcPrivate.Call[ pkt: pkt, callLength: pktLength,
          maxReturnLength: 1, signalHandler: ClientDispatcher];
    Lupine.CheckPktLength[pkt: pkt, pktLength: 1];
    RETURN[resPkt.word];
    END;  -- Input.

  Output: PUBLIC PROCEDURE [word: UNSPECIFIED, address: HANDLEIOAddress]
      =
    BEGIN
    ArgumentOverlay: TYPE = MACHINE DEPENDENT RECORD [
        transferIndex (0): RpcControl.ProcedureIndex ← Output, word (1):
        UNSPECIFIED, address (2): HANDLEIOAddress];
    pktBuffer: ARRAY [1..RpcPrivate.pktOverhead+4] OF WORD;
    pkt: RpcPrivate.StubPkt = RpcPrivate.GetStubPkt[space: @pktBuffer];
    argPkt: POINTER TO ArgumentOverlay = @pkt.data[0];
    pktLength: RpcPrivate.DataLength ← 4;
    lastPkt: BOOLEAN;
    RpcPrivate.StartCall[callPkt: pkt, interface: myInterface];
    argPkt↑ ← [word: word, address: address];
    [returnLength: , lastPkt: lastPkt] ←
      RpcPrivate.Call[ pkt: pkt, callLength: pktLength,
          maxReturnLength: 0, signalHandler: ClientDispatcher];
    Lupine.CheckPktLength[pkt: pkt, pktLength: 0];
    RETURN[];
    END;  -- Output.

  RawRead: PUBLIC PROCEDURE [address: HANDLERawAddress] RETURNS [--word:--
      UNSPECIFIED] =
    BEGIN
    ArgumentOverlay: TYPE = MACHINE DEPENDENT RECORD [
        transferIndex (0): RpcControl.ProcedureIndex ← RawRead, address
        (1): HANDLERawAddress];
    ResultOverlay: TYPE = MACHINE DEPENDENT RECORD [
        word (0): UNSPECIFIED];
    pktBuffer: ARRAY [1..RpcPrivate.pktOverhead+3] OF WORD;
    pkt: RpcPrivate.StubPkt = RpcPrivate.GetStubPkt[space: @pktBuffer];
    argPkt: POINTER TO ArgumentOverlay = @pkt.data[0];
    resPkt: POINTER TO ResultOverlay = @pkt.data[0];
    pktLength: RpcPrivate.DataLength ← 3;
    lastPkt: BOOLEAN;
    RpcPrivate.StartCall[callPkt: pkt, interface: myInterface];
    argPkt↑ ← [address: address];
    [returnLength: , lastPkt: lastPkt] ←
      RpcPrivate.Call[ pkt: pkt, callLength: pktLength,
          maxReturnLength: 1, signalHandler: ClientDispatcher];
    Lupine.CheckPktLength[pkt: pkt, pktLength: 1];
    RETURN[resPkt.word];
    END;  -- RawRead.

  RawWrite: PUBLIC PROCEDURE [word: UNSPECIFIED, address: HANDLERawAddress]
      =
    BEGIN
    ArgumentOverlay: TYPE = MACHINE DEPENDENT RECORD [
        transferIndex (0): RpcControl.ProcedureIndex ← RawWrite, word (1):
        UNSPECIFIED, address (2): HANDLERawAddress];
    pktBuffer: ARRAY [1..RpcPrivate.pktOverhead+4] OF WORD;
    pkt: RpcPrivate.StubPkt = RpcPrivate.GetStubPkt[space: @pktBuffer];
    argPkt: POINTER TO ArgumentOverlay = @pkt.data[0];
    pktLength: RpcPrivate.DataLength ← 4;
    lastPkt: BOOLEAN;
    RpcPrivate.StartCall[callPkt: pkt, interface: myInterface];
    argPkt↑ ← [word: word, address: address];
    [returnLength: , lastPkt: lastPkt] ←
      RpcPrivate.Call[ pkt: pkt, callLength: pktLength,
          maxReturnLength: 0, signalHandler: ClientDispatcher];
    Lupine.CheckPktLength[pkt: pkt, pktLength: 0];
    RETURN[];
    END;  -- RawWrite.

  RawReadPage: PUBLIC PROCEDURE [address: HANDLERawAddress] RETURNS
      [page: Page] =
    BEGIN
    ArgumentOverlay: TYPE = MACHINE DEPENDENT RECORD [
        transferIndex (0): RpcControl.ProcedureIndex ← RawReadPage, address
        (1): HANDLERawAddress];
    pktBuffer: ARRAY [1..RpcPrivate.pktOverhead+254] OF WORD;
    pkt: RpcPrivate.StubPkt = RpcPrivate.GetStubPkt[space: @pktBuffer];
    argPkt: POINTER TO ArgumentOverlay = @pkt.data[0];
    pktLength: RpcPrivate.DataLength ← 3;
    lastPkt: BOOLEAN;
    RpcPrivate.StartCall[callPkt: pkt, interface: myInterface];
    argPkt↑ ← [address: address];
    [returnLength: , lastPkt: lastPkt] ←
      RpcPrivate.Call[ pkt: pkt, callLength: pktLength,
          maxReturnLength: 254, signalHandler: ClientDispatcher];
    pktLength ← 0;
    BEGIN  -- Unmarshal page: Page from pkt.data[pktLength].
      pktLength ← Lupine.CopyFromPkt[pkt: pkt, pktLength: pktLength,
          dataAdr: @page, dataLength: SIZE[Page], alwaysOnePkt: FALSE];
      END;  -- Unmarshal page.
    Lupine.CheckPktLength[pkt: pkt, pktLength: pktLength];
    RETURN[page];
    END;  -- RawReadPage.

  RawWritePage: PUBLIC PROCEDURE [page: Page, address: HANDLERawAddress]
      =
    BEGIN
    ArgumentOverlay: TYPE = MACHINE DEPENDENT RECORD [
        transferIndex (0): RpcControl.ProcedureIndex ← RawWritePage, address
        (1): HANDLERawAddress];
    pktBuffer: ARRAY [1..RpcPrivate.pktOverhead+254] OF WORD;
    pkt: RpcPrivate.StubPkt = RpcPrivate.GetStubPkt[space: @pktBuffer];
    argPkt: POINTER TO ArgumentOverlay = @pkt.data[0];
    pktLength: RpcPrivate.DataLength ← 3;
    lastPkt: BOOLEAN;
    RpcPrivate.StartCall[callPkt: pkt, interface: myInterface];
    argPkt↑ ← [address: address];
    BEGIN  -- Marshal page: Page to pkt.data[pktLength].
      pktLength ← Lupine.CopyToPkt[pkt: pkt, pktLength: pktLength,
          dataAdr: @page, dataLength: SIZE[Page], alwaysOnePkt: FALSE];
      END;  -- Marshal page.
    [returnLength: , lastPkt: lastPkt] ←
      RpcPrivate.Call[ pkt: pkt, callLength: pktLength,
          maxReturnLength: 0, signalHandler: ClientDispatcher];
    Lupine.CheckPktLength[pkt: pkt, pktLength: 0];
    RETURN[];
    END;  -- RawWritePage.

  RawReadBlock: PUBLIC PROCEDURE [address: HANDLERawAddress] RETURNS
      [block: Block] =
    BEGIN
    ArgumentOverlay: TYPE = MACHINE DEPENDENT RECORD [
        transferIndex (0): RpcControl.ProcedureIndex ← RawReadBlock, address
        (1): HANDLERawAddress];
    pktBuffer: ARRAY [1..RpcPrivate.pktOverhead+254] OF WORD;
    pkt: RpcPrivate.StubPkt = RpcPrivate.GetStubPkt[space: @pktBuffer];
    argPkt: POINTER TO ArgumentOverlay = @pkt.data[0];
    pktLength: RpcPrivate.DataLength ← 3;
    lastPkt: BOOLEAN;
    RpcPrivate.StartCall[callPkt: pkt, interface: myInterface];
    argPkt↑ ← [address: address];
    [returnLength: , lastPkt: lastPkt] ←
      RpcPrivate.Call[ pkt: pkt, callLength: pktLength,
          maxReturnLength: 254, signalHandler: ClientDispatcher];
    pktLength ← 0;
    BEGIN  -- Unmarshal block: Block from pkt.data[pktLength].
      pktLength ← Lupine.CopyFromPkt[pkt: pkt, pktLength: pktLength,
          dataAdr: @block, dataLength: SIZE[Block], alwaysOnePkt: FALSE];
      END;  -- Unmarshal block.
    Lupine.CheckPktLength[pkt: pkt, pktLength: pktLength];
    RETURN[block];
    END;  -- RawReadBlock.

  RawWriteBlock: PUBLIC PROCEDURE [block: Block, address: HANDLERawAddress]
      =
    BEGIN
    ArgumentOverlay: TYPE = MACHINE DEPENDENT RECORD [
        transferIndex (0): RpcControl.ProcedureIndex ← RawWriteBlock, address
        (1): HANDLERawAddress];
    pktBuffer: ARRAY [1..RpcPrivate.pktOverhead+254] OF WORD;
    pkt: RpcPrivate.StubPkt = RpcPrivate.GetStubPkt[space: @pktBuffer];
    argPkt: POINTER TO ArgumentOverlay = @pkt.data[0];
    pktLength: RpcPrivate.DataLength ← 3;
    lastPkt: BOOLEAN;
    RpcPrivate.StartCall[callPkt: pkt, interface: myInterface];
    argPkt↑ ← [address: address];
    BEGIN  -- Marshal block: Block to pkt.data[pktLength].
      pktLength ← Lupine.CopyToPkt[pkt: pkt, pktLength: pktLength,
          dataAdr: @block, dataLength: SIZE[Block], alwaysOnePkt: FALSE];
      END;  -- Marshal block.
    [returnLength: , lastPkt: lastPkt] ←
      RpcPrivate.Call[ pkt: pkt, callLength: pktLength,
          maxReturnLength: 0, signalHandler: ClientDispatcher];
    Lupine.CheckPktLength[pkt: pkt, pktLength: 0];
    RETURN[];
    END;  -- RawWriteBlock.


-- Remote public signals and errors.

  Error: PUBLIC ERROR [reason: LONG STRING] = CODE;


-- Public signal and error dispatcher.

  ClientDispatcher: --PROCEDURE [pkt: RPCPkt, callLength: DataLength,
      -- lastPkt: BOOLEAN, localConversation: Conversation] RETURNS [returnLength:
      -- DataLength]-- RpcPrivate.Dispatcher =
    BEGIN

    SELECT LOOPHOLE[pkt.data[0], RpcControl.SignalIndex] FROM
      Error => RETURN[
        ErrorStub[pkt: pkt, callLength: callLength, lastPkt: lastPkt,
            localConversation: localConversation]];
      ENDCASE => RETURN[Lupine.DispatchingError[]];

    END;  -- ClientDispatcher


-- Public signal and error dispatcher stubs.

  ErrorStub: --ERROR [reason: LONG STRING]-- RpcPrivate.Dispatcher
      =
    INLINE BEGIN
    reason: LONG STRING;
    pktLength: RpcPrivate.DataLength ← 1;
    MaxHeapAllocs: CARDINAL = 1;
    heapAllocVector: ARRAY [1..MaxHeapAllocs] OF LONG POINTER←ALL[NIL];
    BEGIN ENABLE UNWIND => BEGIN  -- Free storage.
        FOR ptr: CARDINAL IN [1..LENGTH[heapAllocVector]] DO
          IF heapAllocVector[ptr] = NIL
            THEN EXIT
            ELSE paramZones.heap.FREE[@heapAllocVector[ptr]];
          ENDLOOP;
        END;  -- Free storage.
      BEGIN  -- Unmarshal reason: LONG STRING from pkt.data[pktLength].
        stringIsNIL: Lupine.NilHeader;
        IF pktLength+3 > RpcPrivate.maxDataLength
          THEN pktLength ← Lupine.FinishThisPkt[pkt: pkt, pktLength:
              pktLength];
        stringIsNIL ← pkt.data[pktLength];  pktLength ← pktLength+1;
        IF stringIsNIL
          THEN reason ← NIL
          ELSE BEGIN
            stringHeader: Lupine.StringHeader;
            stringHeader.all ← Lupine.RpcPktDoubleWord[pkt, pktLength]↑;
            pktLength ← pktLength + 2;
            reason ← (heapAllocVector[1] ← paramZones.heap.NEW[StringBody[stringHeader.maxLength]]);
            reason.length ← stringHeader.length;
            pktLength ← Lupine.CopyFromPkt[pkt: pkt, pktLength: pktLength,
                dataAdr: BASE[reason.text], dataLength: Lupine.WordsForChars[stringHeader.length],
                alwaysOnePkt: FALSE];
            END;  -- IF stringIsNIL.
        END;  -- Unmarshal reason.
      Lupine.CheckPktLength[pkt: pkt, pktLength: pktLength];
      ERROR Error[reason];
      END;  -- ENABLE UNWIND => Free storage.
    END;  -- ErrorStub.


-- No module initialization.

END.  -- MultibusRpcClientImpl.