Send IHU, IHave, ...
This commit is contained in:
parent
6c0e1f4d26
commit
6730d8ba85
|
@ -40,9 +40,9 @@ void Neighbours::fullUpdate() {
|
||||||
fullCheck();
|
fullCheck();
|
||||||
|
|
||||||
for(auto nei : unidirNei)
|
for(auto nei : unidirNei)
|
||||||
updateSendPackets(nei, proto);
|
updateSendPackets(nei);
|
||||||
for(auto nei : symNei)
|
for(auto nei : symNei)
|
||||||
updateSendPackets(nei, proto);
|
updateSendPackets(nei);
|
||||||
|
|
||||||
if(potentialNei.size() > 0
|
if(potentialNei.size() > 0
|
||||||
&& symNei.size() < csts::SYM_COUNT_BEFORE_PEEK
|
&& symNei.size() < csts::SYM_COUNT_BEFORE_PEEK
|
||||||
|
@ -51,6 +51,7 @@ void Neighbours::fullUpdate() {
|
||||||
auto it = potentialNei.begin();
|
auto it = potentialNei.begin();
|
||||||
for(int at=0; at < nPeerId; ++at, ++it);
|
for(int at=0; at < nPeerId; ++at, ++it);
|
||||||
proto->sendEmpty(it->id);
|
proto->sendEmpty(it->id);
|
||||||
|
lastPckSent[it->id] = time(NULL);
|
||||||
lastPeerPeek = time(NULL);
|
lastPeerPeek = time(NULL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -65,27 +66,28 @@ void Neighbours::addPotentialNei(const Neighbour& nei) {
|
||||||
|
|
||||||
void Neighbours::receivedFrom(u64 id) {
|
void Neighbours::receivedFrom(u64 id) {
|
||||||
NeiType typ = neiType[id];
|
NeiType typ = neiType[id];
|
||||||
lastRecv.insert({id, time(NULL)});
|
lastRecv[id] = time(NULL);
|
||||||
|
|
||||||
if(typ == NEI_POTENTIAL)
|
if(typ == NEI_POTENTIAL)
|
||||||
changeNeiType(id, NEI_UNIDIR);
|
changeNeiType(id, NEI_UNIDIR);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Neighbours::hadIHU(u64 id) {
|
void Neighbours::hadIHU(u64 id) {
|
||||||
NeiType typ = neiType[id];
|
NeiType typ = neiType[id];
|
||||||
lastRecv.insert({id, time(NULL)});
|
lastRecv[id] = time(NULL);
|
||||||
lastIHU.insert({id, time(NULL)});
|
lastIHU[id] = time(NULL);
|
||||||
if(typ == NEI_POTENTIAL || typ == NEI_UNIDIR)
|
if(typ == NEI_POTENTIAL || typ == NEI_UNIDIR)
|
||||||
changeNeiType(id, NEI_SYM);
|
changeNeiType(id, NEI_SYM);
|
||||||
}
|
}
|
||||||
|
|
||||||
list<Neighbour>& Neighbours::listOfType(NeiType typ) {
|
list<Neighbour>* Neighbours::listOfType(NeiType typ) {
|
||||||
switch(typ) {
|
switch(typ) {
|
||||||
case NEI_POTENTIAL:
|
case NEI_POTENTIAL:
|
||||||
return potentialNei;
|
return &potentialNei;
|
||||||
case NEI_UNIDIR:
|
case NEI_UNIDIR:
|
||||||
return unidirNei;
|
return &unidirNei;
|
||||||
case NEI_SYM:
|
case NEI_SYM:
|
||||||
return symNei;
|
return &symNei;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -93,34 +95,55 @@ list<Neighbour>& Neighbours::listOfType(NeiType typ) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Neighbours::changeNeiType(u64 id, NeiType nType) {
|
void Neighbours::changeNeiType(u64 id, NeiType nType) {
|
||||||
|
printf("TYPE %lX %d\n", id, neiType[id]);
|
||||||
NeiType cType = neiType[id];
|
NeiType cType = neiType[id];
|
||||||
if(cType == nType)
|
if(cType == nType)
|
||||||
return;
|
return;
|
||||||
list<Neighbour>& fromList = listOfType(cType), toList = listOfType(nType);
|
list<Neighbour> *fromList = listOfType(cType),
|
||||||
neiType.insert({id, nType});
|
*toList = listOfType(nType);
|
||||||
|
neiType[id] = nType;
|
||||||
|
|
||||||
bool wasSpliced=false;
|
bool wasSpliced=false;
|
||||||
for(auto it=fromList.begin(); it != fromList.end(); ++it) {
|
for(auto it=fromList->begin(); it != fromList->end(); ++it) {
|
||||||
if(it->id == id) {
|
if(it->id == id) {
|
||||||
fromList.splice(it, toList);
|
printf("%ld %ld\n", fromList->size(), toList->size());
|
||||||
|
toList->push_back(*it); // splice() doesn't work?!
|
||||||
|
fromList->erase(it);
|
||||||
|
printf("%ld %ld %d\n", fromList->size(), toList->size(),
|
||||||
|
toList == &unidirNei);
|
||||||
wasSpliced=true;
|
wasSpliced=true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(!wasSpliced) {
|
if(!wasSpliced) {
|
||||||
//TODO log error.
|
fprintf(stderr, "[ERROR] Node %lX wasn't found (type change)\n", id);
|
||||||
}
|
}
|
||||||
|
printf("TYPE %lX %d\n", id, neiType[id]);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Neighbours::updateSendPackets(const Neighbour& nei, Protocol* proto) {
|
void Neighbours::updateSendPackets(const Neighbour& nei) {
|
||||||
if(time(NULL) - lastIHUSent[nei.id] >= csts::TIME_RESEND_IHU) {
|
if(!sendIHU(nei.id))
|
||||||
lastIHUSent[nei.id] = time(NULL);
|
sendEmpty(nei.id);
|
||||||
lastPckSent[nei.id] = time(NULL);
|
}
|
||||||
proto->sendIHU(nei.id);
|
|
||||||
}
|
bool Neighbours::sendEmpty(u64 id) {
|
||||||
else if(time(NULL) - lastPckSent[nei.id] >= csts::TIME_RESEND_EMPTY) {
|
if(time(NULL) - lastPckSent[id] >= csts::TIME_RESEND_EMPTY) {
|
||||||
lastPckSent[nei.id] = time(NULL);
|
printf("[DBG] sending empty packet to %lX.\n", id);
|
||||||
proto->sendEmpty(nei.id);
|
lastPckSent[id] = time(NULL);
|
||||||
}
|
proto->sendEmpty(id);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Neighbours::sendIHU(u64 id) {
|
||||||
|
if(time(NULL) - lastIHUSent[id] >= csts::TIME_RESEND_IHU) {
|
||||||
|
printf("[DBG] sending IHU packet to %lX.\n", id);
|
||||||
|
lastIHUSent[id] = time(NULL);
|
||||||
|
lastPckSent[id] = time(NULL);
|
||||||
|
proto->sendIHU(id);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -44,9 +44,12 @@ class Neighbours {
|
||||||
NEI_UNDEF, NEI_POTENTIAL, NEI_UNIDIR, NEI_SYM
|
NEI_UNDEF, NEI_POTENTIAL, NEI_UNIDIR, NEI_SYM
|
||||||
};
|
};
|
||||||
|
|
||||||
std::list<Neighbour>& listOfType(NeiType typ);
|
std::list<Neighbour>* listOfType(NeiType typ);
|
||||||
void changeNeiType(u64 id, NeiType nType);
|
void changeNeiType(u64 id, NeiType nType);
|
||||||
void updateSendPackets(const Neighbour& nei, Protocol* proto);
|
void updateSendPackets(const Neighbour& nei);
|
||||||
|
|
||||||
|
bool sendEmpty(u64 id);
|
||||||
|
bool sendIHU(u64 id);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Protocol* proto;
|
Protocol* proto;
|
||||||
|
|
40
protocol.cpp
40
protocol.cpp
|
@ -27,6 +27,7 @@ Protocol::Protocol(const SockAddr& listenAddr, u64 selfId) :
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set socket reception timeout
|
// Set socket reception timeout
|
||||||
|
/*
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
tv.tv_sec = 0;
|
tv.tv_sec = 0;
|
||||||
tv.tv_usec = 100000;
|
tv.tv_usec = 100000;
|
||||||
|
@ -34,6 +35,7 @@ Protocol::Protocol(const SockAddr& listenAddr, u64 selfId) :
|
||||||
perror("Cannot set socket timeout");
|
perror("Cannot set socket timeout");
|
||||||
throw NwError();
|
throw NwError();
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
startPollNetwork();
|
startPollNetwork();
|
||||||
}
|
}
|
||||||
|
@ -159,14 +161,20 @@ void Protocol::startPollNetwork() {
|
||||||
|
|
||||||
void Protocol::pollNetwork() {
|
void Protocol::pollNetwork() {
|
||||||
u8 buffer[MAX_MTU];
|
u8 buffer[MAX_MTU];
|
||||||
SockAddr fromAddr6;
|
|
||||||
struct sockaddr* fromAddr = (struct sockaddr*)&fromAddr6;
|
|
||||||
socklen_t fromAddrLen;
|
|
||||||
while(!terminating) {
|
while(!terminating) {
|
||||||
|
struct sockaddr fromAddr;
|
||||||
|
memset(&fromAddr, 0, sizeof(fromAddr));
|
||||||
|
socklen_t fromAddrLen=sizeof(fromAddr);
|
||||||
ssize_t readDat = recvfrom(sock, buffer, MAX_MTU, 0,
|
ssize_t readDat = recvfrom(sock, buffer, MAX_MTU, 0,
|
||||||
fromAddr, &fromAddrLen);
|
&fromAddr, &fromAddrLen);
|
||||||
if(readDat <= 0)
|
if(readDat < 0) {
|
||||||
|
perror("[WARNING] Bad packet");
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
|
if(readDat <= 0) {
|
||||||
|
fprintf(stderr, "[WARNING] Empty packet.\n");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
Bytes data(buffer, readDat);
|
Bytes data(buffer, readDat);
|
||||||
u8 magic, version;
|
u8 magic, version;
|
||||||
data >> magic >> version;
|
data >> magic >> version;
|
||||||
|
@ -181,7 +189,8 @@ void Protocol::pollNetwork() {
|
||||||
u16 bodyLen;
|
u16 bodyLen;
|
||||||
data >> bodyLen;
|
data >> bodyLen;
|
||||||
if(data.size() < bodyLen + 8u) {
|
if(data.size() < bodyLen + 8u) {
|
||||||
fprintf(stderr, "[WARNING] Body too short\n");
|
fprintf(stderr, "[WARNING] Body too short (%lu < %d)\n",
|
||||||
|
data.size(), bodyLen+8u);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
else if(data.size() != bodyLen + 8u) {
|
else if(data.size() != bodyLen + 8u) {
|
||||||
|
@ -189,15 +198,24 @@ void Protocol::pollNetwork() {
|
||||||
}
|
}
|
||||||
|
|
||||||
SockAddr convFromAddr;
|
SockAddr convFromAddr;
|
||||||
if(fromAddr->sa_family == AF_INET)
|
if(fromAddr.sa_family == AF_INET)
|
||||||
convFromAddr = addrOfV4(*(struct sockaddr_in*)fromAddr);
|
convFromAddr = addrOfV4(*((struct sockaddr_in*)&fromAddr));
|
||||||
else if(fromAddr->sa_family == AF_INET6)
|
else if(fromAddr.sa_family == AF_INET6)
|
||||||
convFromAddr = *(SockAddr*)fromAddr;
|
convFromAddr = *(SockAddr*)&fromAddr;
|
||||||
else {
|
else {
|
||||||
fprintf(stderr, "ERROR: Unknown address family.");
|
fprintf(stderr, "[ERROR] Unknown address family %d.\n",
|
||||||
|
fromAddr.sa_family);
|
||||||
|
fprintf(stderr, "\t%hu\n", ((SockAddr*)&fromAddr)->sin6_port);
|
||||||
|
while(data.size() > 0) {
|
||||||
|
u8 c;
|
||||||
|
data >> c;
|
||||||
|
fprintf(stderr, "%02X|", c);
|
||||||
|
}
|
||||||
|
fprintf(stderr, "\n");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
puts("Received packet");
|
||||||
AvailPacket pck;
|
AvailPacket pck;
|
||||||
pck.from = convFromAddr;
|
pck.from = convFromAddr;
|
||||||
pck.data = data;
|
pck.data = data;
|
||||||
|
|
Loading…
Reference in a new issue