Commit e656daf6 authored by Ian McInerney's avatar Ian McInerney

Initial thread-safe implementation

parent 2d112843
......@@ -27,6 +27,8 @@ private:
LocalizationLogger *networkLogger;
pthread_mutex_t queueMutex;
public:
// The constructor
......
......@@ -9,6 +9,9 @@ NetworkForwarding::NetworkForwarding(int numNodes) {
this->networkLogger = NULL;
// Init the mutex
pthread_mutex_init( &(this->queueMutex), NULL);
// Create the queues for the nodes and the vectors for the edges
this->packetQueues = (std::queue<CCRTPPacket>**) malloc( sizeof(std::queue<CCRTPPacket>*)*numNodes );
this->edges = (std::vector<int>**) malloc( sizeof(std::vector<int>)*numNodes );
......@@ -22,6 +25,9 @@ NetworkForwarding::NetworkForwarding(int numNodes, edge_t *edges, int numEdges)
// Copy the variables
this->numNodes = numNodes;
// Init the mutex
pthread_mutex_init( &(this->queueMutex), NULL);
// Create the queues for the nodes and the vectors for the edges
this->packetQueues = (std::queue<CCRTPPacket>**) malloc( sizeof(std::queue<CCRTPPacket>*)*numNodes );
this->edges = (std::vector<int>**) malloc( sizeof(std::vector<int>)*numNodes );
......@@ -143,12 +149,17 @@ bool NetworkForwarding::retrievePacket(int quadNum, CCRTPPacket *pk) {
return(false);
}
// Get the data mutex
pthread_mutex_lock( &(this->queueMutex) );
// Pull the next packet from the queue
*pk = packetQueues[quadNum]->front();
packetQueues[quadNum]->pop();
std::cout << "Sending comp packet to quad " << quadNum << std::endl;
// Release the data mutex
pthread_mutex_unlock( &(this->queueMutex) );
//std::cout << "Sending comp packet to quad " << quadNum << std::endl;
return(true);
}
......@@ -159,13 +170,20 @@ bool NetworkForwarding::retrievePacket(int quadNum, CCRTPPacket *pk) {
* @param pk Pointer to the CRTP packet that was received
*/
void NetworkForwarding::packetReceived(int quadNum, CCRTPPacket *pk) {
std::cout << "Received comp packet from quad " << quadNum << std::endl;
//std::cout << "Received comp packet from quad " << quadNum << std::endl;
// Get the data mutex
pthread_mutex_lock( &(this->queueMutex) );
// Determine the quads to get the packet
for (std::vector<int>::iterator it = edges[quadNum]->begin(); it != edges[quadNum]->end(); ++it) {
// Add it to the queue of the adjacent quadcopter
packetQueues[*it]->push(*pk);
}
// Release the data mutex
pthread_mutex_unlock( &(this->queueMutex) );
// Pass the packet to the logger
if (this->networkLogger != NULL) {
this->networkLogger->packetReceived(pk);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment