Compare commits
4 commits
86c91f7488
...
574bef34e2
Author | SHA1 | Date | |
---|---|---|---|
574bef34e2 | |||
676a3822ed | |||
e8a2c37d96 | |||
619ea6969e |
9 changed files with 66 additions and 47 deletions
2
Makefile
2
Makefile
|
@ -1,5 +1,5 @@
|
|||
CXX=g++
|
||||
CXXFLAGS=-Wall -Wextra -Werror -pedantic -std=c++14 -O0 -g
|
||||
CXXFLAGS=-Wall -Wextra -Werror -pedantic -std=c++14 -O2
|
||||
CXXLIBS=-lpthread
|
||||
|
||||
OBJS = Bytes.o main.o protocol.o neighbours.o packetParser.o configFile.o \
|
||||
|
|
34
README.md
34
README.md
|
@ -1,3 +1,35 @@
|
|||
# M1-nw-project
|
||||
|
||||
Master 1 network course project <https://tobast.fr/m1/nw-project.pdf>
|
||||
Projet de cours "réseau", M1 <https://tobast.fr/m1/nw-project.pdf>
|
||||
|
||||
## Dépendances
|
||||
|
||||
* `g++` (version supportant c++14) ou tout autre compilateur c++ (éditer le
|
||||
Makefile pour changer `CXX`)
|
||||
* Bibliothèques standard POSIX
|
||||
|
||||
## Compiler
|
||||
|
||||
Lancer simplement `make`.
|
||||
|
||||
## Lancer
|
||||
|
||||
Le programme fourni prend en argument le chemin vers un fichier de
|
||||
configuration, dont chaque ligne commence par un mot-clé suivi de ses
|
||||
arguments.
|
||||
|
||||
* `id [ID du programme]` : laisser vide par défaut, sera généré
|
||||
automatiquement.
|
||||
* `bootstrap [ID du nœud] [adresse IPv6 du nœud] [port]` : déclare le nœud
|
||||
comme nœud de bootstrap. L'adresse peut être IPv4-mapped, eg.
|
||||
`::FFFF:42.42.42.42`.
|
||||
* `data [id de donnée] [donnée]` : déclare une donnée à propager. Si l'id est
|
||||
0, il sera tiré au hasard puis enregistré. La donnée peut contenir des
|
||||
espaces, et s'étend jusqu'à la fin de la ligne.
|
||||
|
||||
Le programme, à l'initialisation, lit le fichier puis le réécrit avec
|
||||
éventuellement des données tirées au hasard si nécessaire (eg. ID du nœud, des
|
||||
données).
|
||||
|
||||
Le programme produit des logs verbeux mais humainement lisibles sur sa sortie
|
||||
d'erreur (stderr).
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
#include "dataStore.h"
|
||||
#include <cstring>
|
||||
#include <algorithm>
|
||||
#include <cstdlib>
|
||||
using namespace std;
|
||||
|
||||
DataStore::DataStore(Protocol* proto) : proto(proto)
|
||||
|
@ -17,7 +18,7 @@ DataStore::~DataStore() {
|
|||
|
||||
void DataStore::update() {
|
||||
while(!timeEvents.empty()) {
|
||||
const TimeEvent& evt = timeEvents.top();
|
||||
TimeEvent evt = timeEvents.top();
|
||||
if(evt.time > time(NULL)) // We're done for now.
|
||||
break;
|
||||
|
||||
|
@ -89,12 +90,18 @@ void DataStore::handleExpire(u64 id, u32 seqno) {
|
|||
cleanData(id);
|
||||
}
|
||||
|
||||
void DataStore::handleRepublish(u64 id) {
|
||||
if(data.find(id) == data.end() || !data[id].isMine)
|
||||
void DataStore::handleRepublish(u64 datId) {
|
||||
if(data.find(datId) == data.end() || !data[datId].isMine) {
|
||||
return;
|
||||
}
|
||||
|
||||
curSeqno[id] = time(NULL);
|
||||
handleFlood(id);
|
||||
curSeqno[datId] = time(NULL);
|
||||
|
||||
timeEvents.push(TimeEvent(
|
||||
time(NULL) + csts::TIME_REPUBLISH_DATA,
|
||||
curSeqno[datId], datId, EV_REPUBLISH));
|
||||
|
||||
handleFlood(datId);
|
||||
}
|
||||
|
||||
void DataStore::handleFlood(u64 id) {
|
||||
|
|
|
@ -57,7 +57,7 @@ class DataStore {
|
|||
|
||||
private: //meth
|
||||
void handleExpire(u64 id, u32 seqno);
|
||||
void handleRepublish(u64 id);
|
||||
void handleRepublish(u64 datId);
|
||||
void handleFlood(u64 id);
|
||||
|
||||
void cleanData(u64 id);
|
||||
|
|
3
main.cpp
3
main.cpp
|
@ -56,7 +56,8 @@ int main(int argc, char** argv) {
|
|||
for(u8 chr : dat.second)
|
||||
pck << chr;
|
||||
dataStore.addData(pck, time(NULL), dat.first, true);
|
||||
fprintf(stderr, "[INFO] Adding data `%s`\n", dat.second.c_str());
|
||||
fprintf(stderr, "[INFO] Adding data `%s` (%lX)\n",
|
||||
dat.second.c_str(), dat.first);
|
||||
}
|
||||
|
||||
Neighbours neighboursManager(&proto, &dataStore);
|
||||
|
|
|
@ -94,6 +94,7 @@ void Neighbours::fullUpdate() {
|
|||
void Neighbours::addPotentialNei(const Neighbour& nei) {
|
||||
if(neiType.find(nei.id) != neiType.end())
|
||||
return; // We already know him
|
||||
fprintf(stderr, "[INFO] Adding potential neighbour %lX\n", nei.id);
|
||||
potentialNei.push_back(nei);
|
||||
lastRecv.insert({nei.id, 0});
|
||||
lastIHU.insert({nei.id, 0});
|
||||
|
|
|
@ -14,6 +14,11 @@ PacketParser::PacketParser(Neighbours* nei, Protocol* proto,
|
|||
|
||||
void PacketParser::parse(Bytes pck, const SockAddr& addr) {
|
||||
u64 peerId;
|
||||
if(pck.size() < 8) {
|
||||
fprintf(stderr, "[WARNING] Bad packet size.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
pck >> peerId;
|
||||
neighbours->receivedFrom(peerId, addr);
|
||||
|
||||
|
@ -24,6 +29,10 @@ void PacketParser::parse(Bytes pck, const SockAddr& addr) {
|
|||
|
||||
void PacketParser::readTLV(Bytes& pck, u64 nei, const SockAddr& addr) {
|
||||
u8 type, len;
|
||||
if(pck.size() < 2) {
|
||||
fprintf(stderr, "[WARNING] Bad packet size from %lX.\n", nei);
|
||||
return;
|
||||
}
|
||||
pck >> type >> len;
|
||||
|
||||
fprintf(stderr, "[INFO] Parsing %d from %lX.\n", type, nei);
|
||||
|
@ -86,8 +95,6 @@ void PacketParser::receiveNeigh(Bytes& pck) {
|
|||
if(id == protocol->getSelfId())
|
||||
continue;
|
||||
|
||||
fprintf(stderr, "[INFO] Adding neighbour %lX\n", id);
|
||||
|
||||
Neighbour nei(id, addr);
|
||||
neighbours->addPotentialNei(nei);
|
||||
}
|
||||
|
|
35
protocol.cpp
35
protocol.cpp
|
@ -83,33 +83,23 @@ void Protocol::sendBody(const Bytes& body, const u64& id) {
|
|||
return sendBody(body, addrOfId(id));
|
||||
}
|
||||
|
||||
void Protocol::sendEmpty(const SockAddr& to) {
|
||||
void Protocol::sendEmpty(u64 to) {
|
||||
sendBody(Bytes(), to);
|
||||
}
|
||||
void Protocol::sendEmpty(u64 to) {
|
||||
sendEmpty(addrOfId(to));
|
||||
}
|
||||
|
||||
void Protocol::sendIHU(const SockAddr& to, u64 id) {
|
||||
void Protocol::sendIHU(u64 id) {
|
||||
Bytes pck;
|
||||
pck << csts::TLV_IHU << (u8) 8 << id;
|
||||
sendBody(pck, to);
|
||||
}
|
||||
void Protocol::sendIHU(u64 id) {
|
||||
sendIHU(addrOfId(id), id);
|
||||
sendBody(pck, id);
|
||||
}
|
||||
|
||||
void Protocol::sendNReq(const SockAddr& to) {
|
||||
void Protocol::sendNReq(u64 to) {
|
||||
Bytes pck;
|
||||
pck << csts::TLV_NR << (u8) 0;
|
||||
sendBody(pck, to);
|
||||
}
|
||||
void Protocol::sendNReq(u64 id) {
|
||||
sendNReq(addrOfId(id));
|
||||
}
|
||||
|
||||
void Protocol::sendNeighbours(const SockAddr& to,
|
||||
const std::vector<Neighbour>& neigh) {
|
||||
void Protocol::sendNeighbours(u64 to, const std::vector<Neighbour>& neigh) {
|
||||
Bytes pck;
|
||||
u8 len = neigh.size() * (8+16+2);
|
||||
pck << csts::TLV_NEIGH << len;
|
||||
|
@ -124,11 +114,8 @@ void Protocol::sendNeighbours(const SockAddr& to,
|
|||
|
||||
sendBody(pck, to);
|
||||
}
|
||||
void Protocol::sendNeighbours(u64 id, const std::vector<Neighbour>& neigh) {
|
||||
sendNeighbours(addrOfId(id), neigh);
|
||||
}
|
||||
|
||||
void Protocol::sendData(const SockAddr& to, const Bytes& data,
|
||||
void Protocol::sendData(u64 to, const Bytes& data,
|
||||
u32 seqno, u64 datId)
|
||||
{
|
||||
Bytes pck;
|
||||
|
@ -138,20 +125,12 @@ void Protocol::sendData(const SockAddr& to, const Bytes& data,
|
|||
<< data;
|
||||
sendBody(pck, to);
|
||||
}
|
||||
void Protocol::sendData(u64 id, const Bytes& data, u32 seqno,
|
||||
u64 datId)
|
||||
{
|
||||
sendData(addrOfId(id), data, seqno, datId);
|
||||
}
|
||||
|
||||
void Protocol::sendIHave(const SockAddr& to, u64 datId, u32 seqno) {
|
||||
void Protocol::sendIHave(u64 to, u64 datId, u32 seqno) {
|
||||
Bytes pck;
|
||||
pck << csts::TLV_IHAVE << (u8) 12 << seqno << datId;
|
||||
sendBody(pck, to);
|
||||
}
|
||||
void Protocol::sendIHave(u64 id, u64 datId, u32 seqno) {
|
||||
sendIHave(addrOfId(id), datId, seqno);
|
||||
}
|
||||
|
||||
void Protocol::startPollNetwork() {
|
||||
pollThread = std::thread([this] { this->pollNetwork(); });
|
||||
|
|
|
@ -44,28 +44,20 @@ class Protocol {
|
|||
void sendBody(const Bytes& body, const u64& id);
|
||||
/** Sends the given `body` (wrapped in headers) */
|
||||
|
||||
void sendEmpty(const SockAddr& to);
|
||||
void sendEmpty(u64 to);
|
||||
|
||||
void sendIHU(const SockAddr& to, u64 id);
|
||||
void sendIHU(u64 id);
|
||||
/** Sends a IHU packet */
|
||||
|
||||
void sendNReq(const SockAddr& to);
|
||||
void sendNReq(u64 id);
|
||||
/** Sends a neighbour request packet */
|
||||
|
||||
void sendNeighbours(const SockAddr& to,
|
||||
const std::vector<Neighbour>& neigh);
|
||||
void sendNeighbours(u64 id,
|
||||
const std::vector<Neighbour>& neigh);
|
||||
/** Sends a neighbours list packet */
|
||||
|
||||
void sendData(const SockAddr& to, const Bytes& data,
|
||||
u32 seqno, u64 datId);
|
||||
void sendData(u64 id, const Bytes& data, u32 seqno, u64 datId);
|
||||
|
||||
void sendIHave(const SockAddr& to, u64 datId, u32 seqno);
|
||||
void sendIHave(u64 id, u64 datId, u32 seqno);
|
||||
/** Sends a IHave packet for `datId` */
|
||||
|
||||
|
|
Loading…
Reference in a new issue