Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [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: Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 9 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 10 | * Yingdi Yu <yingdi@cs.ucla.edu> |
| 11 | */ |
| 12 | |
| 13 | #include "chat-dialog.h" |
| 14 | #include "ui_chat-dialog.h" |
| 15 | |
| 16 | #include <QScrollBar> |
| 17 | #include <QMessageBox> |
| 18 | #include <QCloseEvent> |
| 19 | |
| 20 | #ifndef Q_MOC_RUN |
| 21 | #include <sync-intro-certificate.h> |
| 22 | #include <boost/random/random_device.hpp> |
| 23 | #include <boost/random/uniform_int_distribution.hpp> |
| 24 | #include <ndn-cpp-dev/util/random.hpp> |
| 25 | #include <cryptopp/hex.h> |
| 26 | #include <cryptopp/files.h> |
| 27 | #include "logging.h" |
| 28 | #endif |
| 29 | |
| 30 | using namespace std; |
| 31 | using namespace ndn; |
| 32 | using namespace chronos; |
| 33 | |
| 34 | INIT_LOGGER("ChatDialog"); |
| 35 | |
| 36 | static const int HELLO_INTERVAL = FRESHNESS * 3 / 4; |
| 37 | static const uint8_t CHRONOS_RP_SEPARATOR[2] = {0xF0, 0x2E}; // %F0. |
| 38 | |
| 39 | Q_DECLARE_METATYPE(std::vector<Sync::MissingDataInfo> ) |
| 40 | Q_DECLARE_METATYPE(ndn::Data) |
| 41 | Q_DECLARE_METATYPE(ndn::Interest) |
| 42 | Q_DECLARE_METATYPE(size_t) |
| 43 | |
| 44 | ChatDialog::ChatDialog(ContactManager* contactManager, |
| 45 | shared_ptr<Face> face, |
| 46 | const IdentityCertificate& myCertificate, |
| 47 | const Name& chatroomPrefix, |
| 48 | const Name& localPrefix, |
| 49 | const string& nick, |
| 50 | bool withSecurity, |
| 51 | QWidget* parent) |
| 52 | : QDialog(parent) |
| 53 | , ui(new Ui::ChatDialog) |
| 54 | , m_contactManager(contactManager) |
| 55 | , m_face(face) |
| 56 | , m_myCertificate(myCertificate) |
| 57 | , m_chatroomName(chatroomPrefix.get(-1).toEscapedString()) |
| 58 | , m_chatroomPrefix(chatroomPrefix) |
| 59 | , m_localPrefix(localPrefix) |
| 60 | , m_useRoutablePrefix(false) |
| 61 | , m_nick(nick) |
| 62 | , m_lastMsgTime(time::now()) |
| 63 | , m_joined(false) |
| 64 | , m_sock(NULL) |
| 65 | , m_session(static_cast<uint64_t>(time::now())) |
| 66 | , m_inviteListDialog(new InviteListDialog) |
| 67 | { |
| 68 | qRegisterMetaType<std::vector<Sync::MissingDataInfo> >("std::vector<Sync::MissingDataInfo>"); |
| 69 | qRegisterMetaType<ndn::Data>("ndn.Data"); |
| 70 | qRegisterMetaType<ndn::Interest>("ndn.Interest"); |
| 71 | qRegisterMetaType<size_t>("size_t"); |
| 72 | |
| 73 | m_scene = new DigestTreeScene(this); |
| 74 | m_rosterModel = new QStringListModel(this); |
| 75 | m_timer = new QTimer(this); |
| 76 | |
| 77 | ui->setupUi(this); |
| 78 | ui->treeViewer->setScene(m_scene); |
| 79 | m_scene->setSceneRect(m_scene->itemsBoundingRect()); |
| 80 | ui->treeViewer->hide(); |
| 81 | ui->listView->setModel(m_rosterModel); |
| 82 | |
| 83 | m_identity = IdentityCertificate::certificateNameToPublicKeyName(m_myCertificate.getName()).getPrefix(-1); |
| 84 | updatePrefix(); |
| 85 | updateLabels(); |
| 86 | |
| 87 | m_scene->setCurrentPrefix(QString(m_localChatPrefix.toUri().c_str())); |
| 88 | m_scene->plot("Empty"); |
| 89 | |
| 90 | connect(ui->lineEdit, SIGNAL(returnPressed()), |
| 91 | this, SLOT(onReturnPressed())); |
| 92 | connect(ui->treeButton, SIGNAL(pressed()), |
| 93 | this, SLOT(onTreeButtonPressed())); |
| 94 | connect(m_scene, SIGNAL(replot()), |
| 95 | this, SLOT(onReplot())); |
| 96 | connect(m_scene, SIGNAL(rosterChanged(QStringList)), |
| 97 | this, SLOT(onRosterChanged(QStringList))); |
| 98 | connect(m_timer, SIGNAL(timeout()), |
| 99 | this, SLOT(onReplot())); |
| 100 | |
| 101 | connect(this, SIGNAL(processData(const ndn::Data&, bool, bool)), |
| 102 | this, SLOT(onProcessData(const ndn::Data&, bool, bool))); |
| 103 | connect(this, SIGNAL(processTreeUpdate(const std::vector<Sync::MissingDataInfo>)), |
| 104 | this, SLOT(onProcessTreeUpdate(const std::vector<Sync::MissingDataInfo>))); |
| 105 | connect(this, SIGNAL(reply(const ndn::Interest&, const ndn::Data&, size_t, bool)), |
| 106 | this, SLOT(onReply(const ndn::Interest&, const ndn::Data&, size_t, bool))); |
| 107 | connect(this, SIGNAL(replyTimeout(const ndn::Interest&, size_t)), |
| 108 | this, SLOT(onReplyTimeout(const ndn::Interest&, size_t))); |
| 109 | connect(this, SIGNAL(introCert(const ndn::Interest&, const ndn::Data&)), |
| 110 | this, SLOT(onIntroCert(const ndn::Interest&, const ndn::Data&))); |
| 111 | connect(this, SIGNAL(introCertTimeout(const ndn::Interest&, int, const QString&)), |
| 112 | this, SLOT(onIntroCertTimeout(const ndn::Interest&, int, const QString&))); |
| 113 | |
| 114 | if(withSecurity) |
| 115 | { |
| 116 | |
| 117 | m_invitationValidator = make_shared<chronos::ValidatorInvitation>(); |
| 118 | m_dataRule = make_shared<SecRuleRelative>("([^<CHRONOCHAT-DATA>]*)<CHRONOCHAT-DATA><>", |
| 119 | "^([^<KEY>]*)<KEY>(<>*)<><ID-CERT>$", |
| 120 | "==", "\\1", "\\1", true); |
| 121 | |
| 122 | ui->inviteButton->setEnabled(true); |
| 123 | connect(ui->inviteButton, SIGNAL(clicked()), |
| 124 | this, SLOT(onInviteListDialogRequested())); |
| 125 | connect(m_inviteListDialog, SIGNAL(sendInvitation(const QString&)), |
| 126 | this, SLOT(onSendInvitation(const QString&))); |
| 127 | connect(m_contactManager, SIGNAL(contactAliasListReady(const QStringList&)), |
| 128 | m_inviteListDialog, SLOT(onContactAliasListReady(const QStringList&))); |
| 129 | connect(m_contactManager, SIGNAL(contactIdListReady(const QStringList&)), |
| 130 | m_inviteListDialog, SLOT(onContactIdListReady(const QStringList&))); |
| 131 | } |
| 132 | |
| 133 | initializeSync(); |
| 134 | } |
| 135 | |
| 136 | |
| 137 | ChatDialog::~ChatDialog() |
| 138 | { |
| 139 | if(m_certListPrefixId) |
| 140 | m_face->unsetInterestFilter(m_certListPrefixId); |
| 141 | |
| 142 | if(m_certSinglePrefixId) |
| 143 | m_face->unsetInterestFilter(m_certSinglePrefixId); |
| 144 | |
| 145 | if(m_sock != NULL) |
| 146 | { |
| 147 | sendLeave(); |
| 148 | delete m_sock; |
| 149 | m_sock = NULL; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | // public methods: |
| 154 | void |
| 155 | ChatDialog::addSyncAnchor(const Invitation& invitation) |
| 156 | { |
| 157 | // Add inviter certificate as trust anchor. |
| 158 | m_sock->addParticipant(invitation.getInviterCertificate()); |
| 159 | |
| 160 | // Ask inviter for IntroCertificate |
| 161 | Name inviterNameSpace = IdentityCertificate::certificateNameToPublicKeyName(invitation.getInviterCertificate().getName()).getPrefix(-1); |
| 162 | fetchIntroCert(inviterNameSpace, invitation.getInviterRoutingPrefix()); |
| 163 | } |
| 164 | |
| 165 | void |
| 166 | ChatDialog::processTreeUpdateWrapper(const vector<Sync::MissingDataInfo>& v, Sync::SyncSocket *sock) |
| 167 | { |
| 168 | emit processTreeUpdate(v); |
| 169 | _LOG_DEBUG("<<< Tree update signal emitted"); |
| 170 | } |
| 171 | |
| 172 | void |
| 173 | ChatDialog::processDataWrapper(const shared_ptr<const Data>& data) |
| 174 | { |
| 175 | emit processData(*data, true, false); |
| 176 | _LOG_DEBUG("<<< " << data->getName() << " fetched"); |
| 177 | } |
| 178 | |
| 179 | void |
| 180 | ChatDialog::processDataNoShowWrapper(const shared_ptr<const Data>& data) |
| 181 | { |
| 182 | emit processData(*data, false, false); |
| 183 | } |
| 184 | |
| 185 | void |
| 186 | ChatDialog::processRemoveWrapper(string prefix) |
| 187 | { |
| 188 | _LOG_DEBUG("Sync REMOVE signal received for prefix: " << prefix); |
| 189 | } |
| 190 | |
| 191 | // protected methods: |
| 192 | void |
| 193 | ChatDialog::closeEvent(QCloseEvent *e) |
| 194 | { |
| 195 | QMessageBox::information(this, tr("ChronoChat"), |
| 196 | tr("The chatroom will keep running in the " |
| 197 | "system tray. To close the chatroom, " |
| 198 | "choose <b>Close chatroom</b> in the " |
| 199 | "context memu of the system tray entry.")); |
| 200 | hide(); |
| 201 | e->ignore(); |
| 202 | } |
| 203 | |
| 204 | void |
| 205 | ChatDialog::resizeEvent(QResizeEvent *e) |
| 206 | { |
| 207 | fitView(); |
| 208 | } |
| 209 | |
| 210 | void |
| 211 | ChatDialog::showEvent(QShowEvent *e) |
| 212 | { |
| 213 | fitView(); |
| 214 | } |
| 215 | |
| 216 | // private methods: |
| 217 | void |
| 218 | ChatDialog::updatePrefix() |
| 219 | { |
| 220 | m_certListPrefix.clear(); |
| 221 | m_certSinglePrefix.clear(); |
| 222 | m_localChatPrefix.clear(); |
| 223 | m_chatPrefix.clear(); |
| 224 | m_chatPrefix.append(m_identity).append("CHRONOCHAT-DATA").append(m_chatroomName).append(getRandomString()); |
| 225 | if(!m_localPrefix.isPrefixOf(m_identity)) |
| 226 | { |
| 227 | m_useRoutablePrefix = true; |
| 228 | m_certListPrefix.append(m_localPrefix).append(CHRONOS_RP_SEPARATOR, 2); |
| 229 | m_certSinglePrefix.append(m_localPrefix).append(CHRONOS_RP_SEPARATOR, 2); |
| 230 | m_localChatPrefix.append(m_localPrefix).append(CHRONOS_RP_SEPARATOR, 2); |
| 231 | } |
| 232 | m_certListPrefix.append(m_identity).append("CHRONOCHAT-CERT-LIST").append(m_chatroomName); |
| 233 | m_certSinglePrefix.append(m_identity).append("CHRONOCHAT-CERT-SINGLE").append(m_chatroomName); |
| 234 | m_localChatPrefix.append(m_chatPrefix); |
| 235 | |
| 236 | if(m_certListPrefixId) |
| 237 | m_face->unsetInterestFilter(m_certListPrefixId); |
| 238 | m_certListPrefixId = m_face->setInterestFilter (m_certListPrefix, |
| 239 | bind(&ChatDialog::onCertListInterest, this, _1, _2), |
| 240 | bind(&ChatDialog::onCertListRegisterFailed, this, _1, _2)); |
| 241 | |
| 242 | if(m_certSinglePrefixId) |
| 243 | m_face->unsetInterestFilter(m_certSinglePrefixId); |
| 244 | m_certSinglePrefixId = m_face->setInterestFilter (m_certSinglePrefix, |
| 245 | bind(&ChatDialog::onCertSingleInterest, this, _1, _2), |
| 246 | bind(&ChatDialog::onCertSingleRegisterFailed, this, _1, _2)); |
| 247 | } |
| 248 | |
| 249 | void |
| 250 | ChatDialog::updateLabels() |
| 251 | { |
| 252 | QString settingDisp = QString("Chatroom: %1").arg(QString::fromStdString(m_chatroomName)); |
| 253 | ui->infoLabel->setStyleSheet("QLabel {color: #630; font-size: 16px; font: bold \"Verdana\";}"); |
| 254 | ui->infoLabel->setText(settingDisp); |
| 255 | QString prefixDisp; |
| 256 | Name privatePrefix("/private/local"); |
| 257 | if(privatePrefix.isPrefixOf(m_localChatPrefix)) |
| 258 | { |
| 259 | prefixDisp = |
| 260 | QString("<Warning: no connection to hub or hub does not support prefix autoconfig.>\n <Prefix = %1>") |
| 261 | .arg(QString::fromStdString(m_localChatPrefix.toUri())); |
| 262 | ui->prefixLabel->setStyleSheet("QLabel {color: red; font-size: 12px; font: bold \"Verdana\";}"); |
| 263 | } |
| 264 | else |
| 265 | { |
| 266 | prefixDisp = QString("<Prefix = %1>") |
| 267 | .arg(QString::fromStdString(m_localChatPrefix.toUri())); |
| 268 | ui->prefixLabel->setStyleSheet("QLabel {color: Green; font-size: 12px; font: bold \"Verdana\";}"); |
| 269 | } |
| 270 | ui->prefixLabel->setText(prefixDisp); |
| 271 | } |
| 272 | |
| 273 | void |
| 274 | ChatDialog::initializeSync() |
| 275 | { |
| 276 | |
| 277 | m_sock = new Sync::SyncSocket(m_chatroomPrefix, |
| 278 | m_chatPrefix, |
| 279 | m_session, |
| 280 | m_useRoutablePrefix, |
| 281 | m_localPrefix, |
| 282 | m_face, |
| 283 | m_myCertificate, |
| 284 | m_dataRule, |
| 285 | bind(&ChatDialog::processTreeUpdateWrapper, this, _1, _2), |
| 286 | bind(&ChatDialog::processRemoveWrapper, this, _1)); |
| 287 | |
| 288 | usleep(100000); |
| 289 | |
| 290 | QTimer::singleShot(600, this, SLOT(sendJoin())); |
| 291 | m_timer->start(FRESHNESS * 1000); |
| 292 | disableTreeDisplay(); |
| 293 | QTimer::singleShot(2200, this, SLOT(enableTreeDisplay())); |
| 294 | } |
| 295 | |
| 296 | void |
| 297 | ChatDialog::sendInvitation(shared_ptr<Contact> contact, bool isIntroducer) |
| 298 | { |
| 299 | // Add invitee as a trust anchor. |
| 300 | m_invitationValidator->addTrustAnchor(contact->getPublicKeyName(), |
| 301 | contact->getPublicKey()); |
| 302 | |
| 303 | // Prepared an invitation interest without routable prefix. |
| 304 | Invitation invitation(contact->getNameSpace(), |
| 305 | m_chatroomName, |
| 306 | m_localPrefix, |
| 307 | m_myCertificate); |
| 308 | Interest tmpInterest(invitation.getUnsignedInterestName()); |
| 309 | m_keyChain.sign(tmpInterest, m_myCertificate.getName()); |
| 310 | |
| 311 | // Get invitee's routable prefix (ideally it will do some DNS lookup, but we assume everyone use /ndn/broadcast |
| 312 | Name routablePrefix = getInviteeRoutablePrefix(contact->getNameSpace()); |
| 313 | |
| 314 | // Check if we need to prepend the routable prefix to the interest name. |
| 315 | bool requireRoutablePrefix = false; |
| 316 | Name interestName; |
| 317 | size_t routablePrefixOffset = 0; |
| 318 | if(!routablePrefix.isPrefixOf(tmpInterest.getName())) |
| 319 | { |
| 320 | interestName.append(routablePrefix).append(CHRONOS_RP_SEPARATOR, 2); |
| 321 | requireRoutablePrefix = true; |
| 322 | routablePrefixOffset = routablePrefix.size() + 1; |
| 323 | } |
| 324 | interestName.append(tmpInterest.getName()); |
| 325 | |
| 326 | // Send the invitation out |
| 327 | Interest interest(interestName); |
| 328 | interest.setMustBeFresh(true); |
| 329 | m_face->expressInterest(interest, |
| 330 | bind(&ChatDialog::replyWrapper, this, _1, _2, routablePrefixOffset, isIntroducer), |
| 331 | bind(&ChatDialog::replyTimeoutWrapper, this, _1, routablePrefixOffset)); |
| 332 | } |
| 333 | |
| 334 | void |
| 335 | ChatDialog::replyWrapper(const Interest& interest, |
| 336 | Data& data, |
| 337 | size_t routablePrefixOffset, |
| 338 | bool isIntroducer) |
| 339 | { |
| 340 | emit reply(interest, data, routablePrefixOffset, isIntroducer); |
| 341 | } |
| 342 | |
| 343 | void |
| 344 | ChatDialog::replyTimeoutWrapper(const Interest& interest, |
| 345 | size_t routablePrefixOffset) |
| 346 | { |
| 347 | emit replyTimeout(interest, routablePrefixOffset); |
| 348 | } |
| 349 | |
| 350 | void |
| 351 | ChatDialog::onReplyValidated(const shared_ptr<const Data>& data, |
| 352 | size_t inviteeRoutablePrefixOffset, |
| 353 | bool isIntroducer) |
| 354 | { |
| 355 | if(data->getName().size() <= inviteeRoutablePrefixOffset) |
| 356 | { |
| 357 | Invitation invitation(data->getName()); |
| 358 | invitationRejected(invitation.getInviteeNameSpace()); |
| 359 | } |
| 360 | else |
| 361 | { |
| 362 | Name inviteePrefix; |
| 363 | inviteePrefix.wireDecode(data->getName().get(inviteeRoutablePrefixOffset).blockFromValue()); |
| 364 | IdentityCertificate inviteeCert; |
| 365 | inviteeCert.wireDecode(data->getContent().blockFromValue()); |
| 366 | invitationAccepted(inviteeCert, inviteePrefix, isIntroducer); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | void |
| 371 | ChatDialog::onReplyValidationFailed(const shared_ptr<const Data>& data, |
| 372 | const string& failureInfo) |
| 373 | { |
| 374 | _LOG_DEBUG("Invitation reply cannot be validated: " + failureInfo + " ==> " + data->getName().toUri()); |
| 375 | } |
| 376 | |
| 377 | void |
| 378 | ChatDialog::invitationRejected(const Name& identity) |
| 379 | { |
| 380 | QString msg = QString::fromStdString(identity.toUri()) + " rejected your invitation!"; |
| 381 | emit inivationRejection(msg); |
| 382 | } |
| 383 | |
| 384 | void |
| 385 | ChatDialog::invitationAccepted(const IdentityCertificate& inviteeCert, |
| 386 | const Name& inviteePrefix, |
| 387 | bool isIntroducer) |
| 388 | { |
| 389 | // Add invitee certificate as trust anchor. |
| 390 | m_sock->addParticipant(inviteeCert); |
| 391 | |
| 392 | // Ask invitee for IntroCertificate. |
| 393 | Name inviteeNameSpace = IdentityCertificate::certificateNameToPublicKeyName(inviteeCert.getName()).getPrefix(-1); |
| 394 | fetchIntroCert(inviteeNameSpace, inviteePrefix); |
| 395 | } |
| 396 | |
| 397 | void |
| 398 | ChatDialog::fetchIntroCert(const Name& identity, const Name& prefix) |
| 399 | { |
| 400 | Name interestName; |
| 401 | |
| 402 | if(!prefix.isPrefixOf(identity)) |
| 403 | interestName.append(prefix).append(CHRONOS_RP_SEPARATOR, 2); |
| 404 | |
| 405 | interestName.append(identity) |
| 406 | .append("CHRONOCHAT-CERT-LIST") |
| 407 | .append(m_chatroomName) |
| 408 | .appendVersion(); |
| 409 | |
| 410 | Interest interest(interestName); |
| 411 | interest.setMustBeFresh(true); |
| 412 | |
| 413 | m_face->expressInterest(interest, |
| 414 | bind(&ChatDialog::onIntroCertList, this, _1, _2), |
| 415 | bind(&ChatDialog::onIntroCertListTimeout, this, _1, 1, |
| 416 | "IntroCertList: " + interestName.toUri())); |
| 417 | } |
| 418 | |
| 419 | void |
| 420 | ChatDialog::onIntroCertList(const Interest& interest, const Data& data) |
| 421 | { |
| 422 | Chronos::IntroCertListMsg introCertList; |
| 423 | if(!introCertList.ParseFromArray(data.getContent().value(), data.getContent().value_size())) |
| 424 | return; |
| 425 | |
| 426 | for(int i = 0; i < introCertList.certname_size(); i++) |
| 427 | { |
| 428 | Name certName(introCertList.certname(i)); |
| 429 | Interest interest(certName); |
| 430 | interest.setMustBeFresh(true); |
| 431 | |
| 432 | m_face->expressInterest(interest, |
| 433 | bind(&ChatDialog::introCertWrapper, this, _1, _2), |
| 434 | bind(&ChatDialog::introCertTimeoutWrapper, this, _1, 0, |
| 435 | QString("IntroCert: %1").arg(introCertList.certname(i).c_str()))); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | void |
| 440 | ChatDialog::onIntroCertListTimeout(const Interest& interest, int retry, const string& msg) |
| 441 | { |
| 442 | if(retry > 0) |
| 443 | m_face->expressInterest(interest, |
| 444 | bind(&ChatDialog::onIntroCertList, this, _1, _2), |
| 445 | bind(&ChatDialog::onIntroCertListTimeout, this, _1, retry - 1, msg)); |
| 446 | else |
| 447 | _LOG_DEBUG(msg << " TIMEOUT!"); |
| 448 | } |
| 449 | |
| 450 | void |
| 451 | ChatDialog::introCertWrapper(const Interest& interest, Data& data) |
| 452 | { |
| 453 | emit introCert(interest, data); |
| 454 | } |
| 455 | |
| 456 | void |
| 457 | ChatDialog::introCertTimeoutWrapper(const Interest& interest, int retry, const QString& msg) |
| 458 | { |
| 459 | emit introCertTimeout(interest, retry, msg); |
| 460 | } |
| 461 | |
| 462 | void |
| 463 | ChatDialog::onCertListInterest(const Name& prefix, const ndn::Interest& interest) |
| 464 | { |
| 465 | vector<Name> certNameList; |
| 466 | m_sock->getIntroCertNames(certNameList); |
| 467 | |
| 468 | Chronos::IntroCertListMsg msg; |
| 469 | |
| 470 | vector<Name>::const_iterator it = certNameList.begin(); |
| 471 | vector<Name>::const_iterator end = certNameList.end(); |
| 472 | for(; it != end; it++) |
| 473 | { |
| 474 | Name certName; |
| 475 | certName.append(m_certSinglePrefix).append(*it); |
| 476 | msg.add_certname(certName.toUri()); |
| 477 | } |
| 478 | OBufferStream os; |
| 479 | msg.SerializeToOstream(&os); |
| 480 | |
| 481 | Data data(interest.getName()); |
| 482 | data.setContent(os.buf()); |
| 483 | m_keyChain.sign(data, m_myCertificate.getName()); |
| 484 | |
| 485 | m_face->put(data); |
| 486 | } |
| 487 | |
| 488 | void |
| 489 | ChatDialog::onCertListRegisterFailed(const Name& prefix, const string& msg) |
| 490 | { |
| 491 | _LOG_DEBUG("ChatDialog::onCertListRegisterFailed failed: " + msg); |
| 492 | } |
| 493 | |
| 494 | void |
| 495 | ChatDialog::onCertSingleInterest(const Name& prefix, const ndn::Interest& interest) |
| 496 | { |
| 497 | try |
| 498 | { |
| 499 | Name certName = interest.getName().getSubName(prefix.size()); |
| 500 | const Sync::IntroCertificate& introCert = m_sock->getIntroCertificate(certName); |
| 501 | Data data(interest.getName()); |
| 502 | data.setContent(introCert.wireEncode()); |
| 503 | m_face->put(data); |
| 504 | } |
| 505 | catch(Sync::SyncSocket::Error& e) |
| 506 | { |
| 507 | return; |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | void |
| 512 | ChatDialog::onCertSingleRegisterFailed(const Name& prefix, const string& msg) |
| 513 | { |
| 514 | _LOG_DEBUG("ChatDialog::onCertListRegisterFailed failed: " + msg); |
| 515 | } |
| 516 | |
| 517 | void |
| 518 | ChatDialog::sendMsg(SyncDemo::ChatMessage &msg) |
| 519 | { |
| 520 | // send msg |
| 521 | OBufferStream os; |
| 522 | msg.SerializeToOstream(&os); |
| 523 | |
| 524 | if (!msg.IsInitialized()) |
| 525 | { |
| 526 | _LOG_DEBUG("Errrrr.. msg was not probally initialized "<<__FILE__ <<":"<<__LINE__<<". what is happening?"); |
| 527 | abort(); |
| 528 | } |
| 529 | m_sock->publishData(os.buf()->buf(), os.buf()->size(), FRESHNESS); |
| 530 | |
| 531 | m_lastMsgTime = time::now(); |
| 532 | |
| 533 | uint64_t nextSequence = m_sock->getNextSeq(); |
| 534 | Sync::MissingDataInfo mdi = {m_localChatPrefix.toUri(), Sync::SeqNo(0), Sync::SeqNo(nextSequence - 1)}; |
| 535 | std::vector<Sync::MissingDataInfo> v; |
| 536 | v.push_back(mdi); |
| 537 | { |
| 538 | boost::recursive_mutex::scoped_lock lock(m_sceneMutex); |
| 539 | m_scene->processUpdate(v, m_sock->getRootDigest().c_str()); |
| 540 | m_scene->msgReceived(QString::fromStdString(m_localChatPrefix.toUri()), |
| 541 | QString::fromStdString(m_nick)); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | void ChatDialog::disableTreeDisplay() |
| 546 | { |
| 547 | ui->treeButton->setEnabled(false); |
| 548 | ui->treeViewer->hide(); |
| 549 | fitView(); |
| 550 | } |
| 551 | |
| 552 | void |
| 553 | ChatDialog::appendMessage(const SyncDemo::ChatMessage msg, bool isHistory) |
| 554 | { |
| 555 | boost::recursive_mutex::scoped_lock lock(m_msgMutex); |
| 556 | |
| 557 | if (msg.type() == SyncDemo::ChatMessage::CHAT) |
| 558 | { |
| 559 | |
| 560 | if (!msg.has_data()) |
| 561 | { |
| 562 | return; |
| 563 | } |
| 564 | |
| 565 | if (msg.from().empty() || msg.data().empty()) |
| 566 | { |
| 567 | return; |
| 568 | } |
| 569 | |
| 570 | if (!msg.has_timestamp()) |
| 571 | { |
| 572 | return; |
| 573 | } |
| 574 | |
| 575 | // if (m_history.size() == MAX_HISTORY_ENTRY) |
| 576 | // { |
| 577 | // m_history.dequeue(); |
| 578 | // } |
| 579 | |
| 580 | // m_history.enqueue(msg); |
| 581 | |
| 582 | QTextCharFormat nickFormat; |
| 583 | nickFormat.setForeground(Qt::darkGreen); |
| 584 | nickFormat.setFontWeight(QFont::Bold); |
| 585 | nickFormat.setFontUnderline(true); |
| 586 | nickFormat.setUnderlineColor(Qt::gray); |
| 587 | |
| 588 | QTextCursor cursor(ui->textEdit->textCursor()); |
| 589 | cursor.movePosition(QTextCursor::End); |
| 590 | QTextTableFormat tableFormat; |
| 591 | tableFormat.setBorder(0); |
| 592 | QTextTable *table = cursor.insertTable(1, 2, tableFormat); |
| 593 | QString from = QString("%1 ").arg(msg.from().c_str()); |
| 594 | QTextTableCell fromCell = table->cellAt(0, 0); |
| 595 | fromCell.setFormat(nickFormat); |
| 596 | fromCell.firstCursorPosition().insertText(from); |
| 597 | |
| 598 | time_t timestamp = msg.timestamp(); |
| 599 | printTimeInCell(table, timestamp); |
| 600 | |
| 601 | QTextCursor nextCursor(ui->textEdit->textCursor()); |
| 602 | nextCursor.movePosition(QTextCursor::End); |
| 603 | table = nextCursor.insertTable(1, 1, tableFormat); |
| 604 | table->cellAt(0, 0).firstCursorPosition().insertText(QString::fromUtf8(msg.data().c_str())); |
| 605 | if (!isHistory) |
| 606 | { |
| 607 | showMessage(from, QString::fromUtf8(msg.data().c_str())); |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | if (msg.type() == SyncDemo::ChatMessage::JOIN || msg.type() == SyncDemo::ChatMessage::LEAVE) |
| 612 | { |
| 613 | QTextCharFormat nickFormat; |
| 614 | nickFormat.setForeground(Qt::gray); |
| 615 | nickFormat.setFontWeight(QFont::Bold); |
| 616 | nickFormat.setFontUnderline(true); |
| 617 | nickFormat.setUnderlineColor(Qt::gray); |
| 618 | |
| 619 | QTextCursor cursor(ui->textEdit->textCursor()); |
| 620 | cursor.movePosition(QTextCursor::End); |
| 621 | QTextTableFormat tableFormat; |
| 622 | tableFormat.setBorder(0); |
| 623 | QTextTable *table = cursor.insertTable(1, 2, tableFormat); |
| 624 | QString action; |
| 625 | if (msg.type() == SyncDemo::ChatMessage::JOIN) |
| 626 | { |
| 627 | action = "enters room"; |
| 628 | } |
| 629 | else |
| 630 | { |
| 631 | action = "leaves room"; |
| 632 | } |
| 633 | |
| 634 | QString from = QString("%1 %2 ").arg(msg.from().c_str()).arg(action); |
| 635 | QTextTableCell fromCell = table->cellAt(0, 0); |
| 636 | fromCell.setFormat(nickFormat); |
| 637 | fromCell.firstCursorPosition().insertText(from); |
| 638 | |
| 639 | time_t timestamp = msg.timestamp(); |
| 640 | printTimeInCell(table, timestamp); |
| 641 | } |
| 642 | |
| 643 | QScrollBar *bar = ui->textEdit->verticalScrollBar(); |
| 644 | bar->setValue(bar->maximum()); |
| 645 | } |
| 646 | |
| 647 | void |
| 648 | ChatDialog::processRemove(QString prefix) |
| 649 | { |
| 650 | _LOG_DEBUG("<<< remove node for prefix" << prefix.toStdString()); |
| 651 | |
| 652 | bool removed = m_scene->removeNode(prefix); |
| 653 | if (removed) |
| 654 | { |
| 655 | boost::recursive_mutex::scoped_lock lock(m_sceneMutex); |
| 656 | m_scene->plot(m_sock->getRootDigest().c_str()); |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | Name |
| 661 | ChatDialog::getInviteeRoutablePrefix(const Name& invitee) |
| 662 | { |
| 663 | return ndn::Name("/ndn/broadcast"); |
| 664 | } |
| 665 | |
| 666 | void |
| 667 | ChatDialog::formChatMessage(const QString &text, SyncDemo::ChatMessage &msg) { |
| 668 | msg.set_from(m_nick); |
| 669 | msg.set_to(m_chatroomName); |
| 670 | msg.set_data(text.toStdString()); |
| 671 | int32_t seconds = static_cast<int32_t>(time::now()/1000000000); |
| 672 | msg.set_timestamp(seconds); |
| 673 | msg.set_type(SyncDemo::ChatMessage::CHAT); |
| 674 | } |
| 675 | |
| 676 | void |
| 677 | ChatDialog::formControlMessage(SyncDemo::ChatMessage &msg, SyncDemo::ChatMessage::ChatMessageType type) |
| 678 | { |
| 679 | msg.set_from(m_nick); |
| 680 | msg.set_to(m_chatroomName); |
| 681 | int32_t seconds = static_cast<int32_t>(time::now()/1000000000); |
| 682 | msg.set_timestamp(seconds); |
| 683 | msg.set_type(type); |
| 684 | } |
| 685 | |
| 686 | QString |
| 687 | ChatDialog::formatTime(time_t timestamp) |
| 688 | { |
| 689 | struct tm *tm_time = localtime(×tamp); |
| 690 | int hour = tm_time->tm_hour; |
| 691 | QString amOrPM; |
| 692 | if (hour > 12) |
| 693 | { |
| 694 | hour -= 12; |
| 695 | amOrPM = "PM"; |
| 696 | } |
| 697 | else |
| 698 | { |
| 699 | amOrPM = "AM"; |
| 700 | if (hour == 0) |
| 701 | { |
| 702 | hour = 12; |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | char textTime[12]; |
| 707 | sprintf(textTime, "%d:%02d:%02d %s", hour, tm_time->tm_min, tm_time->tm_sec, amOrPM.toStdString().c_str()); |
| 708 | return QString(textTime); |
| 709 | } |
| 710 | |
| 711 | void |
| 712 | ChatDialog::printTimeInCell(QTextTable *table, time_t timestamp) |
| 713 | { |
| 714 | QTextCharFormat timeFormat; |
| 715 | timeFormat.setForeground(Qt::gray); |
| 716 | timeFormat.setFontUnderline(true); |
| 717 | timeFormat.setUnderlineColor(Qt::gray); |
| 718 | QTextTableCell timeCell = table->cellAt(0, 1); |
| 719 | timeCell.setFormat(timeFormat); |
| 720 | timeCell.firstCursorPosition().insertText(formatTime(timestamp)); |
| 721 | } |
| 722 | |
| 723 | string |
| 724 | ChatDialog::getRandomString() |
| 725 | { |
| 726 | uint32_t r = random::generateWord32(); |
| 727 | stringstream ss; |
| 728 | { |
| 729 | using namespace CryptoPP; |
| 730 | StringSource(reinterpret_cast<uint8_t*>(&r), 4, true, |
| 731 | new HexEncoder(new FileSink(ss), false)); |
| 732 | |
| 733 | } |
| 734 | |
| 735 | return ss.str(); |
| 736 | } |
| 737 | |
| 738 | void |
| 739 | ChatDialog::showMessage(const QString& from, const QString& data) |
| 740 | { |
| 741 | if (!isActiveWindow()) |
| 742 | emit showChatMessage(QString::fromStdString(m_chatroomName), |
| 743 | from, data); |
| 744 | } |
| 745 | |
| 746 | void |
| 747 | ChatDialog::fitView() |
| 748 | { |
| 749 | boost::recursive_mutex::scoped_lock lock(m_sceneMutex); |
| 750 | QRectF rect = m_scene->itemsBoundingRect(); |
| 751 | m_scene->setSceneRect(rect); |
| 752 | ui->treeViewer->fitInView(m_scene->itemsBoundingRect(), Qt::KeepAspectRatio); |
| 753 | } |
| 754 | |
| 755 | void |
| 756 | ChatDialog::summonReaper() |
| 757 | { |
| 758 | Sync::SyncLogic &logic = m_sock->getLogic (); |
| 759 | map<string, bool> branches = logic.getBranchPrefixes(); |
| 760 | QMap<QString, DisplayUserPtr> roster = m_scene->getRosterFull(); |
| 761 | |
| 762 | m_zombieList.clear(); |
| 763 | |
| 764 | QMapIterator<QString, DisplayUserPtr> it(roster); |
| 765 | map<string, bool>::iterator mapIt; |
| 766 | while(it.hasNext()) |
| 767 | { |
| 768 | it.next(); |
| 769 | DisplayUserPtr p = it.value(); |
| 770 | if (p != DisplayUserNullPtr) |
| 771 | { |
| 772 | mapIt = branches.find(p->getPrefix().toStdString()); |
| 773 | if (mapIt != branches.end()) |
| 774 | { |
| 775 | mapIt->second = true; |
| 776 | } |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | for (mapIt = branches.begin(); mapIt != branches.end(); ++mapIt) |
| 781 | { |
| 782 | // this is zombie. all active users should have been marked true |
| 783 | if (! mapIt->second) |
| 784 | { |
| 785 | m_zombieList.append(mapIt->first.c_str()); |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | m_zombieIndex = 0; |
| 790 | |
| 791 | // start reaping |
| 792 | reap(); |
| 793 | } |
| 794 | |
| 795 | |
| 796 | // public slots: |
| 797 | void |
| 798 | ChatDialog::onLocalPrefixUpdated(const QString& localPrefix) |
| 799 | { |
| 800 | Name newLocalPrefix(localPrefix.toStdString()); |
| 801 | if(!newLocalPrefix.empty() && newLocalPrefix != m_localPrefix) |
| 802 | { |
| 803 | // Update localPrefix |
| 804 | m_localPrefix = newLocalPrefix; |
| 805 | |
| 806 | updatePrefix(); |
| 807 | updateLabels(); |
| 808 | m_scene->setCurrentPrefix(QString(m_localChatPrefix.toUri().c_str())); |
| 809 | |
| 810 | if(m_sock != NULL) |
| 811 | { |
| 812 | { |
| 813 | boost::recursive_mutex::scoped_lock lock(m_sceneMutex); |
| 814 | m_scene->clearAll(); |
| 815 | m_scene->plot("Empty"); |
| 816 | } |
| 817 | |
| 818 | ui->textEdit->clear(); |
| 819 | |
| 820 | if (m_joined) |
| 821 | { |
| 822 | sendLeave(); |
| 823 | } |
| 824 | |
| 825 | delete m_sock; |
| 826 | m_sock = NULL; |
| 827 | |
| 828 | usleep(100000); |
| 829 | m_sock = new Sync::SyncSocket(m_chatroomPrefix, |
| 830 | m_chatPrefix, |
| 831 | m_session, |
| 832 | m_useRoutablePrefix, |
| 833 | m_localPrefix, |
| 834 | m_face, |
| 835 | m_myCertificate, |
| 836 | m_dataRule, |
| 837 | bind(&ChatDialog::processTreeUpdateWrapper, this, _1, _2), |
| 838 | bind(&ChatDialog::processRemoveWrapper, this, _1)); |
| 839 | usleep(100000); |
| 840 | QTimer::singleShot(600, this, SLOT(sendJoin())); |
| 841 | m_timer->start(FRESHNESS * 1000); |
| 842 | disableTreeDisplay(); |
| 843 | QTimer::singleShot(2200, this, SLOT(enableTreeDisplay())); |
| 844 | } |
| 845 | else |
| 846 | initializeSync(); |
| 847 | } |
| 848 | else |
| 849 | if (m_sock == NULL) |
| 850 | initializeSync(); |
| 851 | |
| 852 | fitView(); |
| 853 | } |
| 854 | |
| 855 | void |
| 856 | ChatDialog::onClose() |
| 857 | { |
| 858 | hide(); |
| 859 | emit closeChatDialog(QString::fromStdString(m_chatroomName)); |
| 860 | } |
| 861 | |
| 862 | |
| 863 | // private slots: |
| 864 | void |
| 865 | ChatDialog::onReturnPressed() |
| 866 | { |
| 867 | QString text = ui->lineEdit->text(); |
| 868 | if (text.isEmpty()) |
| 869 | return; |
| 870 | |
| 871 | ui->lineEdit->clear(); |
| 872 | |
| 873 | if (text.startsWith("boruoboluomi")) |
| 874 | { |
| 875 | summonReaper (); |
| 876 | // reapButton->show(); |
| 877 | fitView(); |
| 878 | return; |
| 879 | } |
| 880 | |
| 881 | if (text.startsWith("minimanihong")) |
| 882 | { |
| 883 | // reapButton->hide(); |
| 884 | fitView(); |
| 885 | return; |
| 886 | } |
| 887 | |
| 888 | SyncDemo::ChatMessage msg; |
| 889 | formChatMessage(text, msg); |
| 890 | |
| 891 | appendMessage(msg); |
| 892 | |
| 893 | sendMsg(msg); |
| 894 | |
| 895 | fitView(); |
| 896 | } |
| 897 | |
| 898 | void |
| 899 | ChatDialog::onTreeButtonPressed() |
| 900 | { |
| 901 | if (ui->treeViewer->isVisible()) |
| 902 | { |
| 903 | ui->treeViewer->hide(); |
| 904 | ui->treeButton->setText("Show ChronoSync Tree"); |
| 905 | } |
| 906 | else |
| 907 | { |
| 908 | ui->treeViewer->show(); |
| 909 | ui->treeButton->setText("Hide ChronoSync Tree"); |
| 910 | } |
| 911 | |
| 912 | fitView(); |
| 913 | } |
| 914 | |
| 915 | void |
| 916 | ChatDialog::onProcessData(const Data& data, bool show, bool isHistory) |
| 917 | { |
| 918 | SyncDemo::ChatMessage msg; |
| 919 | bool corrupted = false; |
| 920 | if (!msg.ParseFromArray(data.getContent().value(), data.getContent().value_size())) |
| 921 | { |
| 922 | _LOG_DEBUG("Errrrr.. Can not parse msg with name: " << data.getName() << ". what is happening?"); |
| 923 | // nasty stuff: as a remedy, we'll form some standard msg for inparsable msgs |
| 924 | msg.set_from("inconnu"); |
| 925 | msg.set_type(SyncDemo::ChatMessage::OTHER); |
| 926 | corrupted = true; |
| 927 | } |
| 928 | |
| 929 | // display msg received from network |
| 930 | // we have to do so; this function is called by ccnd thread |
| 931 | // so if we call appendMsg directly |
| 932 | // Qt crash as "QObject: Cannot create children for a parent that is in a different thread" |
| 933 | // the "cannonical" way to is use signal-slot |
| 934 | if (show && !corrupted) |
| 935 | { |
| 936 | appendMessage(msg, isHistory); |
| 937 | } |
| 938 | |
| 939 | if (!isHistory) |
| 940 | { |
| 941 | // update the tree view |
| 942 | std::string stdStrName = data.getName().toUri(); |
| 943 | std::string stdStrNameWithoutSeq = stdStrName.substr(0, stdStrName.find_last_of('/')); |
| 944 | std::string prefix = stdStrNameWithoutSeq.substr(0, stdStrNameWithoutSeq.find_last_of('/')); |
| 945 | _LOG_DEBUG("<<< updating scene for" << prefix << ": " << msg.from()); |
| 946 | if (msg.type() == SyncDemo::ChatMessage::LEAVE) |
| 947 | { |
| 948 | processRemove(prefix.c_str()); |
| 949 | } |
| 950 | else |
| 951 | { |
| 952 | boost::recursive_mutex::scoped_lock lock(m_sceneMutex); |
| 953 | m_scene->msgReceived(prefix.c_str(), msg.from().c_str()); |
| 954 | } |
| 955 | } |
| 956 | fitView(); |
| 957 | } |
| 958 | |
| 959 | void |
| 960 | ChatDialog::onProcessTreeUpdate(const vector<Sync::MissingDataInfo>& v) |
| 961 | { |
| 962 | _LOG_DEBUG("<<< processing Tree Update"); |
| 963 | |
| 964 | if (v.empty()) |
| 965 | { |
| 966 | return; |
| 967 | } |
| 968 | |
| 969 | // reflect the changes on digest tree |
| 970 | { |
| 971 | boost::recursive_mutex::scoped_lock lock(m_sceneMutex); |
| 972 | m_scene->processUpdate(v, m_sock->getRootDigest().c_str()); |
| 973 | } |
| 974 | |
| 975 | int n = v.size(); |
| 976 | int totalMissingPackets = 0; |
| 977 | for (int i = 0; i < n; i++) |
| 978 | { |
| 979 | totalMissingPackets += v[i].high.getSeq() - v[i].low.getSeq() + 1; |
| 980 | } |
| 981 | |
| 982 | for (int i = 0; i < n; i++) |
| 983 | { |
| 984 | if (totalMissingPackets < 4) |
| 985 | { |
| 986 | for (Sync::SeqNo seq = v[i].low; seq <= v[i].high; ++seq) |
| 987 | { |
| 988 | m_sock->fetchData(v[i].prefix, seq, bind(&ChatDialog::processDataWrapper, this, _1), 2); |
| 989 | _LOG_DEBUG("<<< Fetching " << v[i].prefix << "/" <<seq.getSession() <<"/" << seq.getSeq()); |
| 990 | } |
| 991 | } |
| 992 | else |
| 993 | { |
| 994 | m_sock->fetchData(v[i].prefix, v[i].high, bind(&ChatDialog::processDataNoShowWrapper, this, _1), 2); |
| 995 | } |
| 996 | } |
| 997 | // adjust the view |
| 998 | fitView(); |
| 999 | } |
| 1000 | |
| 1001 | void |
| 1002 | ChatDialog::onReplot() |
| 1003 | { |
| 1004 | boost::recursive_mutex::scoped_lock lock(m_sceneMutex); |
| 1005 | m_scene->plot(m_sock->getRootDigest().c_str()); |
| 1006 | fitView(); |
| 1007 | } |
| 1008 | |
| 1009 | void |
| 1010 | ChatDialog::onRosterChanged(QStringList staleUserList) |
| 1011 | { |
| 1012 | boost::recursive_mutex::scoped_lock lock(m_sceneMutex); |
| 1013 | QStringList rosterList = m_scene->getRosterList(); |
| 1014 | m_rosterModel->setStringList(rosterList); |
| 1015 | QString user; |
| 1016 | QStringListIterator it(staleUserList); |
| 1017 | while(it.hasNext()) |
| 1018 | { |
| 1019 | std::string nick = it.next().toStdString(); |
| 1020 | if (nick.empty()) |
| 1021 | continue; |
| 1022 | |
| 1023 | SyncDemo::ChatMessage msg; |
| 1024 | formControlMessage(msg, SyncDemo::ChatMessage::LEAVE); |
| 1025 | msg.set_from(nick); |
| 1026 | appendMessage(msg); |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | void |
| 1031 | ChatDialog::onInviteListDialogRequested() |
| 1032 | { |
| 1033 | m_inviteListDialog->setInviteLabel(m_chatroomPrefix.toUri()); |
| 1034 | m_inviteListDialog->show(); |
| 1035 | } |
| 1036 | |
| 1037 | void |
| 1038 | ChatDialog::sendJoin() |
| 1039 | { |
| 1040 | m_joined = true; |
| 1041 | SyncDemo::ChatMessage msg; |
| 1042 | formControlMessage(msg, SyncDemo::ChatMessage::JOIN); |
| 1043 | sendMsg(msg); |
| 1044 | boost::random::random_device rng; |
| 1045 | boost::random::uniform_int_distribution<> uniform(1, FRESHNESS / 5 * 1000); |
| 1046 | m_randomizedInterval = HELLO_INTERVAL * 1000 + uniform(rng); |
| 1047 | QTimer::singleShot(m_randomizedInterval, this, SLOT(sendHello())); |
| 1048 | } |
| 1049 | |
| 1050 | void |
| 1051 | ChatDialog::sendHello() |
| 1052 | { |
| 1053 | int64_t now = time::now(); |
| 1054 | int elapsed = (now - m_lastMsgTime) / 1000000000; |
| 1055 | if (elapsed >= m_randomizedInterval / 1000) |
| 1056 | { |
| 1057 | SyncDemo::ChatMessage msg; |
| 1058 | formControlMessage(msg, SyncDemo::ChatMessage::HELLO); |
| 1059 | sendMsg(msg); |
| 1060 | boost::random::random_device rng; |
| 1061 | boost::random::uniform_int_distribution<> uniform(1, FRESHNESS / 5 * 1000); |
| 1062 | m_randomizedInterval = HELLO_INTERVAL * 1000 + uniform(rng); |
| 1063 | QTimer::singleShot(m_randomizedInterval, this, SLOT(sendHello())); |
| 1064 | } |
| 1065 | else |
| 1066 | { |
| 1067 | QTimer::singleShot((m_randomizedInterval - elapsed * 1000), this, SLOT(sendHello())); |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | void |
| 1072 | ChatDialog::sendLeave() |
| 1073 | { |
| 1074 | SyncDemo::ChatMessage msg; |
| 1075 | formControlMessage(msg, SyncDemo::ChatMessage::LEAVE); |
| 1076 | sendMsg(msg); |
| 1077 | usleep(500000); |
| 1078 | m_sock->leave(); |
| 1079 | usleep(5000); |
| 1080 | m_joined = false; |
| 1081 | _LOG_DEBUG("Sync REMOVE signal sent"); |
| 1082 | } |
| 1083 | |
| 1084 | void ChatDialog::enableTreeDisplay() |
| 1085 | { |
| 1086 | ui->treeButton->setEnabled(true); |
| 1087 | // treeViewer->show(); |
| 1088 | // fitView(); |
| 1089 | } |
| 1090 | |
| 1091 | void |
| 1092 | ChatDialog::reap() |
| 1093 | { |
| 1094 | if (m_zombieIndex < m_zombieList.size()) |
| 1095 | { |
| 1096 | string prefix = m_zombieList.at(m_zombieIndex).toStdString(); |
| 1097 | m_sock->remove(prefix); |
| 1098 | _LOG_DEBUG("Reaped: prefix = " << prefix); |
| 1099 | m_zombieIndex++; |
| 1100 | // reap again in 10 seconds |
| 1101 | QTimer::singleShot(10000, this, SLOT(reap())); |
| 1102 | } |
| 1103 | } |
| 1104 | |
| 1105 | void |
| 1106 | ChatDialog::onSendInvitation(QString invitee) |
| 1107 | { |
| 1108 | Name inviteeNamespace(invitee.toStdString()); |
| 1109 | shared_ptr<Contact> inviteeItem = m_contactManager->getContact(inviteeNamespace); |
| 1110 | sendInvitation(inviteeItem, true); |
| 1111 | } |
| 1112 | |
| 1113 | void |
| 1114 | ChatDialog::onReply(const Interest& interest, |
| 1115 | const Data& data, |
| 1116 | size_t routablePrefixOffset, |
| 1117 | bool isIntroducer) |
| 1118 | { |
| 1119 | OnDataValidated onValidated = bind(&ChatDialog::onReplyValidated, |
| 1120 | this, _1, |
| 1121 | interest.getName().size()-routablePrefixOffset, //RoutablePrefix will be removed before data is passed to the validator |
| 1122 | isIntroducer); |
| 1123 | |
| 1124 | OnDataValidationFailed onFailed = bind(&ChatDialog::onReplyValidationFailed, |
| 1125 | this, _1, _2); |
| 1126 | |
| 1127 | if(routablePrefixOffset > 0) |
| 1128 | { |
| 1129 | // It is an encapsulated packet, we only validate the inner packet. |
| 1130 | Data innerData; |
| 1131 | innerData.wireDecode(data.getContent().blockFromValue()); |
| 1132 | m_invitationValidator->validate(innerData, onValidated, onFailed); |
| 1133 | } |
| 1134 | else |
| 1135 | m_invitationValidator->validate(data, onValidated, onFailed); |
| 1136 | } |
| 1137 | |
| 1138 | void |
| 1139 | ChatDialog::onReplyTimeout(const Interest& interest, |
| 1140 | size_t routablePrefixOffset) |
| 1141 | { |
| 1142 | Name interestName; |
| 1143 | if(routablePrefixOffset > 0) |
| 1144 | interestName = interest.getName().getSubName(routablePrefixOffset); |
| 1145 | else |
| 1146 | interestName = interest.getName(); |
| 1147 | |
| 1148 | Invitation invitation(interestName); |
| 1149 | |
| 1150 | QString msg = QString::fromUtf8("Your invitation to ") + QString::fromStdString(invitation.getInviteeNameSpace().toUri()) + " times out!"; |
| 1151 | emit inivationRejection(msg); |
| 1152 | } |
| 1153 | |
| 1154 | void |
| 1155 | ChatDialog::onIntroCert(const ndn::Interest& interest, const Data& data) |
| 1156 | { |
| 1157 | Data innerData; |
| 1158 | innerData.wireDecode(data.getContent().blockFromValue()); |
| 1159 | Sync::IntroCertificate introCert(innerData); |
| 1160 | m_sock->addParticipant(introCert); |
| 1161 | } |
| 1162 | |
| 1163 | void |
| 1164 | ChatDialog::onIntroCertTimeout(const ndn::Interest& interest, int retry, const QString& msg) |
| 1165 | { |
| 1166 | _LOG_DEBUG("onIntroCertTimeout: " << msg.toStdString()); |
| 1167 | } |
| 1168 | |
| 1169 | |
| 1170 | |
| 1171 | #if WAF |
| 1172 | #include "chat-dialog.moc" |
| 1173 | #include "chat-dialog.cpp.moc" |
| 1174 | #endif |