Dump data on RETURN ; FIX sending

Packet sending was broken: active iterators could be invalidated.
This commit is contained in:
Théophile Bastian 2016-11-27 12:16:56 +01:00
parent 32b56c6c91
commit a31d9e29eb
8 changed files with 95 additions and 6 deletions

View file

@ -33,3 +33,6 @@ données).
Le programme produit des logs verbeux mais humainement lisibles sur sa sortie Le programme produit des logs verbeux mais humainement lisibles sur sa sortie
d'erreur (stderr). d'erreur (stderr).
Le programme affiche son état actuel (voisins + infos sur eux, données + infos
sur elles) lors d'un appui sur RETURN.

View file

@ -83,6 +83,27 @@ void DataStore::setFlooded(u64 id) {
toFlood_.erase(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) { void DataStore::handleExpire(u64 id, u32 seqno) {
if(seqno < curSeqno[id]) if(seqno < curSeqno[id])
return; // Was updated in time return; // Was updated in time

View file

@ -55,6 +55,9 @@ class DataStore {
void setFlooded(u64 id); void setFlooded(u64 id);
/** Marks a data as flooded */ /** Marks a data as flooded */
void dump();
/** Dumps everything on STDIN */
private: //meth private: //meth
void handleExpire(u64 id, u32 seqno); void handleExpire(u64 id, u32 seqno);
void handleRepublish(u64 datId); void handleRepublish(u64 datId);

View file

@ -20,6 +20,26 @@
bool terminate=false; 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) { int main(int argc, char** argv) {
bool hasConfig = false; bool hasConfig = false;
char* configFilePath = nullptr; char* configFilePath = nullptr;
@ -84,7 +104,12 @@ int main(int argc, char** argv) {
dataStore.update(); dataStore.update();
proto.sendAllNow(); proto.sendAllNow();
sleep(1); // sleep(1);
char c = readStdin();
if(c != 0) {
dataStore.dump();
neighboursManager.dump();
}
} }
return 0; return 0;

View file

@ -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) { void Neighbours::changeNeiType(u64 id, NeiType nType) {
NeiType cType = neiType[id]; NeiType cType = neiType[id];
if(cType == nType) if(cType == nType)

View file

@ -52,12 +52,17 @@ class Neighbours {
* this data from the peer `from`. * this data from the peer `from`.
*/ */
void dump();
/** Dumps everything to STDIN. */
private: //meth private: //meth
class WrongNeiType : public std::exception {}; class WrongNeiType : public std::exception {};
enum NeiType { enum NeiType {
NEI_UNDEF, NEI_POTENTIAL, NEI_UNIDIR, NEI_SYM NEI_UNDEF, NEI_POTENTIAL, NEI_UNIDIR, NEI_SYM
}; };
void dumpNei(const Neighbour& nei, NeiType type);
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); void updateSendPackets(const Neighbour& nei);

View file

@ -129,8 +129,9 @@ void Protocol::sendIHave(u64 to, u64 datId, u32 seqno) {
} }
void Protocol::sendAllNow() { void Protocol::sendAllNow() {
for(auto& waiting : aggregatedTLVs) for(const auto& it : aggregatedTLVs)
sendNow(waiting.first); sendNow(it.first, false);
aggregatedTLVs.clear();
} }
void Protocol::startPollNetwork() { void Protocol::startPollNetwork() {
@ -217,11 +218,12 @@ SockAddr Protocol::addrOfV4(const sockaddr_in& addrv4) {
return out; return out;
} }
void Protocol::sendNow(u64 id) { void Protocol::sendNow(u64 id, bool erase) {
if(aggregatedTLVs.find(id) == aggregatedTLVs.end()) if(aggregatedTLVs.find(id) == aggregatedTLVs.end())
return; // Nothing to send. return; // Nothing to send.
Bytes aggregated = aggregatedTLVs[id]; Bytes aggregated = aggregatedTLVs[id];
aggregatedTLVs.erase(id); if(erase)
aggregatedTLVs.erase(id);
Bytes pck; Bytes pck;
pck << csts::MAGIC << csts::VERSION << (u16)aggregated.size() << selfId pck << csts::MAGIC << csts::VERSION << (u16)aggregated.size() << selfId

View file

@ -75,7 +75,7 @@ class Protocol {
const SockAddr& addrOfId(u64 id); const SockAddr& addrOfId(u64 id);
SockAddr addrOfV4(const sockaddr_in& addrv4); SockAddr addrOfV4(const sockaddr_in& addrv4);
void sendNow(u64 id); void sendNow(u64 id, bool erase=true);
/** Sends the aggregated TLVs right now. */ /** Sends the aggregated TLVs right now. */
private: private: