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
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);
}
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

View file

@ -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);

View file

@ -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;

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) {
NeiType cType = neiType[id];
if(cType == nType)

View file

@ -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<Neighbour>* listOfType(NeiType typ);
void changeNeiType(u64 id, NeiType nType);
void updateSendPackets(const Neighbour& nei);

View file

@ -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

View file

@ -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: