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