threaddispatcher.cpp
Go to the documentation of this file.
1 
8 
9 ThreadDispatcher::ThreadDispatcher(const std::shared_ptr<Syntax>& syntax)
10 {
11  this->syntax = syntax;
12  commandResponse = std::queue<std::string>();
13  sensorGroupMessage = std::queue<std::string>();
14  wakeUpCommunication = false;
15  debugMsgEnabled = false;
17  errorCBregistered = false;
18  textCBregistered = false;
19 }
20 
22 {
23  active = true;
24  worker = std::thread(&ThreadDispatcher::workerFunction, this);
27 
28 }
29 
31 {
34  active = false;
35  wakeUp();
36  worker.join();
37 }
38 
40 {
41  this->debug = debug;
42  debugMsgEnabled = true;
43 }
44 
46 {
48 }
49 
51 {
52  while (active)
53  {
54  sleep();
55  while (!readingThread->isQueueEmpty() && active)
56  {
57  std::string data = readingThread->getData();
58  // in case of empty string
59  if (data.size() < 1)
60  continue;
61  if (debugMsgEnabled)
62  debug(data);
64  {
65  continue;
66  }
67  // check for known prefixes
68  if (data.find(syntax->cmdErrorPrefix) == 0)
69  {
71  error("UC board reported the following command error:\n"+data);
72  }
73  else if (data.find(syntax->genErrorPrefix) == 0)
74  {
76  error("UC board reported the following error:\n"+data);
77  }
78  else if (data.find(syntax->textMsgPrefix) == 0)
79  {
80  if (textCBregistered)
81  text(data);
82  }
83  else if (data.find(syntax->channelGrpMsgPrefix) == 0)
84  {
85  sensorGroupMessage.push(data);
87  }
88  else if (data.find(syntax->answerOnCmdPrefix) == 0)
89  {
90  commandResponse.push(data);
92  {
93  comCV->notify_one();
94  wakeUpCommunication = false;
95  }
96  }
97  }
98  }
99 }
100 
102 {
103  if (!active)
104  {
105  this->readingThread = rxThread;
106  }
107 }
108 
110 {
111  if (!active)
112  {
113  this->sensorGroupThread = grpThread;
114  }
115 }
116 
117 void ThreadDispatcher::setCommunicationCondVar(std::condition_variable* condVar)
118 {
119  this->comCV = condVar;
120 }
121 
123 {
124  this->error = error;
125  errorCBregistered = true;
128 
129 }
131 {
132  this->text = text;
133  textCBregistered = true;
134 }
135 
136 void ThreadDispatcher::dequeueResponse(std::string& response)
137 {
138  response = commandResponse.front();
139  commandResponse.pop();
140 }
141 
143 {
144  response = sensorGroupMessage.front();
145  sensorGroupMessage.pop();
146 }
147 
149 {
150  return commandResponse.empty();
151 }
152 
154 {
155  return sensorGroupMessage.empty();
156 }
157 
159 {
161 }
const bool IsResponseQueueEmpty() const
Is there a response in the response queue?
void registerErrorCallback(errorCallbackPtr error)
Register an error function pointer to handle errors during communication.
std::condition_variable * comCV
ReadingThread * readingThread
debugCallbackPtr error
void sleep()
Locks the current worker thread until wakeUp() is called.
The ReadingThread class implements threadded polling of the serial input buffer.
Definition: readingthread.h:31
debugCallbackPtr text
void enableDebugMessages(debugCallbackPtr debug)
Enable pass through of every line in the input buffer.
std::queue< std::string > sensorGroupMessage
void registerErrorCallback(debugCallbackPtr error)
Register a callback to handle transmission/communication errors.
Header file for the ThreadDispatcher class.
void setCommunicationCondVar(std::condition_variable *condVar)
Register the condition variable of the Communication object in order to wakeup a waiting command resp...
void setReadingThread(ReadingThread *rxThread)
Register ReadingThread with the thread dispatcher.
void stopThread()
Stop parsing incoming sensor group messages.
SensorGroupThread * sensorGroupThread
void startThread()
Start dispatching thread, SensorGroupThread and ReadingThread.
void startThread()
Start the polling of the serial input buffer..
void registerErrorCallback(errorCallbackPtr error)
Register an error callback that can be called to treat occuing communication and parsing errors...
void stopThread()
Stop the polling of the serial input buffer..
const bool IsMessageQueueEmpty() const
Is there a message in the message queue?
std::queue< std::string > commandResponse
void stopThread()
Stop dispatching thread, SensorGroupThread and ReadingThread.
void wakeUp()
Unlocks the current worker thread.
ThreadDispatcher(const std::shared_ptr< Syntax > &syntax)
ThreadDispatcher constructor.
boost::function< void(const std::string &)> debugCallbackPtr
void setSensorGroupThread(SensorGroupThread *grpThread)
Register SensorGroupThread with the thread dispatcher.
const bool isQueueEmpty() const
Is a new line availible?
void setCommunicationWakeUp(bool wakeUp)
Set wether there is a command response thread waiting or not.
std::string getData()
Get retrieved data vom the serial input buffer.
void workerFunction()
Worker function, responsible for dispatching the correct thread and observing incoming messages from ...
void startThread()
Start parsing incoming sensor group messages.
void enableRawCommunication()
Enables the transmission of raw messages to the serial device.
void dequeueResponse(std::string &response)
Pop a received command response from the response queue.
std::shared_ptr< Syntax > syntax
The SensorGroupThread class implements functionality to extract the group identifier of an incoming s...
void dequeueSensorGroupMessage(std::string &response)
Pop a received sensor group message from the message queue.
debugCallbackPtr debug
void registerTextCallback(debugCallbackPtr text)
Register a callback to handle plain text messages from the serial device.


pses_ucbridge
Author(s): Sebastian Ehmes
autogenerated on Sat Oct 28 2017 19:16:13