Browse Source

better error checking and variables names in user_data save/load

miguelfreitas
Miguel Freitas 11 years ago
parent
commit
9d1b5915f9
  1. 6
      README.md
  2. 2
      src/twister.cpp
  3. 95
      src/twister_utils.cpp

6
README.md

@ -47,9 +47,9 @@ Testing @@ -47,9 +47,9 @@ Testing
Some security checks are disabled (temporarily) allowing multiple clients per IP.
Therefore it is possible to run multiple twisterd instances at the same machine:
$ twisterd -datadir=/tmp/twister1 -port=30001 -daemon -rpcuser=user -rpcpassword=pwd -rpcallowip=127.0.0.1 -rpcport=40001
$ twisterd -datadir=/tmp/twister2 -port=30002 -daemon -rpcuser=user -rpcpassword=pwd -rpcallowip=127.0.0.1 -rpcport=40002
$ twisterd -rpcuser=user -rpcpassword=pwd -rpcallowip=127.0.0.1 -rpcport=40001 addnode <external-ip>:30002 onetry
$ twisterd -datadir=/tmp/twister1 -port=30001 -daemon -rpcuser=user -rpcpassword=pwd -rpcallowip=127.0.0.1 -rpcport=40001
$ twisterd -datadir=/tmp/twister2 -port=30002 -daemon -rpcuser=user -rpcpassword=pwd -rpcallowip=127.0.0.1 -rpcport=40002
$ twisterd -rpcuser=user -rpcpassword=pwd -rpcallowip=127.0.0.1 -rpcport=40001 addnode <external-ip>:30002 onetry
Note: some features (like block generation and dht put/get) do now work unless
there are at least two known nodes, like these two instances.

2
src/twister.cpp

@ -163,7 +163,7 @@ void ThreadWaitExtIP() @@ -163,7 +163,7 @@ void ThreadWaitExtIP()
boost::filesystem::path userDataPath = GetDataDir() / "user_data";
loadUserData(userDataPath.string(), m_users);
printf("loaded user_data for %d users\n", m_users.size());
printf("loaded user_data for %z users\n", m_users.size());
// now restart the user torrents (all m_following)
std::map<std::string,UserData>::const_iterator i;

95
src/twister_utils.cpp

