Skip to content

Commit

Permalink
Better handling of adding and removing packet 29
Browse files Browse the repository at this point in the history
  • Loading branch information
ZXGuesser committed Aug 17, 2020
1 parent c59c2f3 commit 404448e
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 117 deletions.
12 changes: 2 additions & 10 deletions filemonitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,7 @@ int FileMonitor::readDirectory(std::string path){
q->SetUpdatedFlag(true);
}

if (_pageList->CheckForPacket29(q))
{
std::cerr << "[FileMonitor::run] found packet 29" << std::endl;
_pageList->GetMagazines()[mag]->SetPacket29(_pageList->GetPacket29(mag));
}
_pageList->CheckForPacket29(q);

q->SetModifiedTime(attrib.st_mtime);
// unlock
Expand Down Expand Up @@ -234,11 +230,7 @@ int FileMonitor::readDirectory(std::string path){
}
}

if (_pageList->CheckForPacket29(q))
{
std::cerr << "[FileMonitor::run] found packet 29" << std::endl;
_pageList->GetMagazines()[mag]->SetPacket29(_pageList->GetPacket29(mag));
}
_pageList->CheckForPacket29(q);
}
else
std::cerr << "[FileMonitor::run] Failed to add" << dirp->d_name << std::endl; // should never happen
Expand Down
56 changes: 37 additions & 19 deletions packetmag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ PacketMag::PacketMag(uint8_t mag, std::list<TTXPageStream>* pageSet, ttx::Config
_thisRow(0),
_lastTxt(nullptr),
_nextPacket29DC(0),
_hasPacket29(false),
_magRegion(0),
_specialPagesFlipFlop(false),
_waitingForField(0)
Expand Down Expand Up @@ -73,23 +74,28 @@ Packet* PacketMag::GetPacket(Packet* p)
switch (_state)
{
case PACKETSTATE_HEADER: // Start to send out a new page, which may be a simple page or one of a carousel
if (GetEvent(EVENT_PACKET_29))
if (GetEvent(EVENT_PACKET_29) && _hasPacket29)
{
while (_packet29[_nextPacket29DC] == nullptr)
if (_mtx.try_lock()) // skip if unable to get lock
{
_nextPacket29DC++;
}

if (_nextPacket29DC >= MAXPACKET29TYPES)
{
_nextPacket29DC = 0;
ClearEvent(EVENT_PACKET_29);
}
else
{
p->SetRow(_magNumber, 29, _packet29[_nextPacket29DC]->GetLine(), CODING_13_TRIPLETS);
_nextPacket29DC++;
return p;
while (_packet29[_nextPacket29DC] == nullptr)
{
_nextPacket29DC++;
}

if (_nextPacket29DC >= MAXPACKET29TYPES)
{
_nextPacket29DC = 0;
ClearEvent(EVENT_PACKET_29);
}
else
{
p->SetRow(_magNumber, 29, _packet29[_nextPacket29DC]->GetLine(), CODING_13_TRIPLETS);
_nextPacket29DC++;
_mtx.unlock(); // unlock before we return!
return p;
}
_mtx.unlock(); // we got a lock so unlock again
}
}
_specialPagesFlipFlop = !_specialPagesFlipFlop; // toggle the flag so that we interleave special pages and regular pages during the special pages event so that rolling headers aren't stopped completely
Expand Down Expand Up @@ -420,10 +426,9 @@ bool PacketMag::IsReady(bool force)
}
};

