Index: /trunk/FACT++/src/dataLogger.cc
===================================================================
--- /trunk/FACT++/src/dataLogger.cc	(revision 10970)
+++ /trunk/FACT++/src/dataLogger.cc	(revision 10971)
@@ -221,4 +221,6 @@
     ///run numbers
     list<RunNumberType> fRunNumber;
+    ///old run numbers time-out delay (in minutes)
+    long fRunNumberTimeout;
     ///previous run number. to check if changed while logging
     int fPreviousRunNumber;
@@ -236,11 +238,7 @@
     ///full name of the nightly report file
     string fFullNightlyReportFileName;
-    ///variables for computing statistics
-    DataLoggerStats fStatVar;
     ///variable to track when the statistic were last calculated
     double fPreviousStatsUpdateTime;
     double fPreviousOldRunNumberCheck;
-    ///mutex to make sure that the Stats are not accessed while updating
-    mutex fStatsMutex;
     ///boolean to know whether we should close and reopen daily files or not
     bool fDailyFileDayChangedAlready;
@@ -266,4 +264,5 @@
     static const char* fStartStopOpenedFiles;
     static const char* fStartStopNumSubsAndFits;
+    static const char* fSetRunTimeOutDelay;
 private:
     /***************************************************
@@ -315,8 +314,12 @@
     void ServicesMonitoring();
     inline void NotifyOpenedFile(const string &name, int type, DimDescribedService* service);
+    ///variables for computing statistics
+    DataLoggerStats fStatVar;
+    ///mutex to make sure that the Stats are not accessed while updating
+//    mutex fStatsMutex;
     ///services notification thread
-    boost::thread fMonitoringThread;
+//    boost::thread fMonitoringThread;
     ///end of the monitoring
-    bool fContinueMonitoring;
+//    bool fContinueMonitoring;
     ///stores the size of each file that is or was open
     map<string, long> fFileSizesMap;
@@ -329,4 +332,5 @@
     DimDescribedService* fOpenedRunFiles;
     DimDescribedService* fNumSubAndFits;
+    DimDescribedService* fStatsMonitoring;
     NumSubAndFitsType fNumSubAndFitsData;
     ///Small function for calculating the total size written so far
@@ -351,4 +355,5 @@
     int SetOpenedFilesOnOff(const Event& evt);
     int SetNumSubsAndFitsOnOff(const Event& evt);
+    int SetRunTimeoutDelay(const Event& evt);
 
     ///boolean to prevent DIM update while desctructing the dataLogger
@@ -772,6 +777,7 @@
 bool DataLogger::calculateTotalSizeWritten(DataLoggerStats& statVar, bool isPrinting)
 {
-    if (!isPrinting)
-        fStatsMutex.lock();
+//mutex
+//    if (!isPrinting)
+//        fStatsMutex.lock();
 #ifdef HAVE_FITS
     if (isPrinting)
@@ -841,6 +847,7 @@
     statVar.sizeWritten -= fBaseSizeRun;
 
-    if (!isPrinting)
-        fStatsMutex.unlock();
+//mutex
+//    if (!isPrinting)
+//        fStatsMutex.unlock();
 
     return shouldWarn;
@@ -865,4 +872,5 @@
 const char* DataLogger::fStartStopOpenedFiles = "OPENED_FILES_SRVC";
 const char* DataLogger::fStartStopNumSubsAndFits = "NUM_SUBS_SRVC";
+const char* DataLogger::fSetRunTimeOutDelay = "RUN_TIMEOUT";
 
 // --------------------------------------------------------------------------
@@ -871,4 +879,5 @@
 //
 //
+/*
 void DataLogger::ServicesMonitoring()
 {
@@ -911,5 +920,5 @@
         }
 }
-
+*/
 // --------------------------------------------------------------------------
 //
@@ -1015,5 +1024,5 @@
      AddEvent(fStatsPeriod, "F", kSM_NightlyOpen, kSM_Logging, kSM_WaitingRun, kSM_Ready)
              (boost::bind(&DataLogger::SetStatsPeriod, this, _1))
