Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame^] | 3 | * Copyright (c) 2020, Regents of the University of California |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 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> |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 21 | #include <ChronoSync/socket.hpp> |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 22 | #include <boost/thread.hpp> |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 23 | #include <ndn-cxx/security/validator-config.hpp> |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 24 | #endif |
| 25 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 26 | namespace chronochat { |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 27 | |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame^] | 28 | class NodeInfo |
| 29 | { |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 30 | public: |
| 31 | QString sessionPrefix; |
| 32 | chronosync::SeqNo seqNo; |
| 33 | }; |
| 34 | |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame^] | 35 | class UserInfo |
| 36 | { |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 37 | public: |
| 38 | ndn::Name sessionPrefix; |
| 39 | bool hasNick; |
| 40 | std::string userNick; |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame^] | 41 | ndn::scheduler::ScopedEventId timeoutEventId; |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | class ChatDialogBackend : public QThread |
| 45 | { |
| 46 | Q_OBJECT |
| 47 | |
| 48 | public: |
| 49 | ChatDialogBackend(const Name& chatroomPrefix, |
| 50 | const Name& userChatPrefix, |
| 51 | const Name& routingPrefix, |
| 52 | const std::string& chatroomName, |
| 53 | const std::string& nick, |
Yingdi Yu | 45da92a | 2015-02-02 13:17:03 -0800 | [diff] [blame] | 54 | const Name& signingId = Name(), |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 55 | QObject* parent = nullptr); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 56 | |
| 57 | ~ChatDialogBackend(); |
| 58 | |
| 59 | protected: |
| 60 | void |
| 61 | run(); |
| 62 | |
| 63 | private: |
| 64 | void |
| 65 | initializeSync(); |
| 66 | |
| 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 |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 77 | processChatData(const ndn::Data& data, |
Yingdi Yu | 45da92a | 2015-02-02 13:17:03 -0800 | [diff] [blame] | 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, |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [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 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 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 |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 167 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 168 | std::string m_chatroomName; // chatroom name |
| 169 | std::string m_nick; // user nick |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 170 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 171 | Name m_signingId; // signing identity |
| 172 | shared_ptr<ndn::security::ValidatorConfig> m_validator; // validator |
| 173 | shared_ptr<chronosync::Socket> m_sock; // SyncSocket |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 174 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 175 | unique_ptr<ndn::Scheduler> m_scheduler; // scheduler |
| 176 | ndn::scheduler::EventId m_helloEventId; // event id of timeout |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 177 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 178 | bool m_joined; // true if in a chatroom |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 179 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 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 |