@ -131,37 +131,43 @@ entry jsonToEntry(const Value &v) @@ -131,37 +131,43 @@ entry jsonToEntry(const Value &v)
int saveUserData(std::string const& filename, std::map<std::string,UserData> const &users)
{
entry userEntry;
entry userDict;
std::map<std::string,UserData>::const_iterator i;
for (i = users.begin(); i != users.end(); ++i) {
UserData const &data = i->second;
UserData const &udata = i->second;
entry &dataEntry = userEntry[i->first];
entry &followingEntry = dataEntry["following"];
BOOST_FOREACH( std::string const &n, data.m_following) {
followingEntry.list().push_back(n);
entry &userData = userDict[i->first];
if( udata.m_following.size() ) {
entry &followingList = userData["following"];
BOOST_FOREACH( std::string const &n, udata.m_following) {
followingList.list().push_back(n);
}
}
entry &dmEntry = dataEntry["dm"];
if( udata.m_directmsg.size() ) {
entry &dmDict = userData["dm"];
std::map<std::string, std::list<StoredDirectMsg> >::const_iterator j;
for (j = data.m_directmsg.begin(); j != data.m_directmsg.end(); ++j) {
std::list<StoredDirectMsg> const &stoDmList = j->second;
for (j = udata.m_directmsg.begin(); j != udata.m_directmsg.end(); ++j) {
std::list<StoredDirectMsg> const &dmsToFromUser = j->second;
entry &stoDmLstEntry = dmEntry[j->first];
BOOST_FOREACH( StoredDirectMsg const &dm, stoDmList) {
entry stoDmEntry;
stoDmEntry["time"] = dm.m_utcTime;
stoDmEntry["text"] = dm.m_text;
stoDmEntry["fromMe"] = dm.m_fromMe;
stoDmLstEntry.list().push_back(stoDmEntry);
entry &dmList = dmDict[j->first];
BOOST_FOREACH( StoredDirectMsg const &stoDm, dmsToFromUser) {
entry dmElem;
dmElem["time"] = stoDm.m_utcTime;
dmElem["text"] = stoDm.m_text;
dmElem["fromMe"] = stoDm.m_fromMe;
dmList.list().push_back(dmElem);
}
}
}
}
std::vector<char> buf;
bencode(std::back_inserter(buf), userEntry);
if( users.size() ) {
bencode(std::back_inserter(buf), userDict);
}
return save_file(filename, buf);
}
@ -171,40 +177,47 @@ int loadUserData(std::string const& filename, std::map<std::string,UserData> &us @@ -171,40 +177,47 @@ int loadUserData(std::string const& filename, std::map<std::string,UserData> &us
{
std::vector<char> in;
if (load_file(filename, in) == 0) {
lazy_entry userEntry;
lazy_entry userDict;
error_code ec;
if (lazy_bdecode(&in[0], &in[0] + in.size(), userEntry, ec) == 0) {
if( userEntry.type() != lazy_entry::dict_t ) goto data_error;
for( int i = 0; i < userEntry.dict_size(); i++) {
UserData data;
if (lazy_bdecode(&in[0], &in[0] + in.size(), userDict, ec) == 0) {
if( userDict.type() != lazy_entry::dict_t ) goto data_error;
for( int i = 0; i < userDict.dict_size(); i++) {
UserData udata;
const lazy_entry *dataEntry = userEntry.dict_at(i).second;
const lazy_entry *followingEntry = dataEntry->dict_find("following");
if( followingEntry->type() != lazy_entry::list_t ) goto data_error;
const lazy_entry *userData = userDict.dict_at(i).second;
if( userData->type() != lazy_entry::dict_t ) goto data_error;
for( int j = 0; j < followingEntry->list_size(); j++ ) {
data.m_following.insert( followingEntry->list_string_value_at(j) );
const lazy_entry *followingList = userData->dict_find("following");
if( followingList ) {
if( followingList->type() != lazy_entry::list_t ) goto data_error;
for( int j = 0; j < followingList->list_size(); j++ ) {
udata.m_following.insert( followingList->list_string_value_at(j) );
}
}
const lazy_entry *dmEntry = dataEntry->dict_find("dm");
if( dmEntry->type() != lazy_entry::dict_t ) goto data_error;
const lazy_entry *dmDict = userData->dict_find("dm");
if( dmDict ) {
if( dmDict->type() != lazy_entry::dict_t ) goto data_error;
for( int j = 0; j < dmEntry->dict_size(); j++ ) {
const lazy_entry *stoDmLstEntry = dmEntry->dict_at(j).second;
if( stoDmLstEntry->type() != lazy_entry::list_t ) goto data_error;
for( int j = 0; j < dmDict->dict_size(); j++ ) {
const lazy_entry *dmList = dmDict->dict_at(j).second;
if( !dmList || dmList->type() != lazy_entry::list_t ) goto data_error;
for( int k = 0; k < stoDmLstEntry->list_size(); k++ ) {
const lazy_entry *stoDmEntry = stoDmLstEntry->list_at(k);
if( stoDmEntry->type() != lazy_entry::dict_t ) goto data_error;
for( int k = 0; k < dmList->list_size(); k++ ) {
const lazy_entry *dmElem = dmList->list_at(k);
if( dmElem->type() != lazy_entry::dict_t ) goto data_error;
StoredDirectMsg dm;
dm.m_text = stoDmEntry->dict_find_string_value("text");
dm.m_utcTime = stoDmEntry->dict_find_int_value("time");
dm.m_fromMe = stoDmEntry->dict_find_int_value("fromMe");
data.m_directmsg[dmEntry->dict_at(j).first].push_back(dm);
StoredDirectMsg stoDm;
stoDm.m_text = dmElem->dict_find_string_value("text");
stoDm.m_utcTime = dmElem->dict_find_int_value("time");
stoDm.m_fromMe = dmElem->dict_find_int_value("fromMe");
udata.m_directmsg[dmDict->dict_at(j).first].push_back(stoDm);
}
}
}
users[userEntry.dict_at(i).first] = data;
users[userDict.dict_at(i).first] = udata;
}
return 0;
}

Loading…
Cancel
Save