void PacketMag::SetPacket29(TTXLine *lines[MAXPACKET29TYPES]){
//std::cerr << "[PacketMag::setPacket29]" << std::endl;
for (int i=0;i<MAXPACKET29TYPES;i++)
_packet29[i] = lines[i];
void PacketMag::SetPacket29(int i, TTXLine *line){
_packet29[i] = line;
_hasPacket29 = true;

if (_packet29[0])
{
Expand All @@ -434,3 +439,16 @@ void PacketMag::SetPacket29(TTXLine *lines[MAXPACKET29TYPES]){
_magRegion = ((_packet29[2]->GetCharAt(2) & 0x30) >> 4) | ((_packet29[2]->GetCharAt(3) & 0x3) << 2);
}
}

void PacketMag::DeletePacket29(){
_mtx.lock();
for (int i=0;i<MAXPACKET29TYPES;i++)
{
if (_packet29[i] != nullptr){
delete _packet29[i]; // delete TTXLine created in PageList::CheckForPacket29
_packet29[i] = nullptr;
}
}
_hasPacket29 = false;
_mtx.unlock();
}
14 changes: 10 additions & 4 deletions packetmag.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef PACKETMAG_H
#define PACKETMAG_H
#include <list>
#include <mutex>
#include <packetsource.h>
#include "ttxpagestream.h"
#include "carousel.h"
Expand Down Expand Up @@ -42,8 +43,10 @@ class PacketMag : public PacketSource
void SetPriority(uint8_t priority) { _priority = priority; }

bool IsReady(bool force=false);

void SetPacket29(TTXLine *lines[MAXPACKET29TYPES]);

void SetPacket29(int i, TTXLine *line);
bool GetPacket29Flag() { return _hasPacket29; };
void DeletePacket29();

protected:

Expand All @@ -63,10 +66,13 @@ class PacketMag : public PacketSource
PacketState _state; /// State machine to sequence packet types
uint8_t _thisRow; // The current line that we are outputting
TTXLine* _lastTxt; // The text of the last row that we fetched. Used for enhanced packets


int _nextPacket29DC;
TTXLine* _packet29[MAXPACKET29TYPES]; // space to store magazine related enhancement packets
TTXLine* _nextPacket29;
int _nextPacket29DC;
bool _hasPacket29;
std::mutex _mtx; // Mutex to interlock packet 29 from filemonitor

int _magRegion;
int _status;
int _region;
Expand Down
129 changes: 58 additions & 71 deletions pagelist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ PageList::PageList(Configure *configure) :
for (int i=0;i<8;i++)
{
_mag[i]=nullptr;
for (int j=0;j<MAXPACKET29TYPES;j++)
{
_magPacket29[i][j]=nullptr;
}
}
if (_configure==nullptr)
{
Expand All @@ -32,20 +28,16 @@ PageList::~PageList()

int PageList::LoadPageList(std::string filepath)
{
// std::cerr << "[PageList::LoadPageList] Loading pages from " << filepath << std::endl;
if (ReadDirectory(filepath))
return errno;

// std::cerr << "[PageList::LoadPageList]FINISHED LOADING PAGES" << std::endl;

// How many files did we accept?
// Create PacketMags before loading
for (int i=0;i<8;i++)
{
_mag[i]=new vbit::PacketMag(i, &_pageList[i], _configure, 9); // this creates the eight PacketMags that Service will use. Priority will be set in Service later
_mag[i]->SetPacket29(_magPacket29[i]);
}



// Load files
if (ReadDirectory(filepath))
return errno;

PopulatePageTypeLists(); // add pages to the appropriate lists for their type

// Just for testing
Expand Down Expand Up @@ -112,14 +104,15 @@ int PageList::ReadDirectory(std::string filepath)
// If the page loaded, then push it into the appropriate magazine
if (q->Loaded())
{
q->GetPageCount(); // Use for the side effect of renumbering the subcodes

q->GetPageCount(); // Use for the side effect of renumbering the subcodes

CheckForPacket29(q);
}

int mag=(q->GetPageNumber() >> 16) & 0x7;
_pageList[mag].push_back(*q); // This copies. But we can't copy a mutex

if (CheckForPacket29(q))
std::cerr << "[PageList::LoadPageList] found packet 29" << std::endl;
}

}
}
closedir(dp);
Expand All @@ -129,54 +122,50 @@ int PageList::ReadDirectory(std::string filepath)

void PageList::AddPage(TTXPageStream* page)
{
int mag=(page->GetPageNumber() >> 16) & 0x7;
_pageList[mag].push_back(*page);
if (CheckForPacket29(page))
{
std::cerr << "[PageList::AddPage] found packet 29" << std::endl;
_mag[mag]->SetPacket29(_magPacket29[mag]);
}
int mag=(page->GetPageNumber() >> 16) & 0x7;
_pageList[mag].push_back(*page);
}

bool PageList::CheckForPacket29(TTXPageStream* page)
void PageList::CheckForPacket29(TTXPageStream* page)
{
if (page->IsCarousel()) // page mFF should never be a carousel and this code leads to a crash if it is so bail out now
return false;
int Packet29Flag = false;
int mag=(page->GetPageNumber() >> 16) & 0x7;
if (((page->GetPageNumber() >> 8) & 0xFF) == 0xFF)
{
// only read packet 29 from page mFF

// clear any previous packet 29
_magPacket29[mag][0] = nullptr;
_magPacket29[mag][1] = nullptr;
_magPacket29[mag][2] = nullptr;
TTXLine* tempLine = page->GetTxRow(29);
while (tempLine != nullptr)
{
//std::cerr << "page includes packet 29" << std::endl;
switch (tempLine->GetCharAt(0))
{
case '@':
Packet29Flag = true;
_magPacket29[mag][0] = new TTXLine(tempLine->GetLine(), true);
break;
case 'A':
Packet29Flag = true;
_magPacket29[mag][1] = new TTXLine(tempLine->GetLine(), true);
break;
case 'D':
Packet29Flag = true;
_magPacket29[mag][2] = new TTXLine(tempLine->GetLine(), true);
break;
}

tempLine = tempLine->GetNextLine();
// loop until every row 29 is copied
}
}
return Packet29Flag;
if (page->IsCarousel()) // page mFF should never be a carousel and this code leads to a crash if it is so bail out now
return;

bool Packet29Flag = false;
int mag=(page->GetPageNumber() >> 16) & 0x7;
if (((page->GetPageNumber() >> 8) & 0xFF) == 0xFF) // Only read packet 29 from page mFF
{
if (_mag[mag]->GetPacket29Flag() == false) // Only allow one file to set packet29 per magazine
{
TTXLine* tempLine = page->GetTxRow(29);

while (tempLine != nullptr)
{
switch (tempLine->GetCharAt(0))
{
case '@':
Packet29Flag = true;
_mag[mag]->SetPacket29(0, new TTXLine(tempLine->GetLine(), true));
break;
case 'A':
Packet29Flag = true;
_mag[mag]->SetPacket29(1, new TTXLine(tempLine->GetLine(), true));
break;
case 'D':
Packet29Flag = true;
_mag[mag]->SetPacket29(2, new TTXLine(tempLine->GetLine(), true));
break;
}

tempLine = tempLine->GetNextLine();
// loop until every row 29 is copied
}
page->SetPacket29Flag(Packet29Flag); // mark the page
}

if (Packet29Flag)
std::cerr << "[PageList::CheckForPacket29] Added packet 29 for magazine " << ((mag == 0)?8:mag) << std::endl;
}
}

// Find a page by filename
Expand Down Expand Up @@ -384,7 +373,7 @@ void PageList::ClearFlags()
TTXPageStream* ptr;
ptr=&(*p);
// Don't unmark a file that was MARKED. Once condemned it won't be pardoned
if (ptr->GetStatusFlag()==TTXPageStream::FOUND)
if (ptr->GetStatusFlag()==TTXPageStream::FOUND || ptr->GetStatusFlag()==TTXPageStream::NEW)
{
ptr->SetState(TTXPageStream::NOTFOUND);
}
Expand All @@ -403,13 +392,11 @@ void PageList::DeleteOldPages()
ptr=&(*p);
if (ptr->GetStatusFlag()==TTXPageStream::GONE)
{
if (((ptr->GetPageNumber() >> 8) & 0xFF) == 0xFF)
if (ptr->GetPacket29Flag())
{
// page mFF - make sure packet 29 is removed
_magPacket29[mag][0] = nullptr;
_magPacket29[mag][1] = nullptr;
_magPacket29[mag][2] = nullptr;
_mag[mag]->SetPacket29(_magPacket29[mag]);
// Packet 29 was loaded from this page, so remove it.
_mag[mag]->DeletePacket29();
std::cerr << "[PageList::DeleteOldPages] Removing packet 29 from magazine " << ((mag == 0)?8:mag) << std::endl;
}

//std::cerr << "[PageList::DeleteOldPages] Deleted " << ptr->GetSourcePage() << std::endl;
Expand Down
6 changes: 1 addition & 5 deletions pagelist.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ class PageList
*/
TTXPageStream* FirstPage();

bool CheckForPacket29(TTXPageStream* page);

TTXLine** GetPacket29(int mag){ return _magPacket29[mag];};
void CheckForPacket29(TTXPageStream* page);

private:
Configure* _configure; // The configuration object
Expand All @@ -103,8 +101,6 @@ class PageList
*/
void PopulatePageTypeLists();

TTXLine* _magPacket29[8][MAXPACKET29TYPES];

// iterators through selected pages. (use the same iterator for D command and MD, L etc.)
uint8_t _iterMag; /// Magazine number for the iterator
std::list<TTXPageStream>::iterator _iter; /// pages in a magazine
Expand Down
11 changes: 4 additions & 7 deletions ttxpage.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,9 @@ class TTXPage

void SetSelected(bool value){_Selected=value;}; /// Set the selected state to value
bool Selected(){return _Selected;}; /// Return the selected state

protected:
bool m_LoadTTI(std::string filename);

int m_cycletimeseconds; // CT
int m_fastextlinks[6]; // FL
int m_PageNumber; // PN
Expand All @@ -216,7 +215,6 @@ class TTXPage
// Private objects
TTXPage* m_SubPage;
TTXLine* m_pLine[MAXROW+1];

std::string m_destination; // DS
std::string m_sourcepage; // SP
std::string m_description; // DE
Expand All @@ -227,13 +225,12 @@ class TTXPage
unsigned int m_lastpacket;
PageCoding m_pagecoding;
PageFunction m_pagefunction;
// Private functions
void m_Init();

bool m_Loaded;
bool _Selected; /// True if this page has been selected.

bool _fileChanged; // page was reloaded by the filemonitor

// Private functions
void m_Init();

};

Expand Down
Loading

0 comments on commit 404448e

Please sign in to comment.