-- file: STPsC.mesa - Simple/Stream Transfer Protocol 
-- Remote Stream stuff in here
-- Edited by:
-- Smokey on: 12-Mar-81 11:55:11
-- Evans on: Nov 13, 1980 2:32 PM

DIRECTORY
  HeapString USING [Replace],
  PupStream USING [CloseReason, StreamClosing],
  Storage USING [Free, FreeNodeNil, Node],
  STP USING [Access, CredentialsErrors, defaultOptions, Error, FileErrors, FileType],
  STPOps USING [
    CheckConnection, ErrorIfNextNotEOC, ErrorIfNextNotYes, FillInStreamObject,
    GenerateProtocolError, GenerateStreamClosingError, GetHereIsAndPList, Handle,
    LookAtMark, MakeRemoteName, markEOC, markHereIsFile, markHereIsPList,
    markNewStore, markNo, markRetrieve, markYes, MyGetMark, NameToPList, Object,
    ResetPList, PList, PutCommand, PutPList, RemoteObject, RemoteStream,
    SelectError, SetByteSize, SetCreateTime, SetFileType, SmashClosed, UserStateToPList,
    ValidProperties],
  Stream USING [
    Block, Byte, CompletionCode, DeleteProcedure, EndOfStream, GetBlock,
    GetProcedure, Handle, InputOptions, Object, PutBlock, PutByte,
    PutProcedure, SetSST, SSTChange, SubSequenceType, Word],
  Time USING [Packed];
  
STPsC: PROGRAM
  IMPORTS
    HeapString, PupStream, Storage, STP, STPOps, Stream
  EXPORTS STP, STPOps =
  BEGIN OPEN PupStream, STPOps;
  
  -- Exported Types
  
  Object: PUBLIC TYPE = STPOps.Object;
  
-- Procedures implementing STP interface 

  CreateRemoteStream: PUBLIC PROCEDURE [
    stp: STPOps.Handle, file: STRING, access: STP.Access, fileType: STP.FileType,
    options: Stream.InputOptions ← STP.defaultOptions, creation: Time.Packed]
    RETURNS [stream: Stream.Handle] =
    BEGIN
    rs: STPOps.RemoteStream;
    CheckConnection[stp];
    IF stp.remoteStream # NIL THEN ERROR STP.Error[accessError, "Remote Stream Exists"L];
    rs ← Storage.Node[SIZE[STPOps.RemoteObject]];
    -- order is IMPORTANT here!!
      rs↑ ← STPOps.RemoteObject[
	stream: , access: access, state: initial, stp: stp];
      FillInStreamObject[@rs.stream];
      rs.stream.options ← options;
    ResetPList[stp.plist];
    UserStateToPList[stp];
    NameToPList[stp.plist, file, alto];
    SetFileType[stp, fileType];
    IF access = write THEN {
      STPOps.SetCreateTime[stp, creation];
      SetByteSize[stp, fileType]};
    IF fileType = text THEN HeapString.Replace[@stp.plist[eolConversion], "CR"L];
    stp.remoteStream ← rs;
    RETURN[@rs.stream]
    END;
    
  NextFileName: PUBLIC PROCEDURE [remoteStream: Stream.Handle]
    RETURNS [file: STRING] =
    BEGIN
    rs: RemoteStream ← ConvertHandle[remoteStream];
    reason: PupStream.CloseReason;
    IF rs.access # read THEN ERROR STP.Error[accessError, NIL];
    BEGIN ENABLE PupStream.StreamClosing => {reason ← why; GOTO streamClosing}; 
    SELECT rs.state FROM
      initial => RequestRetrieve[rs];
      confirm =>
	BEGIN
	PutCommand[rs.stp, markNo, 0C, "No Thanks"L];
	IF LookAtMark[rs.stp] = markEOC THEN {[] ←  MyGetMark[rs.stp]; rs.state ← end}
	ELSE GetHereIsAndPList[rs.stp];
	END;
      complete, data =>
	IF LookAtMark[rs.stp] = markEOC THEN {[] ←  MyGetMark[rs.stp]; rs.state ← end}
	ELSE {GetHereIsAndPList[rs.stp]; rs.state ← confirm};
      end => NULL;
      ENDCASE;
    EXITS
      streamClosing => GenerateStreamClosingError[reason];
    END;
    RETURN[IF rs.state = end THEN NIL ELSE MakeRemoteName[rs.stp.plist, alto]]
    END;
    
