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