home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / perqb / pq2rea.pas < prev    next >
Pascal/Delphi Source File  |  2020-01-01  |  18KB  |  570 lines

  1. Module KermitRead;
  2.  
  3. { This module exports routines for receiving files from a remote machine }
  4.  
  5. {========================} exports {=========================================}
  6.   
  7. imports KermitFile from KermitFile;
  8. imports KermitGlobals from KermitGlobals;
  9.  
  10. function   ReadSwitch : KermitStates;
  11.  
  12. function   ReceiveInit( VAR RFile   : FNameType ) : KermitStates;
  13.  
  14. {========================} private {=========================================}
  15.  
  16. const
  17.     SenderAborted = 'Transfer was aborted by sender error packet';
  18.  
  19. imports KermitParameters from KermitParameters;
  20. imports KermitLineIO from KermitLineIO;
  21. imports System from System;
  22. imports UtilProgress from UtilProgress;
  23.  
  24. VAR Mess : String;  { Last file error message }
  25.  
  26. {----------------------------------------------------------------------------}
  27.  
  28. function   ReceiveInit( VAR RFile   : FNameType ) : KermitStates;
  29. {  Prod the server to make it send us a file }
  30. VAR Pack : Packet;
  31. begin
  32.     PutFileName( RFile, Pack );
  33.     SendPacket( RinitPack, 0, -1, Pack );
  34.     ReceiveInit := Init;
  35. end;
  36.  
  37. {----------------------------------------------------------------------------}
  38.  
  39. function    ReadData : KermitStates;
  40. const
  41.     DataExp = '?Illegal packet type received - expected data packet';
  42.  
  43. var     Len, Num, Dummy     : integer;
  44.         RetVal              : KermitStates;
  45.         Pack                : Packet;
  46.         ErrCode             : FileErrs;
  47.  
  48.     handler CtlC;
  49.     begin
  50.         CtrlCPending := false;
  51.         ReadData := AbortCtlC;
  52.         exit( ReadData );
  53.     end;
  54.     
  55. begin
  56.     if Debug then begin
  57.         DbgWrite( ' Entering ReadData ..... ' );
  58.         DbgNL;
  59.     end;
  60.     NumTry := NumTry + 1;
  61.     if NumTry>1 then
  62.         TotTry := TotTry + 1;
  63.     if NumTry > MaxTryPack then begin
  64.         Mess := '?Unable to receive data';
  65.         writeln( Mess );
  66.         RetVal := Abort1;
  67.     end
  68.     else
  69.     begin
  70.         case ReadPacket ( Num , Len , Pack ) of
  71.  
  72.             DataPack :
  73.                 begin
  74.                     if Num <> n then
  75.                     begin
  76.                         OldTry := OldTry + 1;
  77.                         if OldTry > MaxTryPack then begin
  78.                             Mess := '?Unable to acknowledge data packet';
  79.                             writeln( Mess );
  80.                             SendErrPack( Mess );
  81.                             RetVal := AbortAll;
  82.                         end
  83.                         else
  84.                         begin
  85.                             if  Num = Prev ( n ) then
  86.                             begin
  87.                                 SendAck( Num );
  88.                                 NumTry := 0;
  89.                                 RetVal := CurrState;
  90.                             end
  91.                             else begin
  92.                                 Mess := '?Data packet out of sequence';
  93.                                 writeln( Mess ); 
  94.                                 SendErrPack( Mess );
  95.                                 RetVal := AbortAll;
  96.                             end;
  97.                         end;
  98.                     end
  99.                     else
  100.                     begin
  101.                         ErrCode := EmptyBuffer ( Pack );
  102.                         if ErrCode >=FNoError then begin
  103.                             SendACK( n );
  104.                             Succeeded;
  105.                             RetVal := CurrState;
  106.                         end else begin
  107.                             FileError( '', ErrCode, Mess );
  108.                             writeln( Mess );
  109.                             RetVal := Abort1;
  110.                         end
  111.                     end;
  112.                 end;
  113.  
  114.             FHeadPack   :
  115.                 begin
  116.                     OldTry := OldTry + 1;
  117.                     if OldTry > MaxTryPack then begin
  118.                         LocalError
  119.                             ( '?Unable to acknowledge file header packet' );
  120.                         RetVal := AbortAll;
  121.                     end
  122.                     else
  123.                         if Num = Prev ( n ) then
  124.                         begin
  125.                             SendACK( num );
  126.                             NumTry := 0;
  127.                             RetVal := CurrState;
  128.                         end
  129.                         else begin
  130.                             LocalError( DataExp );
  131.                             RetVal := Abort1;
  132.                         end;
  133.                 end;
  134.  
  135.             EOFPack     :
  136.                 begin
  137.                     if Num <> n then begin
  138.                         LocalError( '?EOF packet out of sequence' );
  139.                         RetVal := Abort1;
  140.                     end
  141.                     else
  142.                     begin
  143.                         if (Len > 0) and (Pack.Data[1] = 'D') then
  144.                             ErrCode := DiscardFile
  145.                         else
  146.                             ErrCode := KeepFile;
  147.                         if ErrCode>=FNoError then begin
  148.                             SendAck( n );
  149.                             Succeeded;
  150.                             RetVal := FileHeader;
  151.                         end else begin
  152.                             FileError( '', ErrCode, Mess );
  153.                             SendErrPack( Mess );
  154.                             writeln( Mess );
  155.                             RetVal := AbortAll;
  156.                         end;
  157.                     end;
  158.                 end;
  159.  
  160.             ErrPack:
  161.                 begin
  162.                     TreatErrPack( Pack, Num );
  163.                     RetVal := AbortAll;
  164.                 end;
  165.  
  166.             NAKPack    :
  167.                 begin
  168.                     SendNAK( n );
  169.                     RetVal := CurrState;
  170.                 end;
  171.                      
  172.             ACKPack, SInitPack,
  173.             IllPack    :
  174.                 begin
  175.                     writeln( DataExp );
  176.                     RetVal := Abort1;
  177.                 end;
  178.  
  179.             ChkIllPack :
  180.                 begin
  181.                     if Debug then begin
  182.                         DbgWrite ( 'Illegal CheckSum  - Sending NAK' );
  183.                         DbgNL;
  184.                     end;
  185.                     SendNAK ( n );
  186.                     RetVal := CurrState;
  187.                 end;
  188.  
  189.             TimOutPack  :
  190.                 begin
  191.                     if Debug then begin
  192.                         DbgWrite ( 'Timed out waiting for pack. number:' );
  193.                         DbgInt ( n );
  194.                         DbgNL;
  195.                     end; 
  196.                     SendAck ( Prev(n) );
  197.                    { SendNAK ( n ); }
  198.                     RetVal := CurrState;
  199.                 end;
  200.         end;  { case }
  201.     end;
  202.     ReadData := RetVal;
  203. end;
  204.  
  205. {----------------------------------------------------------------------------}
  206.  
  207. Const OnlyFile = False; TextReply = True;
  208.  
  209. function    ReadFile( ReplyExpected : Boolean ) : KermitStates;
  210.  
  211. const   FHeadExp = 
  212.             '?Illegal packet type received - expected file header packet';
  213.  
  214. var     num         : integer;
  215.         len         : integer;
  216.         Status      : integer;
  217.         Pack        : Packet;
  218.         RetVal      : KermitStates;
  219.         FileName    : FNameType;
  220.         FE          : FileErrs;
  221.  
  222.     handler CtlC;
  223.     begin
  224.         CtrlCPending := false;
  225.         ReadFile := AbortCtlC; 
  226.         exit( ReadFile );
  227.     end;
  228.     
  229. begin
  230.     if Debug then begin
  231.         DbgWrite( 'Entering ReadFile ...... ');
  232.         DbgNL;
  233.     end;
  234.     NumTry := NumTry + 1;
  235.     if NumTry>1 then
  236.         TotTry := TotTry + 1;
  237.     if NumTry > MaxTryPack then begin
  238.         LocalError( '?Unable to receive file header' );
  239.         RetVal := AbortAll;
  240.     end
  241.     else
  242.     begin
  243.         case ReadPacket ( Num , len , Pack ) of
  244.  
  245.             SInitPack   :   { May be our ACK lost }
  246.                 if ReplyExpected then begin
  247.                     Mess := '?Illegal packet type received';
  248.                     writeln( Mess );
  249.                     SendErrPack( Mess );
  250.                     RetVal := AbortAll;
  251.                 end else begin
  252.                     OldTry := OldTry + 1;
  253.                     if OldTry > MaxTryPack then begin
  254.                         writeln
  255.                           ( '?Unable to acknowledge send initiate packet');
  256.                         RetVal := AbortAll; { abort on too many errors }
  257.                     end
  258.                     else
  259.                     begin
  260.                         if num = Prev ( n ) then
  261.                         { Previous packet? }
  262.                         begin
  263.                             ReadPars ( Pack );       { yes - re-ACK }
  264.                             SendPacket( NoChangePack,
  265.                                         num,
  266.                                         -1,
  267.                                         Pack    );
  268.                             NumTry := 0;
  269.                             RetVal := CurrState;
  270.                         end;
  271.                     end;
  272.                 end;
  273.             
  274.             EOFPack   :
  275.                 if ReplyExpected then begin
  276.                     writeln( '?Illegal packet type received' );
  277.                     RetVal := Abort1;
  278.                 end else begin
  279.                     OldTry := OldTry + 1;
  280.                     if OldTry > MaxTryPack then begin
  281.                         writeln( '?Unable to acknowledge EOF packet' );
  282.                         RetVal := Abort1;
  283.                     end
  284.                     else
  285.                     begin
  286.                         if  num = Prev ( n )  then
  287.                         begin
  288.                             SendACK( num );
  289.                             NumTry := 0;
  290.                             RetVal := CurrState;
  291.                         end
  292.                         else begin
  293.                             writeln( FHeadExp );
  294.                             RetVal := Abort1;
  295.                         end;
  296.                     end;
  297.                 end;
  298.  
  299.             THeadPack   :
  300.                 begin
  301.                     if num<> n then
  302.                         RetVal := Abort1
  303.                     else
  304.                     begin
  305.                         WriteScreen;
  306.                         RetVal := FileData;
  307.                     end;
  308.                 end;
  309.             
  310.             FHeadPack   :   { which is what we really want }
  311.                 begin
  312.                     if  num <> n then
  313.                         RetVal := Abort1
  314.                     else
  315.                     begin
  316.                         GetFilename ( Filename, Pack );
  317.                         FE := NextWriteFile( FileName );
  318.                         repeat
  319.                             case FE of
  320.                         
  321.                             FNoError, FRenamed:
  322.                                 begin
  323.                                     SendACK( n );
  324.                                     if Debug then begin
  325.                                         DbgWrite( 'Receiving : ' );
  326.                                         DbgFilename( FileName );
  327.                                         DbgNL;
  328.                                     end;
  329.                                     Succeeded;
  330.                                     RetVal := FileData;
  331.                                     FE := FNoError;
  332.                                 end;
  333.                                 
  334.                             otherwise:      { Retry - error closing prev. file }
  335.                                 begin
  336.                                     FileError( '', FE, Mess );
  337.                                     writeln( Mess );
  338.                                     FE := NextWriteFile( FileName );
  339.                                 end;
  340.                             end;
  341.                         until FE=FNoError;
  342.                     end;
  343.                 end;
  344.  
  345.             BrkPack :
  346.                 begin
  347.                     if  num <> n then begin
  348.                         writeln
  349.                             ( '?Break packet received out of sequence' );
  350.                         RetVal := Abort1;
  351.                     end
  352.                     else
  353.                     begin
  354.                         SendACK( n );
  355.                         RetVal := Complete;
  356.                     end;
  357.                 end;
  358.  
  359.             ErrPack:
  360.                 begin
  361.                     TreatErrPack( Pack, Num );
  362.                     writeln( SenderAborted );
  363.                     RetVal := AbortAll;
  364.                 end;
  365.  
  366.             AckPack :
  367.                 if ReplyExpected then begin
  368.                     if N <> Num then begin
  369.                         RetVal := AbortAll;
  370.                     end else begin
  371.                         WriteScreen;
  372.                         Pack.PType := PackToCh( DataPack );
  373.                         FE := EmptyBuffer( Pack );
  374.                         FE := FileIdle;
  375.                         RetVal := Complete;
  376.                     end;
  377.                 end else begin
  378.                     RetVal := Abort1;
  379.                     writeln( FHeadExp );
  380.                 end;
  381.                      
  382.             DataPack, NAKPack,
  383.             IllPack :
  384.                 begin
  385.                     RetVal := Abort1;
  386.                     writeln( FHeadExp );
  387.                 end;
  388.  
  389.             ChkIllPack :
  390.                 begin
  391.                     if Debug then begin
  392.                         DbgWrite('Wrong checksum - sending NAK');
  393.                         DbgNL;
  394.                     end;
  395.                     SendNAK( n );
  396.                     RetVal := CurrState;
  397.                 end;
  398.  
  399.             TimOutPack  :
  400.                 begin
  401.                     if Debug then begin
  402.                         DbgWrite('Timed out waiting for FHeadPacket');
  403.                         DbgNL;
  404.                     end;
  405.                     SendNAK( n );
  406.                     RetVal := CurrState;
  407.                 end;
  408.         end;
  409.     end;
  410.     ReadFile := RetVal;
  411. end;
  412.  
  413. {----------------------------------------------------------------------------}
  414.  
  415.  
  416.  
  417. function    ReadInit : KermitStates;
  418.  
  419. const   SInitExp = 
  420.            '?Illegal packet type received - expected send initiate packet';
  421.  
  422. var     num     : integer;
  423.         len     : integer;
  424.         Pack    : Packet;
  425.         RetVal  : KermitStates;
  426.         Answer  : PacketType;
  427.  
  428.     handler CtlC;
  429.     begin
  430.         CtrlCPending := false;
  431.         ReadInit := AbortCtlC;
  432.         exit( ReadInit );
  433.     end;
  434.     
  435. begin
  436.     if Debug then begin
  437.         DbgWrite( 'Entering ReadInit ...... ');
  438.         DbgNL;
  439.     end;
  440.     NumTry := NumTry + 1;
  441.     if NumTry>1 then
  442.         TotTry := TotTry + 1;
  443.     if NumTry > MaxTryInit then begin
  444.         LocalError( '?Unable to receive initiate' );
  445.         RetVal := AbortAll;
  446.     end
  447.     else
  448.     begin
  449.         Answer := ReadPacket( Num, len, Pack );
  450.         if Answer = SInitPack then
  451.         begin
  452.             ReadPars( Pack );
  453.             SendPacket(     NoChangePack,
  454.                             n,
  455.                             -1,
  456.                             Pack    );
  457.             Succeeded;
  458.             RetVal := FileHeader;
  459.         end
  460.         else
  461.             if Answer = TimOutPack then begin
  462.                 if Debug then begin
  463.                     DbgWrite('Timed out waiting for Send-init - Retrying');
  464.                     DbgNL;
  465.                 end;
  466.                 SendNAK ( n );
  467.                 RetVal := CurrState;
  468.             end else
  469.                 if Answer = ChkIllPack then begin
  470.                     if Debug then begin
  471.                         DbgWrite('Illegal checksum - retrying');
  472.                         DbgNL;
  473.                     end;
  474.                     SendNAK ( n );
  475.                     RetVal := CurrState;
  476.                 end else
  477.                     if Answer = ErrPack then begin
  478.                         TreatErrPack( Pack, Num );
  479.                         writeln( SenderAborted );
  480.                         RetVal := AbortAll;
  481.                     end else begin
  482.                         if Debug then begin
  483.                             DbgWrite('Unable to receive send-init-packet');
  484.                             DbgNL;
  485.                             DbgPacket( Pack );
  486.                         end;
  487.                         writeln( SInitExp );
  488.                         SendErrPack( SInitExp );
  489.                         RetVal := AbortAll;
  490.                     end;
  491.  
  492.     end;
  493.     ReadInit := RetVal;
  494. end;
  495.  
  496. {----------------------------------------------------------------------------}
  497.  
  498. function   ReadSwitch : KermitStates;
  499.  
  500. var Dummy : FileErrs;
  501.    
  502.     handler CtlCAbort;
  503.     begin
  504.         CtrlCPending := false;
  505.     end;
  506.  
  507. {  This is the state table switcher for the receive file function }
  508. begin
  509.     if (CurrState <> RemoteReply) then
  510.         CurrState := Init;
  511.     n := 0;
  512.     nn := 0;
  513.     NumTry := 0;
  514.     OldTry := 0;
  515.     TotTry := 0;
  516.     InitProgress;
  517.     LoadBusy;       { From UtilProgress - load Busy bee }
  518.     ShowPackNum;
  519.  
  520.     while (CurrState <> AbortAll) and (CurrState <> Complete) 
  521.       and (CurrState <> AbortCtlC) do
  522.     begin
  523.         ShowPackNum;
  524.         case CurrState of
  525.  
  526.             FileData    :   
  527.                 CurrState := ReadData;
  528.  
  529.             FileHeader  :   
  530.                 CurrState := ReadFile( OnlyFile );
  531.  
  532.             RemoteReply :   
  533.                 CurrState := ReadFile( TextReply );
  534.  
  535.             Init        :   
  536.                 CurrState := ReadInit;
  537.  
  538.             EOFile, Break   :
  539.                 begin
  540.                     LocalError
  541.                         ('?Unexpected packet read - EOFile or Break');
  542.                     CurrState := Abort1;
  543.                 end;
  544.  
  545.             Abort1     :    
  546.                 begin
  547.                     FileAbort;   
  548.                     CurrState := FileHeader; 
  549.                 end;     
  550.         end;
  551.         
  552.         ShowProgress( ProgressLines );
  553.         if Debug then begin
  554.             DbgWrite ( 'ReadSwitch :  State transition to --> ' );
  555.             DbgState ( CurrState );
  556.             DbgNL;
  557.         end;
  558.  
  559.     end;
  560.  
  561.     if CurrState = AbortCtlC then begin
  562.         writeln( AbortedByCtlC );
  563.         SendErrPack( AbortedByCtlC );
  564.     end;
  565.  
  566.     ReadSwitch := CurrState;
  567.     Dummy := FileIdle;
  568.     QuitProgress;
  569. end.
  570.