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. 97
      src/twister_utils.cpp

6
README.md

@ -47,9 +47,9 @@ Testing
Some security checks are disabled (temporarily) allowing multiple clients per IP. Some security checks are disabled (temporarily) allowing multiple clients per IP.
Therefore it is possible to run multiple twisterd instances at the same machine: 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/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 -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 -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 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. there are at least two known nodes, like these two instances.

2
src/twister.cpp

@ -163,7 +163,7 @@ void ThreadWaitExtIP()
boost::filesystem::path userDataPath = GetDataDir() / "user_data"; boost::filesystem::path userDataPath = GetDataDir() / "user_data";
loadUserData(userDataPath.string(), m_users); 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) // now restart the user torrents (all m_following)
std::map<std::string,UserData>::const_iterator i; std::map<std::string,UserData>::const_iterator i;

97
src/twister_utils.cpp

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

Loading…
Cancel
Save