From 9104f40aaf9960311f8b8d165b57a8360889105c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophile=20Bastian?= Date: Sun, 27 Nov 2016 13:26:27 +0100 Subject: [PATCH] Conform to datId = senderId --- configFile.cpp | 21 ++++++++++++--------- configFile.h | 6 ++++-- dataStore.cpp | 37 +++++++++++++++++++++++-------------- main.cpp | 14 +++++++------- 4 files changed, 46 insertions(+), 32 deletions(-) diff --git a/configFile.cpp b/configFile.cpp index fdb5171..d0df6dd 100644 --- a/configFile.cpp +++ b/configFile.cpp @@ -10,7 +10,7 @@ #include using namespace std; -ConfigFile::ConfigFile() { +ConfigFile::ConfigFile() : curDataSum(0) { selfId = randId(); } @@ -48,11 +48,6 @@ bool ConfigFile::read(const char* path) { bootstrapNodes.push_back(Neighbour(id, addr)); } else if(attr == "data") { - u64 id; - handle >> hex >> id >> dec; - if(id == 0) - id = randId(); - string dataStr; getline(handle, dataStr); @@ -63,7 +58,16 @@ bool ConfigFile::read(const char* path) { if(nonWSPos == dataStr.size()) continue; - data.push_back(make_pair(id, dataStr.substr(nonWSPos))); + std::string realDat = dataStr.substr(nonWSPos); + if(curDataSum + realDat.size() + 12 + 2 > 0xff) { + fprintf(stderr, "Too much data, won't fit. Discarding `%s`\n", + realDat.c_str()); + continue; + } + + curDataSum += realDat.size() + 2; + + data.push_back(realDat); } else if(attr.empty()) continue; @@ -96,8 +100,7 @@ bool ConfigFile::write(const char* path) { } for(auto dat : data) { - handle << "data " << hex << dat.first << dec << " " - << dat.second << '\n'; + handle << "data " << dat << '\n'; } handle.close(); diff --git a/configFile.h b/configFile.h index f7c8237..f778822 100644 --- a/configFile.h +++ b/configFile.h @@ -12,6 +12,7 @@ class ConfigFile { public: + typedef std::string CfgData; ConfigFile(); bool read(const char* path); @@ -26,7 +27,7 @@ class ConfigFile { }; /** Bootstrap nodes. No setter: wouldn't be useful. */ - const std::vector >& getData() const { + const std::vector& getData() const { return data; } /** Data to publish. */ @@ -36,6 +37,7 @@ class ConfigFile { u64 selfId; std::vector bootstrapNodes; - std::vector > data; + std::vector data; + size_t curDataSum; }; diff --git a/dataStore.cpp b/dataStore.cpp index be9002a..78f2a8b 100644 --- a/dataStore.cpp +++ b/dataStore.cpp @@ -85,22 +85,31 @@ void DataStore::setFlooded(u64 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; + while(dat.size() >= 2) { + printf(">> DATA %lX (%u) ", it->first, curSeqno[it->first]); + if(dat.size() < 2) { + printf("INVALID\n"); + continue; + } + u8 type, len; + dat >> type >> len; + if(dat.size() < len) { + printf("INVALID\n"); + continue; + } + if(type == csts::TLV_DATA_TEXT) { + string val; + for(int i=0; i < len; i++) { + u8 c; + dat >> c; + val += c; + } + printf("'%s'\n", val.c_str()); + } + else + printf("type=%d\n", type); } - 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); } } diff --git a/main.cpp b/main.cpp index ffb2ce0..95349a7 100644 --- a/main.cpp +++ b/main.cpp @@ -70,15 +70,15 @@ int main(int argc, char** argv) { Protocol proto(addr, cfg.getSelfId()); DataStore dataStore(&proto); + Bytes selfData; for(auto dat : cfg.getData()) { - Bytes pck; - pck << csts::TLV_DATA_TEXT << (u8)dat.second.size(); - for(u8 chr : dat.second) - pck << chr; - dataStore.addData(pck, time(NULL), dat.first, true); - fprintf(stderr, "[INFO] Adding data `%s` (%lX)\n", - dat.second.c_str(), dat.first); + selfData << csts::TLV_DATA_TEXT << (u8)dat.size(); + for(u8 chr : dat) + selfData << chr; + fprintf(stderr, "[INFO] Adding data `%s`\n", + dat.c_str()); } + dataStore.addData(selfData, time(NULL), cfg.getSelfId(), true); Neighbours neighboursManager(&proto, &dataStore);