Commit 01394f7f authored by Ian McInerney's avatar Ian McInerney

Added start of a networking library

parent 64d68858
#ifndef NETWORKFORWARDING_H_
#define NETWORKFORWARDING_H_
#include <queue>
#include <stdlib.h>
#include "../CCRTPPacket.h"
typedef struct edge{
int node1;
int node2;
} edge_t;
class NetworkForwarding {
private:
// The edges that the network has
std::vector<int> **edges;
// The number of nodes in the network
int numNodes;
// Array to hold pointers to the queues storing the packets to send to each quadcopter
std::queue<CCRTPPacket> **packetQueues;
public:
// The constructor
NetworkForwarding(int numNodes);
NetworkForwarding(int numNodes, edge_t *edges, int numEdges);
// The destructor
~NetworkForwarding();
/*
* Add an edge to the graph
*
* @param edge The edge to add
*/
void addEdge(edge_t edge);
/**
* Remove an edge from the graph
*
* @param edge The edge to remove
*/
void removeEdge(edge_t edge);
/**
* List all the edges in the computation graph
*/
void listEdges();
/**
* Display the graph adjacency matrix
*
*/
void displayAdjacency();
/*
* Callback to be called when a packet is received from a quadcopter
*
* @param quadNum The number of the quadcopter the packet was received from
* @param pk Pointer to the CRTP packet that was received
*/
void packetReceived(int quadNum, CCRTPPacket *pk);
/**
*
*
*/
};
#endif
\ No newline at end of file
......@@ -4,6 +4,8 @@
#include "vrpn_Connection.h"
#include "vrpn_Tracker.h"
#include "CCrazyflie.h"
#include "computations/NetworkForwarding.h"
#include <iostream>
#include <fstream>
......@@ -96,6 +98,10 @@ extern RADIOS_t radios[];
// Array to hold the hand data
extern HAND_t hand;
extern int networkNumNodes;
extern int networkNumEdges;
extern edge_t networkEdges[];
extern int quad1_numTakeoffSteps;
extern takeoffProfileStep quad1_takeoffSteps[];
......
#include "computations/NetworkForwarding.h"
#include <iostream>
#include <queue>
#include "CCRTPPacket.h"
NetworkForwarding::NetworkForwarding(int numNodes) {
this->numNodes = numNodes;
// 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 );
for (int i=0; i < numNodes; i++) {
this->packetQueues[i] = new std::queue<CCRTPPacket>();
this->edges[i] = new std::vector<int>();
}
}
NetworkForwarding::NetworkForwarding(int numNodes, edge_t *edges, int numEdges) {
// Copy the variables
this->numNodes = numNodes;
// 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 );
for (int i=0; i < numNodes; i++) {
this->packetQueues[i] = new std::queue<CCRTPPacket>();
this->edges[i] = new std::vector<int>();
}
// Add the edges to the computation graph
for (int i=0; i < numEdges; i++) {
this->addEdge(edges[i]);
}
}
NetworkForwarding::~NetworkForwarding() {
// Delete each queue and then the array holding them
for (int i=0; i < this->numNodes; i++) {
delete( packetQueues[i] );
delete( edges[i] );
}
delete[] packetQueues;
delete[] edges;
}
/*
* Add an edge to the graph
*
* @param edge The edge to add
*/
void NetworkForwarding::addEdge(edge_t edge) {
int fromNode = edge.node1 - 1;
int toNode = edge.node2 - 1;
// Iterate over the edges already existing to make sure it is not a duplicate
for (std::vector<int>::iterator it = edges[fromNode]->begin(); it != edges[fromNode]->end(); ++it) {
if (*it == toNode) {
std::cout << "Edge from " << fromNode+1 << " to " << toNode+1 << " already exists" << std::endl;
return;
}
}
// Add the edge
edges[fromNode]->push_back(toNode);
std::cout << "Edge from " << fromNode+1 << " to " << toNode+1 << " added" << std::endl;
}
/**
* Remove an edge from the graph
*
* @param edge The edge to remove
*/
void NetworkForwarding::removeEdge(edge_t edge) {
int fromNode = edge.node1 - 1;
int toNode = edge.node2 - 1;
// Iterate over the edges and delete it if it exists
for (std::vector<int>::iterator it = edges[fromNode]->begin(); it != edges[fromNode]->end(); ++it) {
if (*it == toNode) {
edges[fromNode]->erase(it);
std::cout << "Edge from " << fromNode+1 << " to " << toNode+1 << " removed" << std::endl;
return;
}
}
}
/**
* List all the edges in the computation graph
*/
void NetworkForwarding::listEdges() {
std::cout << "Edges in graph" << std::endl;
for (int i=0; i < numNodes; i++) {
for (std::vector<int>::iterator it = edges[i]->begin(); it != edges[i]->end(); ++it) {
std::cout << i+1 << " to " << *it+1 << std::endl;
}
}
}
/**
* Display the graph adjacency matrix
*
*/
void NetworkForwarding::displayAdjacency() {
std::cout << "Network adjacency matrix" << std::endl;
for (int i=0; i < numNodes; i++) {
int nodeConnection[numNodes];
memset(nodeConnection, 0, sizeof(int)*numNodes);
for (std::vector<int>::iterator it = edges[i]->begin(); it != edges[i]->end(); ++it) {
nodeConnection[*it] = 1;
}
for (int j=0; j < numNodes; j++) {
if (nodeConnection[j] == 1) {
std::cout << " 1";
} else {
std::cout << " 0";
}
}
std::cout << std::endl;
}
}
\ No newline at end of file
......@@ -6,6 +6,7 @@
#include "userOutput.h"
#include "logData.h"
#include "Bridge.h"
#include "computations/NetworkForwarding.h"
#include "quadcopterData.h"
#include <sys/socket.h>
......@@ -25,6 +26,9 @@ const int buffLen = 8;
// The bridge object
Bridge *bridge;
// The computation graph
NetworkForwarding *network;
using namespace std;
void initMulticast();
......@@ -87,6 +91,19 @@ int main(int argc, char **argv) {
signal(SIGINT, &ctrlc_handler);
// Initialize the computation graph
network = new NetworkForwarding(networkNumNodes, networkEdges, networkNumEdges);
/* network->addEdge({1, 2});
network->addEdge({1, 4});
network->addEdge({2, 1});
network->addEdge({2, 3});
network->addEdge({3, 2});
network->addEdge({3, 4});
network->addEdge({4, 1});
network->addEdge({4, 3});
*/
network->displayAdjacency();
// Initialize the crazyflie radios
for (int i = 0; i < NUM_RADIOS; i++) {
cout << "Initializing Radio " << i << endl;
......@@ -249,8 +266,8 @@ int main(int argc, char **argv) {
#if USE_HAND
vrpn_init("192.168.0.120:3883", handle_hand);
#else
vrpn_init("192.168.0.120:3883", NULL);
// vrpn_init("localhost:3883", NULL);
// vrpn_init("192.168.0.120:3883", NULL);
vrpn_init("localhost:3883", NULL);
#endif // END USE_HAND
usleep(10000);
......@@ -300,8 +317,7 @@ int main(int argc, char **argv) {
pthread_create(&threads[0], NULL, UIThread, (void*)1);
pthread_create(&threads[1], NULL, vrpn_go, (void*)2);
printf("Spawning User Output Thread\n");
pthread_create(&threads[2], NULL, displayData, (void*)3);
// pthread_create(&threads[2], NULL, displayData, (void*)3);
pthread_join(threads[1], &status);
// Run the VRPN main loop
......
......@@ -2,6 +2,7 @@
#include "quadcopterData.h"
//#include "eris_vrpn.h"
#include "callbacks.h"
#include "computations/NetworkForwarding.h"
// Takeoff routine steps
......@@ -12,10 +13,23 @@ int quad1_numTakeoffSteps = 3;
takeoffProfileStep quad1_takeoffSteps[] = {
{19000, step_time, 1},
// {50000, step_height, -0.3},
{52000, step_time, 1},
{52000, step_time, 5},
{48739, step_final, 0}
};
int networkNumNodes = 4;
int networkNumEdges = 8;
edge_t networkEdges[] = {
{1, 2},
{1, 4},
{2, 1},
{2, 3},
{3, 2},
{3, 4},
{4, 1},
{4, 3}
};
// Offsets to be used during mirror flight
float mirrorOffsets[][2] = {
{ 0.00, 0.00}, // Quadcopter 1 is the master
......@@ -120,9 +134,9 @@ QUADCOPTERS_t crazyflie_info[] = {
NULL,
NULL,
// 75,
// 100,//swapped 1,2
100,//swapped 1,2
// 80,
45,
// 45,
0,
0,
// The controller data structure
......
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