From 0a23f93877ecbf7006b2c70400f1cfff2fcecd2a Mon Sep 17 00:00:00 2001 From: csoler Date: Fri, 9 Aug 2013 17:02:29 +0000 Subject: [PATCH] enlabled distant messaging by default. Added lock protection in some places. Removed debug info in distant messaging. git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@6573 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- libretroshare/src/services/p3msgservice.cc | 78 +++++++++++++++++----- libretroshare/src/services/p3msgservice.h | 2 + 2 files changed, 65 insertions(+), 15 deletions(-) diff --git a/libretroshare/src/services/p3msgservice.cc b/libretroshare/src/services/p3msgservice.cc index 33d522d43..d58a342c4 100644 --- a/libretroshare/src/services/p3msgservice.cc +++ b/libretroshare/src/services/p3msgservice.cc @@ -45,7 +45,7 @@ #include #include -#define DEBUG_DISTANT_MSG +//#define DEBUG_DISTANT_MSG //#define DISABLE_DISTANT_MESSAGES const int msgservicezone = 54319; @@ -396,6 +396,14 @@ bool p3MsgService::saveList(bool& cleanup, std::list& itemList) itemList.push_back(item) ; } + RsConfigKeyValueSet *vitem = new RsConfigKeyValueSet ; + RsTlvKeyValue kv; + kv.key = "DISTANT_MESSAGES_ENABLED" ; + kv.value = mDistantMessagingEnabled?"YES":"NO" ; + vitem->tlvkvs.pairs.push_back(kv) ; + + itemList.push_back(vitem) ; + return true; } @@ -407,8 +415,11 @@ void p3MsgService::saveDone() RsSerialiser* p3MsgService::setupSerialiser() { - RsSerialiser *rss = new RsSerialiser ; - rss->addSerialType(new RsMsgSerialiser(true)); + RsSerialiser *rss = new RsSerialiser ; + + rss->addSerialType(new RsMsgSerialiser(true)); + rss->addSerialType(new RsGeneralConfigSerialiser()); + return rss; } @@ -468,6 +479,7 @@ bool p3MsgService::loadList(std::list& load) std::map srcIdMsgMap; std::map::iterator srcIt; + bool distant_messaging_set = false ; // load items and calculate next unique msgId for(it = load.begin(); it != load.end(); it++) @@ -513,6 +525,25 @@ bool p3MsgService::loadList(std::list& load) { _messenging_invites[msv->hash].time_of_validity = msv->time_stamp ; } + + RsConfigKeyValueSet *vitem = NULL ; + + if(NULL != (vitem = dynamic_cast(*it))) + for(std::list::const_iterator kit = vitem->tlvkvs.pairs.begin(); kit != vitem->tlvkvs.pairs.end(); ++kit) + if(kit->key == "DISTANT_MESSAGES_ENABLED") + { +#ifdef CHAT_DEBUG + std::cerr << "Loaded config default nick name for distant chat: " << kit->value << std::endl ; +#endif + enableDistantMessaging(kit->value == "YES") ; + distant_messaging_set = true ; + } + + } + if(!distant_messaging_set) + { + std::cerr << "No config value for distant messaging. Setting it to true." << std::endl; + enableDistantMessaging(true) ; } // sort items into lists @@ -1943,24 +1974,36 @@ void p3MsgService::enableDistantMessaging(bool b) if(!getDistantMessageHash(AuthGPG::getAuthGPG()->getGPGOwnId(),hash)) return ; + bool cchanged = false ; + #ifdef DEBUG_DISTANT_MSG std::cerr << (b?"Enabling":"Disabling") << " distant messaging, with hash = " << hash << std::endl; #endif - std::map::iterator it = _messenging_invites.find(hash) ; - - if(b && it == _messenging_invites.end()) { - DistantMessengingInvite invite ; - invite.time_of_validity = time(NULL) + 10*365*86400; // 10 years from now - _messenging_invites[hash] = invite ; + RsStackMutex stack(mMsgMtx); /********** STACK LOCKED MTX ******/ - IndicateConfigChanged() ; + std::map::iterator it = _messenging_invites.find(hash) ; + + if(b && it == _messenging_invites.end()) + { + DistantMessengingInvite invite ; + invite.time_of_validity = time(NULL) + 10*365*86400; // 10 years from now + _messenging_invites[hash] = invite ; + mDistantMessagingEnabled = true ; + + cchanged = true ; + } + if((!b) && it != _messenging_invites.end()) + { + _messenging_invites.erase(it) ; + mDistantMessagingEnabled = false ; + + cchanged = true ; + } } - if((!b) && it != _messenging_invites.end()) - { - _messenging_invites.erase(it) ; + + if(cchanged) IndicateConfigChanged() ; - } #ifdef DEBUG_DISTANT_MSG std::cerr << "List of distant message invites: " << std::endl; @@ -1977,7 +2020,12 @@ bool p3MsgService::distantMessagingEnabled() if(!getDistantMessageHash(AuthGPG::getAuthGPG()->getGPGOwnId(),hash)) return false; - return _messenging_invites.find(hash) != _messenging_invites.end() ; + bool res ; + { + RsStackMutex stack(mMsgMtx); /********** STACK LOCKED MTX ******/ + res = _messenging_invites.find(hash) != _messenging_invites.end() ; + } + return res ; } bool p3MsgService::getDistantMessageHash(const std::string& pgp_id,std::string& hash) { diff --git a/libretroshare/src/services/p3msgservice.h b/libretroshare/src/services/p3msgservice.h index 35d7d6a4a..e46e02326 100644 --- a/libretroshare/src/services/p3msgservice.h +++ b/libretroshare/src/services/p3msgservice.h @@ -193,6 +193,8 @@ void initStandardTagTypes(); std::map mParentId; std::string config_dir; + + bool mDistantMessagingEnabled ; }; #endif // MESSAGE_SERVICE_HEADER