/**********************************************************************/
/**********************************************************************/
/*  THE FILES CONTAINED HEREIN ARE PROVIDED BY SAS INSTITUTE INC.     */
/*  "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,  */
/*  INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF            */
/*  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. RECIPIENTS  */
/*  ACKNOWLEDGE AND AGREE THAT SAS INSTITUTE SHALL NOT BE LIABLE      */
/*  WHATSOEVER FOR ANY DAMAGES ARISING OUT OF THEIR USE OF THIS       */
/*  MATERIAL. IN ADDITION, SAS INSTITUTE WILL PROVIDE NO SUPPORT FOR  */
/*  THE MATERIALS CONTAINED HEREIN.                                   */
/**********************************************************************/
/**********************************************************************/


/***********************************************************/
/* Code for the Clients                                    */
/***********************************************************/

 prepsave:        /* prepares for server save if available */
  
      /* check server status to see if accepting requests  */                           
   statusid=open('server.status','i'); 
   rc=fetch(statusid);                                              
   status=getvarn(statusid,varnum(statusid,'status'));            
   lasttime=getvarn(statusid,varnum(statusid,'lasttime'));          
   if statusid then statusid=close(statusid);                       
                                                                      
      /* server status okay and checked in within the last */
      /* 2 minutes                                         */  
   if status=0 and 0<=datetime()-lasttime<=120 then do;               
      starttm=datetime();                                           
      _msg_='Transferring the data to the server which '||
            'will save the data.';
      refresh;                                                        

         /* transfer the data and capture system error     */
         /* codes                                          */      
      submit continue;                                                
         proc append base=server.tmphdr data=work.tmphdr;                  
         run;                                                         
         %let rc1=&syserr;                                          
                                                                      
         proc append base=server.tmprslt data=work.tmprslt;                 
         run;                                                         
         %let rc2=&syserr;                                          
                                                                      
         proc append base=server.tmpattr data=work.tmpattr;
         run;                                                         
         %let rc3=&syserr;                                          
      endsubmit;                                                      
                                                                      
         /* place in request file after data copied so     */
         /* ready                                          */  
      savedsid=open('server.save_req','u');                        
      rc4=append(savedsid,'noset');                                 
      call putvarc(savedsid,varnum(savedsid,'sku'),sku);              
      call putvarc(savedsid,varnum(savedsid,'cntl'),cntl);            
      call putvarn(savedsid,varnum(savedsid,'starttime'),
                            datetime());  
      call putvarn(savedsid,varnum(savedsid,'savetime'),.);           
      rc5=update(savedsid);                                         
      if savedsid then savedsid=close(savedsid);                      
                                                                      
         /* if any non-zero return codes, then warn user   */  
      if symgetn('RC1') + symgetn('RC2') + symgetn('RC3') +           
         abs(rc4) + abs(rc5) > 0 then do;                
         call display('system.windows.message.program',               
            'Error May Have Occured During Transfer',            
            'An error may have occured while transferring the',   
            'data to the server. Please wait until the lot is',   
            'saved and then browse the lot to check the data.',                                                 
            'W');                   
      end;   /* if statement                               */                             
      _msg_='The server will now save the data. You may'||
            ' continue using the system. '||
            ' ('||trim(left(put(datetime()-starttm,3.)))||
            ' seconds)';
      refresh;                                                        
   end;                                                              
   else do;                                                           
      select (status);                                              
      when (0)     /* lasttime must be over 2 minutes ago  */
          _msg_='Server down?: Last checked in over 2 '||
                'minutes ago.';
      when (1)                                                    
         _msg_='Server unavailable: Too many pending '||
               'requests.';
      when (2)                                                    
         _msg_='Server unavailable: Server shutting '||
               'itself down.';
      when (3)                                                    
         _msg_='Server unavailable: Server shut down.';      
      otherwise                                                   
         _msg_='Server unavailable: Unknown status code.';     
      end;   /* select                                     */                                          
      _msg_=_msg_ || ' Saving the data here.';                      
      refresh;
 
         /* saving the data instead of the server          */
      link savedata;
   end;                                                              
return;