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