-             ("Interval in which the data-logger statitistics service (STATS) is updated."
+             ("Interval in which the data-logger statistics service (STATS) is updated."
               "Interval[s]:Floating point value in seconds.");
 
@@ -1025,4 +1034,8 @@
              (boost::bind(&DataLogger::SetNumSubsAndFitsOnOff, this, _1))
              ("Switch off the service which distributes information about the number of subscriptions and open files.");
+
+     AddEvent(fSetRunTimeOutDelay, "L:1", kSM_Ready, kSM_NightlyOpen, kSM_Logging, kSM_WaitingRun)
+             (boost::bind(&DataLogger::SetRunTimeoutDelay, this, _1))
+             ("Set the timeout delay for old run numbers.");
 
      fDestructing = false;
@@ -1034,9 +1047,21 @@
      fPreviousStatsUpdateTime = Time().Mjd();
      fPreviousOldRunNumberCheck = Time().Mjd();
-     fContinueMonitoring = true;
-     fMonitoringThread = boost::thread(boost::bind(&DataLogger::ServicesMonitoring, this));
+     fPreviousSize = 0;
+
+     struct statvfs vfs;
+     if (!statvfs(fNightlyFilePath.c_str(), &vfs))
+         fStatVar.freeSpace = vfs.f_bsize*vfs.f_bavail;
+     else
+         fStatVar.freeSpace = -1;
+
+     fStatsMonitoring = new DimDescribedService(GetName() + "/STATS", "X:3", fStatVar, "Add description here");
+
+//mutex
+//     fContinueMonitoring = true;
+//     fMonitoringThread = boost::thread(boost::bind(&DataLogger::ServicesMonitoring, this));
      fBaseSizeNightly = 0;
      fBaseSizeRun = 0;
      fDailyFileDayChangedAlready = true;
+     fRunNumberTimeout = 1;
      if(fDebugIsOn)
      {
@@ -1061,7 +1086,7 @@
     fServiceSubscriptions.clear();
     //exit the monitoring loop
-    fContinueMonitoring = false;
-
-    fMonitoringThread.join();
+//mutex
+//    fContinueMonitoring = false;
+//    fMonitoringThread.join();
     //clear any remaining run number (should remain only one)
      while (fRunNumber.size() > 0)
@@ -1136,8 +1161,19 @@
         fPreviousSize = fStatVar.sizeWritten;
         fPreviousStatsUpdateTime = cTime.Mjd();
-    }
-    if ((cTime.Mjd() - fPreviousOldRunNumberCheck)*24*60*60 > 10.0)
-    {
-        while (fRunNumber.size() > 1 && (cTime - fRunNumber.back().time) > boost::posix_time::time_duration(0,0,10,0))
+        //update the service. No need to check if data has been written, because some must have been, otherwise we would not have hit this piece of code
+        fStatsMonitoring->updateService();
+
+        if(fDebugIsOn)
+        {
+            ostringstream str;
+            str << "Size written: " << fStatVar.sizeWritten/1000 << " kB; writing rate: ";
+            str << fStatVar.writingRate/1000 << " kB/s; free space: ";
+            str << fStatVar.freeSpace/(1000*1000) << " MB";
+            Debug(str);
+        }
+    }
+    if ((cTime.Mjd() - fPreviousOldRunNumberCheck)*24*60 >= fRunNumberTimeout)
+    {
+        while (fRunNumber.size() > 1 && (cTime - fRunNumber.back().time) > boost::posix_time::minutes(fRunNumberTimeout))
         {
              RemoveOldestRunNumber();
@@ -1487,4 +1523,8 @@
         str << " <none>";
     Message(str);
+    //timeout value
+    str.str("");
+    str << "Timeout delay for old run numbers: " << fRunNumberTimeout << " minute(s)";
+    Message(str);
 
     //print all the open files. 
@@ -1663,4 +1703,34 @@
         ostringstream str;
         str << "Number of subscriptions service mode is now " << fNumSubAndFitsIsOn;
+        Message(str);
+    }
+    return GetCurrentState();
+}
+// --------------------------------------------------------------------------
+//
+//! set the timeout delay for old run numbers
+//! @param evt
+//!        the current event. contains the timeout delay long value
+//! @returns
+//!        the new state. Which, in that case, is the current state
+//!
+int DataLogger::SetRunTimeoutDelay(const Event& evt)
+{
+    const long backupTimeout = fRunNumberTimeout;
+    fRunNumberTimeout = evt.GetXtra();
+
+    if (fRunNumberTimeout == 0)
+    {
+        fRunNumberTimeout = backupTimeout;
+        Error("timeout delays for old run numbers must be greater than 0. Ignored.");
+        return GetCurrentState();
+    }
+
+    if (fRunNumberTimeout == backupTimeout)
+        Warn("Warning: given timeout for old run numbers is same value as previous one");
+    else
+    {
+        ostringstream str;
+        str  << "Timeout delay for old run numbers is now " << fRunNumberTimeout;
         Message(str);
     }
@@ -2314,4 +2384,5 @@
     fBlackList.insert("DIS_DNS/");
 
+    //set the black list
     if (conf.Has("block"))
     {
@@ -2321,4 +2392,5 @@
     }
 
+    //set the white list
     if (conf.Has("allow"))
     {
@@ -2332,4 +2404,14 @@
         const vector<string> vec = conf.Get<vector<string>>("group");
         fGrouping.insert(vec.begin(), vec.end());
+    }
+
+    //set the old run numbers timeout delay
+    if (conf.Has("runtimeout"))
+    {
+        const long timeout = conf.Get<long>("runtimeout");
+        if (timeout != 0)
+            fRunNumberTimeout = timeout;
+        else
+            Error("Time out delay for old run numbers should be greater than 0 minute");
     }
     return true;
@@ -2475,4 +2557,5 @@
         ("debug",   po_bool(),      "Debug mode. Print clear text of received service reports to log-stream")
         ("group,g", vars<string>(), "Grouping of services into a single run-Fits")
+        ("runtimeout", var<long>(), "Time out delay for old run numbers")
         ;
 
