Back to index

CFileWaiter.h

 
//----------------------------------------------------------------------------- 
//  $Header$ 
// 
//  COOL Program Library   
//  Copyright (C) CERES collaboration, 1996 
// 
//----------------------------------------------------------------------------- 
#ifndef CFILEWAITER_H 
#define CFILEWAITER_H 
 
#include <unistd.h> 
#include <sys/types.h> 
#include <time.h> 
#include <sys/stat.h> 
#include "iostream.h" 
 
#include "rw/rwtime.h" 
 
#include "cool.h"  
 
class CFileWaiter {  
 
public:  
  CFileWaiter(char* name="/tmp/CFileWaiter.checkFile", int chk=60, int slp=60)  
    : fileName(name), maxSecondsToCheck(chk), secondsToSleep(slp) 
  { timeAtLastCheck = getTime(); } 
 
  ~CFileWaiter() { /* nop */; }  
 
protected:  
  CFileWaiter(const CFileWaiter &)               { /* nop */; }  
  CFileWaiter & operator = (const CFileWaiter &) { return *this; }  
 
public:  
  inline void checkAndWait() { if (timeIsOver()) doRealCheckAndWait();} 
 
protected:  
  inline time_t   getTime()    const { return time(0); } 
  inline CBoolean timeIsOver() const {  
    return ( (getTime()-timeAtLastCheck) > maxSecondsToCheck );  
  } 
 
  void doRealCheckAndWait() { 
    CBoolean isThere = False; 
    RWTime t; 
    time_t startPause; 
    while (fileExists()) { 
      if (!isThere) {  
	isThere = True ;  
	startPause = getTime(); 
	cout << "checkAndWait: file found at : " << t.now() << endl; 
      } 
      sleep(secondsToSleep); 
    } 
    if (isThere)  
      cout << "checkAndWait: file gone  at : " << t.now()  
	   << " (paused " << getTime()-startPause << " sec)" 
	   << endl; 
    timeAtLastCheck = getTime();	// remember time ... 
    return; 
  } 
 
  CBoolean fileExists() { 
    return (stat(fileName, &statusBuffer) == 0);  
  } 
 
protected: 
  int 	  timeAtLastCheck; 
  int 	  maxSecondsToCheck; 
  int 	  secondsToSleep; 
  CString fileName; 
 
  struct stat statusBuffer; 
};  
 
#endif /* CFILEWAITER_H */  

Back to index