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 <ndn-cxx/util/scheduler.hpp> |
| 20 | #include <boost/random.hpp> |
| 21 | #include <mutex> |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 22 | #include <socket.hpp> |
| 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, |
| 58 | QObject* parent = 0); |
| 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 | |
| 127 | public slots: |
| 128 | |
| 129 | /** |
| 130 | * @brief update routing prefix |
| 131 | * |
| 132 | * @param routingPrefix the new routing prefix |
| 133 | */ |
| 134 | void |
| 135 | updateRoutingPrefix(const QString& routingPrefix); |
| 136 | |
| 137 | /** |
| 138 | * @brief erase a participant in a chatroom's roster |
| 139 | * |
| 140 | * This slot is called by chat dialog will erase a participant from a chatroom. |
| 141 | * If the user is the manager of this chatroom, he will publish an update. |
| 142 | * |
| 143 | * @param sessionPrefix the prefix of the participant to erase |
| 144 | * @param chatroomName the name of the chatroom |
| 145 | */ |
| 146 | void |
| 147 | onEraseInRoster(ndn::Name sessionPrefix, ndn::Name::Component chatroomName); |
| 148 | |
| 149 | /** |
| 150 | * @brief add a participant in a chatroom's roster |
| 151 | * |
| 152 | * This slot is called by chat dialog and will add a participant from a chatroom. |
| 153 | * If the user is the manager of this chatroom, he will publish an update. |
| 154 | * |
| 155 | * @param sessionPrefix the prefix of the participant to erase |
| 156 | * @param chatroomName the name of the chatroom |
| 157 | */ |
| 158 | void |
| 159 | onAddInRoster(ndn::Name sessionPrefix, ndn::Name::Component chatroomName); |
| 160 | |
| 161 | /** |
| 162 | * @brief is called when user himself join a chatroom |
| 163 | * |
| 164 | * This slot is called by controller and will modify the chatroom list in discovery |
| 165 | * |
| 166 | * @param chatroomName the name of chatroom the user join |
| 167 | */ |
| 168 | void |
| 169 | onNewChatroomForDiscovery(Name::Component chatroomName); |
| 170 | |
| 171 | /** |
| 172 | * @brief get chatroom info from chat dialog |
| 173 | * |
| 174 | * This slot is called by controller. It get the chatroom info of a chatroom and |
| 175 | * if the user is the manager, he will publish an update |
| 176 | * |
| 177 | * @param chatroomInfo chatroom info |
| 178 | * @param isManager whether the user is the manager of the chatroom |
| 179 | */ |
| 180 | void |
| 181 | onRespondChatroomInfoRequest(ChatroomInfo chatroomInfo, bool isManager); |
| 182 | |
| 183 | /** |
| 184 | * @brief reset when the identity updates |
| 185 | * |
| 186 | * This slot is called when the identity is updated, it will update the identity and |
| 187 | * reset the socket |
| 188 | * |
| 189 | * @param identity new identity |
| 190 | */ |
| 191 | void |
| 192 | onIdentityUpdated(const QString& identity); |
| 193 | |
| 194 | /** |
| 195 | * @brief prepare chatroom info for discovery panel |
| 196 | * |
| 197 | * This slot is called by discovery panel when it wants to display the info of a chatroom |
| 198 | * |
| 199 | * @param chatroomName the name of chatroom the discovery panel requested |
| 200 | */ |
| 201 | void |
| 202 | onWaitForChatroomInfo(const QString& chatroomName); |
| 203 | |
| 204 | void |
| 205 | shutdown(); |
| 206 | |
| 207 | private: |
| 208 | |
| 209 | typedef std::map<ndn::Name::Component, ChatroomInfoBackend> ChatroomList; |
| 210 | |
| 211 | Name m_discoveryPrefix; |
| 212 | Name m_routableUserDiscoveryPrefix; |
| 213 | Name m_routingPrefix; |
| 214 | Name m_userDiscoveryPrefix; |
| 215 | Name m_identity; |
| 216 | |
| 217 | boost::mt19937 m_randomGenerator; |
| 218 | boost::variate_generator<boost::mt19937&, boost::uniform_int<> > m_rangeUniformRandom; |
| 219 | |
| 220 | shared_ptr<ndn::Face> m_face; |
| 221 | |
| 222 | unique_ptr<ndn::Scheduler> m_scheduler; // scheduler |
| 223 | ndn::EventId m_refreshPanelId; |
| 224 | shared_ptr<chronosync::Socket> m_sock; // SyncSocket |
| 225 | |
| 226 | ChatroomList m_chatroomList; |
Qiuhan Ding | f22c41b | 2015-03-11 13:19:01 -0700 | [diff] [blame^] | 227 | std::mutex m_mutex; |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 228 | |
| 229 | bool m_shouldResume; |
| 230 | }; |
| 231 | |
| 232 | } // namespace chronochat |
| 233 | |
| 234 | #endif // CHRONOCHAT_CHATROOM_DISCOVERY_BACKEND_HPP |