M1-nw-project/neighbours.h

63 lines
1.7 KiB
C
Raw Normal View History

/***************************************************************************
* By Théophile Bastian, 2017
* M1 Network course project at ENS Cachan, Juliusz Chroboczek.
* License: WTFPL v2 <http://www.wtfpl.net/>
**************************************************************************/
#pragma once
#include <list>
#include <ctime>
#include <unordered_map>
#include "data.h"
#include "protocol.h"
class Neighbours {
public:
Neighbours(Protocol* proto);
void fullCheck();
/** Cleans the peers lists by removing the expired entries. */
void fullUpdate();
/** Triggers a full update of the peers lists: sends the appropriate
* packets to the peers (IHU, ...) when approaching expiracy, and
* performs a `fullCheck()`.
*/
void addPotentialNei(const Neighbour& nei);
/** Adds a `Neighbour` to the list of potential neighbours. */
void receivedFrom(u64 id);
/** Signals that a packet was received from `id`, performs the
* appropriate bookkeeping actions.
*/
void hadIHU(u64 id);
/** Signals that a IHU was received from `id`, performs the
* appropriate bookkeeping actions.
*/
private: //meth
class WrongNeiType : public std::exception {};
enum NeiType {
NEI_UNDEF, NEI_POTENTIAL, NEI_UNIDIR, NEI_SYM
};
2016-11-25 15:47:35 +01:00
std::list<Neighbour>* listOfType(NeiType typ);
void changeNeiType(u64 id, NeiType nType);
2016-11-25 15:47:35 +01:00
void updateSendPackets(const Neighbour& nei);
bool sendEmpty(u64 id);
bool sendIHU(u64 id);
private:
Protocol* proto;
std::list<Neighbour> potentialNei, unidirNei, symNei;
std::unordered_map<u64, time_t> lastRecv, lastIHU;
std::unordered_map<u64, time_t> lastPckSent, lastIHUSent;
std::unordered_map<u64, NeiType> neiType;
time_t lastPeerPeek;
};