From 791f456a4e5beb29ed8a1d986ba3817f28687a77 Mon Sep 17 00:00:00 2001 From: peterkvt80 Date: Wed, 2 Nov 2016 12:10:28 +0000 Subject: [PATCH] File Monitor now allows pages to be added --- carousel.cpp | 2 +- filemonitor.cpp | 22 +++++++++++++++------- filemonitor.h | 3 --- pagelist.cpp | 6 ++++++ pagelist.h | 5 +++++ 5 files changed, 27 insertions(+), 11 deletions(-) diff --git a/carousel.cpp b/carousel.cpp index 2cd04e2..18676de 100644 --- a/carousel.cpp +++ b/carousel.cpp @@ -41,7 +41,7 @@ TTXPageStream* Carousel::nextCarousel() if (p->Expired()) { - std::cerr << "[Carousel::nextCarousel] page " << std::hex << p->GetPageNumber() << std::dec << " cycle time=" << p->GetCycleTime() << std::endl; + // std::cerr << "[Carousel::nextCarousel] page " << std::hex << p->GetPageNumber() << std::dec << " cycle time=" << p->GetCycleTime() << std::endl; p->SetTransitionTime(); // We found a carousel that is ready to step break; } diff --git a/filemonitor.cpp b/filemonitor.cpp index 7a40db7..4b22ac5 100644 --- a/filemonitor.cpp +++ b/filemonitor.cpp @@ -55,6 +55,7 @@ std::thread FileMonitor::run() void FileMonitor::run() { + // @todo This thread will clash. They need proper protection. std::cerr << "[FileMonitor::run] File monitoring started" << std::endl; std::string path=_configure->GetPageDirectory() ; // @@ -89,22 +90,21 @@ void FileMonitor::run() name+="/"; name+=dirp->d_name; // Find the modification time - struct tm* clock; // create a time structure struct stat attrib; // create a file attribute structure 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 + // struct tm* 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; // Now we want to process changes // 1) Is it a new page? Then add it. TTXPageStream* p=_pageList->Locate(name); - if (p) + if (p) // File was found { //std::cerr << dirp->d_name << " was found" << std::endl; - // Existing page. Has it changed? - if (attrib.st_mtime!=p->GetModifiedTime()) + + if (attrib.st_mtime!=p->GetModifiedTime()) // File exists. Has it changed? { - std::cerr << "File has been modified" << std::endl; + std::cerr << "File has been modified" << dirp->d_name << 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 @@ -124,7 +124,15 @@ void FileMonitor::run() } else { - std::cerr << dirp->d_name << " not found (is it new?)" << std::endl; + std::cerr << "[FileMonitor::run] " << " Adding a new page" << dirp->d_name << std::endl; + // A new file. Create the page object and add it to the page list. + if ((p=new TTXPageStream(name))) + { + //p->SetModifiedTime(attrib.st_mtime); // This line is redundant + _pageList->AddPage(p); + } + else + std::cerr << "[FileMonitor::run] Failed to load" << dirp->d_name << std::endl; } // 2) Is it an existing page that has changed? diff --git a/filemonitor.h b/filemonitor.h index f3f33d7..c036fc5 100644 --- a/filemonitor.h +++ b/filemonitor.h @@ -1,8 +1,6 @@ #ifndef _FILEMONITOR_H_ #define _FILEMONITOR_H_ -#define _GNU_SOURCE - #include #include #include @@ -12,7 +10,6 @@ #include "configure.h" #include "pagelist.h" - /** * @brief Watches for changes to teletext page files and updates the page list as needed * www.ibm.com/developerworks/linux/library/l-ubuntu-inotify/index.html diff --git a/pagelist.cpp b/pagelist.cpp index 6b4f287..fd1b0ab 100644 --- a/pagelist.cpp +++ b/pagelist.cpp @@ -80,6 +80,12 @@ int PageList::LoadPageList(std::string filepath) return 0; } +void PageList::AddPage(TTXPageStream* page) +{ + int mag=(page->GetPageNumber() >> 16) & 0x7; + _pageList[mag].push_back(*page); +} + TTXPageStream* PageList::Locate(std::string filename) { // std::cerr << "[PageList::Locate] *** TODO *** " << filename << std::endl; diff --git a/pagelist.h b/pagelist.h index 0b54df8..43018af 100644 --- a/pagelist.h +++ b/pagelist.h @@ -40,6 +40,11 @@ class PageList */ TTXPageStream* Locate(std::string filename); + /** Add a teletext page to the proper magazine + * @param page TTXPageStream object that has already been loaded + */ + void AddPage(TTXPageStream* page); + private: Configure* _configure; // The configuration object std::list _pageList[8]; /// The list of Pages in this service. One list per magazine