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