M1-nw-project/main.cpp

89 lines
1.9 KiB
C++
Raw Normal View History

2016-11-18 10:56:51 +01:00
/***************************************************************************
* By Théophile Bastian, 2017
* M1 Network course project at ENS Cachan, Juliusz Chroboczek.
* License: WTFPL v2 <http://www.wtfpl.net/>
**************************************************************************/
#include "data.h"
2016-11-20 00:16:13 +01:00
#include "protocol.h"
#include "nw_constants.h"
#include "neighbours.h"
2016-11-24 14:39:03 +01:00
#include "packetParser.h"
2016-11-20 00:16:13 +01:00
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <unistd.h>
2016-11-20 00:16:13 +01:00
int main(int /*argc*/, char** /*argv*/) {
srand(time(NULL)+42);
SockAddr addr;
memset(&addr, 0, sizeof(addr));
addr.sin6_family = AF_INET6;
addr.sin6_port = htons(csts::DEFAULT_PORT);
2016-11-20 00:16:13 +01:00
u64 myId=0;
for(int i=0; i < 8; i++) {
myId <<= 8;
myId += rand() % (1<<8);
}
printf("%lu\n", myId);
2016-11-20 00:16:13 +01:00
SockAddr jch_addr;
memset(&jch_addr, 0, sizeof(jch_addr));
jch_addr.sin6_family = AF_INET6;
jch_addr.sin6_port = htons(1212);
2016-11-20 00:23:48 +01:00
int rc = inet_pton(AF_INET6, "::FFFF:81.194.27.155", &jch_addr.sin6_addr);
if(rc != 1) {
if(rc == 0)
fprintf(stderr, "Address uses an invalid format.\n");
else
perror("Cannot convert JCh address");
2016-11-20 00:16:13 +01:00
exit(1);
}
u64 jch_id = 0x43e3a5e0;
jch_id <<= 32;
jch_id += 0x10095a0f;
2016-11-18 10:56:51 +01:00
2016-11-20 00:16:13 +01:00
Protocol proto(addr, myId);
Neighbours neighboursManager(&proto);
neighboursManager.addPotentialNei(Neighbour(jch_id, jch_addr));
2016-11-24 14:39:03 +01:00
PacketParser pckParser(&neighboursManager, &proto);
// proto.sendEmpty(loc_addr);
// proto.sendEmpty(jch_addr);
while(true) {
neighboursManager.fullUpdate();
2016-11-24 14:39:03 +01:00
while(proto.readyRead()) {
SockAddr fromAddr;
Bytes pck = proto.getPacket(&fromAddr);
pckParser.parse(pck);
}
sleep(2);
}
/*
SockAddr loc_addr;
memset(&loc_addr, 0, sizeof(loc_addr));
loc_addr.sin6_family = AF_INET6;
loc_addr.sin6_port = htons(1212);
rc = inet_pton(AF_INET6, "::FFFF:127.0.0.1", &loc_addr.sin6_addr);
if(rc != 1) {
fprintf(stderr, "Error.");
exit(1);
}
while(true);
*/
2016-11-20 00:16:13 +01:00
return 0;
2016-11-18 10:56:51 +01:00
}