Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Junxiao Shi | d5798f2 | 2016-08-22 02:33:26 +0000 | [diff] [blame] | 3 | * Copyright (c) 2013-2016, Regents of the University of California |
| 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 | } |
| 71 | catch (std::runtime_error& e) { |
| 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 | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 134 | if (m_helloEventId) { |
| 135 | m_helloEventId.cancel(); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 136 | m_helloEventId.reset(); |
| 137 | } |
| 138 | } |
| 139 | |
Yingdi Yu | 45da92a | 2015-02-02 13:17:03 -0800 | [diff] [blame] | 140 | class IoDeviceSource |
| 141 | { |
| 142 | public: |
| 143 | typedef char char_type; |
| 144 | typedef boost::iostreams::source_tag category; |
| 145 | |
| 146 | explicit |
| 147 | IoDeviceSource(QIODevice& source) |
| 148 | : m_source(source) |
| 149 | { |
| 150 | } |
| 151 | |
| 152 | std::streamsize |
| 153 | read(char* buffer, std::streamsize n) |
| 154 | { |
| 155 | return m_source.read(buffer, n); |
| 156 | } |
| 157 | private: |
| 158 | QIODevice& m_source; |
| 159 | }; |
| 160 | |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 161 | void |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 162 | ChatDialogBackend::exitChatroom() { |
Yingdi Yu | 4647f02 | 2015-02-01 00:26:38 -0800 | [diff] [blame] | 163 | if (m_joined) |
| 164 | sendLeave(); |
| 165 | |
| 166 | usleep(100000); |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 167 | } |
Yingdi Yu | 4647f02 | 2015-02-01 00:26:38 -0800 | [diff] [blame] | 168 | |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 169 | void |
| 170 | ChatDialogBackend::close() |
| 171 | { |
Yingdi Yu | 4647f02 | 2015-02-01 00:26:38 -0800 | [diff] [blame] | 172 | m_scheduler->cancelAllEvents(); |
| 173 | m_helloEventId.reset(); |
| 174 | m_roster.clear(); |
Yingdi Yu | 45da92a | 2015-02-02 13:17:03 -0800 | [diff] [blame] | 175 | m_validator.reset(); |
Yingdi Yu | 4647f02 | 2015-02-01 00:26:38 -0800 | [diff] [blame] | 176 | m_sock.reset(); |
| 177 | } |
| 178 | |
| 179 | void |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 180 | ChatDialogBackend::processSyncUpdate(const std::vector<chronosync::MissingDataInfo>& updates) |
| 181 | { |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 182 | if (updates.empty()) { |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | std::vector<NodeInfo> nodeInfos; |
| 187 | |
| 188 | |
Yingdi Yu | 1cc45d9 | 2015-02-09 14:19:54 -0800 | [diff] [blame] | 189 | for (size_t i = 0; i < updates.size(); i++) { |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 190 | // update roster |
| 191 | if (m_roster.find(updates[i].session) == m_roster.end()) { |
| 192 | m_roster[updates[i].session].sessionPrefix = updates[i].session; |
| 193 | m_roster[updates[i].session].hasNick = false; |
| 194 | } |
| 195 | |
| 196 | // fetch missing chat data |
| 197 | if (updates[i].high - updates[i].low < 3) { |
| 198 | for (chronosync::SeqNo seq = updates[i].low; seq <= updates[i].high; ++seq) { |
| 199 | m_sock->fetchData(updates[i].session, seq, |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 200 | bind(&ChatDialogBackend::processChatData, this, _1, true, true), |
| 201 | bind(&ChatDialogBackend::processChatData, this, _1, true, false), |
| 202 | [] (const ndn::Interest& interest) {}, |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 203 | 2); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | else { |
| 207 | // There are too many msgs to fetch, let's just fetch the latest one |
| 208 | m_sock->fetchData(updates[i].session, updates[i].high, |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 209 | bind(&ChatDialogBackend::processChatData, this, _1, true, true), |
| 210 | bind(&ChatDialogBackend::processChatData, this, _1, true, false), |
| 211 | [] (const ndn::Interest& interest) {}, |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 212 | 2); |
| 213 | } |
| 214 | |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | // reflect the changes on GUI |
| 218 | emit syncTreeUpdated(nodeInfos, |
| 219 | QString::fromStdString(getHexEncodedDigest(m_sock->getRootDigest()))); |
| 220 | } |
| 221 | |
| 222 | void |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 223 | ChatDialogBackend::processChatData(const ndn::Data& data, |
Yingdi Yu | 45da92a | 2015-02-02 13:17:03 -0800 | [diff] [blame] | 224 | bool needDisplay, |
| 225 | bool isValidated) |
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 | ChatMessage msg; |
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 | try { |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 230 | msg.wireDecode(data.getContent().blockFromValue()); |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 231 | } |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 232 | catch (tlv::Error&) { |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 233 | // 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] | 234 | msg.setNick("inconnu"); |
| 235 | msg.setMsgType(ChatMessage::OTHER); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 236 | return; |
| 237 | } |
| 238 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 239 | Name remoteSessionPrefix = data.getName().getPrefix(-1); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 240 | |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 241 | if (msg.getMsgType() == ChatMessage::LEAVE) { |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 242 | BackendRoster::iterator it = m_roster.find(remoteSessionPrefix); |
| 243 | |
| 244 | if (it != m_roster.end()) { |
| 245 | // cancel timeout event |
| 246 | if (static_cast<bool>(it->second.timeoutEventId)) |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 247 | it->second.timeoutEventId.cancel(); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 248 | |
| 249 | // notify frontend to remove the remote session (node) |
| 250 | emit sessionRemoved(QString::fromStdString(remoteSessionPrefix.toUri()), |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 251 | QString::fromStdString(msg.getNick()), |
| 252 | msg.getTimestamp()); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 253 | |
| 254 | // remove roster entry |
| 255 | m_roster.erase(remoteSessionPrefix); |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 256 | |
| 257 | emit eraseInRoster(remoteSessionPrefix.getPrefix(IDENTITY_OFFSET), |
| 258 | Name::Component(m_chatroomName)); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | else { |
| 262 | BackendRoster::iterator it = m_roster.find(remoteSessionPrefix); |
| 263 | |
| 264 | if (it == m_roster.end()) { |
| 265 | // Should not happen |
| 266 | BOOST_ASSERT(false); |
| 267 | } |
| 268 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 269 | uint64_t seqNo = data.getName().get(-1).toNumber(); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 270 | |
| 271 | // If a timeout event has been scheduled, cancel it. |
| 272 | if (static_cast<bool>(it->second.timeoutEventId)) |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 273 | it->second.timeoutEventId.cancel(); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 274 | |
| 275 | // (Re)schedule another timeout event after 3 HELLO_INTERVAL; |
| 276 | it->second.timeoutEventId = |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 277 | m_scheduler->schedule(HELLO_INTERVAL * 3, |
| 278 | bind(&ChatDialogBackend::remoteSessionTimeout, |
| 279 | this, remoteSessionPrefix)); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 280 | |
| 281 | // If chat message, notify the frontend |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 282 | if (msg.getMsgType() == ChatMessage::CHAT) { |
Yingdi Yu | 45da92a | 2015-02-02 13:17:03 -0800 | [diff] [blame] | 283 | if (isValidated) |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 284 | emit chatMessageReceived(QString::fromStdString(msg.getNick()), |
| 285 | QString::fromStdString(msg.getData()), |
| 286 | msg.getTimestamp()); |
Yingdi Yu | 45da92a | 2015-02-02 13:17:03 -0800 | [diff] [blame] | 287 | else |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 288 | emit chatMessageReceived(QString::fromStdString(msg.getNick() + " (Unverified)"), |
| 289 | QString::fromStdString(msg.getData()), |
| 290 | msg.getTimestamp()); |
Yingdi Yu | 45da92a | 2015-02-02 13:17:03 -0800 | [diff] [blame] | 291 | } |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 292 | |
| 293 | // Notify frontend to plot notification on DigestTree. |
Qiuhan Ding | 7a4e7ef | 2015-02-03 20:25:50 -0800 | [diff] [blame] | 294 | |
| 295 | // If we haven't got any message from this session yet. |
| 296 | if (m_roster[remoteSessionPrefix].hasNick == false) { |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 297 | m_roster[remoteSessionPrefix].userNick = msg.getNick(); |
Qiuhan Ding | 7a4e7ef | 2015-02-03 20:25:50 -0800 | [diff] [blame] | 298 | m_roster[remoteSessionPrefix].hasNick = true; |
| 299 | |
| 300 | emit messageReceived(QString::fromStdString(remoteSessionPrefix.toUri()), |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 301 | QString::fromStdString(msg.getNick()), |
Qiuhan Ding | 7a4e7ef | 2015-02-03 20:25:50 -0800 | [diff] [blame] | 302 | seqNo, |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 303 | msg.getTimestamp(), |
Qiuhan Ding | 7a4e7ef | 2015-02-03 20:25:50 -0800 | [diff] [blame] | 304 | true); |
| 305 | |
| 306 | emit addInRoster(remoteSessionPrefix.getPrefix(IDENTITY_OFFSET), |
| 307 | Name::Component(m_chatroomName)); |
| 308 | } |
| 309 | else |
| 310 | emit messageReceived(QString::fromStdString(remoteSessionPrefix.toUri()), |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 311 | QString::fromStdString(msg.getNick()), |
Qiuhan Ding | 7a4e7ef | 2015-02-03 20:25:50 -0800 | [diff] [blame] | 312 | seqNo, |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 313 | msg.getTimestamp(), |
Qiuhan Ding | 7a4e7ef | 2015-02-03 20:25:50 -0800 | [diff] [blame] | 314 | false); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | |
| 318 | void |
| 319 | ChatDialogBackend::remoteSessionTimeout(const Name& sessionPrefix) |
| 320 | { |
| 321 | time_t timestamp = |
| 322 | static_cast<time_t>(time::toUnixTimestamp(time::system_clock::now()).count() / 1000); |
| 323 | |
| 324 | // notify frontend |
| 325 | emit sessionRemoved(QString::fromStdString(sessionPrefix.toUri()), |
| 326 | QString::fromStdString(m_roster[sessionPrefix].userNick), |
| 327 | timestamp); |
| 328 | |
| 329 | // remove roster entry |
| 330 | m_roster.erase(sessionPrefix); |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 331 | |
| 332 | emit eraseInRoster(sessionPrefix.getPrefix(IDENTITY_OFFSET), |
| 333 | Name::Component(m_chatroomName)); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | void |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 337 | ChatDialogBackend::sendMsg(ChatMessage& msg) |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 338 | { |
| 339 | // send msg |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 340 | ndn::Block buf = msg.wireEncode(); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 341 | |
| 342 | uint64_t nextSequence = m_sock->getLogic().getSeqNo() + 1; |
| 343 | |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 344 | m_sock->publishData(buf.wire(), buf.size(), FRESHNESS_PERIOD); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 345 | |
| 346 | std::vector<NodeInfo> nodeInfos; |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 347 | Name sessionName = m_sock->getLogic().getSessionName(); |
| 348 | NodeInfo nodeInfo = {QString::fromStdString(sessionName.toUri()), |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 349 | nextSequence}; |
| 350 | nodeInfos.push_back(nodeInfo); |
| 351 | |
| 352 | emit syncTreeUpdated(nodeInfos, |
| 353 | QString::fromStdString(getHexEncodedDigest(m_sock->getRootDigest()))); |
Qiuhan Ding | 7a4e7ef | 2015-02-03 20:25:50 -0800 | [diff] [blame] | 354 | |
| 355 | emit messageReceived(QString::fromStdString(sessionName.toUri()), |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 356 | QString::fromStdString(msg.getNick()), |
Qiuhan Ding | 7a4e7ef | 2015-02-03 20:25:50 -0800 | [diff] [blame] | 357 | nextSequence, |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 358 | msg.getTimestamp(), |
| 359 | msg.getMsgType() == ChatMessage::JOIN); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | void |
| 363 | ChatDialogBackend::sendJoin() |
| 364 | { |
| 365 | m_joined = true; |
| 366 | |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 367 | ChatMessage msg; |
| 368 | prepareControlMessage(msg, ChatMessage::JOIN); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 369 | sendMsg(msg); |
| 370 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 371 | m_helloEventId = m_scheduler->schedule(HELLO_INTERVAL, |
| 372 | bind(&ChatDialogBackend::sendHello, this)); |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 373 | emit newChatroomForDiscovery(Name::Component(m_chatroomName)); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | void |
| 377 | ChatDialogBackend::sendHello() |
| 378 | { |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 379 | ChatMessage msg; |
| 380 | prepareControlMessage(msg, ChatMessage::HELLO); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 381 | sendMsg(msg); |
| 382 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 383 | m_helloEventId = m_scheduler->schedule(HELLO_INTERVAL, |
| 384 | bind(&ChatDialogBackend::sendHello, this)); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | void |
| 388 | ChatDialogBackend::sendLeave() |
| 389 | { |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 390 | ChatMessage msg; |
| 391 | prepareControlMessage(msg, ChatMessage::LEAVE); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 392 | sendMsg(msg); |
| 393 | |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 394 | // get my own identity with routable prefix by getPrefix(-2) |
| 395 | emit eraseInRoster(m_routableUserChatPrefix.getPrefix(-2), |
| 396 | Name::Component(m_chatroomName)); |
| 397 | |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 398 | usleep(5000); |
| 399 | m_joined = false; |
| 400 | } |
| 401 | |
| 402 | void |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 403 | ChatDialogBackend::prepareControlMessage(ChatMessage& msg, |
| 404 | ChatMessage::ChatMessageType type) |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 405 | { |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 406 | msg.setNick(m_nick); |
| 407 | msg.setChatroomName(m_chatroomName); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 408 | int32_t seconds = |
| 409 | static_cast<int32_t>(time::toUnixTimestamp(time::system_clock::now()).count() / 1000); |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 410 | msg.setTimestamp(seconds); |
| 411 | msg.setMsgType(type); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | void |
| 415 | ChatDialogBackend::prepareChatMessage(const QString& text, |
| 416 | time_t timestamp, |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 417 | ChatMessage &msg) |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 418 | { |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 419 | msg.setNick(m_nick); |
| 420 | msg.setChatroomName(m_chatroomName); |
| 421 | msg.setData(text.toStdString()); |
| 422 | msg.setTimestamp(timestamp); |
| 423 | msg.setMsgType(ChatMessage::CHAT); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | void |
| 427 | ChatDialogBackend::updatePrefixes() |
| 428 | { |
| 429 | m_routableUserChatPrefix.clear(); |
| 430 | |
| 431 | if (m_localRoutingPrefix.isPrefixOf(m_userChatPrefix)) |
| 432 | m_routableUserChatPrefix = m_userChatPrefix; |
| 433 | else |
| 434 | m_routableUserChatPrefix.append(m_localRoutingPrefix) |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 435 | .append(ROUTING_HINT_SEPARATOR) |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 436 | .append(m_userChatPrefix); |
| 437 | |
| 438 | emit chatPrefixChanged(m_routableUserChatPrefix); |
| 439 | } |
| 440 | |
| 441 | std::string |
| 442 | ChatDialogBackend::getHexEncodedDigest(ndn::ConstBufferPtr digest) |
| 443 | { |
| 444 | std::stringstream os; |
| 445 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 446 | CryptoPP::StringSource(digest->data(), digest->size(), true, |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 447 | new CryptoPP::HexEncoder(new CryptoPP::FileSink(os), false)); |
| 448 | return os.str(); |
| 449 | } |
| 450 | |
| 451 | |
| 452 | // public slots: |
| 453 | void |
| 454 | ChatDialogBackend::sendChatMessage(QString text, time_t timestamp) |
| 455 | { |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 456 | ChatMessage msg; |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 457 | prepareChatMessage(text, timestamp, msg); |
| 458 | sendMsg(msg); |
| 459 | |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 460 | emit chatMessageReceived(QString::fromStdString(msg.getNick()), |
| 461 | QString::fromStdString(msg.getData()), |
| 462 | msg.getTimestamp()); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | void |
| 466 | ChatDialogBackend::updateRoutingPrefix(const QString& localRoutingPrefix) |
| 467 | { |
| 468 | Name newLocalRoutingPrefix(localRoutingPrefix.toStdString()); |
| 469 | |
| 470 | if (!newLocalRoutingPrefix.empty() && newLocalRoutingPrefix != m_localRoutingPrefix) { |
| 471 | // Update localPrefix |
| 472 | m_localRoutingPrefix = newLocalRoutingPrefix; |
| 473 | |
Qiuhan Ding | f22c41b | 2015-03-11 13:19:01 -0700 | [diff] [blame] | 474 | { |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 475 | std::lock_guard<std::mutex>lock(m_resumeMutex); |
Qiuhan Ding | f22c41b | 2015-03-11 13:19:01 -0700 | [diff] [blame] | 476 | m_shouldResume = true; |
| 477 | } |
Yingdi Yu | 4647f02 | 2015-02-01 00:26:38 -0800 | [diff] [blame] | 478 | |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 479 | exitChatroom(); |
Yingdi Yu | 4647f02 | 2015-02-01 00:26:38 -0800 | [diff] [blame] | 480 | |
Qiuhan Ding | 112ee48 | 2015-03-11 11:54:11 -0700 | [diff] [blame] | 481 | updatePrefixes(); |
| 482 | |
Yingdi Yu | 4647f02 | 2015-02-01 00:26:38 -0800 | [diff] [blame] | 483 | m_face->getIoService().stop(); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | |
| 487 | void |
| 488 | ChatDialogBackend::shutdown() |
| 489 | { |
Qiuhan Ding | f22c41b | 2015-03-11 13:19:01 -0700 | [diff] [blame] | 490 | { |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 491 | std::lock_guard<std::mutex>lock(m_resumeMutex); |
Qiuhan Ding | f22c41b | 2015-03-11 13:19:01 -0700 | [diff] [blame] | 492 | m_shouldResume = false; |
| 493 | } |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 494 | |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 495 | { |
| 496 | // In this case, we just stop checking the nfd connection and exit |
| 497 | std::lock_guard<std::mutex>lock(m_nfdConnectionMutex); |
| 498 | m_isNfdConnected = true; |
| 499 | } |
| 500 | |
| 501 | exitChatroom(); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 502 | |
Yingdi Yu | 4647f02 | 2015-02-01 00:26:38 -0800 | [diff] [blame] | 503 | m_face->getIoService().stop(); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 504 | } |
| 505 | |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 506 | void |
| 507 | ChatDialogBackend::onNfdReconnect() |
| 508 | { |
| 509 | std::lock_guard<std::mutex>lock(m_nfdConnectionMutex); |
| 510 | m_isNfdConnected = true; |
| 511 | } |
| 512 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 513 | } // namespace chronochat |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 514 | |
| 515 | #if WAF |
| 516 | #include "chat-dialog-backend.moc" |
| 517 | // #include "chat-dialog-backend.cpp.moc" |
| 518 | #endif |