From 92884680aa6689abce75cf7195bb737385f8010d Mon Sep 17 00:00:00 2001 From: Peter Kwan Date: Tue, 1 Nov 2016 22:54:21 +0000 Subject: [PATCH] File monitoring Added code to reload a page if it changes. The usual issues about threading have not been addressed yet. --- filemonitor.cpp | 28 ++++++++++++++++++++++++-- pagelist.cpp | 4 ++-- ttxpage.h | 50 ++++++++++++++++++++++++----------------------- ttxpagestream.cpp | 10 ++++++++++ ttxpagestream.h | 7 +++++++ 5 files changed, 71 insertions(+), 28 deletions(-) diff --git a/filemonitor.cpp b/filemonitor.cpp index 318ab55..7a40db7 100644 --- a/filemonitor.cpp +++ b/filemonitor.cpp @@ -76,7 +76,11 @@ void FileMonitor::run() while ((dirp = readdir(dp)) != NULL) { // Select only pages that might be teletext. tti or ttix at the moment. +#ifdef _WIN32 + char* p=strstr(dirp->d_name,".tti"); +#else char* p=strcasestr(dirp->d_name,".tti"); +#endif // std::cerr << path << "/" << dirp->d_name << std::endl; if (p) { @@ -90,13 +94,33 @@ void FileMonitor::run() stat(name.c_str(), &attrib); // get the attributes of the file clock = gmtime(&(attrib.st_mtime)); // Get the last modified time and put it into the time structure - std::cerr << path << "/" << dirp->d_name << std::dec << " time:" << std::setw(2) << clock->tm_hour << ":" << std::setw(2) << clock->tm_min << std::endl; + // std::cerr << path << "/" << dirp->d_name << std::dec << " time:" << std::setw(2) << clock->tm_hour << ":" << std::setw(2) << clock->tm_min << std::endl; // Now we want to process changes // 1) Is it a new page? Then add it. TTXPageStream* p=_pageList->Locate(name); if (p) { - std::cerr << dirp->d_name << " was found" << std::endl; + //std::cerr << dirp->d_name << " was found" << std::endl; + // Existing page. Has it changed? + if (attrib.st_mtime!=p->GetModifiedTime()) + { + std::cerr << "File has been modified" << std::endl; + // We just load the new page and update the modified time + // This isn't good enough. + // We need a mutex or semaphore to lock out this page while we do that + // lock + p->LoadPage(name); // What if this fails? We can see the bool. What to do ? + p->SetModifiedTime(attrib.st_mtime); + // unlock + + //Load in the modified file. That should also reset the file time. + // 1) Lock the page. We can't transmit it + // 2) Remove it from PageList + // 3) Delete the page. + // 4) Load the updated page + // 5) Add it to PageList + // 6) Remove the lock + } } else { diff --git a/pagelist.cpp b/pagelist.cpp index a8a2ac1..6b4f287 100644 --- a/pagelist.cpp +++ b/pagelist.cpp @@ -82,7 +82,7 @@ int PageList::LoadPageList(std::string filepath) TTXPageStream* PageList::Locate(std::string filename) { - std::cerr << "[PageList::Locate] *** TODO *** " << filename << std::endl; + // std::cerr << "[PageList::Locate] *** TODO *** " << filename << std::endl; for (int mag=0;mag<8;mag++) { //for (auto p : _pageList[mag]) @@ -90,7 +90,7 @@ TTXPageStream* PageList::Locate(std::string filename) { TTXPageStream* ptr; ptr=&(*p); - std::cerr << "[PageList::Locate]scan:" << ptr->GetSourcePage() << std::endl; + // std::cerr << "[PageList::Locate]scan:" << ptr->GetSourcePage() << std::endl; if (filename==ptr->GetSourcePage()) return ptr; } diff --git a/ttxpage.h b/ttxpage.h index 541206b..8bd83fc 100644 --- a/ttxpage.h +++ b/ttxpage.h @@ -121,30 +121,6 @@ class TTXPage */ void SetRow(unsigned int rownumber, std::string line); - /** Load an EP1 page - * \param filename : The source file - * \return true if the page was loaded - */ - bool m_LoadEP1(std::string filename); - - /** Load a VTX page - * \param filename : The source file - * \return true if the page was loaded - */ - bool m_LoadVTX(std::string filename); - - /** Load a TTX page (Cebra) - * \param filename : The source file - * \return true if the page was loaded - */ - bool m_LoadTTX(std::string filename); - - /** Load a TTI page (MRG Systems) - * \param filename : The source file - * \return true if the page was loaded - */ - bool m_LoadTTI(std::string filename); - /** Save the whole page set * \param filename : The destination file * \return false if the save failed @@ -221,6 +197,12 @@ class TTXPage inline bool Loaded() const {return m_Loaded;}; protected: + /** Load a TTI page (MRG Systems) + * \param filename : The source file + * \return true if the page was loaded + */ + bool m_LoadTTI(std::string filename); + int m_cycletimeseconds; // CT int m_fastextlinks[6]; // FL int m_PageNumber; // PN @@ -246,6 +228,26 @@ class TTXPage std::string m_FormatPageNumber(TTXPage* p); /// \return the page number ready to write to file int findPageNumber(char* buf); bool m_Loaded; + /** Load an EP1 page + * \param filename : The source file + * \return true if the page was loaded + */ + bool m_LoadEP1(std::string filename); + + /** Load a VTX page + * \param filename : The source file + * \return true if the page was loaded + */ + bool m_LoadVTX(std::string filename); + + /** Load a TTX page (Cebra) + * \param filename : The source file + * \return true if the page was loaded + */ + bool m_LoadTTX(std::string filename); + + + }; diff --git a/ttxpagestream.cpp b/ttxpagestream.cpp index e54e20e..cec537c 100644 --- a/ttxpagestream.cpp +++ b/ttxpagestream.cpp @@ -60,4 +60,14 @@ void TTXPageStream::printList() std::cerr << "DUMP TODO" << std::endl; } +bool TTXPageStream::LoadPage(std::string filename) +{ + bool Loaded=false; + // std::cerr << "[TTXPage] file constructor loading " << filename<< std::endl; + //m_Init(); // Careful! We should move inits to the initialisation list and call the default constructor + + if (m_LoadTTI(filename)) + Loaded=true; + return Loaded; +} diff --git a/ttxpagestream.h b/ttxpagestream.h index ac0a2f5..8f0a225 100644 --- a/ttxpagestream.h +++ b/ttxpagestream.h @@ -78,6 +78,13 @@ class TTXPageStream : public TTXPage */ TTXLine* GetTxRow(uint8_t row); + // The time that the file was modified. + time_t GetModifiedTime(){return _modifiedTime;}; + void SetModifiedTime(time_t timeVal){_modifiedTime=timeVal;}; + + bool LoadPage(std::string filename); + + protected: private: