SLProject  4.3.020
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SENSRecorderDataHandler< T > Class Template Referenceabstract

#include <SENSRecorderDataHandler.h>

Public Member Functions

 SENSRecorderDataHandler (const std::string &name)
 
virtual ~SENSRecorderDataHandler ()
 
void start (const std::string &outputDir)
 start the store thread More...
 
void stop ()
 stop the store thread and clear values in queue More...
 
void add (T &&item)
 add new value to store queue More...
 
bool getErrorMsg (std::string &msg)
 get error msg (valid if function returns true) More...
 

Protected Member Functions

virtual void writeOnThreadStart ()
 called in thread store routine when thread starts More...
 
virtual void writeHeaderToFile (ofstream &file)
 called in thread store routine when file could be opened More...
 
virtual void writeLineToFile (ofstream &file, const T &data)=0
 called in thread store routine for every new line More...
 
virtual void writeOnThreadFinish ()
 called in thread store routine when thread finished More...
 

Protected Attributes

std::string _outputDir
 output directory More...
 

Private Member Functions

void store ()
 

Private Attributes

std::deque< T > _queue
 
std::mutex _mutex
 
std::condition_variable _condVar
 
std::thread _thread
 
bool _stop = false
 
std::string _name
 
std::mutex _msgMutex
 
std::string _errorMsg
 

Detailed Description

template<typename T>
class SENSRecorderDataHandler< T >

SENSRecorderDataHandler This class is meant to be used exclusively by the SENSRecorder class. The SENSRecorder listens to sensors and informs SENSRecorderDataHandler backends about new data. The SENSRecorderDataHandler stores values to file.

Definition at line 32 of file SENSRecorderDataHandler.h.

Constructor & Destructor Documentation

◆ SENSRecorderDataHandler()

template<typename T >
SENSRecorderDataHandler< T >::SENSRecorderDataHandler ( const std::string &  name)

Definition at line 5 of file SENSRecorderDataHandler.cpp.

6  : _name(name)
7 {
8 }

◆ ~SENSRecorderDataHandler()

template<typename T >
SENSRecorderDataHandler< T >::~SENSRecorderDataHandler
virtual

Definition at line 11 of file SENSRecorderDataHandler.cpp.

12 {
13  stop();
14 }
void stop()
stop the store thread and clear values in queue

Member Function Documentation

◆ add()

template<typename T >
void SENSRecorderDataHandler< T >::add ( T &&  item)

add new value to store queue

Definition at line 101 of file SENSRecorderDataHandler.cpp.

102 {
103  std::unique_lock<std::mutex> lock(_mutex);
104  _queue.push_back(std::move(item));
105  lock.unlock();
106  _condVar.notify_one();
107 }
std::condition_variable _condVar

◆ getErrorMsg()

template<typename T >
bool SENSRecorderDataHandler< T >::getErrorMsg ( std::string &  msg)

get error msg (valid if function returns true)

Definition at line 110 of file SENSRecorderDataHandler.cpp.

111 {
112  std::lock_guard<std::mutex> lock(_msgMutex);
113  if (_errorMsg.empty())
114  return false;
115  else
116  {
117  msg = _errorMsg;
118  return true;
119  }
120 }

◆ start()

template<typename T >
void SENSRecorderDataHandler< T >::start ( const std::string &  outputDir)

start the store thread

Definition at line 17 of file SENSRecorderDataHandler.cpp.

18 {
19  stop();
20  //start writer thread
21  _errorMsg.clear();
22  _outputDir = outputDir;
23  _thread = std::thread(&SENSRecorderDataHandler::store, this);
24 }
std::string _outputDir
output directory

◆ stop()

template<typename T >
void SENSRecorderDataHandler< T >::stop

stop the store thread and clear values in queue

Definition at line 27 of file SENSRecorderDataHandler.cpp.

28 {
29  std::unique_lock<std::mutex> lock(_mutex);
30  _stop = true;
31  lock.unlock();
32  _condVar.notify_one();
33 
34  if (_thread.joinable())
35  _thread.join();
36 
37  lock.lock();
38  _queue.clear();
39  _stop = false;
40 }

◆ store()

template<typename T >
void SENSRecorderDataHandler< T >::store
private

Definition at line 43 of file SENSRecorderDataHandler.cpp.

44 {
45  SENS_DEBUG("SENSRecorderDataHandler: starting store");
47  //open file
48  std::string fileName = _outputDir + _name + ".txt";
49  ofstream file;
50  file.open(fileName);
51  if (file.is_open())
52  {
53  writeHeaderToFile(file);
54  while (true)
55  {
56  std::unique_lock<std::mutex> lock(_mutex);
57 
58  _condVar.wait(lock, [&]
59  { return (_stop == true || _queue.size() != 0); });
60  if (_stop)
61  break;
62 
63  SENS_DEBUG("SENSRecorderDataHandler: queue size: %d", _queue.size());
64 
65  std::deque<T> localQueue;
66  while (_queue.size())
67  {
68  localQueue.push_back(_queue.front());
69  _queue.pop_front();
70  }
71 
72  lock.unlock();
73 
74  //write data
75  if (localQueue.size())
76  {
77  writeLineToFile(file, localQueue.front());
78  localQueue.pop_front();
79  }
80 
81  if (localQueue.size())
82  {
83  std::stringstream ss;
84  ss << "Data writing is too slow. Skipping " << localQueue.size() << " values from queue!";
85  SENS_WARN("SENSRecorderDataHandler store: %s", ss.str().c_str());
86  //if buffer is running full, warn and skip values
87  {
88  std::lock_guard<std::mutex> lock(_msgMutex);
89  _errorMsg = ss.str();
90  }
91  localQueue.clear();
92  }
93  }
94  }
96  //close file
97  file.close();
98 }
#define SENS_DEBUG(...)
Definition: SENS.h:60
#define SENS_WARN(...)
Definition: SENS.h:64
virtual void writeOnThreadFinish()
called in thread store routine when thread finished
virtual void writeOnThreadStart()
called in thread store routine when thread starts
virtual void writeLineToFile(ofstream &file, const T &data)=0
called in thread store routine for every new line
virtual void writeHeaderToFile(ofstream &file)
called in thread store routine when file could be opened

◆ writeHeaderToFile()

template<typename T >
virtual void SENSRecorderDataHandler< T >::writeHeaderToFile ( ofstream &  file)
inlineprotectedvirtual

called in thread store routine when file could be opened

Definition at line 51 of file SENSRecorderDataHandler.h.

51 {}

◆ writeLineToFile()

template<typename T >
virtual void SENSRecorderDataHandler< T >::writeLineToFile ( ofstream &  file,
const T &  data 
)
protectedpure virtual

called in thread store routine for every new line

Implemented in SENSOrientationRecorderDataHandler, SENSGpsRecorderDataHandler, and SENSCameraRecorderDataHandler.

◆ writeOnThreadFinish()

template<typename T >
virtual void SENSRecorderDataHandler< T >::writeOnThreadFinish ( )
inlineprotectedvirtual

called in thread store routine when thread finished

Reimplemented in SENSCameraRecorderDataHandler.

Definition at line 55 of file SENSRecorderDataHandler.h.

55 {}

◆ writeOnThreadStart()

template<typename T >
virtual void SENSRecorderDataHandler< T >::writeOnThreadStart ( )
inlineprotectedvirtual

called in thread store routine when thread starts

Reimplemented in SENSCameraRecorderDataHandler.

Definition at line 49 of file SENSRecorderDataHandler.h.

49 {}

Member Data Documentation

◆ _condVar

template<typename T >
std::condition_variable SENSRecorderDataHandler< T >::_condVar
private

Definition at line 68 of file SENSRecorderDataHandler.h.

◆ _errorMsg

template<typename T >
std::string SENSRecorderDataHandler< T >::_errorMsg
private

Definition at line 77 of file SENSRecorderDataHandler.h.

◆ _msgMutex

template<typename T >
std::mutex SENSRecorderDataHandler< T >::_msgMutex
private

Definition at line 76 of file SENSRecorderDataHandler.h.

◆ _mutex

template<typename T >
std::mutex SENSRecorderDataHandler< T >::_mutex
private

Definition at line 67 of file SENSRecorderDataHandler.h.

◆ _name

template<typename T >
std::string SENSRecorderDataHandler< T >::_name
private

Definition at line 74 of file SENSRecorderDataHandler.h.

◆ _outputDir

template<typename T >
std::string SENSRecorderDataHandler< T >::_outputDir
protected

output directory

Definition at line 58 of file SENSRecorderDataHandler.h.

◆ _queue

template<typename T >
std::deque<T> SENSRecorderDataHandler< T >::_queue
private

Definition at line 65 of file SENSRecorderDataHandler.h.

◆ _stop

template<typename T >
bool SENSRecorderDataHandler< T >::_stop = false
private

Definition at line 72 of file SENSRecorderDataHandler.h.

◆ _thread

template<typename T >
std::thread SENSRecorderDataHandler< T >::_thread
private

Definition at line 70 of file SENSRecorderDataHandler.h.


The documentation for this class was generated from the following files: