From dc2f2f5eb492ca242e346792f2b89b8ff7d0342e Mon Sep 17 00:00:00 2001 From: csoler Date: Sun, 29 Dec 2019 13:49:56 +0100 Subject: [PATCH 1/4] fixed ambiguous variable --- libretroshare/src/turtle/p3turtle.cc | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/libretroshare/src/turtle/p3turtle.cc b/libretroshare/src/turtle/p3turtle.cc index 328b04772..677b282cd 100644 --- a/libretroshare/src/turtle/p3turtle.cc +++ b/libretroshare/src/turtle/p3turtle.cc @@ -1886,11 +1886,15 @@ void p3turtle::handleTunnelResult(RsTurtleTunnelOkItem *item) // because there is not too much file hashes to be active at a time, // and this mostly prevents from sending the hash back in the tunnel. - bool found = false ; +#ifdef P3TURTLE_DEBUG + bool ext_found = false ; +#endif for(std::map::iterator it(_incoming_file_hashes.begin());it!=_incoming_file_hashes.end();++it) if(it->second.last_request == item->request_id) { - found = true ; +#ifdef P3TURTLE_DEBUG + ext_found = true ; +#endif { // add the tunnel uniquely @@ -1917,7 +1921,7 @@ void p3turtle::handleTunnelResult(RsTurtleTunnelOkItem *item) } } #ifdef P3TURTLE_DEBUG - if(!found) + if(!ext_found) std::cerr << "p3turtle: error. Could not find hash that emmitted tunnel request " << reinterpret_cast(item->tunnel_id) << std::endl ; #endif } From c544b1da7c09e01756d888b27091fa516909cb73 Mon Sep 17 00:00:00 2001 From: csoler Date: Sun, 29 Dec 2019 13:50:54 +0100 Subject: [PATCH 2/4] changed RsEvents so that it takes event types when registering handlers, which limits the number of handlers called for each event --- .../src/gossipdiscovery/p3gossipdiscovery.cc | 6 +-- .../src/retroshare/rsbroadcastdiscovery.h | 25 +++++++---- libretroshare/src/retroshare/rsevents.h | 10 +++-- .../src/retroshare/rsgossipdiscovery.h | 16 +++++-- libretroshare/src/retroshare/rsmsgs.h | 18 ++++---- libretroshare/src/rsitems/rsposteditems.cc | 4 +- libretroshare/src/rsserver/p3msgs.cc | 4 +- .../src/services/broadcastdiscoveryservice.cc | 12 ++++-- libretroshare/src/services/p3msgservice.cc | 14 +++--- libretroshare/src/services/rseventsservice.cc | 43 +++++++++++++++---- libretroshare/src/services/rseventsservice.h | 10 +++-- retroshare-gui/src/gui/NewsFeed.cpp | 24 ++++++++--- retroshare-gui/src/gui/NewsFeed.h | 3 +- .../src/gui/common/NewFriendList.cpp | 11 ++--- .../src/gui/gxschannels/GxsChannelDialog.cpp | 25 +++++------ .../gui/gxschannels/GxsChannelPostsWidget.cpp | 35 +++++++-------- .../gui/gxsforums/GxsForumThreadWidget.cpp | 35 +++++++-------- .../src/gui/gxsforums/GxsForumsDialog.cpp | 25 +++++------ .../src/util/RsGxsUpdateBroadcast.cpp | 6 +-- 19 files changed, 186 insertions(+), 140 deletions(-) diff --git a/libretroshare/src/gossipdiscovery/p3gossipdiscovery.cc b/libretroshare/src/gossipdiscovery/p3gossipdiscovery.cc index d7cf5f1a7..a6a24f79c 100644 --- a/libretroshare/src/gossipdiscovery/p3gossipdiscovery.cc +++ b/libretroshare/src/gossipdiscovery/p3gossipdiscovery.cc @@ -112,6 +112,7 @@ p3discovery2::p3discovery2( if(rsEvents) rsEvents->registerEventsHandler( + RsEventType::GOSSIP_DISCOVERY, [this](std::shared_ptr event) { rsEventsHandler(*event); @@ -1348,8 +1349,3 @@ void p3discovery2::rsEventsHandler(const RsEvent& event) // // /* ignore other operations */ // } - -// (cyril) do we still need this?? -RsGossipDiscoveryFriendInviteReceivedEvent::RsGossipDiscoveryFriendInviteReceivedEvent(const std::string& invite) : - RsEvent(RsEventType::GOSSIP_DISCOVERY_INVITE_RECEIVED), - mInvite(invite) {} diff --git a/libretroshare/src/retroshare/rsbroadcastdiscovery.h b/libretroshare/src/retroshare/rsbroadcastdiscovery.h index 1bb1e1cc6..92b0e5b0e 100644 --- a/libretroshare/src/retroshare/rsbroadcastdiscovery.h +++ b/libretroshare/src/retroshare/rsbroadcastdiscovery.h @@ -71,23 +71,32 @@ struct RsBroadcastDiscoveryResult : RsSerializable * @brief Event emitted when a non friend new peer is found in the local network * @see RsEvents */ -struct RsBroadcastDiscoveryPeerFoundEvent : RsEvent -{ - RsBroadcastDiscoveryPeerFoundEvent( - const RsBroadcastDiscoveryResult& eventData ) : - RsEvent(RsEventType::BROADCAST_DISCOVERY_PEER_FOUND), mData(eventData) {} +enum class RsBroadcastDiscoveryEventType: uint32_t { + UNKNOWN = 0x00, + PEER_FOUND = 0x01 +}; - RsBroadcastDiscoveryResult mData; +struct RsBroadcastDiscoveryEvent : RsEvent +{ + RsBroadcastDiscoveryEvent() + : RsEvent(RsEventType::BROADCAST_DISCOVERY), + mDiscoveryEventType(RsBroadcastDiscoveryEventType::UNKNOWN) + {} + + virtual ~RsBroadcastDiscoveryEvent() override = default; + + RsBroadcastDiscoveryEventType mDiscoveryEventType; + RsBroadcastDiscoveryResult mData; /// @see RsSerializable void serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx) override { RsEvent::serial_process(j, ctx); + + RS_SERIAL_PROCESS(mDiscoveryEventType); RS_SERIAL_PROCESS(mData); } - - ~RsBroadcastDiscoveryPeerFoundEvent() override; }; diff --git a/libretroshare/src/retroshare/rsevents.h b/libretroshare/src/retroshare/rsevents.h index 249a6f50b..f46218313 100644 --- a/libretroshare/src/retroshare/rsevents.h +++ b/libretroshare/src/retroshare/rsevents.h @@ -50,10 +50,10 @@ enum class RsEventType : uint32_t NONE = 0, /// Used to detect uninitialized event /// @see RsBroadcastDiscovery - BROADCAST_DISCOVERY_PEER_FOUND = 1, + BROADCAST_DISCOVERY = 1, /// @see RsDiscPendingPgpReceivedEvent - GOSSIP_DISCOVERY_INVITE_RECEIVED = 2, + GOSSIP_DISCOVERY = 2, /// @see AuthSSL AUTHSSL_CONNECTION_AUTENTICATION = 3, @@ -61,14 +61,14 @@ enum class RsEventType : uint32_t /// @see pqissl PEER_CONNECTION = 4, - /// @see RsGxsChanges // this one should probably be removed because it's not used anywhere + /// @see RsGxsChanges // this one is used in RsGxsBroadcast GXS_CHANGES = 5, /// Emitted when a peer state changes, @see RsPeers PEER_STATE_CHANGED = 6, /// @see RsMailStatusEvent - MAIL_STATUS_CHANGE = 7, + MAIL_STATUS = 7, /// @see RsGxsCircleEvent GXS_CIRCLES = 8, @@ -160,6 +160,7 @@ public: * Every time an event is dispatced the registered events handlers will get * their method handleEvent called with the event passed as paramether. * @jsonapi{development,manualwrapper} + * @param eventType Type of event for which the callback is called * @param multiCallback Function that will be called each time an event * is dispatched. * @param[inout] hId Optional storage for handler id, useful to @@ -170,6 +171,7 @@ public: * @return False on error, true otherwise. */ virtual bool registerEventsHandler( + RsEventType eventType, std::function)> multiCallback, RsEventsHandlerId_t& hId = RS_DEFAULT_STORAGE_PARAM(RsEventsHandlerId_t, 0) ) = 0; diff --git a/libretroshare/src/retroshare/rsgossipdiscovery.h b/libretroshare/src/retroshare/rsgossipdiscovery.h index c9dc9924a..ca1ab0d7c 100644 --- a/libretroshare/src/retroshare/rsgossipdiscovery.h +++ b/libretroshare/src/retroshare/rsgossipdiscovery.h @@ -45,11 +45,18 @@ extern std::shared_ptr rsGossipDiscovery; /** * @brief Emitted when a pending PGP certificate is received */ -struct RsGossipDiscoveryFriendInviteReceivedEvent : RsEvent -{ - RsGossipDiscoveryFriendInviteReceivedEvent( - const std::string& invite ); +enum class RsGossipDiscoveryEventType: uint32_t { + UNKNOWN = 0x00, + PEER_INVITE_RECEIVED = 0x01 +}; + +struct RsGossipDiscoveryEvent : RsEvent +{ + RsGossipDiscoveryEvent(): RsEvent(RsEventType::GOSSIP_DISCOVERY) {} + virtual ~RsGossipDiscoveryEvent() override {} + + RsGossipDiscoveryEventType mGossipDiscoveryEventType; std::string mInvite; /// @see RsSerializable @@ -57,6 +64,7 @@ struct RsGossipDiscoveryFriendInviteReceivedEvent : RsEvent RsGenericSerializer::SerializeContext& ctx ) { RsEvent::serial_process(j,ctx); + RS_SERIAL_PROCESS(mGossipDiscoveryEventType); RS_SERIAL_PROCESS(mInvite); } }; diff --git a/libretroshare/src/retroshare/rsmsgs.h b/libretroshare/src/retroshare/rsmsgs.h index cbdd525bc..ea2af0d4a 100644 --- a/libretroshare/src/retroshare/rsmsgs.h +++ b/libretroshare/src/retroshare/rsmsgs.h @@ -296,19 +296,19 @@ struct MsgTagType : RsSerializable } //namespace Rs } //namespace Msgs -struct RsMailStatusEvent : RsEvent -{ - RsMailStatusEvent() : RsEvent(RsEventType::MAIL_STATUS_CHANGE) {} - - enum MailStatusEventCode: uint8_t { +enum class RsMailStatusEventType: uint8_t { NEW_MESSAGE = 0x00, MESSAGE_REMOVED = 0x01, MESSAGE_SENT = 0x02, MESSAGE_RECEIVED_ACK = 0x03, // means the peer received the message FAILED_SIGNATURE = 0x04, // means the signature of the message cannot be verified - }; +}; - MailStatusEventCode mMailStatusEventCode; +struct RsMailStatusEvent : RsEvent +{ + RsMailStatusEvent() : RsEvent(RsEventType::MAIL_STATUS) {} + + RsMailStatusEventType mMailStatusEventCode; std::set mChangedMsgIds; /// @see RsEvent @@ -320,7 +320,7 @@ struct RsMailStatusEvent : RsEvent RS_SERIAL_PROCESS(mMailStatusEventCode); } - ~RsMailStatusEvent() override; + ~RsMailStatusEvent() override = default; }; #define RS_CHAT_PUBLIC 0x0001 @@ -329,7 +329,7 @@ struct RsMailStatusEvent : RsEvent #define RS_DISTANT_CHAT_STATUS_UNKNOWN 0x0000 #define RS_DISTANT_CHAT_STATUS_TUNNEL_DN 0x0001 -#define RS_DISTANT_CHAT_STATUS_CAN_TALK 0x0002 +#define RS_DISTANT_CHAT_STATUS_CAN_TALK 0x0002 #define RS_DISTANT_CHAT_STATUS_REMOTELY_CLOSED 0x0003 #define RS_DISTANT_CHAT_ERROR_NO_ERROR 0x0000 diff --git a/libretroshare/src/rsitems/rsposteditems.cc b/libretroshare/src/rsitems/rsposteditems.cc index 23fb6633b..3c16ff5bd 100644 --- a/libretroshare/src/rsitems/rsposteditems.cc +++ b/libretroshare/src/rsitems/rsposteditems.cc @@ -33,8 +33,8 @@ void RsGxsPostedPostItem::serial_process(RsGenericSerializer::SerializeJob j,RsG // and do not expect to deserialize mImage member if the data block has been consummed entirely (keeps compatibility // of new RS with older posts. - if(j == RsGenericSerializer::DESERIALIZE && ctx.mOffset == ctx.mSize) - return ; + if(j == RsGenericSerializer::DESERIALIZE && ctx.mOffset == ctx.mSize) + return ; if((j == RsGenericSerializer::SIZE_ESTIMATE || j == RsGenericSerializer::SERIALIZE) && mImage.empty()) return ; diff --git a/libretroshare/src/rsserver/p3msgs.cc b/libretroshare/src/rsserver/p3msgs.cc index ad74dbdde..48a694fb0 100644 --- a/libretroshare/src/rsserver/p3msgs.cc +++ b/libretroshare/src/rsserver/p3msgs.cc @@ -538,8 +538,7 @@ bool p3Msgs::initiateDistantChatConnexion( const RsGxsId& to_gxs_id, const RsGxsId& from_gxs_id, DistantChatPeerId& pid, uint32_t& error_code, bool notify ) { - return mChatSrv->initiateDistantChatConnexion( to_gxs_id, from_gxs_id, - pid, error_code, notify ); + return mChatSrv->initiateDistantChatConnexion( to_gxs_id, from_gxs_id, pid, error_code, notify ); } bool p3Msgs::getDistantChatStatus(const DistantChatPeerId& pid,DistantChatPeerInfo& info) { @@ -559,7 +558,6 @@ uint32_t p3Msgs::getDistantChatPermissionFlags() } RsMsgs::~RsMsgs() = default; -RsMailStatusEvent::~RsMailStatusEvent() = default; Rs::Msgs::MessageInfo::~MessageInfo() = default; MsgInfoSummary::~MsgInfoSummary() = default; VisibleChatLobbyRecord::~VisibleChatLobbyRecord() = default; diff --git a/libretroshare/src/services/broadcastdiscoveryservice.cc b/libretroshare/src/services/broadcastdiscoveryservice.cc index d49e04a17..9b39e302e 100644 --- a/libretroshare/src/services/broadcastdiscoveryservice.cc +++ b/libretroshare/src/services/broadcastdiscoveryservice.cc @@ -182,10 +182,15 @@ void BroadcastDiscoveryService::data_tick() } else if(!isFriend) { - typedef RsBroadcastDiscoveryPeerFoundEvent Evt_t; if(rsEvents) - rsEvents->postEvent( - std::shared_ptr(new Evt_t(rbdr)) ); + { + auto ev = std::make_shared(); + + ev->mDiscoveryEventType = RsBroadcastDiscoveryEventType::PEER_FOUND; + ev->mData = rbdr; + + rsEvents->postEvent(ev); + } } } } @@ -307,5 +312,4 @@ bool BroadcastDiscoveryService::assertMulticastLockIsvalid() RsBroadcastDiscovery::~RsBroadcastDiscovery() = default; RsBroadcastDiscoveryResult::~RsBroadcastDiscoveryResult() = default; -RsBroadcastDiscoveryPeerFoundEvent::~RsBroadcastDiscoveryPeerFoundEvent() = default; BroadcastDiscoveryPack::~BroadcastDiscoveryPack() = default; diff --git a/libretroshare/src/services/p3msgservice.cc b/libretroshare/src/services/p3msgservice.cc index dc67a0f60..49640330b 100644 --- a/libretroshare/src/services/p3msgservice.cc +++ b/libretroshare/src/services/p3msgservice.cc @@ -178,7 +178,7 @@ void p3MsgService::processIncomingMsg(RsMsgItem *mi) if (rsEvents) { auto ev = std::make_shared(); - ev->mMailStatusEventCode = RsMailStatusEvent::NEW_MESSAGE; + ev->mMailStatusEventCode = RsMailStatusEventType::NEW_MESSAGE; ev->mChangedMsgIds.insert(std::to_string(mi->msgId)); rsEvents->sendEvent(ev); @@ -338,7 +338,7 @@ int p3MsgService::checkOutgoingMessages() std::list output_queue; auto pEvent = std::make_shared(); - pEvent->mMailStatusEventCode = RsMailStatusEvent::MESSAGE_SENT; + pEvent->mMailStatusEventCode = RsMailStatusEventType::MESSAGE_SENT; { RS_STACK_MUTEX(mMsgMtx); /********** STACK LOCKED MTX ******/ @@ -900,7 +900,7 @@ bool p3MsgService::removeMsgId(const std::string &mid) auto pEvent = std::make_shared(); - pEvent->mMailStatusEventCode = RsMailStatusEvent::MESSAGE_REMOVED; + pEvent->mMailStatusEventCode = RsMailStatusEventType::MESSAGE_REMOVED; { RsStackMutex stack(mMsgMtx); /********** STACK LOCKED MTX ******/ @@ -1269,7 +1269,7 @@ uint32_t p3MsgService::sendMail( uint32_t ret = 0; auto pEvent = std::make_shared(); - pEvent->mMailStatusEventCode = RsMailStatusEvent::MESSAGE_SENT; + pEvent->mMailStatusEventCode = RsMailStatusEventType::MESSAGE_SENT; auto pSend = [&](const std::set& sDest) { @@ -2085,7 +2085,7 @@ void p3MsgService::notifyDataStatus( const GRouterMsgPropagationId& id, auto pEvent = std::make_shared(); - pEvent->mMailStatusEventCode = RsMailStatusEvent::NEW_MESSAGE; + pEvent->mMailStatusEventCode = RsMailStatusEventType::NEW_MESSAGE; pEvent->mChangedMsgIds.insert(std::to_string(msg_id)); if(rsEvents) rsEvents->postEvent(pEvent); @@ -2189,7 +2189,7 @@ bool p3MsgService::notifyGxsTransSendStatus( RsGxsTransId mailId, if( status == GxsTransSendStatus::RECEIPT_RECEIVED ) { - pEvent->mMailStatusEventCode = RsMailStatusEvent::NEW_MESSAGE; + pEvent->mMailStatusEventCode = RsMailStatusEventType::NEW_MESSAGE; uint32_t msg_id; { @@ -2244,7 +2244,7 @@ bool p3MsgService::notifyGxsTransSendStatus( RsGxsTransId mailId, else if( status >= GxsTransSendStatus::FAILED_RECEIPT_SIGNATURE ) { uint32_t msg_id; - pEvent->mMailStatusEventCode = RsMailStatusEvent::FAILED_SIGNATURE; + pEvent->mMailStatusEventCode = RsMailStatusEventType::FAILED_SIGNATURE; { RS_STACK_MUTEX(gxsOngoingMutex); diff --git a/libretroshare/src/services/rseventsservice.cc b/libretroshare/src/services/rseventsservice.cc index 2dfc12d48..8d1a28fa3 100644 --- a/libretroshare/src/services/rseventsservice.cc +++ b/libretroshare/src/services/rseventsservice.cc @@ -99,22 +99,42 @@ RsEventsHandlerId_t RsEventsService::generateUniqueHandlerId_unlocked() } bool RsEventsService::registerEventsHandler( + RsEventType eventType, std::function)> multiCallback, RsEventsHandlerId_t& hId ) { RS_STACK_MUTEX(mHandlerMapMtx); - if(!hId) hId = generateUniqueHandlerId_unlocked(); - mHandlerMap[hId] = multiCallback; + + if( (int)eventType > mHandlerMaps.size() + 10) + { + RsErr() << "Cannot register an event handler for an event type larger than 10 plus the max pre-defined event (value passed was " << (int)eventType << " whereas max is " << (int)RsEventType::MAX << ")" << std::endl; + return false; + } + + if( (int)eventType >= mHandlerMaps.size()) + mHandlerMaps.resize( (int)eventType +1 ); + + if(!hId) + hId = generateUniqueHandlerId_unlocked(); + + mHandlerMaps[(int)eventType][hId] = multiCallback; return true; } bool RsEventsService::unregisterEventsHandler(RsEventsHandlerId_t hId) { RS_STACK_MUTEX(mHandlerMapMtx); - auto it = mHandlerMap.find(hId); - if(it == mHandlerMap.end()) return false; - mHandlerMap.erase(it); - return true; + + for(uint32_t i=0;i event) { std::function)> mCallback; + uint32_t event_type_index = (uint32_t)event->mType; + mHandlerMapMtx.lock(); - auto cbpt = mHandlerMap.begin(); + if(event_type_index >= mHandlerMaps.size() || event_type_index < 1) + { + RsErr() << "Cannot handle an event of type " << event_type_index << ": out of scope!" << std::endl; + return; + } + auto cbpt = mHandlerMaps[event_type_index].begin(); mHandlerMapMtx.unlock(); getHandlerFromMapLock: mHandlerMapMtx.lock(); - if(cbpt != mHandlerMap.end()) + if(cbpt != mHandlerMaps[event_type_index].end()) { mCallback = cbpt->second; ++cbpt; diff --git a/libretroshare/src/services/rseventsservice.h b/libretroshare/src/services/rseventsservice.h index 05fff772e..182a73f6b 100644 --- a/libretroshare/src/services/rseventsservice.h +++ b/libretroshare/src/services/rseventsservice.h @@ -35,6 +35,7 @@ class RsEventsService : public: RsEventsService(): mHandlerMapMtx("RsEventsService::mHandlerMapMtx"), mLastHandlerId(1), + mHandlerMaps(static_cast(RsEventType::MAX)), mEventQueueMtx("RsEventsService::mEventQueueMtx") {} /// @see RsEvents @@ -54,6 +55,7 @@ public: /// @see RsEvents bool registerEventsHandler( + RsEventType eventType, std::function)> multiCallback, RsEventsHandlerId_t& hId = RS_DEFAULT_STORAGE_PARAM(RsEventsHandlerId_t, 0) ) override; @@ -64,9 +66,11 @@ public: protected: RsMutex mHandlerMapMtx; RsEventsHandlerId_t mLastHandlerId; - std::map< - RsEventsHandlerId_t, - std::function)> > mHandlerMap; + + std::vector< + std::map< + RsEventsHandlerId_t, + std::function)> > > mHandlerMaps; RsMutex mEventQueueMtx; std::deque< std::shared_ptr > mEventQueue; diff --git a/retroshare-gui/src/gui/NewsFeed.cpp b/retroshare-gui/src/gui/NewsFeed.cpp index ac44de06b..7f3504055 100644 --- a/retroshare-gui/src/gui/NewsFeed.cpp +++ b/retroshare-gui/src/gui/NewsFeed.cpp @@ -72,8 +72,21 @@ static NewsFeed *instance = NULL; /** Constructor */ NewsFeed::NewsFeed(QWidget *parent) : MainPage(parent), ui(new Ui::NewsFeed) { - mEventHandlerId =0; // needed to force intialization by registerEventsHandler() - rsEvents->registerEventsHandler( [this](std::shared_ptr event) { handleEvent(event); }, mEventHandlerId ); + mEventTypes = { + RsEventType::AUTHSSL_CONNECTION_AUTENTICATION, + RsEventType::PEER_CONNECTION , + RsEventType::GXS_CIRCLES , + RsEventType::GXS_CHANNELS , + RsEventType::GXS_FORUMS , + RsEventType::GXS_POSTED , + RsEventType::MAIL_STATUS + }; + + for(uint32_t i=0;iregisterEventsHandler(mEventTypes[i], [this](std::shared_ptr event) { handleEvent(event); }, mEventHandlerIds.back() ); + } /* Invoke the Qt Designer generated object setup routine */ ui->setupUi(this); @@ -117,7 +130,8 @@ QString hlp_str = tr( NewsFeed::~NewsFeed() { - rsEvents->unregisterEventsHandler(mEventHandlerId); + for(uint32_t i=0;iunregisterEventsHandler(mEventHandlerIds[i]); // save settings processSettings(false); @@ -190,7 +204,7 @@ void NewsFeed::handleEvent_main_thread(std::shared_ptr event) if(event->mType == RsEventType::GXS_POSTED && (flags & RS_FEED_TYPE_POSTED)) handleMailEvent(event); - if(event->mType == RsEventType::MAIL_STATUS_CHANGE && (flags & RS_FEED_TYPE_MSG)) + if(event->mType == RsEventType::MAIL_STATUS && (flags & RS_FEED_TYPE_MSG)) handlePostedEvent(event); } @@ -202,7 +216,7 @@ void NewsFeed::handleMailEvent(std::shared_ptr event) switch(pe->mMailStatusEventCode) { - case RsMailStatusEvent::NEW_MESSAGE: + case RsMailStatusEventType::NEW_MESSAGE: for(auto msgid: pe->mChangedMsgIds) addFeedItem( new MsgItem(this, NEWSFEED_MESSAGELIST, msgid, false)); break; diff --git a/retroshare-gui/src/gui/NewsFeed.h b/retroshare-gui/src/gui/NewsFeed.h index c255d56ac..79208e344 100644 --- a/retroshare-gui/src/gui/NewsFeed.h +++ b/retroshare-gui/src/gui/NewsFeed.h @@ -118,7 +118,8 @@ private: /* UI - from Designer */ Ui::NewsFeed *ui; - RsEventsHandlerId_t mEventHandlerId; + std::vector mEventHandlerIds; + std::vector mEventTypes; }; #endif diff --git a/retroshare-gui/src/gui/common/NewFriendList.cpp b/retroshare-gui/src/gui/common/NewFriendList.cpp index 7b07f9086..cb4c68030 100644 --- a/retroshare-gui/src/gui/common/NewFriendList.cpp +++ b/retroshare-gui/src/gui/common/NewFriendList.cpp @@ -178,7 +178,7 @@ NewFriendList::NewFriendList(QWidget *parent) : /* RsAutoUpdatePage(5000,parent) ui->filterLineEdit->showFilterIcon(); mEventHandlerId=0; // forces initialization - rsEvents->registerEventsHandler( [this](std::shared_ptr e) { handleEvent(e); }, mEventHandlerId ); + rsEvents->registerEventsHandler( RsEventType::PEER_CONNECTION, [this](std::shared_ptr e) { handleEvent(e); }, mEventHandlerId ); mModel = new RsFriendListModel(); mProxyModel = new FriendListSortFilterProxyModel(ui->peerTreeWidget->header(),this); @@ -258,13 +258,10 @@ NewFriendList::NewFriendList(QWidget *parent) : /* RsAutoUpdatePage(5000,parent) void NewFriendList::handleEvent(std::shared_ptr e) { - if(e->mType == RsEventType::PEER_CONNECTION) - { - // /!\ The function we're in is called from a different thread. It's very important - // to use this trick in order to avoid data races. + // /!\ The function we're in is called from a different thread. It's very important + // to use this trick in order to avoid data races. - RsQThreadUtils::postToObject( [=]() { forceUpdateDisplay() ; }, this ) ; - } + RsQThreadUtils::postToObject( [=]() { forceUpdateDisplay() ; }, this ) ; } NewFriendList::~NewFriendList() diff --git a/retroshare-gui/src/gui/gxschannels/GxsChannelDialog.cpp b/retroshare-gui/src/gui/gxschannels/GxsChannelDialog.cpp index 137f37f26..d0b5044c6 100644 --- a/retroshare-gui/src/gui/gxschannels/GxsChannelDialog.cpp +++ b/retroshare-gui/src/gui/gxschannels/GxsChannelDialog.cpp @@ -52,26 +52,23 @@ GxsChannelDialog::GxsChannelDialog(QWidget *parent) { mEventHandlerId = 0; // Needs to be asynced because this function is likely to be called by another thread! - rsEvents->registerEventsHandler( [this](std::shared_ptr event) { RsQThreadUtils::postToObject( [=]() { handleEvent_main_thread(event); }, this ); }, mEventHandlerId ); + rsEvents->registerEventsHandler(RsEventType::GXS_CHANNELS, [this](std::shared_ptr event) { RsQThreadUtils::postToObject( [=]() { handleEvent_main_thread(event); }, this ); }, mEventHandlerId ); } void GxsChannelDialog::handleEvent_main_thread(std::shared_ptr event) { - if(event->mType == RsEventType::GXS_CHANNELS) - { - const RsGxsChannelEvent *e = dynamic_cast(event.get()); + const RsGxsChannelEvent *e = dynamic_cast(event.get()); - if(!e) - return; + if(!e) + return; - switch(e->mChannelEventCode) - { - case RsGxsChannelEvent::ChannelEventCode::SUBSCRIBE_STATUS_CHANGED: updateDisplay(true); - break; - default: - break; - } - } + switch(e->mChannelEventCode) + { + case RsGxsChannelEvent::ChannelEventCode::SUBSCRIBE_STATUS_CHANGED: updateDisplay(true); + break; + default: + break; + } } GxsChannelDialog::~GxsChannelDialog() diff --git a/retroshare-gui/src/gui/gxschannels/GxsChannelPostsWidget.cpp b/retroshare-gui/src/gui/gxschannels/GxsChannelPostsWidget.cpp index 02abc3ba5..dfa322934 100644 --- a/retroshare-gui/src/gui/gxschannels/GxsChannelPostsWidget.cpp +++ b/retroshare-gui/src/gui/gxschannels/GxsChannelPostsWidget.cpp @@ -132,31 +132,28 @@ GxsChannelPostsWidget::GxsChannelPostsWidget(const RsGxsGroupId &channelId, QWid mEventHandlerId = 0; // Needs to be asynced because this function is likely to be called by another thread! - rsEvents->registerEventsHandler( [this](std::shared_ptr event) { RsQThreadUtils::postToObject( [=]() { handleEvent_main_thread(event); }, this ); }, mEventHandlerId ); + rsEvents->registerEventsHandler(RsEventType::GXS_CHANNELS, [this](std::shared_ptr event) { RsQThreadUtils::postToObject( [=]() { handleEvent_main_thread(event); }, this ); }, mEventHandlerId ); } void GxsChannelPostsWidget::handleEvent_main_thread(std::shared_ptr event) { - if(event->mType == RsEventType::GXS_CHANNELS) - { - const RsGxsChannelEvent *e = dynamic_cast(event.get()); + const RsGxsChannelEvent *e = dynamic_cast(event.get()); - if(!e) - return; + if(!e) + return; - switch(e->mChannelEventCode) - { - case RsGxsChannelEvent::ChannelEventCode::UPDATED_CHANNEL: - case RsGxsChannelEvent::ChannelEventCode::NEW_CHANNEL: - case RsGxsChannelEvent::ChannelEventCode::UPDATED_MESSAGE: - case RsGxsChannelEvent::ChannelEventCode::NEW_MESSAGE: - if(e->mChannelGroupId == mChannelGroupId) - updateDisplay(true); - break; - default: - break; - } - } + switch(e->mChannelEventCode) + { + case RsGxsChannelEvent::ChannelEventCode::UPDATED_CHANNEL: + case RsGxsChannelEvent::ChannelEventCode::NEW_CHANNEL: + case RsGxsChannelEvent::ChannelEventCode::UPDATED_MESSAGE: + case RsGxsChannelEvent::ChannelEventCode::NEW_MESSAGE: + if(e->mChannelGroupId == mChannelGroupId) + updateDisplay(true); + break; + default: + break; + } } GxsChannelPostsWidget::~GxsChannelPostsWidget() diff --git a/retroshare-gui/src/gui/gxsforums/GxsForumThreadWidget.cpp b/retroshare-gui/src/gui/gxsforums/GxsForumThreadWidget.cpp index 5b02a977e..e46c67273 100644 --- a/retroshare-gui/src/gui/gxsforums/GxsForumThreadWidget.cpp +++ b/retroshare-gui/src/gui/gxsforums/GxsForumThreadWidget.cpp @@ -436,31 +436,28 @@ GxsForumThreadWidget::GxsForumThreadWidget(const RsGxsGroupId &forumId, QWidget mEventHandlerId = 0; // Needs to be asynced because this function is likely to be called by another thread! - rsEvents->registerEventsHandler( [this](std::shared_ptr event) { RsQThreadUtils::postToObject( [=]() { handleEvent_main_thread(event); }, this ); }, mEventHandlerId ); + rsEvents->registerEventsHandler(RsEventType::GXS_FORUMS, [this](std::shared_ptr event) { RsQThreadUtils::postToObject( [=]() { handleEvent_main_thread(event); }, this ); }, mEventHandlerId ); } void GxsForumThreadWidget::handleEvent_main_thread(std::shared_ptr event) { - if(event->mType == RsEventType::GXS_FORUMS) - { - const RsGxsForumEvent *e = dynamic_cast(event.get()); + const RsGxsForumEvent *e = dynamic_cast(event.get()); - if(!e) - return; + if(!e) + return; - switch(e->mForumEventCode) - { - case RsGxsForumEvent::ForumEventCode::UPDATED_FORUM: - case RsGxsForumEvent::ForumEventCode::NEW_FORUM: - case RsGxsForumEvent::ForumEventCode::UPDATED_MESSAGE: - case RsGxsForumEvent::ForumEventCode::NEW_MESSAGE: - if(e->mForumGroupId == mForumGroup.mMeta.mGroupId) - updateDisplay(true); - break; - default: - break; - } - } + switch(e->mForumEventCode) + { + case RsGxsForumEvent::ForumEventCode::UPDATED_FORUM: + case RsGxsForumEvent::ForumEventCode::NEW_FORUM: + case RsGxsForumEvent::ForumEventCode::UPDATED_MESSAGE: + case RsGxsForumEvent::ForumEventCode::NEW_MESSAGE: + if(e->mForumGroupId == mForumGroup.mMeta.mGroupId) + updateDisplay(true); + break; + default: + break; + } } void GxsForumThreadWidget::blank() diff --git a/retroshare-gui/src/gui/gxsforums/GxsForumsDialog.cpp b/retroshare-gui/src/gui/gxsforums/GxsForumsDialog.cpp index 0e0a8447a..80f83bd5e 100644 --- a/retroshare-gui/src/gui/gxsforums/GxsForumsDialog.cpp +++ b/retroshare-gui/src/gui/gxsforums/GxsForumsDialog.cpp @@ -45,26 +45,23 @@ GxsForumsDialog::GxsForumsDialog(QWidget *parent) mEventHandlerId = 0; // Needs to be asynced because this function is likely to be called by another thread! - rsEvents->registerEventsHandler( [this](std::shared_ptr event) { RsQThreadUtils::postToObject( [=]() { handleEvent_main_thread(event); }, this ); }, mEventHandlerId ); + rsEvents->registerEventsHandler(RsEventType::GXS_FORUMS, [this](std::shared_ptr event) { RsQThreadUtils::postToObject( [=]() { handleEvent_main_thread(event); }, this ); }, mEventHandlerId ); } void GxsForumsDialog::handleEvent_main_thread(std::shared_ptr event) { - if(event->mType == RsEventType::GXS_FORUMS) - { - const RsGxsForumEvent *e = dynamic_cast(event.get()); + const RsGxsForumEvent *e = dynamic_cast(event.get()); - if(!e) - return; + if(!e) + return; - switch(e->mForumEventCode) - { - case RsGxsForumEvent::ForumEventCode::SUBSCRIBE_STATUS_CHANGED: updateDisplay(true); - break; - default: - break; - } - } + switch(e->mForumEventCode) + { + case RsGxsForumEvent::ForumEventCode::SUBSCRIBE_STATUS_CHANGED: updateDisplay(true); + break; + default: + break; + } } GxsForumsDialog::~GxsForumsDialog() diff --git a/retroshare-gui/src/util/RsGxsUpdateBroadcast.cpp b/retroshare-gui/src/util/RsGxsUpdateBroadcast.cpp index bd2d647f5..934226da4 100644 --- a/retroshare-gui/src/util/RsGxsUpdateBroadcast.cpp +++ b/retroshare-gui/src/util/RsGxsUpdateBroadcast.cpp @@ -39,11 +39,9 @@ RsGxsUpdateBroadcast::RsGxsUpdateBroadcast(RsGxsIfaceHelper *ifaceImpl) : { mEventHandlerId = 0; // forces initialization in registerEventsHandler() - rsEvents->registerEventsHandler( [this](std::shared_ptr event) + rsEvents->registerEventsHandler(RsEventType::GXS_CHANGES, [this](std::shared_ptr event) { - if(event->mType == RsEventType::GXS_CHANGES) - onChangesReceived(*dynamic_cast(event.get())); - + onChangesReceived(*dynamic_cast(event.get())); }, mEventHandlerId ); } From f76b81ba490994040b8309b8a7a85c94fc3cc299 Mon Sep 17 00:00:00 2001 From: csoler Date: Sat, 11 Jan 2020 16:10:28 +0100 Subject: [PATCH 3/4] fixed compilation in jsonapi to cope with new event type field in registerEventHandler() --- libretroshare/src/jsonapi/jsonapi.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/libretroshare/src/jsonapi/jsonapi.cpp b/libretroshare/src/jsonapi/jsonapi.cpp index b9ddb867c..2d1593fcf 100644 --- a/libretroshare/src/jsonapi/jsonapi.cpp +++ b/libretroshare/src/jsonapi/jsonapi.cpp @@ -331,6 +331,15 @@ JsonApiServer::JsonApiServer(): configMutex("JsonApiServer config"), rsEvents, "rsEvents", cAns, session ) ) return; + uint32_t event_type; + + // deserialize input parameters from JSON + { + RsGenericSerializer::SerializeContext& ctx(cReq); + RsGenericSerializer::SerializeJob j(RsGenericSerializer::FROM_JSON); + RS_SERIAL_PROCESS(event_type); + } + const std::weak_ptr weakSession(session); RsEventsHandlerId_t hId = rsEvents->generateUniqueHandlerId(); std::function)> multiCallback = @@ -365,7 +374,7 @@ JsonApiServer::JsonApiServer(): configMutex("JsonApiServer config"), } ); }; - bool retval = rsEvents->registerEventsHandler(multiCallback, hId); + bool retval = rsEvents->registerEventsHandler(static_cast(event_type),multiCallback, hId); { RsGenericSerializer::SerializeContext& ctx(cAns); From 6a4c98b2eda407138fd47ebc8643723c9bf1ea52 Mon Sep 17 00:00:00 2001 From: csoler Date: Sat, 11 Jan 2020 19:01:43 +0100 Subject: [PATCH 4/4] allowed some clients to Event handler to register with type NONE, which means that all event types will be sent to the client --- libretroshare/src/jsonapi/jsonapi.cpp | 6 +-- libretroshare/src/services/rseventsservice.cc | 39 ++++++++----------- 2 files changed, 20 insertions(+), 25 deletions(-) diff --git a/libretroshare/src/jsonapi/jsonapi.cpp b/libretroshare/src/jsonapi/jsonapi.cpp index 2d1593fcf..70443bf5f 100644 --- a/libretroshare/src/jsonapi/jsonapi.cpp +++ b/libretroshare/src/jsonapi/jsonapi.cpp @@ -331,13 +331,13 @@ JsonApiServer::JsonApiServer(): configMutex("JsonApiServer config"), rsEvents, "rsEvents", cAns, session ) ) return; - uint32_t event_type; + RsEventType eventType = RsEventType::NONE; // deserialize input parameters from JSON { RsGenericSerializer::SerializeContext& ctx(cReq); RsGenericSerializer::SerializeJob j(RsGenericSerializer::FROM_JSON); - RS_SERIAL_PROCESS(event_type); + RS_SERIAL_PROCESS(eventType); } const std::weak_ptr weakSession(session); @@ -374,7 +374,7 @@ JsonApiServer::JsonApiServer(): configMutex("JsonApiServer config"), } ); }; - bool retval = rsEvents->registerEventsHandler(static_cast(event_type),multiCallback, hId); + bool retval = rsEvents->registerEventsHandler(eventType,multiCallback, hId); { RsGenericSerializer::SerializeContext& ctx(cAns); diff --git a/libretroshare/src/services/rseventsservice.cc b/libretroshare/src/services/rseventsservice.cc index d8966f0bd..c1aa0958c 100644 --- a/libretroshare/src/services/rseventsservice.cc +++ b/libretroshare/src/services/rseventsservice.cc @@ -176,30 +176,25 @@ void RsEventsService::handleEvent(std::shared_ptr event) { std::function)> mCallback; - uint32_t event_type_index = (uint32_t)event->mType; + uint32_t event_type_index = static_cast(event->mType); - mHandlerMapMtx.lock(); - if(event_type_index >= mHandlerMaps.size() || event_type_index < 1) - { - RsErr() << "Cannot handle an event of type " << event_type_index << ": out of scope!" << std::endl; - return; - } - auto cbpt = mHandlerMaps[event_type_index].begin(); - mHandlerMapMtx.unlock(); - -getHandlerFromMapLock: - mHandlerMapMtx.lock(); - if(cbpt != mHandlerMaps[event_type_index].end()) { - mCallback = cbpt->second; - ++cbpt; - } - mHandlerMapMtx.unlock(); + RS_STACK_MUTEX(mHandlerMapMtx); /* LOCKED AREA */ - if(mCallback) - { - mCallback(event); // It is relevant that this happens outside mutex - mCallback = std::function)>(nullptr); - goto getHandlerFromMapLock; + if(event_type_index >= mHandlerMaps.size() || event_type_index < 1) + { + RsErr() << "Cannot handle an event of type " << event_type_index << ": out of scope!" << std::endl; + return; + } + + // Call all clients that registered a callback for this event type + + for(auto cbit: mHandlerMaps[event_type_index]) + cbit.second(event); + + // Also call all clients that registered with NONE, meaning that they expect all events + + for(auto cbit: mHandlerMaps[static_cast(RsEventType::NONE)]) + cbit.second(event); } }