Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [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: Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 8 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 9 | * Yingdi Yu <yingdi@cs.ucla.edu> |
| 10 | */ |
| 11 | |
| 12 | #ifndef CHRONOS_CHAT_DIALOG_HPP |
| 13 | #define CHRONOS_CHAT_DIALOG_HPP |
| 14 | |
| 15 | #include <QDialog> |
| 16 | #include <QTextTable> |
| 17 | #include <QStringListModel> |
| 18 | #include <QSystemTrayIcon> |
| 19 | #include <QMenu> |
| 20 | #include <QTimer> |
| 21 | |
| 22 | #ifndef Q_MOC_RUN |
| 23 | #include "common.hpp" |
| 24 | #include "contact-manager.hpp" |
| 25 | #include "invitation.hpp" |
| 26 | #include "contact.hpp" |
| 27 | #include "chatbuf.pb.h" |
| 28 | #include "intro-cert-list.pb.h" |
| 29 | #include "digest-tree-scene.hpp" |
| 30 | #include "trust-tree-scene.hpp" |
| 31 | #include "trust-tree-node.hpp" |
| 32 | #include "validator-invitation.hpp" |
| 33 | #include <sync-socket.h> |
| 34 | #include <sync-seq-no.h> |
| 35 | #include <ndn-cxx/security/key-chain.hpp> |
| 36 | #include <boost/thread/locks.hpp> |
| 37 | #include <boost/thread/recursive_mutex.hpp> |
| 38 | #include <boost/thread/thread.hpp> |
| 39 | #endif |
| 40 | |
| 41 | #include "invite-list-dialog.hpp" |
| 42 | |
| 43 | |
| 44 | #define MAX_HISTORY_ENTRY 20 |
| 45 | |
| 46 | namespace Ui { |
| 47 | class ChatDialog; |
| 48 | } |
| 49 | |
| 50 | namespace chronos { |
| 51 | |
| 52 | class ChatDialog : public QDialog |
| 53 | { |
| 54 | Q_OBJECT |
| 55 | |
| 56 | public: |
| 57 | explicit |
| 58 | ChatDialog(ContactManager* contactManager, |
| 59 | shared_ptr<ndn::Face> face, |
| 60 | const ndn::IdentityCertificate& myCertificate, |
| 61 | const Name& chatroomPrefix, |
| 62 | const Name& localPrefix, |
| 63 | const std::string& nick, |
| 64 | bool witSecurity, |
| 65 | QWidget* parent = 0); |
| 66 | |
| 67 | ~ChatDialog(); |
| 68 | |
| 69 | void |
| 70 | addSyncAnchor(const Invitation& invitation); |
| 71 | |
| 72 | void |
| 73 | processTreeUpdateWrapper(const std::vector<Sync::MissingDataInfo>&, Sync::SyncSocket *); |
| 74 | |
| 75 | void |
| 76 | processDataWrapper(const shared_ptr<const Data>& data); |
| 77 | |
| 78 | void |
| 79 | processDataNoShowWrapper(const shared_ptr<const Data>& data); |
| 80 | |
| 81 | void |
| 82 | processRemoveWrapper(const std::string& prefix); |
| 83 | |
| 84 | protected: |
| 85 | void |
| 86 | closeEvent(QCloseEvent* e); |
| 87 | |
| 88 | void |
| 89 | changeEvent(QEvent* e); |
| 90 | |
| 91 | void |
| 92 | resizeEvent(QResizeEvent* e); |
| 93 | |
| 94 | void |
| 95 | showEvent(QShowEvent* e); |
| 96 | |
| 97 | private: |
| 98 | void |
| 99 | updatePrefix(); |
| 100 | |
| 101 | void |
| 102 | updateLabels(); |
| 103 | |
| 104 | void |
| 105 | initializeSync(); |
| 106 | |
| 107 | void |
| 108 | sendInvitation(shared_ptr<Contact> contact, bool isIntroducer); |
| 109 | |
| 110 | void |
| 111 | replyWrapper(const Interest& interest, Data& data, |
| 112 | size_t routablePrefixOffset, bool isIntroducer); |
| 113 | |
| 114 | void |
| 115 | replyTimeoutWrapper(const Interest& interest, |
| 116 | size_t routablePrefixOffset); |
| 117 | |
| 118 | void |
| 119 | onReplyValidated(const shared_ptr<const Data>& data, |
| 120 | size_t inviteeRoutablePrefixOffset, |
| 121 | bool isIntroduce); |
| 122 | |
| 123 | void |
| 124 | onReplyValidationFailed(const shared_ptr<const Data>& data, |
| 125 | const std::string& failureInfo); |
| 126 | |
| 127 | void |
| 128 | invitationRejected(const Name& identity); |
| 129 | |
| 130 | void |
| 131 | invitationAccepted(const ndn::IdentityCertificate& inviteeCert, |
| 132 | const Name& inviteePrefix, bool isIntroducer); |
| 133 | |
| 134 | void |
| 135 | fetchIntroCert(const Name& identity, const Name& prefix); |
| 136 | |
| 137 | void |
| 138 | onIntroCertList(const Interest& interest, const Data& data); |
| 139 | |
| 140 | void |
| 141 | onIntroCertListTimeout(const Interest& interest, int retry, const std::string& msg); |
| 142 | |
| 143 | void |
| 144 | introCertWrapper(const Interest& interest, Data& data); |
| 145 | |
| 146 | void |
| 147 | introCertTimeoutWrapper(const Interest& interest, int retry, const QString& msg); |
| 148 | |
| 149 | void |
| 150 | onCertListInterest(const Name& prefix, const Interest& interest); |
| 151 | |
| 152 | void |
| 153 | onCertListRegisterFailed(const Name& prefix, const std::string& msg); |
| 154 | |
| 155 | void |
| 156 | onCertSingleInterest(const Name& prefix, const Interest& interest); |
| 157 | |
| 158 | void |
| 159 | onCertSingleRegisterFailed(const Name& prefix, const std::string& msg); |
| 160 | |
| 161 | |
| 162 | void |
| 163 | sendMsg(SyncDemo::ChatMessage& msg); |
| 164 | |
| 165 | void |
| 166 | disableSyncTreeDisplay(); |
| 167 | |
| 168 | void |
| 169 | appendMessage(const SyncDemo::ChatMessage msg, bool isHistory = false); |
| 170 | |
| 171 | void |
| 172 | processRemove(QString prefix); |
| 173 | |
| 174 | ndn::Name |
| 175 | getInviteeRoutablePrefix(const Name& invitee); |
| 176 | |
| 177 | void |
| 178 | formChatMessage(const QString& text, SyncDemo::ChatMessage& msg); |
| 179 | |
| 180 | void |
| 181 | formControlMessage(SyncDemo::ChatMessage &msg, SyncDemo::ChatMessage::ChatMessageType type); |
| 182 | |
| 183 | QString |
| 184 | formatTime(time_t timestamp); |
| 185 | |
| 186 | void |
| 187 | printTimeInCell(QTextTable* table, time_t timestamp); |
| 188 | |
| 189 | std::string |
| 190 | getRandomString(); |
| 191 | |
| 192 | void |
| 193 | showMessage(const QString&, const QString&); |
| 194 | |
| 195 | void |
| 196 | fitView(); |
| 197 | |
| 198 | void |
| 199 | summonReaper(); |
| 200 | |
| 201 | void |
| 202 | getTree(TrustTreeNodeList& nodeList); |
| 203 | |
| 204 | void |
| 205 | plotTrustTree(); |
| 206 | |
| 207 | signals: |
| 208 | void |
| 209 | processData(const shared_ptr<const Data>& data, bool show, bool isHistory); |
| 210 | |
| 211 | void |
| 212 | processTreeUpdate(const std::vector<Sync::MissingDataInfo>); |
| 213 | |
| 214 | void |
| 215 | closeChatDialog(const QString& chatroomName); |
| 216 | |
| 217 | void |
| 218 | inivationRejection(const QString& msg); |
| 219 | |
| 220 | void |
| 221 | showChatMessage(const QString& chatroomName, const QString& from, const QString& data); |
| 222 | |
| 223 | void |
| 224 | resetIcon(); |
| 225 | |
| 226 | void |
| 227 | reply(const Interest& interest, const shared_ptr<const Data>& data, |
| 228 | size_t routablePrefixOffset, bool isIntroducer); |
| 229 | |
| 230 | void |
| 231 | replyTimeout(const Interest& interest, size_t routablePrefixOffset); |
| 232 | |
| 233 | void |
| 234 | introCert(const Interest& interest, const shared_ptr<const Data>& data); |
| 235 | |
| 236 | void |
| 237 | introCertTimeout(const Interest& interest, int retry, const QString& msg); |
| 238 | |
| 239 | void |
| 240 | waitForContactList(); |
| 241 | |
| 242 | public slots: |
| 243 | void |
| 244 | onLocalPrefixUpdated(const QString& localPrefix); |
| 245 | |
| 246 | void |
| 247 | onClose(); |
| 248 | |
| 249 | private slots: |
| 250 | void |
| 251 | onReturnPressed(); |
| 252 | |
| 253 | void |
| 254 | onSyncTreeButtonPressed(); |
| 255 | |
| 256 | void |
| 257 | onTrustTreeButtonPressed(); |
| 258 | |
| 259 | void |
| 260 | onProcessData(const shared_ptr<const Data>& data, bool show, bool isHistory); |
| 261 | |
| 262 | void |
| 263 | onProcessTreeUpdate(const std::vector<Sync::MissingDataInfo>&); |
| 264 | |
| 265 | void |
| 266 | onReplot(); |
| 267 | |
| 268 | void |
| 269 | onRosterChanged(QStringList staleUserList); |
| 270 | |
| 271 | void |
| 272 | onInviteListDialogRequested(); |
| 273 | |
| 274 | void |
| 275 | sendJoin(); |
| 276 | |
| 277 | void |
| 278 | sendHello(); |
| 279 | |
| 280 | void |
| 281 | sendLeave(); |
| 282 | |
| 283 | void |
| 284 | enableSyncTreeDisplay(); |
| 285 | |
| 286 | void |
| 287 | reap(); |
| 288 | |
| 289 | void |
| 290 | onSendInvitation(QString invitee); |
| 291 | |
| 292 | void |
| 293 | onReply(const Interest& interest, const shared_ptr<const Data>& data, |
| 294 | size_t routablePrefixOffset, bool isIntroducer); |
| 295 | |
| 296 | void |
| 297 | onReplyTimeout(const Interest& interest, size_t routablePrefixOffset); |
| 298 | |
| 299 | void |
| 300 | onIntroCert(const Interest& interest, const shared_ptr<const Data>& data); |
| 301 | |
| 302 | void |
| 303 | onIntroCertTimeout(const Interest& interest, int retry, const QString& msg); |
| 304 | |
| 305 | private: |
| 306 | Ui::ChatDialog *ui; |
| 307 | ndn::KeyChain m_keyChain; |
| 308 | |
| 309 | ContactManager* m_contactManager; |
| 310 | shared_ptr<ndn::Face> m_face; |
| 311 | |
| 312 | ndn::IdentityCertificate m_myCertificate; |
| 313 | Name m_identity; |
| 314 | |
| 315 | Name m_certListPrefix; |
| 316 | const ndn::RegisteredPrefixId* m_certListPrefixId; |
| 317 | Name m_certSinglePrefix; |
| 318 | const ndn::RegisteredPrefixId* m_certSinglePrefixId; |
| 319 | |
| 320 | std::string m_chatroomName; |
| 321 | Name m_chatroomPrefix; |
| 322 | Name m_localPrefix; |
| 323 | bool m_useRoutablePrefix; |
| 324 | Name m_chatPrefix; |
| 325 | Name m_localChatPrefix; |
| 326 | std::string m_nick; |
| 327 | DigestTreeScene *m_scene; |
| 328 | TrustTreeScene *m_trustScene; |
| 329 | QStringListModel *m_rosterModel; |
| 330 | QTimer* m_timer; |
| 331 | |
| 332 | |
| 333 | int64_t m_lastMsgTime; |
| 334 | int m_randomizedInterval; |
| 335 | bool m_joined; |
| 336 | |
| 337 | Sync::SyncSocket *m_sock; |
| 338 | uint64_t m_session; |
| 339 | shared_ptr<ndn::SecRuleRelative> m_dataRule; |
| 340 | |
| 341 | InviteListDialog* m_inviteListDialog; |
| 342 | shared_ptr<ValidatorInvitation> m_invitationValidator; |
| 343 | |
| 344 | boost::recursive_mutex m_msgMutex; |
| 345 | boost::recursive_mutex m_sceneMutex; |
| 346 | QList<QString> m_zombieList; |
| 347 | int m_zombieIndex; |
| 348 | }; |
| 349 | |
| 350 | } // namespace chronos |
| 351 | |
| 352 | #endif // CHRONOS_CHAT_DIALOG_HPP |