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