From a31d9e29eb0c8805ac22f97b419e73482a2530dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophile=20Bastian?= Date: Sun, 27 Nov 2016 12:16:56 +0100 Subject: [PATCH] Dump data on RETURN ; FIX sending Packet sending was broken: active iterators could be invalidated. --- README.md | 3 +++ dataStore.cpp | 21 +++++++++++++++++++++ dataStore.h | 3 +++ main.cpp | 27 ++++++++++++++++++++++++++- neighbours.cpp | 30 ++++++++++++++++++++++++++++++ neighbours.h | 5 +++++ protocol.cpp | 10 ++++++---- protocol.h | 2 +- 8 files changed, 95 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index bfe9178..aad4313 100644 --- a/README.md +++ b/README.md @@ -33,3 +33,6 @@ données). Le programme produit des logs verbeux mais humainement lisibles sur sa sortie d'erreur (stderr). + +Le programme affiche son état actuel (voisins + infos sur eux, données + infos +sur elles) lors d'un appui sur RETURN. diff --git a/dataStore.cpp b/dataStore.cpp index 2f87249..be9002a 100644 --- a/dataStore.cpp +++ b/dataStore.cpp @@ -83,6 +83,27 @@ void DataStore::setFlooded(u64 id) { toFlood_.erase(id); } +void DataStore::dump() { + for(auto it=data.begin(); it != data.end(); ++it) { + printf(">> DATA %lX (%u) ", it->first, curSeqno[it->first]); + Bytes dat = it->second.data; + if(dat.size() < 2) { + printf("INVALID\n"); + continue; + } + u8 type, len; + dat >> type >> len; + if(type == csts::TLV_DATA_TEXT) { + char val[1024]; + dat.writeToBuffer(val, 1023); + val[min((int)dat.size(), 1023)] = '\0'; + printf("'%s'\n", val); + } + else + printf("type=%d\n", type); + } +} + void DataStore::handleExpire(u64 id, u32 seqno) { if(seqno < curSeqno[id]) return; // Was updated in time diff --git a/dataStore.h b/dataStore.h index 2a32360..89542f3 100644 --- a/dataStore.h +++ b/dataStore.h @@ -55,6 +55,9 @@ class DataStore { void setFlooded(u64 id); /** Marks a data as flooded */ + void dump(); + /** Dumps everything on STDIN */ + private: //meth void handleExpire(u64 id, u32 seqno); void handleRepublish(u64 datId); diff --git a/main.cpp b/main.cpp index dc0891f..ffb2ce0 100644 --- a/main.cpp +++ b/main.cpp @@ -20,6 +20,26 @@ bool terminate=false; +char readStdin() { + fd_set fdSet; + FD_ZERO(&fdSet); + FD_SET(STDIN_FILENO, &fdSet); + struct timeval tv = {1, 0}; + + if(select(STDIN_FILENO+1, &fdSet, NULL, NULL, &tv) < 0) { + perror("[WARNING] Bad select"); + return 0; + } + else { + if(FD_ISSET(STDIN_FILENO, &fdSet)) { + char c = getchar(); + fflush(stdin); + return c; + } + return 0; + } +} + int main(int argc, char** argv) { bool hasConfig = false; char* configFilePath = nullptr; @@ -84,7 +104,12 @@ int main(int argc, char** argv) { dataStore.update(); proto.sendAllNow(); - sleep(1); +// sleep(1); + char c = readStdin(); + if(c != 0) { + dataStore.dump(); + neighboursManager.dump(); + } } return 0; diff --git a/neighbours.cpp b/neighbours.cpp index 5a80298..5d35d63 100644 --- a/neighbours.cpp +++ b/neighbours.cpp @@ -167,6 +167,36 @@ void Neighbours::gotIHave(u64 from, u64 datId, u32 seqno) { } } +void Neighbours::dump() { + for(auto nei : potentialNei) + dumpNei(nei, NEI_POTENTIAL); + for(auto nei : unidirNei) + dumpNei(nei, NEI_UNIDIR); + for(auto nei : symNei) + dumpNei(nei, NEI_SYM); +} + +void Neighbours::dumpNei(const Neighbour& nei, NeiType type) { + printf(">> NEIGHBOUR "); + switch(type) { + case NEI_POTENTIAL: + printf("potential "); + break; + case NEI_UNIDIR: + printf("unidir "); + break; + case NEI_SYM: + printf("sym "); + break; + default: + break; + } + printf("%lX lastPck=%ld lastIHU=%ld stateMatch=%d\n", + nei.id, time(NULL) - lastRecv[nei.id], + time(NULL) - lastIHU[nei.id], + type == neiType[nei.id]); +} + void Neighbours::changeNeiType(u64 id, NeiType nType) { NeiType cType = neiType[id]; if(cType == nType) diff --git a/neighbours.h b/neighbours.h index 391733d..b30094e 100644 --- a/neighbours.h +++ b/neighbours.h @@ -52,12 +52,17 @@ class Neighbours { * this data from the peer `from`. */ + void dump(); + /** Dumps everything to STDIN. */ + private: //meth class WrongNeiType : public std::exception {}; enum NeiType { NEI_UNDEF, NEI_POTENTIAL, NEI_UNIDIR, NEI_SYM }; + void dumpNei(const Neighbour& nei, NeiType type); + std::list* listOfType(NeiType typ); void changeNeiType(u64 id, NeiType nType); void updateSendPackets(const Neighbour& nei); diff --git a/protocol.cpp b/protocol.cpp index ba6b173..33208e2 100644 --- a/protocol.cpp +++ b/protocol.cpp @@ -129,8 +129,9 @@ void Protocol::sendIHave(u64 to, u64 datId, u32 seqno) { } void Protocol::sendAllNow() { - for(auto& waiting : aggregatedTLVs) - sendNow(waiting.first); + for(const auto& it : aggregatedTLVs) + sendNow(it.first, false); + aggregatedTLVs.clear(); } void Protocol::startPollNetwork() { @@ -217,11 +218,12 @@ SockAddr Protocol::addrOfV4(const sockaddr_in& addrv4) { return out; } -void Protocol::sendNow(u64 id) { +void Protocol::sendNow(u64 id, bool erase) { if(aggregatedTLVs.find(id) == aggregatedTLVs.end()) return; // Nothing to send. Bytes aggregated = aggregatedTLVs[id]; - aggregatedTLVs.erase(id); + if(erase) + aggregatedTLVs.erase(id); Bytes pck; pck << csts::MAGIC << csts::VERSION << (u16)aggregated.size() << selfId diff --git a/protocol.h b/protocol.h index 949f1f0..139f94c 100644 --- a/protocol.h +++ b/protocol.h @@ -75,7 +75,7 @@ class Protocol { const SockAddr& addrOfId(u64 id); SockAddr addrOfV4(const sockaddr_in& addrv4); - void sendNow(u64 id); + void sendNow(u64 id, bool erase=true); /** Sends the aggregated TLVs right now. */ private: