Improve receiveFromMon and serverTask

This commit is contained in:
Yohan Simard 2021-03-15 10:51:03 +01:00
parent 7fcc183012
commit 3a6ae4db19
4 changed files with 78 additions and 63 deletions

View file

@ -100,7 +100,10 @@ int ComMonitor::Open(int port) {
throw std::runtime_error{"Can not bind socket"}; throw std::runtime_error{"Can not bind socket"};
} }
listen(socketFD, 1); if (listen(socketFD, 1) < 0) {
cerr<<"["<<__PRETTY_FUNCTION__<<"] Can not listen on port "<<to_string(port)<<endl<<flush;
throw std::runtime_error{"Can not listen on this port"};
}
return socketFD; return socketFD;
} }

View file

@ -23,7 +23,7 @@
using namespace std; using namespace std;
#define SERVER_PORT 5544 constexpr int SERVER_PORT = 5544;
/** /**
* Class used for generating a server and communicating through it with monitor * Class used for generating a server and communicating through it with monitor

View file

@ -220,9 +220,7 @@ void Tasks::Join() {
/** /**
* @brief Thread handling server communication with the monitor. * @brief Thread handling server communication with the monitor.
*/ */
void Tasks::ServerTask(void *arg) { [[noreturn]] void Tasks::ServerTask(void *arg) {
int status;
cout << "Start " << __PRETTY_FUNCTION__ << endl; cout << "Start " << __PRETTY_FUNCTION__ << endl;
// Synchronization barrier (waiting that all tasks are started) // Synchronization barrier (waiting that all tasks are started)
rt_sem_p(&sem_barrier, TM_INFINITE); rt_sem_p(&sem_barrier, TM_INFINITE);
@ -230,19 +228,28 @@ void Tasks::ServerTask(void *arg) {
/* ************************************************************************************** /* **************************************************************************************
* The task server starts here * The task server starts here
* *************************************************************************************/ * *************************************************************************************/
while (true) {
rt_mutex_acquire(&mutex_monitor, TM_INFINITE); rt_mutex_acquire(&mutex_monitor, TM_INFINITE);
status = monitor.Open(SERVER_PORT); int status = monitor.Open(SERVER_PORT);
rt_mutex_release(&mutex_monitor); rt_mutex_release(&mutex_monitor);
cout << "Open server on port " << (SERVER_PORT) << " (" << status << ")" << endl; cout << "Open server on port " << SERVER_PORT << " (" << status << ")" << endl;
if (status < 0) {
throw runtime_error{"Unable to start server on port " + to_string(SERVER_PORT)};
}
if (status < 0)
throw runtime_error{
"Unable to start server on port " + to_string(SERVER_PORT)
};
monitor.AcceptClient(); // Wait the monitor client monitor.AcceptClient(); // Wait the monitor client
cout << "Rock'n'Roll baby, client accepted!" << endl; cout << "Rock'n'Roll baby, client accepted!" << endl;
rt_sem_broadcast(&sem_serverOk); rt_sem_broadcast(&sem_serverOk);
// Wait for stopServer signal
rt_sem_p(&sem_stopServer, TM_INFINITE);
rt_mutex_acquire(&mutex_monitor, TM_INFINITE);
monitor.Close();
rt_mutex_release(&mutex_monitor);
}
} }
/** /**
@ -283,10 +290,13 @@ void Tasks::ServerTask(void *arg) {
/* ************************************************************************************* /* *************************************************************************************
* The task receiveFromMon starts here * The task receiveFromMon starts here
* *************************************************************************************/ * *************************************************************************************/
while (true) {
rt_sem_p(&sem_serverOk, TM_INFINITE); rt_sem_p(&sem_serverOk, TM_INFINITE);
cout << "Received message from monitor activated" << endl; cout << "Received message from monitor activated" << endl;
while (true) { bool lostConnection = false;
while (!lostConnection) {
msgRcv = monitor.Read(); msgRcv = monitor.Read();
cout << "Rcv <= " << msgRcv->ToString() << endl; cout << "Rcv <= " << msgRcv->ToString() << endl;
@ -296,6 +306,7 @@ void Tasks::ServerTask(void *arg) {
rt_sem_v(&sem_stopRobot); rt_sem_v(&sem_stopRobot);
rt_sem_v(&sem_stopComRobot); rt_sem_v(&sem_stopComRobot);
rt_sem_v(&sem_stopServer); rt_sem_v(&sem_stopServer);
lostConnection = true;
} else if (msgRcv->CompareID(MESSAGE_ROBOT_COM_OPEN)) { } else if (msgRcv->CompareID(MESSAGE_ROBOT_COM_OPEN)) {
rt_sem_v(&sem_openComRobot); rt_sem_v(&sem_openComRobot);
} else if (msgRcv->CompareID(MESSAGE_ROBOT_START_WITHOUT_WD)) { } else if (msgRcv->CompareID(MESSAGE_ROBOT_START_WITHOUT_WD)) {
@ -336,6 +347,7 @@ void Tasks::ServerTask(void *arg) {
delete (msgRcv); // mus be deleted manually, no consumer delete (msgRcv); // mus be deleted manually, no consumer
} }
} }
}
/** /**
* @brief Thread opening communication with the robot. * @brief Thread opening communication with the robot.

View file

@ -116,7 +116,7 @@ private:
/** /**
* @brief Thread handling server communication with the monitor. * @brief Thread handling server communication with the monitor.
*/ */
void ServerTask(void *arg); [[noreturn]] void ServerTask(void *arg);
/** /**
* @brief Thread sending data to monitor. * @brief Thread sending data to monitor.