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