-- RapunzelClientImpl.mesa
-- Demo	 9-Jan-87 14:02:33 PST

DIRECTORY Courier, Heap, System, Rapunzel, RapunzelCourier;

RapunzelClientImpl: PROGRAM
  IMPORTS Heap, Courier, RapunzelCourier  EXPORTS Rapunzel = {

  BindHandle: TYPE = LONG POINTER TO READONLY BindObject;
  BindObject: PUBLIC TYPE = Courier.Object;

  Fault: PUBLIC ERROR [
    bH: BindHandle, code: Rapunzel.FaultCode, address: Rapunzel.Address]= CODE;

  PeekShort: PUBLIC PROCEDURE [
    bH: BindHandle, address: Rapunzel.Address]
    RETURNS [result: Rapunzel.Short]= {
    args: RapunzelCourier.PeekShortArgs ← [address];
    res: RapunzelCourier.PeekShortRes;
    DoCourierCall[
      cH: bH, procedureNumber: RapunzelCourier.PeekShort,
      arguments: [@args, RapunzelCourier.DescribePeekShortArgs],
      results: [@res, RapunzelCourier.DescribePeekShortRes]];
    [result] ← res;
    };

  PokeShort: PUBLIC PROCEDURE [
    bH: BindHandle, address: Rapunzel.Address, value: Rapunzel.Short]= {
    args: RapunzelCourier.PokeShortArgs ← [address, value];
    DoCourierCall[
      cH: bH, procedureNumber: RapunzelCourier.PokeShort,
      arguments: [@args, RapunzelCourier.DescribePokeShortArgs]];
    };

  PeekSeqShort: PUBLIC PROCEDURE [
    bH: BindHandle, address: Rapunzel.Address, count: CARDINAL]
    RETURNS [resultSeq: LONG POINTER TO Rapunzel.SeqShort]= {
    args: RapunzelCourier.PeekSeqShortArgs ← [address, count];
    res: RapunzelCourier.PeekSeqShortRes;
    DoCourierCall[
      cH: bH, procedureNumber: RapunzelCourier.PeekSeqShort,
      arguments: [@args, RapunzelCourier.DescribePeekSeqShortArgs],
      results: [@res, RapunzelCourier.DescribePeekSeqShortRes]];
    [resultSeq] ← res;
    };

  FreePeekSeqShortResults: PUBLIC PROCEDURE[
    bH: BindHandle, resultSeq: LONG POINTER TO Rapunzel.SeqShort] = {
    res: RapunzelCourier.PeekSeqShortRes;
    res.resultSeq ← resultSeq;
    Courier.Free[[@res, RapunzelCourier.DescribePeekSeqShortRes], bH.zone];
    };

  PokeSeqShort: PUBLIC PROCEDURE [
    bH: BindHandle, address: Rapunzel.Address, valueSeq: LONG POINTER TO Rapunzel.SeqShort]= {
    args: RapunzelCourier.PokeSeqShortArgs ← [address, valueSeq];
    DoCourierCall[
      cH: bH, procedureNumber: RapunzelCourier.PokeSeqShort,
      arguments: [@args, RapunzelCourier.DescribePokeSeqShortArgs]];
    };

  PeekLong: PUBLIC PROCEDURE [
    bH: BindHandle, address: Rapunzel.Address]
    RETURNS [result: Rapunzel.Long]= {
    args: RapunzelCourier.PeekLongArgs ← [address];
    res: RapunzelCourier.PeekLongRes;
    DoCourierCall[
      cH: bH, procedureNumber: RapunzelCourier.PeekLong,
      arguments: [@args, RapunzelCourier.DescribePeekLongArgs],
      results: [@res, RapunzelCourier.DescribePeekLongRes]];
    [result] ← res;
    };

  PokeLong: PUBLIC PROCEDURE [
    bH: BindHandle, address: Rapunzel.Address, value: Rapunzel.Long]= {
    args: RapunzelCourier.PokeLongArgs ← [address, value];
    DoCourierCall[
      cH: bH, procedureNumber: RapunzelCourier.PokeLong,
      arguments: [@args, RapunzelCourier.DescribePokeLongArgs]];
    };

  DoCmds: PUBLIC PROCEDURE [
    bH: BindHandle, cmdSeq: LONG POINTER TO Rapunzel.SeqCmd]
    RETURNS [resultSeq: LONG POINTER TO Rapunzel.SeqResult]= {
    args: RapunzelCourier.DoCmdsArgs ← [cmdSeq];
    res: RapunzelCourier.DoCmdsRes;
    DoCourierCall[
      cH: bH, procedureNumber: RapunzelCourier.DoCmds,
      arguments: [@args, RapunzelCourier.DescribeDoCmdsArgs],
      results: [@res, RapunzelCourier.DescribeDoCmdsRes]];
    [resultSeq] ← res;
    };

  FreeDoCmdsResults: PUBLIC PROCEDURE[
    bH: BindHandle, resultSeq: LONG POINTER TO Rapunzel.SeqResult] = {
    res: RapunzelCourier.DoCmdsRes;
    res.resultSeq ← resultSeq;
    Courier.Free[[@res, RapunzelCourier.DescribeDoCmdsRes], bH.zone];
    };

  SetShftAddrs: PUBLIC PROCEDURE [
    bH: BindHandle, shftA, shftB: Rapunzel.Address]= {
    args: RapunzelCourier.SetShftAddrsArgs ← [shftA, shftB];
    DoCourierCall[
      cH: bH, procedureNumber: RapunzelCourier.SetShftAddrs,
      arguments: [@args, RapunzelCourier.DescribeSetShftAddrsArgs]];
    };


  RemoteBind: PUBLIC PROCEDURE[
    host: System.NetworkAddress, zone: UNCOUNTED ZONE ← NIL]
    RETURNS[bH: BindHandle] = {
    IF zone = NIL THEN zone ← Heap.systemZone;
    bH ← Courier.Create[
      remote: host, programNumber: RapunzelCourier.programNumber, 
    versionNumber: RapunzelCourier.version, zone: zone, classOfService: transactional];
    };

  RemoteUnbind: PUBLIC PROCEDURE[
    bH: BindHandle] RETURNS [nil: BindHandle] = {
    nil ← NIL;
    IF bH # NIL THEN Courier.Delete[bH];
    };


  DoCourierCall: PROCEDURE[
    cH: Courier.Handle, procedureNumber: CARDINAL,
    arguments: Courier.Parameters ← Courier.nullParameters, 
    results: Courier.Parameters ← Courier.nullParameters] = {
    ENABLE {
      Courier.RemoteErrorSignalled => {
        SELECT errorNumber FROM
          0 => DoFault[cH, arguments];
          ENDCASE;
        };
      Courier.Error => NULL;
      };
    [] ← Courier.Call[cH, procedureNumber, arguments, results];
    };

  DoFault: PROCEDURE[
    bH: BindHandle, arguments: Courier.Arguments] = {
    args: RapunzelCourier.FaultArgs;
    arguments[[@args, RapunzelCourier.DescribeFaultArgs]];
    ERROR Fault[bH, args.code, args.address];
    };



  }.