M1-nw-project/main.cpp

71 lines
1.6 KiB
C++

/***************************************************************************
* By Théophile Bastian, 2017
* M1 Network course project at ENS Cachan, Juliusz Chroboczek.
* License: WTFPL v2 <http://www.wtfpl.net/>
**************************************************************************/
#include "data.h"
#include "protocol.h"
#include "nw_constants.h"
#include "neighbours.h"
#include "packetParser.h"
#include "configFile.h"
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <unistd.h>
int main(int argc, char** argv) {
bool hasConfig = false;
char* configFilePath = nullptr;
if(argc > 1) {
hasConfig = true;
configFilePath = argv[1];
}
srand(time(NULL)+42);
ConfigFile cfg;
if(hasConfig) {
if(!cfg.read(configFilePath) || !cfg.write(configFilePath)) {
fprintf(stderr, "Could not read/write on '%s'.\n", configFilePath);
exit(1);
}
}
SockAddr addr;
memset(&addr, 0, sizeof(addr));
addr.sin6_family = AF_INET6;
addr.sin6_port = htons(csts::DEFAULT_PORT);
printf("%lX\n", cfg.getSelfId());
Protocol proto(addr, cfg.getSelfId());
Neighbours neighboursManager(&proto);
for(const Neighbour& nei : cfg.getBootstrapNodes()) {
char addr[54];
inet_ntop(AF_INET6, &nei.addr.sin6_addr, addr, 54);
printf("Neigh: %lX [%s]:%hu\n", nei.id, addr,
ntohs(nei.addr.sin6_port));
neighboursManager.addPotentialNei(nei);
}
PacketParser pckParser(&neighboursManager, &proto);
while(true) {
neighboursManager.fullUpdate();
while(proto.readyRead()) {
SockAddr fromAddr;
Bytes pck = proto.getPacket(&fromAddr);
pckParser.parse(pck);
}
sleep(2);
}
return 0;
}