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