Skip to content

Commit

Permalink
File monitoring
Browse files Browse the repository at this point in the history
Added code to reload a page if it changes. The usual issues about
threading have not been addressed yet.
  • Loading branch information
peterkvt80 committed Nov 1, 2016
1 parent 69eda01 commit 9288468
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 28 deletions.
28 changes: 26 additions & 2 deletions filemonitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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
{
Expand Down
4 changes: 2 additions & 2 deletions pagelist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ 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])
for (std::list<TTXPageStream>::iterator p=_pageList[mag].begin();p!=_pageList[mag].end();++p)
{
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;
}
Expand Down
50 changes: 26 additions & 24 deletions ttxpage.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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);




};

Expand Down
10 changes: 10 additions & 0 deletions ttxpagestream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

7 changes: 7 additions & 0 deletions ttxpagestream.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 9288468

Please sign in to comment.