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 | { |
| 270 | SyncDemo::ChatMessage msg; |
| 271 | |
| 272 | if (!msg.ParseFromArray(data->getContent().value(), data->getContent().value_size())) { |
| 273 | _LOG_DEBUG("Errrrr.. Can not parse msg with name: " << |
| 274 | data->getName() << ". what is happening?"); |
| 275 | // nasty stuff: as a remedy, we'll form some standard msg for inparsable msgs |
| 276 | msg.set_from("inconnu"); |
| 277 | msg.set_type(SyncDemo::ChatMessage::OTHER); |
| 278 | return; |
| 279 | } |
| 280 | |
| 281 | Name remoteSessionPrefix = data->getName().getPrefix(-1); |
| 282 | |
| 283 | if (msg.type() == SyncDemo::ChatMessage::LEAVE) { |
| 284 | BackendRoster::iterator it = m_roster.find(remoteSessionPrefix); |
| 285 | |
| 286 | if (it != m_roster.end()) { |
| 287 | // cancel timeout event |
| 288 | if (static_cast<bool>(it->second.timeoutEventId)) |
Yingdi Yu | 4647f02 | 2015-02-01 00:26:38 -0800 | [diff] [blame] | 289 | m_scheduler->cancelEvent(it->second.timeoutEventId); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 290 | |
| 291 | // notify frontend to remove the remote session (node) |
| 292 | emit sessionRemoved(QString::fromStdString(remoteSessionPrefix.toUri()), |
| 293 | QString::fromStdString(msg.from()), |
| 294 | msg.timestamp()); |
| 295 | |
| 296 | // remove roster entry |
| 297 | m_roster.erase(remoteSessionPrefix); |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 298 | |
| 299 | emit eraseInRoster(remoteSessionPrefix.getPrefix(IDENTITY_OFFSET), |
| 300 | Name::Component(m_chatroomName)); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | else { |
| 304 | BackendRoster::iterator it = m_roster.find(remoteSessionPrefix); |
| 305 | |
| 306 | if (it == m_roster.end()) { |
| 307 | // Should not happen |
| 308 | BOOST_ASSERT(false); |
| 309 | } |
| 310 | |
Qiuhan Ding | 7a4e7ef | 2015-02-03 20:25:50 -0800 | [diff] [blame] | 311 | uint64_t seqNo = data->getName().get(-1).toNumber(); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 312 | |
| 313 | // If a timeout event has been scheduled, cancel it. |
| 314 | if (static_cast<bool>(it->second.timeoutEventId)) |
Yingdi Yu | 4647f02 | 2015-02-01 00:26:38 -0800 | [diff] [blame] | 315 | m_scheduler->cancelEvent(it->second.timeoutEventId); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 316 | |
| 317 | // (Re)schedule another timeout event after 3 HELLO_INTERVAL; |
| 318 | it->second.timeoutEventId = |
Yingdi Yu | 4647f02 | 2015-02-01 00:26:38 -0800 | [diff] [blame] | 319 | m_scheduler->scheduleEvent(HELLO_INTERVAL * 3, |
| 320 | bind(&ChatDialogBackend::remoteSessionTimeout, |
| 321 | this, remoteSessionPrefix)); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 322 | |
| 323 | // If chat message, notify the frontend |
Yingdi Yu | 45da92a | 2015-02-02 13:17:03 -0800 | [diff] [blame] | 324 | if (msg.type() == SyncDemo::ChatMessage::CHAT) { |
| 325 | if (isValidated) |
| 326 | emit chatMessageReceived(QString::fromStdString(msg.from()), |
| 327 | QString::fromStdString(msg.data()), |
| 328 | msg.timestamp()); |
| 329 | else |
| 330 | emit chatMessageReceived(QString::fromStdString(msg.from() + " (Unverified)"), |
| 331 | QString::fromStdString(msg.data()), |
| 332 | msg.timestamp()); |
| 333 | } |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 334 | |
| 335 | // Notify frontend to plot notification on DigestTree. |
Qiuhan Ding | 7a4e7ef | 2015-02-03 20:25:50 -0800 | [diff] [blame] | 336 | |
| 337 | // If we haven't got any message from this session yet. |
| 338 | if (m_roster[remoteSessionPrefix].hasNick == false) { |
| 339 | m_roster[remoteSessionPrefix].userNick = msg.from(); |
| 340 | m_roster[remoteSessionPrefix].hasNick = true; |
| 341 | |
| 342 | emit messageReceived(QString::fromStdString(remoteSessionPrefix.toUri()), |
| 343 | QString::fromStdString(msg.from()), |
| 344 | seqNo, |
| 345 | msg.timestamp(), |
| 346 | true); |
| 347 | |
| 348 | emit addInRoster(remoteSessionPrefix.getPrefix(IDENTITY_OFFSET), |
| 349 | Name::Component(m_chatroomName)); |
| 350 | } |
| 351 | else |
| 352 | emit messageReceived(QString::fromStdString(remoteSessionPrefix.toUri()), |
| 353 | QString::fromStdString(msg.from()), |
| 354 | seqNo, |
| 355 | msg.timestamp(), |
| 356 | false); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
| 360 | void |
| 361 | ChatDialogBackend::remoteSessionTimeout(const Name& sessionPrefix) |
| 362 | { |
| 363 | time_t timestamp = |
| 364 | static_cast<time_t>(time::toUnixTimestamp(time::system_clock::now()).count() / 1000); |
| 365 | |
| 366 | // notify frontend |
| 367 | emit sessionRemoved(QString::fromStdString(sessionPrefix.toUri()), |
| 368 | QString::fromStdString(m_roster[sessionPrefix].userNick), |
| 369 | timestamp); |
| 370 | |
| 371 | // remove roster entry |
| 372 | m_roster.erase(sessionPrefix); |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 373 | |
| 374 | emit eraseInRoster(sessionPrefix.getPrefix(IDENTITY_OFFSET), |
| 375 | Name::Component(m_chatroomName)); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | void |
| 379 | ChatDialogBackend::sendMsg(SyncDemo::ChatMessage& msg) |
| 380 | { |
| 381 | // send msg |
| 382 | ndn::OBufferStream os; |
| 383 | msg.SerializeToOstream(&os); |
| 384 | |
| 385 | if (!msg.IsInitialized()) { |
| 386 | _LOG_DEBUG("Errrrr.. msg was not probally initialized " << __FILE__ << |
| 387 | ":" << __LINE__ << ". what is happening?"); |
| 388 | abort(); |
| 389 | } |
| 390 | |
| 391 | uint64_t nextSequence = m_sock->getLogic().getSeqNo() + 1; |
| 392 | |
| 393 | m_sock->publishData(os.buf()->buf(), os.buf()->size(), FRESHNESS_PERIOD); |
| 394 | |
| 395 | std::vector<NodeInfo> nodeInfos; |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 396 | Name sessionName = m_sock->getLogic().getSessionName(); |
| 397 | NodeInfo nodeInfo = {QString::fromStdString(sessionName.toUri()), |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 398 | nextSequence}; |
| 399 | nodeInfos.push_back(nodeInfo); |
| 400 | |
| 401 | emit syncTreeUpdated(nodeInfos, |
| 402 | QString::fromStdString(getHexEncodedDigest(m_sock->getRootDigest()))); |
Qiuhan Ding | 7a4e7ef | 2015-02-03 20:25:50 -0800 | [diff] [blame] | 403 | |
| 404 | emit messageReceived(QString::fromStdString(sessionName.toUri()), |
| 405 | QString::fromStdString(msg.from()), |
| 406 | nextSequence, |
| 407 | msg.timestamp(), |
| 408 | msg.type() == SyncDemo::ChatMessage::JOIN); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | void |
| 412 | ChatDialogBackend::sendJoin() |
| 413 | { |
| 414 | m_joined = true; |
| 415 | |
| 416 | SyncDemo::ChatMessage msg; |
| 417 | prepareControlMessage(msg, SyncDemo::ChatMessage::JOIN); |
| 418 | sendMsg(msg); |
| 419 | |
Yingdi Yu | 4647f02 | 2015-02-01 00:26:38 -0800 | [diff] [blame] | 420 | m_helloEventId = m_scheduler->scheduleEvent(HELLO_INTERVAL, |
| 421 | bind(&ChatDialogBackend::sendHello, this)); |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame^] | 422 | emit newChatroomForDiscovery(Name::Component(m_chatroomName)); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | void |
| 426 | ChatDialogBackend::sendHello() |
| 427 | { |
| 428 | SyncDemo::ChatMessage msg; |
| 429 | prepareControlMessage(msg, SyncDemo::ChatMessage::HELLO); |
| 430 | sendMsg(msg); |
| 431 | |
Yingdi Yu | 4647f02 | 2015-02-01 00:26:38 -0800 | [diff] [blame] | 432 | m_helloEventId = m_scheduler->scheduleEvent(HELLO_INTERVAL, |
| 433 | bind(&ChatDialogBackend::sendHello, this)); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | void |
| 437 | ChatDialogBackend::sendLeave() |
| 438 | { |
| 439 | SyncDemo::ChatMessage msg; |
| 440 | prepareControlMessage(msg, SyncDemo::ChatMessage::LEAVE); |
| 441 | sendMsg(msg); |
| 442 | |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 443 | // get my own identity with routable prefix by getPrefix(-2) |
| 444 | emit eraseInRoster(m_routableUserChatPrefix.getPrefix(-2), |
| 445 | Name::Component(m_chatroomName)); |
| 446 | |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 447 | usleep(5000); |
| 448 | m_joined = false; |
| 449 | } |
| 450 | |
| 451 | void |
| 452 | ChatDialogBackend::prepareControlMessage(SyncDemo::ChatMessage& msg, |
| 453 | SyncDemo::ChatMessage::ChatMessageType type) |
| 454 | { |
| 455 | msg.set_from(m_nick); |
| 456 | msg.set_to(m_chatroomName); |
| 457 | int32_t seconds = |
| 458 | static_cast<int32_t>(time::toUnixTimestamp(time::system_clock::now()).count() / 1000); |
| 459 | msg.set_timestamp(seconds); |
| 460 | msg.set_type(type); |
| 461 | } |
| 462 | |
| 463 | void |
| 464 | ChatDialogBackend::prepareChatMessage(const QString& text, |
| 465 | time_t timestamp, |
| 466 | SyncDemo::ChatMessage &msg) |
| 467 | { |
| 468 | msg.set_from(m_nick); |
| 469 | msg.set_to(m_chatroomName); |
| 470 | msg.set_data(text.toStdString()); |
| 471 | msg.set_timestamp(timestamp); |
| 472 | msg.set_type(SyncDemo::ChatMessage::CHAT); |
| 473 | } |
| 474 | |
| 475 | void |
| 476 | ChatDialogBackend::updatePrefixes() |
| 477 | { |
| 478 | m_routableUserChatPrefix.clear(); |
| 479 | |
| 480 | if (m_localRoutingPrefix.isPrefixOf(m_userChatPrefix)) |
| 481 | m_routableUserChatPrefix = m_userChatPrefix; |
| 482 | else |
| 483 | m_routableUserChatPrefix.append(m_localRoutingPrefix) |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 484 | .append(ROUTING_HINT_SEPARATOR) |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 485 | .append(m_userChatPrefix); |
| 486 | |
| 487 | emit chatPrefixChanged(m_routableUserChatPrefix); |
| 488 | } |
| 489 | |
| 490 | std::string |
| 491 | ChatDialogBackend::getHexEncodedDigest(ndn::ConstBufferPtr digest) |
| 492 | { |
| 493 | std::stringstream os; |
| 494 | |
| 495 | CryptoPP::StringSource(digest->buf(), digest->size(), true, |
| 496 | new CryptoPP::HexEncoder(new CryptoPP::FileSink(os), false)); |
| 497 | return os.str(); |
| 498 | } |
| 499 | |
| 500 | |
| 501 | // public slots: |
| 502 | void |
| 503 | ChatDialogBackend::sendChatMessage(QString text, time_t timestamp) |
| 504 | { |
| 505 | SyncDemo::ChatMessage msg; |
| 506 | prepareChatMessage(text, timestamp, msg); |
| 507 | sendMsg(msg); |
| 508 | |
| 509 | emit chatMessageReceived(QString::fromStdString(msg.from()), |
| 510 | QString::fromStdString(msg.data()), |
| 511 | msg.timestamp()); |
| 512 | } |
| 513 | |
| 514 | void |
| 515 | ChatDialogBackend::updateRoutingPrefix(const QString& localRoutingPrefix) |
| 516 | { |
| 517 | Name newLocalRoutingPrefix(localRoutingPrefix.toStdString()); |
| 518 | |
| 519 | if (!newLocalRoutingPrefix.empty() && newLocalRoutingPrefix != m_localRoutingPrefix) { |
| 520 | // Update localPrefix |
| 521 | m_localRoutingPrefix = newLocalRoutingPrefix; |
| 522 | |
Qiuhan Ding | f22c41b | 2015-03-11 13:19:01 -0700 | [diff] [blame] | 523 | { |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame^] | 524 | std::lock_guard<std::mutex>lock(m_resumeMutex); |
Qiuhan Ding | f22c41b | 2015-03-11 13:19:01 -0700 | [diff] [blame] | 525 | m_shouldResume = true; |
| 526 | } |
Yingdi Yu | 4647f02 | 2015-02-01 00:26:38 -0800 | [diff] [blame] | 527 | |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame^] | 528 | exitChatroom(); |
Yingdi Yu | 4647f02 | 2015-02-01 00:26:38 -0800 | [diff] [blame] | 529 | |
Qiuhan Ding | 112ee48 | 2015-03-11 11:54:11 -0700 | [diff] [blame] | 530 | updatePrefixes(); |
| 531 | |
Yingdi Yu | 4647f02 | 2015-02-01 00:26:38 -0800 | [diff] [blame] | 532 | m_face->getIoService().stop(); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 533 | } |
| 534 | } |
| 535 | |
| 536 | void |
| 537 | ChatDialogBackend::shutdown() |
| 538 | { |
Qiuhan Ding | f22c41b | 2015-03-11 13:19:01 -0700 | [diff] [blame] | 539 | { |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame^] | 540 | std::lock_guard<std::mutex>lock(m_resumeMutex); |
Qiuhan Ding | f22c41b | 2015-03-11 13:19:01 -0700 | [diff] [blame] | 541 | m_shouldResume = false; |
| 542 | } |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 543 | |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame^] | 544 | { |
| 545 | // In this case, we just stop checking the nfd connection and exit |
| 546 | std::lock_guard<std::mutex>lock(m_nfdConnectionMutex); |
| 547 | m_isNfdConnected = true; |
| 548 | } |
| 549 | |
| 550 | exitChatroom(); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 551 | |
Yingdi Yu | 4647f02 | 2015-02-01 00:26:38 -0800 | [diff] [blame] | 552 | m_face->getIoService().stop(); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 553 | } |
| 554 | |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame^] | 555 | void |
| 556 | ChatDialogBackend::onNfdReconnect() |
| 557 | { |
| 558 | std::lock_guard<std::mutex>lock(m_nfdConnectionMutex); |
| 559 | m_isNfdConnected = true; |
| 560 | } |
| 561 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 562 | } // namespace chronochat |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 563 | |
| 564 | #if WAF |
| 565 | #include "chat-dialog-backend.moc" |
| 566 | // #include "chat-dialog-backend.cpp.moc" |
| 567 | #endif |