Skip to content

Commit

Permalink
File Monitor now allows pages to be added
Browse files Browse the repository at this point in the history
  • Loading branch information
peterkvt80 committed Nov 2, 2016
1 parent 413f134 commit 791f456
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 11 deletions.
2 changes: 1 addition & 1 deletion carousel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
22 changes: 15 additions & 7 deletions filemonitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() ; //
Expand Down Expand Up @@ -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
Expand All @@ -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?
Expand Down
3 changes: 0 additions & 3 deletions filemonitor.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#ifndef _FILEMONITOR_H_
#define _FILEMONITOR_H_

#define _GNU_SOURCE

#include <iostream>
#include <thread>
#include <list>
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions pagelist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions pagelist.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<TTXPageStream> _pageList[8]; /// The list of Pages in this service. One list per magazine
Expand Down

0 comments on commit 791f456

Please sign in to comment.