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 |
| 252 | onLocalPrefixUpdated(const QString& localPrefix); |
| 253 | |
| 254 | void |
| 255 | onClose(); |
| 256 | |
| 257 | private slots: |
| 258 | void |
| 259 | onReturnPressed(); |
| 260 | |
| 261 | void |
| 262 | onSyncTreeButtonPressed(); |
| 263 | |
| 264 | void |
| 265 | onTrustTreeButtonPressed(); |
| 266 | |
| 267 | void |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame^] | 268 | 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] | 269 | |
| 270 | void |
| 271 | onProcessTreeUpdate(const std::vector<Sync::MissingDataInfo>&); |
| 272 | |
| 273 | void |
| 274 | onReplot(); |
| 275 | |
| 276 | void |
| 277 | onRosterChanged(QStringList staleUserList); |
| 278 | |
| 279 | void |
| 280 | onInviteListDialogRequested(); |
| 281 | |
| 282 | void |
| 283 | sendJoin(); |
| 284 | |
| 285 | void |
| 286 | sendHello(); |
| 287 | |
| 288 | void |
| 289 | sendLeave(); |
| 290 | |
| 291 | void |
| 292 | enableSyncTreeDisplay(); |
| 293 | |
| 294 | void |
| 295 | reap(); |
| 296 | |
| 297 | void |
| 298 | onSendInvitation(QString invitee); |
| 299 | |
| 300 | void |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame^] | 301 | 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] | 302 | size_t routablePrefixOffset, bool isIntroducer); |
| 303 | |
| 304 | void |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame^] | 305 | onReplyTimeout(const ndn::Interest& interest, size_t routablePrefixOffset); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 306 | |
| 307 | void |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame^] | 308 | 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] | 309 | |
| 310 | void |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame^] | 311 | onIntroCertTimeout(const ndn::Interest& interest, int retry, const QString& msg); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 312 | |
| 313 | private: |
| 314 | Ui::ChatDialog *ui; |
| 315 | ndn::KeyChain m_keyChain; |
| 316 | |
| 317 | ContactManager* m_contactManager; |
| 318 | shared_ptr<ndn::Face> m_face; |
| 319 | |
| 320 | ndn::IdentityCertificate m_myCertificate; |
| 321 | Name m_identity; |
| 322 | |
| 323 | Name m_certListPrefix; |
| 324 | const ndn::RegisteredPrefixId* m_certListPrefixId; |
| 325 | Name m_certSinglePrefix; |
| 326 | const ndn::RegisteredPrefixId* m_certSinglePrefixId; |
| 327 | |
| 328 | std::string m_chatroomName; |
| 329 | Name m_chatroomPrefix; |
| 330 | Name m_localPrefix; |
| 331 | bool m_useRoutablePrefix; |
| 332 | Name m_chatPrefix; |
| 333 | Name m_localChatPrefix; |
| 334 | std::string m_nick; |
| 335 | DigestTreeScene *m_scene; |
| 336 | TrustTreeScene *m_trustScene; |
| 337 | QStringListModel *m_rosterModel; |
| 338 | QTimer* m_timer; |
| 339 | |
| 340 | |
| 341 | int64_t m_lastMsgTime; |
| 342 | int m_randomizedInterval; |
| 343 | bool m_joined; |
| 344 | |
| 345 | Sync::SyncSocket *m_sock; |
| 346 | uint64_t m_session; |
| 347 | shared_ptr<ndn::SecRuleRelative> m_dataRule; |
| 348 | |
| 349 | InviteListDialog* m_inviteListDialog; |
| 350 | shared_ptr<ValidatorInvitation> m_invitationValidator; |
| 351 | |
| 352 | boost::recursive_mutex m_msgMutex; |
| 353 | boost::recursive_mutex m_sceneMutex; |
| 354 | QList<QString> m_zombieList; |
| 355 | int m_zombieIndex; |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame^] | 356 | |
| 357 | //ymj |
| 358 | ChatroomInfo::TrustModel m_trustModel; |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 359 | }; |
| 360 | |
| 361 | } // namespace chronos |
| 362 | |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame^] | 363 | #endif // CHRONOCHAT_CHAT_DIALOG_HPP |