Yingdi Yu | d45777b | 2014-10-16 23:54:11 -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: Yingdi Yu <yingdi@cs.ucla.edu> |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 8 | * Qiuhan Ding <qiuhanding@cs.ucla.edu> |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #ifndef CHRONOCHAT_CHAT_DIALOG_BACKEND_HPP |
| 12 | #define CHRONOCHAT_CHAT_DIALOG_BACKEND_HPP |
| 13 | |
| 14 | #include <QThread> |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 15 | |
| 16 | #ifndef Q_MOC_RUN |
| 17 | #include "common.hpp" |
| 18 | #include "chatroom-info.hpp" |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 19 | #include "chat-message.hpp" |
Qiuhan Ding | f22c41b | 2015-03-11 13:19:01 -0700 | [diff] [blame] | 20 | #include <mutex> |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 21 | #include <socket.hpp> |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 22 | #include <boost/thread.hpp> |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 23 | #endif |
| 24 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 25 | namespace chronochat { |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 26 | |
| 27 | class NodeInfo { |
| 28 | public: |
| 29 | QString sessionPrefix; |
| 30 | chronosync::SeqNo seqNo; |
| 31 | }; |
| 32 | |
| 33 | class UserInfo { |
| 34 | public: |
| 35 | ndn::Name sessionPrefix; |
| 36 | bool hasNick; |
| 37 | std::string userNick; |
| 38 | ndn::EventId timeoutEventId; |
| 39 | }; |
| 40 | |
| 41 | class ChatDialogBackend : public QThread |
| 42 | { |
| 43 | Q_OBJECT |
| 44 | |
| 45 | public: |
| 46 | ChatDialogBackend(const Name& chatroomPrefix, |
| 47 | const Name& userChatPrefix, |
| 48 | const Name& routingPrefix, |
| 49 | const std::string& chatroomName, |
| 50 | const std::string& nick, |
Yingdi Yu | 45da92a | 2015-02-02 13:17:03 -0800 | [diff] [blame] | 51 | const Name& signingId = Name(), |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 52 | QObject* parent = nullptr); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 53 | |
| 54 | ~ChatDialogBackend(); |
| 55 | |
| 56 | protected: |
| 57 | void |
| 58 | run(); |
| 59 | |
| 60 | private: |
| 61 | void |
| 62 | initializeSync(); |
| 63 | |
Yingdi Yu | 45da92a | 2015-02-02 13:17:03 -0800 | [diff] [blame] | 64 | shared_ptr<ndn::IdentityCertificate> |
| 65 | loadTrustAnchor(); |
| 66 | |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 67 | void |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 68 | exitChatroom(); |
| 69 | |
| 70 | void |
Yingdi Yu | 4647f02 | 2015-02-01 00:26:38 -0800 | [diff] [blame] | 71 | close(); |
| 72 | |
| 73 | void |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 74 | processSyncUpdate(const std::vector<chronosync::MissingDataInfo>& updates); |
| 75 | |
| 76 | void |
Yingdi Yu | 45da92a | 2015-02-02 13:17:03 -0800 | [diff] [blame] | 77 | processChatData(const ndn::shared_ptr<const ndn::Data>& data, |
| 78 | bool needDisplay, |
| 79 | bool isValidated); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 80 | |
| 81 | void |
| 82 | remoteSessionTimeout(const Name& sessionPrefix); |
| 83 | |
| 84 | void |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 85 | sendMsg(ChatMessage& msg); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 86 | |
| 87 | void |
| 88 | sendJoin(); |
| 89 | |
| 90 | void |
| 91 | sendHello(); |
| 92 | |
| 93 | void |
| 94 | sendLeave(); |
| 95 | |
| 96 | void |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 97 | prepareControlMessage(ChatMessage& msg, |
| 98 | ChatMessage::ChatMessageType type); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 99 | |
| 100 | void |
| 101 | prepareChatMessage(const QString& text, |
| 102 | time_t timestamp, |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 103 | ChatMessage &msg); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 104 | |
| 105 | void |
| 106 | updatePrefixes(); |
| 107 | |
| 108 | std::string |
| 109 | getHexEncodedDigest(ndn::ConstBufferPtr digest); |
| 110 | |
| 111 | signals: |
| 112 | void |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 113 | syncTreeUpdated(std::vector<chronochat::NodeInfo> updates, QString digest); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 114 | |
| 115 | void |
| 116 | chatMessageReceived(QString nick, QString text, time_t timestamp); |
| 117 | |
| 118 | void |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 119 | sessionRemoved(QString sessionPrefix, QString nick, time_t timestamp); |
| 120 | |
| 121 | void |
Qiuhan Ding | 7a4e7ef | 2015-02-03 20:25:50 -0800 | [diff] [blame] | 122 | messageReceived(QString sessionPrefix, QString nick, uint64_t seqNo, time_t timestamp, |
| 123 | bool addSession); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 124 | |
| 125 | void |
| 126 | chatPrefixChanged(ndn::Name newChatPrefix); |
| 127 | |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 128 | void |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 129 | refreshChatDialog(ndn::Name chatPrefix); |
| 130 | |
| 131 | void |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 132 | eraseInRoster(ndn::Name sessionPrefix, ndn::Name::Component chatroomName); |
| 133 | |
| 134 | void |
| 135 | addInRoster(ndn::Name sessionPrefix, ndn::Name::Component chatroomName); |
| 136 | |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 137 | void |
| 138 | newChatroomForDiscovery(ndn::Name::Component chatroomName); |
| 139 | |
| 140 | void |
| 141 | nfdError(); |
| 142 | |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 143 | public slots: |
| 144 | void |
| 145 | sendChatMessage(QString text, time_t timestamp); |
| 146 | |
| 147 | void |
| 148 | updateRoutingPrefix(const QString& localRoutingPrefix); |
| 149 | |
| 150 | void |
| 151 | shutdown(); |
| 152 | |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 153 | void |
| 154 | onNfdReconnect(); |
| 155 | |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 156 | private: |
| 157 | typedef std::map<ndn::Name, UserInfo> BackendRoster; |
| 158 | |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 159 | bool m_shouldResume; |
| 160 | bool m_isNfdConnected; |
Yingdi Yu | 45da92a | 2015-02-02 13:17:03 -0800 | [diff] [blame] | 161 | shared_ptr<ndn::Face> m_face; |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 162 | |
| 163 | Name m_localRoutingPrefix; // routable local prefix |
| 164 | Name m_chatroomPrefix; // chatroom sync prefix |
| 165 | Name m_userChatPrefix; // user chat prefix |
| 166 | Name m_routableUserChatPrefix; // routable user chat prefix |
| 167 | |
| 168 | std::string m_chatroomName; // chatroom name |
| 169 | std::string m_nick; // user nick |
| 170 | |
Yingdi Yu | 45da92a | 2015-02-02 13:17:03 -0800 | [diff] [blame] | 171 | Name m_signingId; // signing identity |
| 172 | shared_ptr<ndn::Validator> m_validator;// validator |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 173 | shared_ptr<chronosync::Socket> m_sock; // SyncSocket |
| 174 | |
Yingdi Yu | 4647f02 | 2015-02-01 00:26:38 -0800 | [diff] [blame] | 175 | unique_ptr<ndn::Scheduler> m_scheduler;// scheduler |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 176 | ndn::EventId m_helloEventId; // event id of timeout |
| 177 | |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 178 | bool m_joined; // true if in a chatroom |
| 179 | |
| 180 | BackendRoster m_roster; // User roster |
Yingdi Yu | 4647f02 | 2015-02-01 00:26:38 -0800 | [diff] [blame] | 181 | |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 182 | std::mutex m_resumeMutex; |
| 183 | std::mutex m_nfdConnectionMutex; |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 184 | }; |
| 185 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 186 | } // namespace chronochat |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 187 | |
| 188 | #endif // CHRONOCHAT_CHAT_DIALOG_BACKEND_HPP |