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 | b3d03b9 | 2021-01-08 17:09:32 +0530 | [diff] [blame] | 3 | * Copyright (c) 2013-2021, Regents of the University of California |
Junxiao Shi | d5798f2 | 2016-08-22 02:33:26 +0000 | [diff] [blame] | 4 | * Yingdi Yu |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 5 | * |
| 6 | * BSD license, See the LICENSE file for more information |
| 7 | * |
| 8 | * Author: Yingdi Yu <yingdi@cs.ucla.edu> |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 9 | * Qiuhan Ding <qiuhanding@cs.ucla.edu> |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include "chat-dialog-backend.hpp" |
| 13 | |
Yingdi Yu | 45da92a | 2015-02-02 13:17:03 -0800 | [diff] [blame] | 14 | #include <QFile> |
| 15 | |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 16 | #ifndef Q_MOC_RUN |
Junxiao Shi | d5798f2 | 2016-08-22 02:33:26 +0000 | [diff] [blame] | 17 | #include <boost/iostreams/stream.hpp> |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 18 | |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 19 | #include <ndn-cxx/util/io.hpp> |
| 20 | #include <ndn-cxx/util/string-helper.hpp> |
| 21 | #endif |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 22 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 23 | namespace chronochat { |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 24 | |
| 25 | static const time::milliseconds FRESHNESS_PERIOD(60000); |
| 26 | static const time::seconds HELLO_INTERVAL(60); |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 27 | static const Name::Component ROUTING_HINT_SEPARATOR = Name::Component::fromEscapedString("%F0%2E"); |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 28 | static const int IDENTITY_OFFSET = -3; |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 29 | static const int CONNECTION_RETRY_TIMER = 3; |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 30 | |
| 31 | ChatDialogBackend::ChatDialogBackend(const Name& chatroomPrefix, |
| 32 | const Name& userChatPrefix, |
| 33 | const Name& routingPrefix, |
| 34 | const std::string& chatroomName, |
| 35 | const std::string& nick, |
Yingdi Yu | 45da92a | 2015-02-02 13:17:03 -0800 | [diff] [blame] | 36 | const Name& signingId, |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 37 | QObject* parent) |
| 38 | : QThread(parent) |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 39 | , m_shouldResume(false) |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 40 | , m_localRoutingPrefix(routingPrefix) |
| 41 | , m_chatroomPrefix(chatroomPrefix) |
| 42 | , m_userChatPrefix(userChatPrefix) |
| 43 | , m_chatroomName(chatroomName) |
| 44 | , m_nick(nick) |
Yingdi Yu | 45da92a | 2015-02-02 13:17:03 -0800 | [diff] [blame] | 45 | , m_signingId(signingId) |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 46 | { |
| 47 | updatePrefixes(); |
| 48 | } |
| 49 | |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 50 | ChatDialogBackend::~ChatDialogBackend() = default; |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 51 | |
| 52 | // protected methods: |
| 53 | void |
| 54 | ChatDialogBackend::run() |
| 55 | { |
Yingdi Yu | 4647f02 | 2015-02-01 00:26:38 -0800 | [diff] [blame] | 56 | bool shouldResume = false; |
| 57 | do { |
| 58 | initializeSync(); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 59 | |
Yingdi Yu | 4647f02 | 2015-02-01 00:26:38 -0800 | [diff] [blame] | 60 | if (m_face == nullptr) |
| 61 | break; |
| 62 | |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 63 | try { |
| 64 | m_face->getIoService().run(); |
| 65 | } |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 66 | catch (const std::runtime_error& e) { |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 67 | { |
| 68 | std::lock_guard<std::mutex>lock(m_nfdConnectionMutex); |
| 69 | m_isNfdConnected = false; |
| 70 | } |
| 71 | emit nfdError(); |
| 72 | { |
| 73 | std::lock_guard<std::mutex>lock(m_resumeMutex); |
| 74 | m_shouldResume = true; |
| 75 | } |
| 76 | #ifdef BOOST_THREAD_USES_CHRONO |
| 77 | time::seconds reconnectTimer = time::seconds(CONNECTION_RETRY_TIMER); |
| 78 | #else |
| 79 | boost::posix_time::time_duration reconnectTimer; |
| 80 | reconnectTimer = boost::posix_time::seconds(CONNECTION_RETRY_TIMER); |
| 81 | #endif |
| 82 | while (!m_isNfdConnected) { |
| 83 | #ifdef BOOST_THREAD_USES_CHRONO |
| 84 | boost::this_thread::sleep_for(reconnectTimer); |
| 85 | #else |
| 86 | boost::this_thread::sleep(reconnectTimer); |
| 87 | #endif |
| 88 | } |
| 89 | emit refreshChatDialog(m_routableUserChatPrefix); |
| 90 | } |
Qiuhan Ding | f22c41b | 2015-03-11 13:19:01 -0700 | [diff] [blame] | 91 | { |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 92 | std::lock_guard<std::mutex>lock(m_resumeMutex); |
Qiuhan Ding | f22c41b | 2015-03-11 13:19:01 -0700 | [diff] [blame] | 93 | shouldResume = m_shouldResume; |
| 94 | m_shouldResume = false; |
| 95 | } |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 96 | close(); |
Yingdi Yu | 4647f02 | 2015-02-01 00:26:38 -0800 | [diff] [blame] | 97 | |
| 98 | } while (shouldResume); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | // private methods: |
| 102 | void |
| 103 | ChatDialogBackend::initializeSync() |
| 104 | { |
Yingdi Yu | 4647f02 | 2015-02-01 00:26:38 -0800 | [diff] [blame] | 105 | BOOST_ASSERT(m_sock == nullptr); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 106 | |
Varun Patil | b3d03b9 | 2021-01-08 17:09:32 +0530 | [diff] [blame] | 107 | m_face = std::make_shared<ndn::Face>(); |
| 108 | m_scheduler = std::make_unique<ndn::Scheduler>(m_face->getIoService()); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 109 | |
Yingdi Yu | 45da92a | 2015-02-02 13:17:03 -0800 | [diff] [blame] | 110 | // initialize validator |
Varun Patil | b3d03b9 | 2021-01-08 17:09:32 +0530 | [diff] [blame] | 111 | m_validator = std::make_shared<ndn::security::ValidatorConfig>(*m_face); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 112 | m_validator->load("security/validation-chat.conf"); |
Yingdi Yu | 45da92a | 2015-02-02 13:17:03 -0800 | [diff] [blame] | 113 | |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 114 | // create a new SyncSocket |
Varun Patil | b3d03b9 | 2021-01-08 17:09:32 +0530 | [diff] [blame] | 115 | m_sock = std::make_shared<chronosync::Socket>(m_chatroomPrefix, |
| 116 | m_routableUserChatPrefix, |
| 117 | ref(*m_face), |
| 118 | bind(&ChatDialogBackend::processSyncUpdate, this, _1), |
| 119 | m_signingId, |
| 120 | m_validator); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 121 | |
| 122 | // schedule a new join event |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 123 | m_scheduler->schedule(time::milliseconds(600), |
| 124 | bind(&ChatDialogBackend::sendJoin, this)); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 125 | |
| 126 | // cancel existing hello event if it exists |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 127 | if (m_helloEventId) |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 128 | m_helloEventId.cancel(); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Yingdi Yu | 45da92a | 2015-02-02 13:17:03 -0800 | [diff] [blame] | 131 | class IoDeviceSource |
| 132 | { |
| 133 | public: |
| 134 | typedef char char_type; |
| 135 | typedef boost::iostreams::source_tag category; |
| 136 | |
| 137 | explicit |
| 138 | IoDeviceSource(QIODevice& source) |
| 139 | : m_source(source) |
| 140 | { |
| 141 | } |
| 142 | |
| 143 | std::streamsize |
| 144 | read(char* buffer, std::streamsize n) |
| 145 | { |
| 146 | return m_source.read(buffer, n); |
| 147 | } |
| 148 | private: |
| 149 | QIODevice& m_source; |
| 150 | }; |
| 151 | |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 152 | void |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 153 | ChatDialogBackend::exitChatroom() { |
Yingdi Yu | 4647f02 | 2015-02-01 00:26:38 -0800 | [diff] [blame] | 154 | if (m_joined) |
| 155 | sendLeave(); |
| 156 | |
| 157 | usleep(100000); |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 158 | } |
Yingdi Yu | 4647f02 | 2015-02-01 00:26:38 -0800 | [diff] [blame] | 159 | |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 160 | void |
| 161 | ChatDialogBackend::close() |
| 162 | { |
Yingdi Yu | 4647f02 | 2015-02-01 00:26:38 -0800 | [diff] [blame] | 163 | m_scheduler->cancelAllEvents(); |
| 164 | m_helloEventId.reset(); |
| 165 | m_roster.clear(); |
Yingdi Yu | 45da92a | 2015-02-02 13:17:03 -0800 | [diff] [blame] | 166 | m_validator.reset(); |
Yingdi Yu | 4647f02 | 2015-02-01 00:26:38 -0800 | [diff] [blame] | 167 | m_sock.reset(); |
| 168 | } |
| 169 | |
| 170 | void |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 171 | ChatDialogBackend::processSyncUpdate(const std::vector<chronosync::MissingDataInfo>& updates) |
| 172 | { |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 173 | if (updates.empty()) { |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | std::vector<NodeInfo> nodeInfos; |
| 178 | |
Yingdi Yu | 1cc45d9 | 2015-02-09 14:19:54 -0800 | [diff] [blame] | 179 | for (size_t i = 0; i < updates.size(); i++) { |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 180 | // update roster |
| 181 | if (m_roster.find(updates[i].session) == m_roster.end()) { |
| 182 | m_roster[updates[i].session].sessionPrefix = updates[i].session; |
| 183 | m_roster[updates[i].session].hasNick = false; |
| 184 | } |
| 185 | |
| 186 | // fetch missing chat data |
| 187 | if (updates[i].high - updates[i].low < 3) { |
| 188 | for (chronosync::SeqNo seq = updates[i].low; seq <= updates[i].high; ++seq) { |
| 189 | m_sock->fetchData(updates[i].session, seq, |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 190 | bind(&ChatDialogBackend::processChatData, this, _1, true, true), |
| 191 | bind(&ChatDialogBackend::processChatData, this, _1, true, false), |
| 192 | [] (const ndn::Interest& interest) {}, |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 193 | 2); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | else { |
| 197 | // There are too many msgs to fetch, let's just fetch the latest one |
| 198 | m_sock->fetchData(updates[i].session, updates[i].high, |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 199 | bind(&ChatDialogBackend::processChatData, this, _1, true, true), |
| 200 | bind(&ChatDialogBackend::processChatData, this, _1, true, false), |
| 201 | [] (const ndn::Interest& interest) {}, |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 202 | 2); |
| 203 | } |
| 204 | |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | // reflect the changes on GUI |
| 208 | emit syncTreeUpdated(nodeInfos, |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 209 | QString::fromStdString(ndn::toHex(*m_sock->getRootDigest(), false))); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | void |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 213 | ChatDialogBackend::processChatData(const ndn::Data& data, bool needDisplay, bool isValidated) |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 214 | { |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 215 | ChatMessage msg; |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 216 | |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 217 | try { |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 218 | msg.wireDecode(data.getContent().blockFromValue()); |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 219 | } |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 220 | catch (const tlv::Error&) { |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 221 | // nasty stuff: as a remedy, we'll form some standard msg for inparsable msgs |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 222 | msg.setNick("inconnu"); |
| 223 | msg.setMsgType(ChatMessage::OTHER); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 224 | return; |
| 225 | } |
| 226 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 227 | Name remoteSessionPrefix = data.getName().getPrefix(-1); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 228 | |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 229 | if (msg.getMsgType() == ChatMessage::LEAVE) { |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 230 | BackendRoster::iterator it = m_roster.find(remoteSessionPrefix); |
| 231 | |
| 232 | if (it != m_roster.end()) { |
| 233 | // cancel timeout event |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 234 | if (it->second.timeoutEventId) |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 235 | it->second.timeoutEventId.cancel(); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 236 | |
| 237 | // notify frontend to remove the remote session (node) |
| 238 | emit sessionRemoved(QString::fromStdString(remoteSessionPrefix.toUri()), |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 239 | QString::fromStdString(msg.getNick()), |
| 240 | msg.getTimestamp()); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 241 | |
| 242 | // remove roster entry |
| 243 | m_roster.erase(remoteSessionPrefix); |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 244 | |
| 245 | emit eraseInRoster(remoteSessionPrefix.getPrefix(IDENTITY_OFFSET), |
| 246 | Name::Component(m_chatroomName)); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | else { |
| 250 | BackendRoster::iterator it = m_roster.find(remoteSessionPrefix); |
| 251 | |
| 252 | if (it == m_roster.end()) { |
| 253 | // Should not happen |
| 254 | BOOST_ASSERT(false); |
| 255 | } |
| 256 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 257 | uint64_t seqNo = data.getName().get(-1).toNumber(); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 258 | |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 259 | // (Re)schedule another timeout event after 3 HELLO_INTERVAL; |
| 260 | it->second.timeoutEventId = |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 261 | m_scheduler->schedule(HELLO_INTERVAL * 3, |
| 262 | bind(&ChatDialogBackend::remoteSessionTimeout, |
| 263 | this, remoteSessionPrefix)); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 264 | |
| 265 | // If chat message, notify the frontend |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 266 | if (msg.getMsgType() == ChatMessage::CHAT) { |
Yingdi Yu | 45da92a | 2015-02-02 13:17:03 -0800 | [diff] [blame] | 267 | if (isValidated) |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 268 | emit chatMessageReceived(QString::fromStdString(msg.getNick()), |
| 269 | QString::fromStdString(msg.getData()), |
| 270 | msg.getTimestamp()); |
Yingdi Yu | 45da92a | 2015-02-02 13:17:03 -0800 | [diff] [blame] | 271 | else |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 272 | emit chatMessageReceived(QString::fromStdString(msg.getNick() + " (Unverified)"), |
| 273 | QString::fromStdString(msg.getData()), |
| 274 | msg.getTimestamp()); |
Yingdi Yu | 45da92a | 2015-02-02 13:17:03 -0800 | [diff] [blame] | 275 | } |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 276 | |
| 277 | // Notify frontend to plot notification on DigestTree. |
Qiuhan Ding | 7a4e7ef | 2015-02-03 20:25:50 -0800 | [diff] [blame] | 278 | |
| 279 | // If we haven't got any message from this session yet. |
| 280 | if (m_roster[remoteSessionPrefix].hasNick == false) { |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 281 | m_roster[remoteSessionPrefix].userNick = msg.getNick(); |
Qiuhan Ding | 7a4e7ef | 2015-02-03 20:25:50 -0800 | [diff] [blame] | 282 | m_roster[remoteSessionPrefix].hasNick = true; |
| 283 | |
| 284 | emit messageReceived(QString::fromStdString(remoteSessionPrefix.toUri()), |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 285 | QString::fromStdString(msg.getNick()), |
Qiuhan Ding | 7a4e7ef | 2015-02-03 20:25:50 -0800 | [diff] [blame] | 286 | seqNo, |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 287 | msg.getTimestamp(), |
Qiuhan Ding | 7a4e7ef | 2015-02-03 20:25:50 -0800 | [diff] [blame] | 288 | true); |
| 289 | |
| 290 | emit addInRoster(remoteSessionPrefix.getPrefix(IDENTITY_OFFSET), |
| 291 | Name::Component(m_chatroomName)); |
| 292 | } |
| 293 | else |
| 294 | emit messageReceived(QString::fromStdString(remoteSessionPrefix.toUri()), |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 295 | QString::fromStdString(msg.getNick()), |
Qiuhan Ding | 7a4e7ef | 2015-02-03 20:25:50 -0800 | [diff] [blame] | 296 | seqNo, |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 297 | msg.getTimestamp(), |
Qiuhan Ding | 7a4e7ef | 2015-02-03 20:25:50 -0800 | [diff] [blame] | 298 | false); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | |
| 302 | void |
| 303 | ChatDialogBackend::remoteSessionTimeout(const Name& sessionPrefix) |
| 304 | { |
| 305 | time_t timestamp = |
| 306 | static_cast<time_t>(time::toUnixTimestamp(time::system_clock::now()).count() / 1000); |
| 307 | |
| 308 | // notify frontend |
| 309 | emit sessionRemoved(QString::fromStdString(sessionPrefix.toUri()), |
| 310 | QString::fromStdString(m_roster[sessionPrefix].userNick), |
| 311 | timestamp); |
| 312 | |
| 313 | // remove roster entry |
| 314 | m_roster.erase(sessionPrefix); |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 315 | |
| 316 | emit eraseInRoster(sessionPrefix.getPrefix(IDENTITY_OFFSET), |
| 317 | Name::Component(m_chatroomName)); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | void |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 321 | ChatDialogBackend::sendMsg(ChatMessage& msg) |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 322 | { |
| 323 | // send msg |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 324 | ndn::Block buf = msg.wireEncode(); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 325 | |
| 326 | uint64_t nextSequence = m_sock->getLogic().getSeqNo() + 1; |
| 327 | |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 328 | m_sock->publishData(buf.wire(), buf.size(), FRESHNESS_PERIOD); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 329 | |
| 330 | std::vector<NodeInfo> nodeInfos; |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 331 | Name sessionName = m_sock->getLogic().getSessionName(); |
| 332 | NodeInfo nodeInfo = {QString::fromStdString(sessionName.toUri()), |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 333 | nextSequence}; |
| 334 | nodeInfos.push_back(nodeInfo); |
| 335 | |
| 336 | emit syncTreeUpdated(nodeInfos, |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 337 | QString::fromStdString(ndn::toHex(*m_sock->getRootDigest(), false))); |
Qiuhan Ding | 7a4e7ef | 2015-02-03 20:25:50 -0800 | [diff] [blame] | 338 | |
| 339 | emit messageReceived(QString::fromStdString(sessionName.toUri()), |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 340 | QString::fromStdString(msg.getNick()), |
Qiuhan Ding | 7a4e7ef | 2015-02-03 20:25:50 -0800 | [diff] [blame] | 341 | nextSequence, |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 342 | msg.getTimestamp(), |
| 343 | msg.getMsgType() == ChatMessage::JOIN); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | void |
| 347 | ChatDialogBackend::sendJoin() |
| 348 | { |
| 349 | m_joined = true; |
| 350 | |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 351 | ChatMessage msg; |
| 352 | prepareControlMessage(msg, ChatMessage::JOIN); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 353 | sendMsg(msg); |
| 354 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 355 | m_helloEventId = m_scheduler->schedule(HELLO_INTERVAL, |
| 356 | bind(&ChatDialogBackend::sendHello, this)); |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 357 | emit newChatroomForDiscovery(Name::Component(m_chatroomName)); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | void |
| 361 | ChatDialogBackend::sendHello() |
| 362 | { |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 363 | ChatMessage msg; |
| 364 | prepareControlMessage(msg, ChatMessage::HELLO); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 365 | sendMsg(msg); |
| 366 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 367 | m_helloEventId = m_scheduler->schedule(HELLO_INTERVAL, |
| 368 | bind(&ChatDialogBackend::sendHello, this)); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | void |
| 372 | ChatDialogBackend::sendLeave() |
| 373 | { |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 374 | ChatMessage msg; |
| 375 | prepareControlMessage(msg, ChatMessage::LEAVE); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 376 | sendMsg(msg); |
| 377 | |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 378 | // get my own identity with routable prefix by getPrefix(-2) |
| 379 | emit eraseInRoster(m_routableUserChatPrefix.getPrefix(-2), |
| 380 | Name::Component(m_chatroomName)); |
| 381 | |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 382 | usleep(5000); |
| 383 | m_joined = false; |
| 384 | } |
| 385 | |
| 386 | void |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 387 | ChatDialogBackend::prepareControlMessage(ChatMessage& msg, |
| 388 | ChatMessage::ChatMessageType type) |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 389 | { |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 390 | msg.setNick(m_nick); |
| 391 | msg.setChatroomName(m_chatroomName); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 392 | int32_t seconds = |
| 393 | static_cast<int32_t>(time::toUnixTimestamp(time::system_clock::now()).count() / 1000); |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 394 | msg.setTimestamp(seconds); |
| 395 | msg.setMsgType(type); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | void |
| 399 | ChatDialogBackend::prepareChatMessage(const QString& text, |
| 400 | time_t timestamp, |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 401 | ChatMessage& msg) |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 402 | { |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 403 | msg.setNick(m_nick); |
| 404 | msg.setChatroomName(m_chatroomName); |
| 405 | msg.setData(text.toStdString()); |
| 406 | msg.setTimestamp(timestamp); |
| 407 | msg.setMsgType(ChatMessage::CHAT); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | void |
| 411 | ChatDialogBackend::updatePrefixes() |
| 412 | { |
| 413 | m_routableUserChatPrefix.clear(); |
| 414 | |
| 415 | if (m_localRoutingPrefix.isPrefixOf(m_userChatPrefix)) |
| 416 | m_routableUserChatPrefix = m_userChatPrefix; |
| 417 | else |
| 418 | m_routableUserChatPrefix.append(m_localRoutingPrefix) |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 419 | .append(ROUTING_HINT_SEPARATOR) |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 420 | .append(m_userChatPrefix); |
| 421 | |
| 422 | emit chatPrefixChanged(m_routableUserChatPrefix); |
| 423 | } |
| 424 | |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 425 | |
| 426 | // public slots: |
| 427 | void |
| 428 | ChatDialogBackend::sendChatMessage(QString text, time_t timestamp) |
| 429 | { |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 430 | ChatMessage msg; |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 431 | prepareChatMessage(text, timestamp, msg); |
| 432 | sendMsg(msg); |
| 433 | |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 434 | emit chatMessageReceived(QString::fromStdString(msg.getNick()), |
| 435 | QString::fromStdString(msg.getData()), |
| 436 | msg.getTimestamp()); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | void |
| 440 | ChatDialogBackend::updateRoutingPrefix(const QString& localRoutingPrefix) |
| 441 | { |
| 442 | Name newLocalRoutingPrefix(localRoutingPrefix.toStdString()); |
| 443 | |
| 444 | if (!newLocalRoutingPrefix.empty() && newLocalRoutingPrefix != m_localRoutingPrefix) { |
| 445 | // Update localPrefix |
| 446 | m_localRoutingPrefix = newLocalRoutingPrefix; |
| 447 | |
Qiuhan Ding | f22c41b | 2015-03-11 13:19:01 -0700 | [diff] [blame] | 448 | { |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 449 | std::lock_guard<std::mutex>lock(m_resumeMutex); |
Qiuhan Ding | f22c41b | 2015-03-11 13:19:01 -0700 | [diff] [blame] | 450 | m_shouldResume = true; |
| 451 | } |
Yingdi Yu | 4647f02 | 2015-02-01 00:26:38 -0800 | [diff] [blame] | 452 | |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 453 | exitChatroom(); |
Yingdi Yu | 4647f02 | 2015-02-01 00:26:38 -0800 | [diff] [blame] | 454 | |
Qiuhan Ding | 112ee48 | 2015-03-11 11:54:11 -0700 | [diff] [blame] | 455 | updatePrefixes(); |
| 456 | |
Yingdi Yu | 4647f02 | 2015-02-01 00:26:38 -0800 | [diff] [blame] | 457 | m_face->getIoService().stop(); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | |
| 461 | void |
| 462 | ChatDialogBackend::shutdown() |
| 463 | { |
Qiuhan Ding | f22c41b | 2015-03-11 13:19:01 -0700 | [diff] [blame] | 464 | { |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 465 | std::lock_guard<std::mutex>lock(m_resumeMutex); |
Qiuhan Ding | f22c41b | 2015-03-11 13:19:01 -0700 | [diff] [blame] | 466 | m_shouldResume = false; |
| 467 | } |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 468 | |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 469 | { |
| 470 | // In this case, we just stop checking the nfd connection and exit |
| 471 | std::lock_guard<std::mutex>lock(m_nfdConnectionMutex); |
| 472 | m_isNfdConnected = true; |
| 473 | } |
| 474 | |
| 475 | exitChatroom(); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 476 | |
Yingdi Yu | 4647f02 | 2015-02-01 00:26:38 -0800 | [diff] [blame] | 477 | m_face->getIoService().stop(); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 478 | } |
| 479 | |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 480 | void |
| 481 | ChatDialogBackend::onNfdReconnect() |
| 482 | { |
| 483 | std::lock_guard<std::mutex>lock(m_nfdConnectionMutex); |
| 484 | m_isNfdConnected = true; |
| 485 | } |
| 486 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 487 | } // namespace chronochat |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 488 | |
| 489 | #if WAF |
| 490 | #include "chat-dialog-backend.moc" |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 491 | #endif |