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