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