Conform to datId = senderId

This commit is contained in:
Théophile Bastian 2016-11-27 13:26:27 +01:00
parent 6d3774bc99
commit 9104f40aaf
4 changed files with 46 additions and 32 deletions

View file

@ -10,7 +10,7 @@
#include <cstdlib>
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();

View file

@ -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<std::pair<u64, std::string> >& getData() const {
const std::vector<CfgData>& getData() const {
return data;
}
/** Data to publish. */
@ -36,6 +37,7 @@ class ConfigFile {
u64 selfId;
std::vector<Neighbour> bootstrapNodes;
std::vector<std::pair<u64, std::string> > data;
std::vector<CfgData> data;
size_t curDataSum;
};

View file

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

View file

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