Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 3 | * Copyright (c) 2020, Regents of the University of California |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 4 | * |
| 5 | * BSD license, See the LICENSE file for more information |
| 6 | * |
| 7 | * Author: Qiuhan Ding <qiuhanding@cs.ucla.edu> |
| 8 | * Yingdi Yu <yingdi@cs.ucla.edu> |
| 9 | */ |
| 10 | |
| 11 | #ifndef CHRONOCHAT_CHATROOM_DISCOVERY_BACKEND_HPP |
| 12 | #define CHRONOCHAT_CHATROOM_DISCOVERY_BACKEND_HPP |
| 13 | |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 14 | #include <QThread> |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 15 | |
| 16 | #ifndef Q_MOC_RUN |
| 17 | #include "common.hpp" |
| 18 | #include "chatroom-info.hpp" |
Qiuhan Ding | f22c41b | 2015-03-11 13:19:01 -0700 | [diff] [blame] | 19 | #include <boost/random.hpp> |
| 20 | #include <mutex> |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 21 | #include <ChronoSync/socket.hpp> |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 22 | #include <boost/thread.hpp> |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 23 | #endif |
| 24 | |
| 25 | namespace chronochat { |
| 26 | |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 27 | class ChatroomInfoBackend |
| 28 | { |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 29 | public: |
| 30 | std::string chatroomName; |
| 31 | Name chatroomPrefix; |
| 32 | ChatroomInfo info; |
| 33 | // For a chatroom's user to check whether his own chatroom is alive |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 34 | ndn::scheduler::EventId localChatroomTimeoutEventId; |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 35 | // If the manager no longer exist, set a random timer to compete for manager |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 36 | ndn::scheduler::EventId managerSelectionTimeoutEventId; |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 37 | // For a user to check the status of the chatroom that he is not in. |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 38 | ndn::scheduler::EventId remoteChatroomTimeoutEventId; |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 39 | // If the user is manager, he will need the helloEventId to keep track of hello message |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 40 | ndn::scheduler::ScopedEventId helloTimeoutEventId; |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 41 | // To tell whether the user is in this chatroom |
| 42 | bool isParticipant; |
| 43 | // To tell whether the user is the manager |
| 44 | bool isManager; |
| 45 | // Variable to tell whether another manager exists |
| 46 | int count; |
| 47 | // Variable to tell whether to print on the panel |
| 48 | bool isPrint; |
| 49 | |
| 50 | }; |
| 51 | |
| 52 | class ChatroomDiscoveryBackend : public QThread |
| 53 | { |
| 54 | Q_OBJECT |
| 55 | |
| 56 | public: |
| 57 | ChatroomDiscoveryBackend(const Name& routingPrefix, |
| 58 | const Name& identity, |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 59 | QObject* parent = nullptr); |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 60 | |
| 61 | ~ChatroomDiscoveryBackend(); |
| 62 | |
| 63 | |
| 64 | protected: |
| 65 | void |
| 66 | run(); |
| 67 | |
| 68 | private: |
| 69 | void |
| 70 | initializeSync(); |
| 71 | |
| 72 | void |
| 73 | close(); |
| 74 | |
| 75 | void |
| 76 | processSyncUpdate(const std::vector<chronosync::MissingDataInfo>& updates); |
| 77 | |
| 78 | void |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 79 | processChatroomData(const ndn::Data& data); |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 80 | |
| 81 | void |
| 82 | localSessionTimeout(const Name::Component& chatroomName); |
| 83 | |
| 84 | void |
| 85 | remoteSessionTimeout(const Name::Component& chatroomName); |
| 86 | |
| 87 | void |
| 88 | randomSessionTimeout(const Name::Component& chatroomName); |
| 89 | |
| 90 | void |
| 91 | sendUpdate(const Name::Component& chatroomName); |
| 92 | |
| 93 | void |
| 94 | updatePrefixes(); |
| 95 | |
| 96 | void |
| 97 | sendChatroomList(); |
| 98 | |
| 99 | signals: |
| 100 | /** |
| 101 | * @brief request to get chatroom info |
| 102 | * |
| 103 | * This signal will be sent to controller to get chatroom info |
| 104 | * |
| 105 | * @param chatroomName specify which chatroom's info to request |
| 106 | * @param isManager if the user who send the signal is the manager of the chatroom |
| 107 | */ |
| 108 | void |
| 109 | chatroomInfoRequest(std::string chatroomName, bool isManager); |
| 110 | |
| 111 | /** |
| 112 | * @brief send chatroom list to front end |
| 113 | * |
| 114 | * @param chatroomList the list of chatrooms |
| 115 | */ |
| 116 | void |
| 117 | chatroomListReady(const QStringList& chatroomList); |
| 118 | |
| 119 | /** |
| 120 | * @brief send chatroom info to front end |
| 121 | * |
| 122 | * @param info the chatroom info request by front end |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 123 | * @param isParticipant if the user is a participant of the chatroom |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 124 | */ |
| 125 | void |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 126 | chatroomInfoReady(const ChatroomInfo& info, bool isParticipant); |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 127 | |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 128 | void |
| 129 | nfdError(); |
| 130 | |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 131 | public slots: |
| 132 | |
| 133 | /** |
| 134 | * @brief update routing prefix |
| 135 | * |
| 136 | * @param routingPrefix the new routing prefix |
| 137 | */ |
| 138 | void |
| 139 | updateRoutingPrefix(const QString& routingPrefix); |
| 140 | |
| 141 | /** |
| 142 | * @brief erase a participant in a chatroom's roster |
| 143 | * |
| 144 | * This slot is called by chat dialog will erase a participant from a chatroom. |
| 145 | * If the user is the manager of this chatroom, he will publish an update. |
| 146 | * |
| 147 | * @param sessionPrefix the prefix of the participant to erase |
| 148 | * @param chatroomName the name of the chatroom |
| 149 | */ |
| 150 | void |
| 151 | onEraseInRoster(ndn::Name sessionPrefix, ndn::Name::Component chatroomName); |
| 152 | |
| 153 | /** |
| 154 | * @brief add a participant in a chatroom's roster |
| 155 | * |
| 156 | * This slot is called by chat dialog and will add a participant from a chatroom. |
| 157 | * If the user is the manager of this chatroom, he will publish an update. |
| 158 | * |
| 159 | * @param sessionPrefix the prefix of the participant to erase |
| 160 | * @param chatroomName the name of the chatroom |
| 161 | */ |
| 162 | void |
| 163 | onAddInRoster(ndn::Name sessionPrefix, ndn::Name::Component chatroomName); |
| 164 | |
| 165 | /** |
| 166 | * @brief is called when user himself join a chatroom |
| 167 | * |
| 168 | * This slot is called by controller and will modify the chatroom list in discovery |
| 169 | * |
| 170 | * @param chatroomName the name of chatroom the user join |
| 171 | */ |
| 172 | void |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 173 | onNewChatroomForDiscovery(ndn::Name::Component chatroomName); |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 174 | |
| 175 | /** |
| 176 | * @brief get chatroom info from chat dialog |
| 177 | * |
| 178 | * This slot is called by controller. It get the chatroom info of a chatroom and |
| 179 | * if the user is the manager, he will publish an update |
| 180 | * |
| 181 | * @param chatroomInfo chatroom info |
| 182 | * @param isManager whether the user is the manager of the chatroom |
| 183 | */ |
| 184 | void |
| 185 | onRespondChatroomInfoRequest(ChatroomInfo chatroomInfo, bool isManager); |
| 186 | |
| 187 | /** |
| 188 | * @brief reset when the identity updates |
| 189 | * |
| 190 | * This slot is called when the identity is updated, it will update the identity and |
| 191 | * reset the socket |
| 192 | * |
| 193 | * @param identity new identity |
| 194 | */ |
| 195 | void |
| 196 | onIdentityUpdated(const QString& identity); |
| 197 | |
| 198 | /** |
| 199 | * @brief prepare chatroom info for discovery panel |
| 200 | * |
| 201 | * This slot is called by discovery panel when it wants to display the info of a chatroom |
| 202 | * |
| 203 | * @param chatroomName the name of chatroom the discovery panel requested |
| 204 | */ |
| 205 | void |
| 206 | onWaitForChatroomInfo(const QString& chatroomName); |
| 207 | |
| 208 | void |
| 209 | shutdown(); |
| 210 | |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 211 | void |
| 212 | onNfdReconnect(); |
| 213 | |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 214 | private: |
| 215 | |
| 216 | typedef std::map<ndn::Name::Component, ChatroomInfoBackend> ChatroomList; |
| 217 | |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 218 | bool m_shouldResume; |
| 219 | bool m_isNfdConnected; |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 220 | Name m_discoveryPrefix; |
| 221 | Name m_routableUserDiscoveryPrefix; |
| 222 | Name m_routingPrefix; |
| 223 | Name m_userDiscoveryPrefix; |
| 224 | Name m_identity; |
| 225 | |
| 226 | boost::mt19937 m_randomGenerator; |
| 227 | boost::variate_generator<boost::mt19937&, boost::uniform_int<> > m_rangeUniformRandom; |
| 228 | |
| 229 | shared_ptr<ndn::Face> m_face; |
| 230 | |
| 231 | unique_ptr<ndn::Scheduler> m_scheduler; // scheduler |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 232 | ndn::scheduler::ScopedEventId m_refreshPanelId; |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 233 | shared_ptr<chronosync::Socket> m_sock; // SyncSocket |
| 234 | |
| 235 | ChatroomList m_chatroomList; |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 236 | std::mutex m_resumeMutex; |
| 237 | std::mutex m_nfdConnectionMutex; |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 238 | |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 239 | }; |
| 240 | |
| 241 | } // namespace chronochat |
| 242 | |
| 243 | #endif // CHRONOCHAT_CHATROOM_DISCOVERY_BACKEND_HPP |