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