From 19b4fb892c73e6db57cff5f71bfdd66fde152e4b Mon Sep 17 00:00:00 2001 From: Phenom Date: Thu, 27 Apr 2017 16:42:25 +0200 Subject: [PATCH] Add Friend Time Offset news feed. --- libretroshare/src/retroshare/rsnotify.h | 17 ++--- libretroshare/src/retroshare/rsrtt.h | 3 +- libretroshare/src/services/p3rtt.cc | 32 ++++++++- libretroshare/src/services/p3rtt.h | 2 + retroshare-gui/src/gui/NewsFeed.cpp | 37 ++++++++-- retroshare-gui/src/gui/NewsFeed.h | 3 +- retroshare-gui/src/gui/feeds/PeerItem.cpp | 29 +++++++- retroshare-gui/src/gui/feeds/PeerItem.h | 3 + retroshare-gui/src/gui/feeds/PeerItem.ui | 88 ++++++++++++++++------- 9 files changed, 172 insertions(+), 42 deletions(-) diff --git a/libretroshare/src/retroshare/rsnotify.h b/libretroshare/src/retroshare/rsnotify.h index fd09218bd..7ca7385ee 100644 --- a/libretroshare/src/retroshare/rsnotify.h +++ b/libretroshare/src/retroshare/rsnotify.h @@ -78,15 +78,16 @@ const uint32_t RS_FEED_TYPE_SECURITY = 0x0800; const uint32_t RS_FEED_TYPE_POSTED = 0x1000; const uint32_t RS_FEED_TYPE_SECURITY_IP = 0x2000; -const uint32_t RS_FEED_ITEM_PEER_CONNECT = RS_FEED_TYPE_PEER | 0x0001; -const uint32_t RS_FEED_ITEM_PEER_DISCONNECT = RS_FEED_TYPE_PEER | 0x0002; -const uint32_t RS_FEED_ITEM_PEER_HELLO = RS_FEED_TYPE_PEER | 0x0003; -const uint32_t RS_FEED_ITEM_PEER_NEW = RS_FEED_TYPE_PEER | 0x0004; +const uint32_t RS_FEED_ITEM_PEER_CONNECT = RS_FEED_TYPE_PEER | 0x0001; +const uint32_t RS_FEED_ITEM_PEER_DISCONNECT = RS_FEED_TYPE_PEER | 0x0002; +const uint32_t RS_FEED_ITEM_PEER_HELLO = RS_FEED_TYPE_PEER | 0x0003; +const uint32_t RS_FEED_ITEM_PEER_NEW = RS_FEED_TYPE_PEER | 0x0004; +const uint32_t RS_FEED_ITEM_PEER_OFFSET = RS_FEED_TYPE_PEER | 0x0005; -const uint32_t RS_FEED_ITEM_SEC_CONNECT_ATTEMPT = RS_FEED_TYPE_SECURITY | 0x0001; -const uint32_t RS_FEED_ITEM_SEC_AUTH_DENIED = RS_FEED_TYPE_SECURITY | 0x0002; -const uint32_t RS_FEED_ITEM_SEC_UNKNOWN_IN = RS_FEED_TYPE_SECURITY | 0x0003; -const uint32_t RS_FEED_ITEM_SEC_UNKNOWN_OUT = RS_FEED_TYPE_SECURITY | 0x0004; +const uint32_t RS_FEED_ITEM_SEC_CONNECT_ATTEMPT = RS_FEED_TYPE_SECURITY | 0x0001; +const uint32_t RS_FEED_ITEM_SEC_AUTH_DENIED = RS_FEED_TYPE_SECURITY | 0x0002; +const uint32_t RS_FEED_ITEM_SEC_UNKNOWN_IN = RS_FEED_TYPE_SECURITY | 0x0003; +const uint32_t RS_FEED_ITEM_SEC_UNKNOWN_OUT = RS_FEED_TYPE_SECURITY | 0x0004; const uint32_t RS_FEED_ITEM_SEC_WRONG_SIGNATURE = RS_FEED_TYPE_SECURITY | 0x0005; const uint32_t RS_FEED_ITEM_SEC_BAD_CERTIFICATE = RS_FEED_TYPE_SECURITY | 0x0006; const uint32_t RS_FEED_ITEM_SEC_INTERNAL_ERROR = RS_FEED_TYPE_SECURITY | 0x0007; diff --git a/libretroshare/src/retroshare/rsrtt.h b/libretroshare/src/retroshare/rsrtt.h index aba776a17..6d9422562 100644 --- a/libretroshare/src/retroshare/rsrtt.h +++ b/libretroshare/src/retroshare/rsrtt.h @@ -2,7 +2,7 @@ #define RETROSHARE_RTT_INTERFACE_H /* - * libretroshare/src/retroshare: rsrtt.h + * libretroshare/src/retroshare: rsrtt.h * * RetroShare C++ Interface. * @@ -58,6 +58,7 @@ class RsRtt virtual ~RsRtt() { return; } virtual uint32_t getPongResults(const RsPeerId& id, int n, std::list &results) = 0; +virtual double getMeanOffset(const RsPeerId& id) = 0; }; diff --git a/libretroshare/src/services/p3rtt.cc b/libretroshare/src/services/p3rtt.cc index 987467b77..250cea007 100644 --- a/libretroshare/src/services/p3rtt.cc +++ b/libretroshare/src/services/p3rtt.cc @@ -30,11 +30,13 @@ #include "pqi/pqibin.h" #include "pqi/pqistore.h" #include "pqi/p3linkmgr.h" +#include "rsserver/p3face.h" #include "services/p3rtt.h" #include "serialiser/rsrttitems.h" #include +#include /**** * #define DEBUG_RTT 1 @@ -358,7 +360,26 @@ int p3rtt::storePongResult(const RsPeerId& id, uint32_t counter, double recv_ts, peerInfo->mPongResults.pop_front(); } - /* should do calculations */ + //Wait at least 20 pongs before compute mean time offset + if(peerInfo->mPongResults.size() > 20) + { + double mean = 0; + for(std::list::const_iterator prIt = peerInfo->mPongResults.begin(), end = peerInfo->mPongResults.end(); prIt != end; ++ prIt) + { + mean += prIt->mOffset; + } + peerInfo->mCurrentMeanOffset = mean / peerInfo->mPongResults.size(); + if(fabs(peerInfo->mCurrentMeanOffset) > 120) + { + p3Notify *notify = RsServer::notify(); + if (notify) + { + //notify->AddPopupMessage(RS_POPUP_OFFSET, eerInfo->mId.toStdString(),"", "Time Offset: "); + notify->AddFeedItem(RS_FEED_ITEM_PEER_OFFSET, peerInfo->mId.toStdString()); + } + std::cerr << "(WW) Peer:" << peerInfo->mId << " get time offset more than two minutes with you!!!" << std::endl; + } + } return 1; } @@ -379,7 +400,16 @@ uint32_t p3rtt::getPongResults(const RsPeerId& id, int n, std::listmCurrentMeanOffset; + else + return 0; +} RttPeerInfo *p3rtt::locked_GetPeerInfo(const RsPeerId& id) { diff --git a/libretroshare/src/services/p3rtt.h b/libretroshare/src/services/p3rtt.h index 9b881fba8..fdf6ef01c 100644 --- a/libretroshare/src/services/p3rtt.h +++ b/libretroshare/src/services/p3rtt.h @@ -46,6 +46,7 @@ class RttPeerInfo double mCurrentPingTS; double mCurrentPingCounter; bool mCurrentPongRecvd; + double mCurrentMeanOffset; uint32_t mLostPongs; uint32_t mSentPings; @@ -69,6 +70,7 @@ virtual RsServiceInfo getServiceInfo(); /***** overloaded from rsRtt *****/ virtual uint32_t getPongResults(const RsPeerId& id, int n, std::list &results); +virtual double getMeanOffset(const RsPeerId &id); /***** overloaded from p3Service *****/ diff --git a/retroshare-gui/src/gui/NewsFeed.cpp b/retroshare-gui/src/gui/NewsFeed.cpp index a3351d335..fc42faf7f 100644 --- a/retroshare-gui/src/gui/NewsFeed.cpp +++ b/retroshare-gui/src/gui/NewsFeed.cpp @@ -217,13 +217,17 @@ void NewsFeed::updateDisplay() if (flags & RS_FEED_TYPE_PEER) addFeedItemPeerDisconnect(fi); break; + case RS_FEED_ITEM_PEER_HELLO: + if (flags & RS_FEED_TYPE_PEER) + addFeedItemPeerHello(fi); + break; case RS_FEED_ITEM_PEER_NEW: if (flags & RS_FEED_TYPE_PEER) addFeedItemPeerNew(fi); break; - case RS_FEED_ITEM_PEER_HELLO: + case RS_FEED_ITEM_PEER_OFFSET: if (flags & RS_FEED_TYPE_PEER) - addFeedItemPeerHello(fi); + addFeedItemPeerOffset(fi); break; case RS_FEED_ITEM_SEC_CONNECT_ATTEMPT: @@ -412,8 +416,9 @@ void NewsFeed::testFeeds(uint notifyFlags) instance->addFeedItemPeerConnect(fi); instance->addFeedItemPeerDisconnect(fi); - instance->addFeedItemPeerNew(fi); instance->addFeedItemPeerHello(fi); + instance->addFeedItemPeerNew(fi); + instance->addFeedItemPeerOffset(fi); break; case RS_FEED_TYPE_SECURITY: @@ -906,6 +911,7 @@ struct AddFeedItemIfUniqueData AddFeedItemIfUniqueData(FeedItem *feedItem, int type, const RsPeerId &sslId, const std::string& ipAddr, const std::string& ipAddrReported) : mType(type), mSslId(sslId), mIpAddr(ipAddr), mIpAddrReported(ipAddrReported) { + mPeerItem = dynamic_cast(feedItem); mSecItem = dynamic_cast(feedItem); mSecurityIpItem = dynamic_cast(feedItem); } @@ -914,6 +920,7 @@ struct AddFeedItemIfUniqueData const RsPeerId &mSslId; const std::string& mIpAddr; const std::string& mIpAddrReported; + PeerItem *mPeerItem; SecurityItem *mSecItem; SecurityIpItem *mSecurityIpItem; }; @@ -925,6 +932,14 @@ static bool addFeedItemIfUniqueCallback(FeedItem *feedItem, void *data) return false; } + if (findData->mPeerItem) { + PeerItem *peerItem = dynamic_cast(feedItem); + if (peerItem && peerItem->isSame(findData->mSslId, findData->mType)) { + return true; + } + return false; + } + if (findData->mSecItem) { SecurityItem *secitem = dynamic_cast(feedItem); if (secitem && secitem->isSame(findData->mSslId, findData->mType)) { @@ -955,7 +970,7 @@ void NewsFeed::addFeedItemIfUnique(FeedItem *item, int itemType, const RsPeerId return; } - ui->feedWidget->removeFeedItem(item); + ui->feedWidget->removeFeedItem(feedItem); } addFeedItem(item); @@ -1017,6 +1032,20 @@ void NewsFeed::addFeedItemPeerNew(const RsFeedItem &fi) #endif } +void NewsFeed::addFeedItemPeerOffset(const RsFeedItem &fi) +{ + /* make new widget */ + PeerItem *pi = new PeerItem(this, NEWSFEED_PEERLIST, RsPeerId(fi.mId1), PEER_TYPE_OFFSET, false); + + /* add to layout */ + addFeedItemIfUnique(pi, PEER_TYPE_OFFSET, RsPeerId(fi.mId1), "", "", false); + +#ifdef NEWS_DEBUG + std::cerr << "NewsFeed::addFeedItemPeerOffset()"; + std::cerr << std::endl; +#endif +} + void NewsFeed::addFeedItemSecurityConnectAttempt(const RsFeedItem &fi) { /* make new widget */ diff --git a/retroshare-gui/src/gui/NewsFeed.h b/retroshare-gui/src/gui/NewsFeed.h index b49779b2b..28b8dabaa 100644 --- a/retroshare-gui/src/gui/NewsFeed.h +++ b/retroshare-gui/src/gui/NewsFeed.h @@ -87,8 +87,9 @@ private: void addFeedItemPeerConnect(const RsFeedItem &fi); void addFeedItemPeerDisconnect(const RsFeedItem &fi); - void addFeedItemPeerNew(const RsFeedItem &fi); void addFeedItemPeerHello(const RsFeedItem &fi); + void addFeedItemPeerNew(const RsFeedItem &fi); + void addFeedItemPeerOffset(const RsFeedItem &fi); void addFeedItemSecurityConnectAttempt(const RsFeedItem &fi); void addFeedItemSecurityAuthDenied(const RsFeedItem &fi); diff --git a/retroshare-gui/src/gui/feeds/PeerItem.cpp b/retroshare-gui/src/gui/feeds/PeerItem.cpp index 62e92e5c1..3eddd5718 100644 --- a/retroshare-gui/src/gui/feeds/PeerItem.cpp +++ b/retroshare-gui/src/gui/feeds/PeerItem.cpp @@ -34,6 +34,7 @@ #include #include +#include /***** * #define DEBUG_ITEM 1 @@ -68,6 +69,16 @@ PeerItem::PeerItem(FeedHolder *parent, uint32_t feedId, const RsPeerId &peerId, } +bool PeerItem::isSame(const RsPeerId &peerId, uint32_t type) +{ + if ((mPeerId == peerId) && (mType == type)) + { + return true; + } + return false; +} + + void PeerItem::updateItemStatic() { if (!rsPeers) @@ -95,6 +106,9 @@ void PeerItem::updateItemStatic() case PEER_TYPE_NEW_FOF: title = tr("Friend of Friend"); break; + case PEER_TYPE_OFFSET: + title = tr("Friend Time Offset"); + break; default: title = tr("Peer"); break; @@ -111,8 +125,14 @@ void PeerItem::updateItemStatic() /* expanded Info */ nameLabel->setText(QString::fromUtf8(details.name.c_str())); - idLabel->setText(QString::fromStdString(details.id.toStdString())); + idLabel->setText(QString::fromStdString(details.id.toStdString())); locLabel->setText(QString::fromUtf8(details.location.c_str())); + + if (rsRtt) + { + double offset = rsRtt->getMeanOffset(RsPeerId(mPeerId)); + offsetLabel->setText(QString::number(offset,'f',2).append(" s")); + } } else { @@ -187,6 +207,13 @@ void PeerItem::updateItem() { sendmsgButton->setEnabled(false); } + + if (rsRtt) + { + double offset = rsRtt->getMeanOffset(RsPeerId(mPeerId)); + offsetLabel->setText(QString::number(offset,'f',2).append(" s")); + } + } /* slow Tick */ diff --git a/retroshare-gui/src/gui/feeds/PeerItem.h b/retroshare-gui/src/gui/feeds/PeerItem.h index 6f8f9a85d..9bccaeb1c 100644 --- a/retroshare-gui/src/gui/feeds/PeerItem.h +++ b/retroshare-gui/src/gui/feeds/PeerItem.h @@ -30,6 +30,7 @@ const uint32_t PEER_TYPE_STD = 0x0001; const uint32_t PEER_TYPE_CONNECT = 0x0002; const uint32_t PEER_TYPE_HELLO = 0x0003; /* failed Connect Attempt */ const uint32_t PEER_TYPE_NEW_FOF = 0x0004; /* new Friend of Friend */ +const uint32_t PEER_TYPE_OFFSET = 0x0005; /* received time offset */ class FeedHolder; @@ -43,6 +44,8 @@ public: void updateItemStatic(); + bool isSame(const RsPeerId &peerId, uint32_t type); + protected: /* FeedItem */ virtual void doExpand(bool open); diff --git a/retroshare-gui/src/gui/feeds/PeerItem.ui b/retroshare-gui/src/gui/feeds/PeerItem.ui index 49eb48937..dc37abc28 100644 --- a/retroshare-gui/src/gui/feeds/PeerItem.ui +++ b/retroshare-gui/src/gui/feeds/PeerItem.ui @@ -7,11 +7,20 @@ 0 0 476 - 247 + 250 - - + + + 1 + + + 1 + + + 1 + + 1 @@ -95,13 +104,13 @@ QFrame::Sunken - + 0 - + 0 @@ -128,7 +137,7 @@ - + @@ -165,9 +174,9 @@ - + - + Qt::Horizontal @@ -204,9 +213,9 @@ - + - + Qt::Horizontal @@ -317,13 +326,13 @@ - + - + - + - + 75 @@ -336,7 +345,7 @@ - + 75 @@ -349,7 +358,7 @@ - + 0 @@ -368,7 +377,7 @@ - + 75 @@ -381,7 +390,7 @@ - + 75 @@ -394,7 +403,7 @@ - + 75 @@ -407,7 +416,7 @@ - + 0 @@ -426,10 +435,30 @@ + + + + + 0 + 0 + + + + + 75 + true + true + + + + Time offset: + + + - + @@ -509,6 +538,13 @@ + + + + TextLabel + + + @@ -531,17 +567,17 @@ + + StyledLabel + QLabel +
gui/common/StyledLabel.h
+
AvatarWidget QLabel
gui/common/AvatarWidget.h
1
- - StyledLabel - QLabel -
gui/common/StyledLabel.h
-