-- Procedures for Remote streams  

  conversionFudge: CARDINAL = LOOPHOLE[@LOOPHOLE[0, RemoteStream].stream];
  
  ConvertHandle: PROCEDURE [h: Stream.Handle] RETURNS [rs: RemoteStream] = INLINE
    BEGIN RETURN[LOOPHOLE[h - conversionFudge]] END;
    
  DeleteRemoteStream: PUBLIC Stream.DeleteProcedure =
    BEGIN
    rs: RemoteStream ← ConvertHandle[sH];
    stp: STPOps.Handle = rs.stp;
    {ENABLE UNWIND => stp.remoteStream ← Storage.FreeNodeNil[rs];
    IF rs.access = read THEN {IF rs.state # end THEN SmashClosed[stp]} -- in mid-transfer
    ELSE SELECT rs.state FROM
      initial => NULL;
      data => {
	PutCommand[rs.stp, markYes, 0C, "Transfer Completed"L];
	ErrorIfNextNotYes[rs.stp];
	ErrorIfNextNotEOC[rs.stp]};
      confirm, complete, end => NULL; -- worry about later
      ENDCASE;
    stp.remoteStream ← Storage.FreeNodeNil[rs]};
    END;
    
  GetByte: PUBLIC PROCEDURE [sH: Stream.Handle] RETURNS [byte: Stream.Byte] =
    BEGIN
    rs: RemoteStream ← ConvertHandle[sH];
    bytesTransferred: CARDINAL;
    charArray: PACKED ARRAY [0..1] OF Stream.Byte;
    block: Stream.Block ← [@charArray, 0, 1];
    [bytesTransferred,,] ← GetBlock[sH, block, rs.stream.options];
    RETURN[IF bytesTransferred # 0 THEN charArray[0] ELSE 0]
    END;
    
  GetWord: PUBLIC PROCEDURE [sH: Stream.Handle] RETURNS [word: Stream.Word] =
    BEGIN
    t: UNSPECIFIED = CARDINAL[GetByte[sH]]*256;
    RETURN[t + GetByte[sH]];
    END;
    
  GetBlock: PUBLIC Stream.GetProcedure =
    BEGIN
    rs: RemoteStream ← ConvertHandle[sH];
    reason: PupStream.CloseReason;
    BEGIN
    ENABLE PupStream.StreamClosing => {reason ← why; GOTO streamClosing};
    IF rs.state # data THEN
      BEGIN
      StartRemote[rs, read];
      IF rs.state = end THEN GOTO done;
      END;
    [bytesTransferred, why, sst] ← Stream.GetBlock[ rs.stp.byteStream, block !
      Stream.EndOfStream => {bytesTransferred ← nextIndex; GOTO done};
      Stream.SSTChange => {
	rs.stp.mark ← sst;
	rs.stp.gotMark ← TRUE;
	bytesTransferred ← nextIndex;
	CONTINUE}];
    IF rs.stp.gotMark THEN {SetupForNextOrEnd[rs.stp]; GOTO done};
    EXITS
      streamClosing => {rs.state ← end; GenerateStreamClosingError[reason]};
      done =>
	BEGIN
	why ← endOfStream;
	IF rs.stream.options.signalEndOfStream THEN
	  Stream.EndOfStream[nextIndex: bytesTransferred];
	END;
    END;
    END;
    
  PutByte: PUBLIC PROCEDURE [sH: Stream.Handle, byte: Stream.Byte] =
    BEGIN
    rs: RemoteStream ← ConvertHandle[sH];
    reason: PupStream.CloseReason;
    BEGIN
    ENABLE PupStream.StreamClosing => {reason ← why; GOTO streamClosing};
    IF rs.state # data THEN StartRemote[rs, write];
    Stream.PutByte[rs.stp.byteStream, byte];
    EXITS streamClosing => {rs.state ← end; GenerateStreamClosingError[reason]};
    END;
    END;
    
  PutWord: PUBLIC PROCEDURE [sH: Stream.Handle, word: Stream.Word] =
    BEGIN
    PutByte[sH, CARDINAL[word]/256];
    PutByte[sH, CARDINAL[word] MOD 256];
    END;
    
  PutBlock: PUBLIC Stream.PutProcedure =
    BEGIN
    rs: RemoteStream ← ConvertHandle[sH];
    reason: PupStream.CloseReason;
    BEGIN
    ENABLE PupStream.StreamClosing => {reason ← why; GOTO streamClosing};
    IF rs.state # data THEN StartRemote[rs, write];
    Stream.PutBlock[rs.stp.byteStream, block, FALSE];
    EXITS streamClosing => {rs.state ← end; GenerateStreamClosingError[reason]};
    END;
    END;
    
    
-- Utilities  

  SetupForNextOrEnd: PROCEDURE [stp: STPOps.Handle] =
    BEGIN
    ErrorIfNextNotYes[stp];
    SELECT LookAtMark[stp] FROM
      markEOC => {[] ← MyGetMark[stp]; stp.remoteStream.state ← end};
      markHereIsPList => stp.remoteStream.state ← complete;
      ENDCASE => GenerateProtocolError[badMark, MyGetMark[stp]];
    END;
    
  RequestRetrieve: PROCEDURE [rs: RemoteStream] =
    BEGIN
    PutPList[rs.stp, markRetrieve];
    GetHereIsAndPList[rs.stp ! STP.Error =>
      SELECT code FROM
	IN STP.CredentialsErrors, IN STP.FileErrors  => rs.state ← end;
	ENDCASE];
    rs.state ← confirm;
    END;
    
  StartRemote: PROCEDURE [rs: RemoteStream, access: STP.Access] =
    BEGIN
    reason: PupStream.CloseReason;
    IF rs.access # access THEN
      ERROR STP.Error[accessError, "Remote stream access Error"L];
    BEGIN
    ENABLE PupStream.StreamClosing => {reason ← why; GOTO streamClosing};
    SELECT access FROM
      read =>
	BEGIN
	IF rs.state = initial THEN RequestRetrieve[rs]; 
	IF rs.state = complete THEN {
	  IF LookAtMark[rs.stp] = markEOC THEN {rs.state ← end; RETURN}
	  ELSE GetHereIsAndPList[rs.stp]; rs.state ← confirm};
	IF rs.state = confirm THEN 
	  BEGIN
	  mark: Stream.SubSequenceType;
	  PutCommand[rs.stp, markYes, 0C, "Yes, please"L];
	  SELECT (mark ← MyGetMark[rs.stp]) FROM
	    markHereIsFile => NULL;
	    ENDCASE => SelectError[rs.stp, "HereIsFile Expected"L, mark];
	  END;
	END;
      write =>
	BEGIN
	PutPList[rs.stp, markNewStore];
	GetHereIsAndPList[rs.stp];
	Stream.SetSST[rs.stp.byteStream, markHereIsFile];
	END;
      ENDCASE => ERROR;
    rs.state ← data;
    EXITS streamClosing => GenerateStreamClosingError[reason];
    END;
    END;
    
  END. of STPsC