Alexander Afanasyev | b4b9229 | 2013-07-09 13:54:59 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013, Regents of the University of California |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 4 | * Yingdi Yu |
Alexander Afanasyev | b4b9229 | 2013-07-09 13:54:59 -0700 | [diff] [blame] | 5 | * |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 6 | * BSD license, See the LICENSE file for more information |
Alexander Afanasyev | b4b9229 | 2013-07-09 13:54:59 -0700 | [diff] [blame] | 7 | * |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 8 | * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 9 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 10 | * Yingdi Yu <yingdi@cs.ucla.edu> |
Alexander Afanasyev | b4b9229 | 2013-07-09 13:54:59 -0700 | [diff] [blame] | 11 | */ |
| 12 | |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 13 | #include "chatdialog.h" |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 14 | #include "ui_chatdialog.h" |
Alexander Afanasyev | f829f4d | 2013-05-07 15:59:36 -0700 | [diff] [blame] | 15 | |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 16 | #include <QScrollBar> |
Yingdi Yu | 702d6f1 | 2013-11-07 17:00:54 -0800 | [diff] [blame] | 17 | #include <QMessageBox> |
| 18 | #include <QCloseEvent> |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 19 | |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 20 | #ifndef Q_MOC_RUN |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 21 | #include <sync-intro-certificate.h> |
Yingdi Yu | b6fb030 | 2014-01-21 11:05:11 -0800 | [diff] [blame^] | 22 | #include "chronos-invitation.h" |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 23 | #include <boost/random/random_device.hpp> |
| 24 | #include <boost/random/uniform_int_distribution.hpp> |
Yingdi Yu | b6fb030 | 2014-01-21 11:05:11 -0800 | [diff] [blame^] | 25 | #include <ndn-cpp-dev/security/signature-sha256-with-rsa.hpp> |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 26 | #include "logging.h" |
| 27 | #endif |
| 28 | |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 29 | using namespace std; |
Alexander Afanasyev | f829f4d | 2013-05-07 15:59:36 -0700 | [diff] [blame] | 30 | |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 31 | INIT_LOGGER("ChatDialog"); |
| 32 | |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 33 | static const int HELLO_INTERVAL = FRESHNESS * 3 / 4; |
| 34 | |
| 35 | Q_DECLARE_METATYPE(std::vector<Sync::MissingDataInfo> ) |
| 36 | Q_DECLARE_METATYPE(size_t) |
| 37 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 38 | ChatDialog::ChatDialog(ndn::ptr_lib::shared_ptr<ContactManager> contactManager, |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 39 | const ndn::Name& chatroomPrefix, |
| 40 | const ndn::Name& localPrefix, |
| 41 | const ndn::Name& defaultIdentity, |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 42 | const std::string& nick, |
| 43 | bool trial, |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 44 | QWidget *parent) |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 45 | : QDialog(parent) |
| 46 | , ui(new Ui::ChatDialog) |
| 47 | , m_contactManager(contactManager) |
| 48 | , m_chatroomPrefix(chatroomPrefix) |
| 49 | , m_localPrefix(localPrefix) |
| 50 | , m_defaultIdentity(defaultIdentity) |
Yingdi Yu | 93adb1a | 2014-01-16 10:30:26 -0800 | [diff] [blame] | 51 | , m_invitationPolicy(new SecPolicyChronoChatInvitation(m_chatroomPrefix.get(-1).toEscapedString(), m_defaultIdentity)) |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 52 | , m_keyChain(new ndn::KeyChain()) |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 53 | , m_nick(nick) |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 54 | , m_sock(NULL) |
| 55 | , m_lastMsgTime(0) |
| 56 | // , m_historyInitialized(false) |
| 57 | , m_joined(false) |
| 58 | , m_inviteListDialog(new InviteListDialog(m_contactManager)) |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 59 | { |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 60 | qRegisterMetaType<std::vector<Sync::MissingDataInfo> >("std::vector<Sync::MissingDataInfo>"); |
| 61 | qRegisterMetaType<size_t>("size_t"); |
| 62 | |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 63 | ui->setupUi(this); |
| 64 | |
Yingdi Yu | fdb8ab8 | 2013-11-10 01:38:21 -0800 | [diff] [blame] | 65 | QString randString = getRandomString(); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 66 | m_localChatPrefix = m_localPrefix; |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 67 | m_localChatPrefix.append("%F0.").append(m_defaultIdentity); |
Yingdi Yu | fdb8ab8 | 2013-11-10 01:38:21 -0800 | [diff] [blame] | 68 | m_localChatPrefix.append("chronos").append(m_chatroomPrefix.get(-1)).append(randString.toStdString()); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 69 | |
| 70 | m_session = time(NULL); |
| 71 | m_scene = new DigestTreeScene(this); |
| 72 | |
| 73 | initializeSetting(); |
| 74 | updateLabels(); |
| 75 | |
| 76 | ui->treeViewer->setScene(m_scene); |
| 77 | ui->treeViewer->hide(); |
| 78 | m_scene->plot("Empty"); |
| 79 | QRectF rect = m_scene->itemsBoundingRect(); |
| 80 | m_scene->setSceneRect(rect); |
| 81 | |
| 82 | m_rosterModel = new QStringListModel(this); |
| 83 | ui->listView->setModel(m_rosterModel); |
| 84 | |
Yingdi Yu | 83eae84 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 85 | createActions(); |
| 86 | createTrayIcon(); |
| 87 | |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 88 | m_timer = new QTimer(this); |
| 89 | |
Yingdi Yu | 6eabbd7 | 2013-12-27 08:44:12 +0800 | [diff] [blame] | 90 | startFace(); |
Yingdi Yu | 68de61d | 2014-01-16 19:13:03 -0800 | [diff] [blame] | 91 | m_verifier = ndn::ptr_lib::make_shared<ndn::Verifier>(m_invitationPolicy); |
| 92 | m_verifier->setFace(m_face); |
Yingdi Yu | 6eabbd7 | 2013-12-27 08:44:12 +0800 | [diff] [blame] | 93 | |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 94 | ndn::Name certificateName = m_keyChain->getDefaultCertificateNameForIdentity(m_defaultIdentity); |
Yingdi Yu | 93adb1a | 2014-01-16 10:30:26 -0800 | [diff] [blame] | 95 | m_syncPolicy = ndn::ptr_lib::make_shared<SecPolicySync>(m_defaultIdentity, certificateName, m_chatroomPrefix, m_face); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 96 | |
| 97 | connect(ui->inviteButton, SIGNAL(clicked()), |
| 98 | this, SLOT(openInviteListDialog())); |
| 99 | connect(m_inviteListDialog, SIGNAL(invitionDetermined(QString, bool)), |
| 100 | this, SLOT(sendInvitationWrapper(QString, bool))); |
| 101 | connect(ui->lineEdit, SIGNAL(returnPressed()), |
| 102 | this, SLOT(returnPressed())); |
| 103 | connect(ui->treeButton, SIGNAL(pressed()), |
| 104 | this, SLOT(treeButtonPressed())); |
| 105 | connect(this, SIGNAL(dataReceived(QString, const char *, size_t, bool, bool)), |
| 106 | this, SLOT(processData(QString, const char *, size_t, bool, bool))); |
| 107 | connect(this, SIGNAL(treeUpdated(const std::vector<Sync::MissingDataInfo>)), |
| 108 | this, SLOT(processTreeUpdate(const std::vector<Sync::MissingDataInfo>))); |
| 109 | connect(m_timer, SIGNAL(timeout()), |
| 110 | this, SLOT(replot())); |
| 111 | connect(m_scene, SIGNAL(replot()), |
| 112 | this, SLOT(replot())); |
Yingdi Yu | 83eae84 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 113 | connect(trayIcon, SIGNAL(messageClicked()), |
| 114 | this, SLOT(showNormal())); |
| 115 | connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), |
| 116 | this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason))); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 117 | connect(m_scene, SIGNAL(rosterChanged(QStringList)), |
| 118 | this, SLOT(updateRosterList(QStringList))); |
| 119 | |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 120 | |
| 121 | initializeSync(); |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 124 | |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 125 | ChatDialog::~ChatDialog() |
| 126 | { |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 127 | if(m_sock != NULL) |
| 128 | { |
| 129 | sendLeave(); |
| 130 | delete m_sock; |
| 131 | m_sock = NULL; |
| 132 | } |
Yingdi Yu | 6eabbd7 | 2013-12-27 08:44:12 +0800 | [diff] [blame] | 133 | shutdownFace(); |
| 134 | } |
| 135 | |
| 136 | void |
| 137 | ChatDialog::startFace() |
| 138 | { |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 139 | m_face = ndn::ptr_lib::make_shared<ndn::Face>(); |
Yingdi Yu | 6eabbd7 | 2013-12-27 08:44:12 +0800 | [diff] [blame] | 140 | |
| 141 | connectToDaemon(); |
| 142 | |
| 143 | m_running = true; |
| 144 | m_thread = boost::thread (&ChatDialog::eventLoop, this); |
| 145 | } |
| 146 | |
| 147 | void |
| 148 | ChatDialog::shutdownFace() |
| 149 | { |
| 150 | { |
| 151 | boost::unique_lock<boost::recursive_mutex> lock(m_mutex); |
| 152 | m_running = false; |
| 153 | } |
| 154 | |
| 155 | m_thread.join(); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 156 | m_face->shutdown(); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Zhenkai Zhu | 86df741 | 2012-09-27 16:30:20 -0700 | [diff] [blame] | 159 | void |
Yingdi Yu | 6eabbd7 | 2013-12-27 08:44:12 +0800 | [diff] [blame] | 160 | ChatDialog::eventLoop() |
| 161 | { |
| 162 | while (m_running) |
| 163 | { |
| 164 | try{ |
| 165 | m_face->processEvents(); |
| 166 | usleep(1000); |
| 167 | }catch(std::exception& e){ |
| 168 | _LOG_DEBUG(" " << e.what() ); |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | void |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 174 | ChatDialog::connectToDaemon() |
Zhenkai Zhu | cf02444 | 2012-10-05 10:33:08 -0700 | [diff] [blame] | 175 | { |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 176 | //Hack! transport does not connect to daemon unless an interest is expressed. |
| 177 | ndn::Name name("/ndn"); |
| 178 | ndn::ptr_lib::shared_ptr<ndn::Interest> interest = ndn::ptr_lib::make_shared<ndn::Interest>(name); |
| 179 | m_face->expressInterest(*interest, |
| 180 | boost::bind(&ChatDialog::onConnectionData, this, _1, _2), |
| 181 | boost::bind(&ChatDialog::onConnectionDataTimeout, this, _1)); |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 182 | } |
Alexander Afanasyev | b4b9229 | 2013-07-09 13:54:59 -0700 | [diff] [blame] | 183 | |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 184 | void |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 185 | ChatDialog::onConnectionData(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest, |
| 186 | const ndn::ptr_lib::shared_ptr<ndn::Data>& data) |
| 187 | { _LOG_DEBUG("onConnectionData"); } |
| 188 | |
| 189 | void |
| 190 | ChatDialog::onConnectionDataTimeout(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest) |
| 191 | { _LOG_DEBUG("onConnectionDataTimeout"); } |
| 192 | |
| 193 | void |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 194 | ChatDialog::initializeSetting() |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 195 | { |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 196 | m_user.setNick(QString::fromStdString(m_nick)); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 197 | m_user.setChatroom(QString::fromStdString(m_chatroomPrefix.get(-1).toEscapedString())); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 198 | m_user.setOriginPrefix(QString::fromStdString(m_localPrefix.toUri())); |
| 199 | m_user.setPrefix(QString::fromStdString(m_localChatPrefix.toUri())); |
| 200 | m_scene->setCurrentPrefix(QString::fromStdString(m_localChatPrefix.toUri())); |
| 201 | } |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 202 | |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 203 | void |
| 204 | ChatDialog::updateLabels() |
| 205 | { |
| 206 | QString settingDisp = QString("Chatroom: %1").arg(m_user.getChatroom()); |
| 207 | ui->infoLabel->setStyleSheet("QLabel {color: #630; font-size: 16px; font: bold \"Verdana\";}"); |
| 208 | ui->infoLabel->setText(settingDisp); |
| 209 | QString prefixDisp; |
| 210 | if (m_user.getPrefix().startsWith("/private/local")) |
| 211 | { |
| 212 | prefixDisp = QString("<Warning: no connection to hub or hub does not support prefix autoconfig.>\n <Prefix = %1>").arg(m_user.getPrefix()); |
| 213 | ui->prefixLabel->setStyleSheet("QLabel {color: red; font-size: 12px; font: bold \"Verdana\";}"); |
| 214 | } |
| 215 | else |
| 216 | { |
| 217 | prefixDisp = QString("<Prefix = %1>").arg(m_user.getPrefix()); |
| 218 | ui->prefixLabel->setStyleSheet("QLabel {color: Green; font-size: 12px; font: bold \"Verdana\";}"); |
| 219 | } |
| 220 | ui->prefixLabel->setText(prefixDisp); |
| 221 | } |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 222 | |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 223 | void |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 224 | ChatDialog::sendInterest(const ndn::Interest& interest, |
| 225 | const ndn::OnVerified& onVerified, |
| 226 | const ndn::OnVerifyFailed& onVerifyFailed, |
| 227 | const OnEventualTimeout& timeoutNotify, |
Yingdi Yu | 68de61d | 2014-01-16 19:13:03 -0800 | [diff] [blame] | 228 | int retry /* = 1 */) |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 229 | { |
| 230 | m_face->expressInterest(interest, |
| 231 | boost::bind(&ChatDialog::onTargetData, |
| 232 | this, |
| 233 | _1, |
| 234 | _2, |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 235 | onVerified, |
Yingdi Yu | 68de61d | 2014-01-16 19:13:03 -0800 | [diff] [blame] | 236 | onVerifyFailed), |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 237 | boost::bind(&ChatDialog::onTargetTimeout, |
| 238 | this, |
| 239 | _1, |
| 240 | retry, |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 241 | onVerified, |
| 242 | onVerifyFailed, |
Yingdi Yu | 68de61d | 2014-01-16 19:13:03 -0800 | [diff] [blame] | 243 | timeoutNotify)); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | void |
| 247 | ChatDialog::onTargetData(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest, |
| 248 | const ndn::ptr_lib::shared_ptr<ndn::Data>& data, |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 249 | const ndn::OnVerified& onVerified, |
Yingdi Yu | 68de61d | 2014-01-16 19:13:03 -0800 | [diff] [blame] | 250 | const ndn::OnVerifyFailed& onVerifyFailed) |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 251 | { |
Yingdi Yu | 68de61d | 2014-01-16 19:13:03 -0800 | [diff] [blame] | 252 | m_verifier->verifyData(data, onVerified, onVerifyFailed); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | void |
| 256 | ChatDialog::onTargetTimeout(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest, |
| 257 | int retry, |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 258 | const ndn::OnVerified& onVerified, |
| 259 | const ndn::OnVerifyFailed& onVerifyFailed, |
Yingdi Yu | 68de61d | 2014-01-16 19:13:03 -0800 | [diff] [blame] | 260 | const OnEventualTimeout& timeoutNotify) |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 261 | { |
| 262 | if(retry > 0) |
Yingdi Yu | 68de61d | 2014-01-16 19:13:03 -0800 | [diff] [blame] | 263 | sendInterest(*interest, onVerified, onVerifyFailed, timeoutNotify, retry-1); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 264 | else |
| 265 | { |
| 266 | _LOG_DEBUG("Interest: " << interest->getName().toUri() << " eventually times out!"); |
| 267 | timeoutNotify(); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | void |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 272 | ChatDialog::sendInvitation(ndn::ptr_lib::shared_ptr<ContactItem> contact, bool isIntroducer) |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 273 | { |
Yingdi Yu | 93adb1a | 2014-01-16 10:30:26 -0800 | [diff] [blame] | 274 | m_invitationPolicy->addTrustAnchor(contact->getSelfEndorseCertificate()); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 275 | |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 276 | ndn::Name certificateName = m_keyChain->getDefaultCertificateNameForIdentity(m_defaultIdentity); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 277 | |
Yingdi Yu | b6fb030 | 2014-01-21 11:05:11 -0800 | [diff] [blame^] | 278 | ChronosInvitation invitation(contact->getNameSpace(), |
| 279 | m_chatroomPrefix.getSubName(m_chatroomPrefix.size()-1, 1), //!!Should be changed! |
| 280 | m_localPrefix, |
| 281 | certificateName); |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 282 | |
Yingdi Yu | b6fb030 | 2014-01-21 11:05:11 -0800 | [diff] [blame^] | 283 | const ndn::Buffer &signedBlob = invitation.getSignedBlob(); |
| 284 | ndn::Signature sig = m_keyChain->sign(signedBlob.buf(), signedBlob.size(), certificateName); |
| 285 | invitation.setSignatureValue(sig.getValue()); |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 286 | |
Yingdi Yu | b6fb030 | 2014-01-21 11:05:11 -0800 | [diff] [blame^] | 287 | ndn::Interest interest(invitation.getInterestName()); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 288 | ndn::OnVerified onVerified = boost::bind(&ChatDialog::onInviteReplyVerified, |
| 289 | this, |
| 290 | _1, |
| 291 | contact->getNameSpace(), |
| 292 | isIntroducer); |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 293 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 294 | ndn::OnVerifyFailed onVerifyFailed = boost::bind(&ChatDialog::onInviteReplyVerifyFailed, |
| 295 | this, |
| 296 | _1, |
| 297 | contact->getNameSpace()); |
| 298 | |
| 299 | OnEventualTimeout timeoutNotify = boost::bind(&ChatDialog::onInviteReplyTimeout, |
| 300 | this, |
| 301 | contact->getNameSpace()); |
| 302 | |
| 303 | |
Yingdi Yu | 68de61d | 2014-01-16 19:13:03 -0800 | [diff] [blame] | 304 | sendInterest(interest, onVerified, onVerifyFailed, timeoutNotify); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | void |
| 308 | ChatDialog::onInviteReplyVerified(const ndn::ptr_lib::shared_ptr<ndn::Data>& data, |
| 309 | const ndn::Name& identity, |
| 310 | bool isIntroducer) |
| 311 | { |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 312 | string content(reinterpret_cast<const char*>(data->getContent().value()), data->getContent().value_size()); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 313 | if(content == string("nack")) |
| 314 | invitationRejected(identity); |
| 315 | else |
| 316 | invitationAccepted(identity, data, content, isIntroducer); |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | void |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 320 | ChatDialog::onInviteReplyVerifyFailed(const ndn::ptr_lib::shared_ptr<ndn::Data>& data, |
| 321 | const ndn::Name& identity) |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 322 | { |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 323 | _LOG_DEBUG("Reply from " << identity.toUri() << " cannot be verified!"); |
| 324 | QString msg = QString::fromUtf8("Reply from ") + QString::fromStdString(identity.toUri()) + " cannot be verified!"; |
| 325 | emit inivationRejection(msg); |
| 326 | } |
| 327 | |
| 328 | |
| 329 | void |
| 330 | ChatDialog::onInviteReplyTimeout(const ndn::Name& identity) |
| 331 | { |
| 332 | _LOG_DEBUG("Your invitation to " << identity.toUri() << " times out!"); |
| 333 | QString msg = QString::fromUtf8("Your invitation to ") + QString::fromStdString(identity.toUri()) + " times out!"; |
| 334 | emit inivationRejection(msg); |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | void |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 338 | ChatDialog::invitationRejected(const ndn::Name& identity) |
| 339 | { |
Yingdi Yu | 72781e5 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 340 | _LOG_DEBUG(" " << identity.toUri() << " Rejected your invitation!"); |
Yingdi Yu | 0aca265 | 2013-11-10 10:47:44 -0800 | [diff] [blame] | 341 | QString msg = QString::fromStdString(identity.toUri()) + " Rejected your invitation!"; |
| 342 | emit inivationRejection(msg); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | void |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 346 | ChatDialog::invitationAccepted(const ndn::Name& identity, ndn::ptr_lib::shared_ptr<ndn::Data> data, const string& inviteePrefix, bool isIntroducer) |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 347 | { |
Yingdi Yu | 72781e5 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 348 | _LOG_DEBUG(" " << identity.toUri() << " Accepted your invitation!"); |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 349 | ndn::SignatureSha256WithRsa sig(data->getSignature()); |
| 350 | const ndn::Name & keyLocatorName = sig.getKeyLocator().getName(); |
Yingdi Yu | 93adb1a | 2014-01-16 10:30:26 -0800 | [diff] [blame] | 351 | ndn::ptr_lib::shared_ptr<ndn::IdentityCertificate> dskCertificate = m_invitationPolicy->getValidatedDskCertificate(keyLocatorName); |
Yingdi Yu | b6fb030 | 2014-01-21 11:05:11 -0800 | [diff] [blame^] | 352 | m_syncPolicy->addSyncDataRule(inviteePrefix, *dskCertificate, isIntroducer); |
Yingdi Yu | 83eae84 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 353 | publishIntroCert(*dskCertificate, isIntroducer); |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 356 | void |
| 357 | ChatDialog::publishIntroCert(const ndn::IdentityCertificate& dskCertificate, bool isIntroducer) |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 358 | { |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 359 | SyncIntroCertificate syncIntroCertificate(m_chatroomPrefix, |
| 360 | dskCertificate.getPublicKeyName(), |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 361 | m_keyChain->getDefaultKeyNameForIdentity(m_defaultIdentity), |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 362 | dskCertificate.getNotBefore(), |
| 363 | dskCertificate.getNotAfter(), |
| 364 | dskCertificate.getPublicKeyInfo(), |
| 365 | (isIntroducer ? SyncIntroCertificate::INTRODUCER : SyncIntroCertificate::PRODUCER)); |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 366 | ndn::Name certName = m_keyChain->getDefaultCertificateNameForIdentity(m_defaultIdentity); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 367 | _LOG_DEBUG("Publish Intro Certificate: " << syncIntroCertificate.getName()); |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 368 | m_keyChain->sign(syncIntroCertificate, certName); |
| 369 | m_face->put(syncIntroCertificate); |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 372 | void |
| 373 | ChatDialog::addTrustAnchor(const EndorseCertificate& selfEndorseCertificate) |
Yingdi Yu | 93adb1a | 2014-01-16 10:30:26 -0800 | [diff] [blame] | 374 | { m_invitationPolicy->addTrustAnchor(selfEndorseCertificate); } |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 375 | |
| 376 | void |
| 377 | ChatDialog::addChatDataRule(const ndn::Name& prefix, |
| 378 | const ndn::IdentityCertificate& identityCertificate, |
| 379 | bool isIntroducer) |
Yingdi Yu | b6fb030 | 2014-01-21 11:05:11 -0800 | [diff] [blame^] | 380 | { m_syncPolicy->addSyncDataRule(prefix, identityCertificate, isIntroducer); } |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 381 | |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 382 | |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 383 | |
| 384 | void |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 385 | ChatDialog::initializeSync() |
| 386 | { |
| 387 | |
| 388 | m_sock = new Sync::SyncSocket(m_chatroomPrefix.toUri(), |
Yingdi Yu | 93adb1a | 2014-01-16 10:30:26 -0800 | [diff] [blame] | 389 | m_syncPolicy, |
Yingdi Yu | 6eabbd7 | 2013-12-27 08:44:12 +0800 | [diff] [blame] | 390 | m_face, |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 391 | boost::bind(&ChatDialog::processTreeUpdateWrapper, this, _1, _2), |
| 392 | boost::bind(&ChatDialog::processRemoveWrapper, this, _1)); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 393 | |
| 394 | usleep(100000); |
| 395 | |
| 396 | QTimer::singleShot(600, this, SLOT(sendJoin())); |
| 397 | m_timer->start(FRESHNESS * 1000); |
| 398 | disableTreeDisplay(); |
| 399 | QTimer::singleShot(2200, this, SLOT(enableTreeDisplay())); |
| 400 | // Sync::CcnxWrapperPtr handle = boost::make_shared<Sync::CcnxWrapper> (); |
| 401 | // handle->setInterestFilter(m_user.getPrefix().toStdString(), bind(&ChatDialog::respondHistoryRequest, this, _1)); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | void |
| 405 | ChatDialog::returnPressed() |
| 406 | { |
| 407 | QString text = ui->lineEdit->text(); |
| 408 | if (text.isEmpty()) |
| 409 | return; |
| 410 | |
| 411 | ui->lineEdit->clear(); |
| 412 | |
| 413 | if (text.startsWith("boruoboluomi")) |
| 414 | { |
| 415 | summonReaper (); |
| 416 | // reapButton->show(); |
| 417 | fitView(); |
| 418 | return; |
| 419 | } |
| 420 | |
| 421 | if (text.startsWith("minimanihong")) |
| 422 | { |
| 423 | // reapButton->hide(); |
| 424 | fitView(); |
| 425 | return; |
| 426 | } |
| 427 | |
| 428 | SyncDemo::ChatMessage msg; |
| 429 | formChatMessage(text, msg); |
| 430 | |
| 431 | appendMessage(msg); |
| 432 | |
| 433 | sendMsg(msg); |
| 434 | |
| 435 | fitView(); |
| 436 | } |
| 437 | |
| 438 | void |
| 439 | ChatDialog::treeButtonPressed() |
| 440 | { |
| 441 | if (ui->treeViewer->isVisible()) |
| 442 | { |
| 443 | ui->treeViewer->hide(); |
| 444 | ui->treeButton->setText("Show ChronoSync Tree"); |
| 445 | } |
| 446 | else |
| 447 | { |
| 448 | ui->treeViewer->show(); |
| 449 | ui->treeButton->setText("Hide ChronoSync Tree"); |
| 450 | } |
| 451 | |
| 452 | fitView(); |
| 453 | } |
| 454 | |
| 455 | void ChatDialog::disableTreeDisplay() |
| 456 | { |
| 457 | ui->treeButton->setEnabled(false); |
| 458 | ui->treeViewer->hide(); |
| 459 | fitView(); |
| 460 | } |
| 461 | |
| 462 | void ChatDialog::enableTreeDisplay() |
| 463 | { |
| 464 | ui->treeButton->setEnabled(true); |
| 465 | // treeViewer->show(); |
| 466 | // fitView(); |
| 467 | } |
| 468 | |
| 469 | void |
| 470 | ChatDialog::processTreeUpdateWrapper(const std::vector<Sync::MissingDataInfo> v, Sync::SyncSocket *sock) |
| 471 | { |
| 472 | emit treeUpdated(v); |
| 473 | _LOG_DEBUG("<<< Tree update signal emitted"); |
| 474 | } |
| 475 | |
| 476 | void |
| 477 | ChatDialog::processRemoveWrapper(std::string prefix) |
| 478 | { |
| 479 | _LOG_DEBUG("Sync REMOVE signal received for prefix: " << prefix); |
| 480 | } |
| 481 | |
| 482 | void |
| 483 | ChatDialog::processTreeUpdate(const std::vector<Sync::MissingDataInfo> v) |
| 484 | { |
| 485 | _LOG_DEBUG("<<< processing Tree Update"); |
| 486 | |
| 487 | if (v.empty()) |
| 488 | { |
| 489 | return; |
| 490 | } |
| 491 | |
| 492 | // reflect the changes on digest tree |
| 493 | { |
| 494 | boost::recursive_mutex::scoped_lock lock(m_sceneMutex); |
| 495 | m_scene->processUpdate(v, m_sock->getRootDigest().c_str()); |
| 496 | } |
| 497 | |
| 498 | int n = v.size(); |
| 499 | int totalMissingPackets = 0; |
| 500 | for (int i = 0; i < n; i++) |
| 501 | { |
| 502 | totalMissingPackets += v[i].high.getSeq() - v[i].low.getSeq() + 1; |
| 503 | } |
| 504 | |
| 505 | for (int i = 0; i < n; i++) |
| 506 | { |
| 507 | if (totalMissingPackets < 4) |
| 508 | { |
| 509 | for (Sync::SeqNo seq = v[i].low; seq <= v[i].high; ++seq) |
| 510 | { |
| 511 | m_sock->fetchData(v[i].prefix, seq, bind(&ChatDialog::processDataWrapper, this, _1), 2); |
| 512 | _LOG_DEBUG("<<< Fetching " << v[i].prefix << "/" <<seq.getSession() <<"/" << seq.getSeq()); |
| 513 | } |
| 514 | } |
| 515 | else |
| 516 | { |
| 517 | m_sock->fetchData(v[i].prefix, v[i].high, bind(&ChatDialog::processDataNoShowWrapper, this, _1), 2); |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | // adjust the view |
| 522 | fitView(); |
| 523 | |
| 524 | } |
| 525 | |
| 526 | void |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 527 | ChatDialog::processDataWrapper(const ndn::ptr_lib::shared_ptr<ndn::Data>& data) |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 528 | { |
| 529 | string name = data->getName().toUri(); |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 530 | const char* buf = reinterpret_cast<const char*>(data->getContent().value()); |
| 531 | size_t len = data->getContent().value_size(); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 532 | |
| 533 | char *tempBuf = new char[len]; |
| 534 | memcpy(tempBuf, buf, len); |
| 535 | emit dataReceived(name.c_str(), tempBuf, len, true, false); |
| 536 | _LOG_DEBUG("<<< " << name << " fetched"); |
| 537 | } |
| 538 | |
| 539 | void |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 540 | ChatDialog::processDataNoShowWrapper(const ndn::ptr_lib::shared_ptr<ndn::Data>& data) |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 541 | { |
| 542 | string name = data->getName().toUri(); |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 543 | const char* buf = reinterpret_cast<const char*>(data->getContent().value()); |
| 544 | size_t len = data->getContent().value_size(); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 545 | |
| 546 | char *tempBuf = new char[len]; |
| 547 | memcpy(tempBuf, buf, len); |
| 548 | emit dataReceived(name.c_str(), tempBuf, len, false, false); |
| 549 | |
| 550 | // if (!m_historyInitialized) |
| 551 | // { |
| 552 | // fetchHistory(name); |
| 553 | // m_historyInitialized = true; |
| 554 | // } |
| 555 | } |
| 556 | |
| 557 | // void |
| 558 | // ChatDialog::fetchHistory(std::string name) |
| 559 | // { |
| 560 | |
| 561 | // /****************************/ |
| 562 | // /* TODO: fix following part */ |
| 563 | // /****************************/ |
| 564 | // string nameWithoutSeq = name.substr(0, name.find_last_of('/')); |
| 565 | // string prefix = nameWithoutSeq.substr(0, nameWithoutSeq.find_last_of('/')); |
| 566 | // prefix += "/history"; |
| 567 | // // Ptr<Wrapper>CcnxWrapperPtr handle = boost::make_shared<Sync::CcnxWrapper> (); |
| 568 | // // QString randomString = getRandomString(); |
| 569 | // // for (int i = 0; i < MAX_HISTORY_ENTRY; i++) |
| 570 | // // { |
| 571 | // // QString interest = QString("%1/%2/%3").arg(prefix.c_str()).arg(randomString).arg(i); |
| 572 | // // handle->sendInterest(interest.toStdString(), bind(&ChatDialog::processDataHistoryWrapper, this, _1, _2, _3)); |
| 573 | // // } |
| 574 | // } |
| 575 | |
| 576 | void |
| 577 | ChatDialog::processData(QString name, const char *buf, size_t len, bool show, bool isHistory) |
| 578 | { |
| 579 | SyncDemo::ChatMessage msg; |
| 580 | bool corrupted = false; |
| 581 | if (!msg.ParseFromArray(buf, len)) |
| 582 | { |
| 583 | _LOG_DEBUG("Errrrr.. Can not parse msg with name: " << name.toStdString() << ". what is happening?"); |
| 584 | // nasty stuff: as a remedy, we'll form some standard msg for inparsable msgs |
| 585 | msg.set_from("inconnu"); |
| 586 | msg.set_type(SyncDemo::ChatMessage::OTHER); |
| 587 | corrupted = true; |
| 588 | } |
| 589 | |
| 590 | delete [] buf; |
| 591 | buf = NULL; |
| 592 | |
| 593 | // display msg received from network |
| 594 | // we have to do so; this function is called by ccnd thread |
| 595 | // so if we call appendMsg directly |
| 596 | // Qt crash as "QObject: Cannot create children for a parent that is in a different thread" |
| 597 | // the "cannonical" way to is use signal-slot |
| 598 | if (show && !corrupted) |
| 599 | { |
| 600 | appendMessage(msg, isHistory); |
| 601 | } |
| 602 | |
| 603 | if (!isHistory) |
| 604 | { |
| 605 | // update the tree view |
| 606 | std::string stdStrName = name.toStdString(); |
| 607 | std::string stdStrNameWithoutSeq = stdStrName.substr(0, stdStrName.find_last_of('/')); |
| 608 | std::string prefix = stdStrNameWithoutSeq.substr(0, stdStrNameWithoutSeq.find_last_of('/')); |
| 609 | _LOG_DEBUG("<<< updating scene for" << prefix << ": " << msg.from()); |
| 610 | if (msg.type() == SyncDemo::ChatMessage::LEAVE) |
| 611 | { |
| 612 | processRemove(prefix.c_str()); |
| 613 | } |
| 614 | else |
| 615 | { |
| 616 | boost::recursive_mutex::scoped_lock lock(m_sceneMutex); |
| 617 | m_scene->msgReceived(prefix.c_str(), msg.from().c_str()); |
| 618 | } |
| 619 | } |
| 620 | fitView(); |
| 621 | } |
| 622 | |
| 623 | void |
| 624 | ChatDialog::processRemove(QString prefix) |
| 625 | { |
| 626 | _LOG_DEBUG("<<< remove node for prefix" << prefix.toStdString()); |
| 627 | |
| 628 | bool removed = m_scene->removeNode(prefix); |
| 629 | if (removed) |
| 630 | { |
| 631 | boost::recursive_mutex::scoped_lock lock(m_sceneMutex); |
| 632 | m_scene->plot(m_sock->getRootDigest().c_str()); |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | void |
| 637 | ChatDialog::sendJoin() |
| 638 | { |
| 639 | m_joined = true; |
| 640 | SyncDemo::ChatMessage msg; |
| 641 | formControlMessage(msg, SyncDemo::ChatMessage::JOIN); |
| 642 | sendMsg(msg); |
| 643 | boost::random::random_device rng; |
| 644 | boost::random::uniform_int_distribution<> uniform(1, FRESHNESS / 5 * 1000); |
| 645 | m_randomizedInterval = HELLO_INTERVAL * 1000 + uniform(rng); |
| 646 | QTimer::singleShot(m_randomizedInterval, this, SLOT(sendHello())); |
| 647 | } |
| 648 | |
| 649 | void |
| 650 | ChatDialog::sendHello() |
| 651 | { |
| 652 | time_t now = time(NULL); |
| 653 | int elapsed = now - m_lastMsgTime; |
| 654 | if (elapsed >= m_randomizedInterval / 1000) |
| 655 | { |
| 656 | SyncDemo::ChatMessage msg; |
| 657 | formControlMessage(msg, SyncDemo::ChatMessage::HELLO); |
| 658 | sendMsg(msg); |
| 659 | boost::random::random_device rng; |
| 660 | boost::random::uniform_int_distribution<> uniform(1, FRESHNESS / 5 * 1000); |
| 661 | m_randomizedInterval = HELLO_INTERVAL * 1000 + uniform(rng); |
| 662 | QTimer::singleShot(m_randomizedInterval, this, SLOT(sendHello())); |
| 663 | } |
| 664 | else |
| 665 | { |
| 666 | QTimer::singleShot((m_randomizedInterval - elapsed * 1000), this, SLOT(sendHello())); |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | void |
| 671 | ChatDialog::sendLeave() |
| 672 | { |
| 673 | SyncDemo::ChatMessage msg; |
| 674 | formControlMessage(msg, SyncDemo::ChatMessage::LEAVE); |
| 675 | sendMsg(msg); |
| 676 | usleep(500000); |
| 677 | m_sock->remove(m_user.getPrefix().toStdString()); |
| 678 | usleep(5000); |
| 679 | m_joined = false; |
| 680 | _LOG_DEBUG("Sync REMOVE signal sent"); |
| 681 | } |
| 682 | |
| 683 | void |
| 684 | ChatDialog::replot() |
| 685 | { |
| 686 | boost::recursive_mutex::scoped_lock lock(m_sceneMutex); |
| 687 | m_scene->plot(m_sock->getRootDigest().c_str()); |
| 688 | fitView(); |
| 689 | } |
| 690 | |
| 691 | void |
| 692 | ChatDialog::summonReaper() |
| 693 | { |
| 694 | Sync::SyncLogic &logic = m_sock->getLogic (); |
| 695 | map<string, bool> branches = logic.getBranchPrefixes(); |
| 696 | QMap<QString, DisplayUserPtr> roster = m_scene->getRosterFull(); |
| 697 | |
| 698 | m_zombieList.clear(); |
| 699 | |
| 700 | QMapIterator<QString, DisplayUserPtr> it(roster); |
| 701 | map<string, bool>::iterator mapIt; |
| 702 | while(it.hasNext()) |
| 703 | { |
| 704 | it.next(); |
| 705 | DisplayUserPtr p = it.value(); |
| 706 | if (p != DisplayUserNullPtr) |
| 707 | { |
| 708 | mapIt = branches.find(p->getPrefix().toStdString()); |
| 709 | if (mapIt != branches.end()) |
| 710 | { |
| 711 | mapIt->second = true; |
| 712 | } |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | for (mapIt = branches.begin(); mapIt != branches.end(); ++mapIt) |
| 717 | { |
| 718 | // this is zombie. all active users should have been marked true |
| 719 | if (! mapIt->second) |
| 720 | { |
| 721 | m_zombieList.append(mapIt->first.c_str()); |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | m_zombieIndex = 0; |
| 726 | |
| 727 | // start reaping |
| 728 | reap(); |
| 729 | } |
| 730 | |
| 731 | void |
| 732 | ChatDialog::reap() |
| 733 | { |
| 734 | if (m_zombieIndex < m_zombieList.size()) |
| 735 | { |
| 736 | string prefix = m_zombieList.at(m_zombieIndex).toStdString(); |
| 737 | m_sock->remove(prefix); |
| 738 | _LOG_DEBUG("Reaped: prefix = " << prefix); |
| 739 | m_zombieIndex++; |
| 740 | // reap again in 10 seconds |
| 741 | QTimer::singleShot(10000, this, SLOT(reap())); |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | void |
| 746 | ChatDialog::updateRosterList(QStringList staleUserList) |
| 747 | { |
| 748 | boost::recursive_mutex::scoped_lock lock(m_sceneMutex); |
| 749 | QStringList rosterList = m_scene->getRosterList(); |
| 750 | m_rosterModel->setStringList(rosterList); |
| 751 | QString user; |
| 752 | QStringListIterator it(staleUserList); |
| 753 | while(it.hasNext()) |
| 754 | { |
| 755 | std::string nick = it.next().toStdString(); |
| 756 | if (nick.empty()) |
| 757 | continue; |
| 758 | |
| 759 | SyncDemo::ChatMessage msg; |
| 760 | formControlMessage(msg, SyncDemo::ChatMessage::LEAVE); |
| 761 | msg.set_from(nick); |
| 762 | appendMessage(msg); |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | void |
Yingdi Yu | fdb8ab8 | 2013-11-10 01:38:21 -0800 | [diff] [blame] | 767 | ChatDialog::settingUpdated(QString nick, QString chatroom, QString originPrefix) |
| 768 | { |
Yingdi Yu | 1ecf81f | 2013-11-10 12:25:43 -0800 | [diff] [blame] | 769 | _LOG_DEBUG("called"); |
Yingdi Yu | fdb8ab8 | 2013-11-10 01:38:21 -0800 | [diff] [blame] | 770 | QString randString = getRandomString(); |
| 771 | bool needWrite = false; |
| 772 | bool needFresh = false; |
| 773 | |
| 774 | QString oldPrefix = m_user.getPrefix(); |
| 775 | if (!originPrefix.isEmpty() && originPrefix != m_user.getOriginPrefix()) { |
| 776 | m_user.setOriginPrefix(originPrefix); |
| 777 | |
| 778 | m_localPrefix = ndn::Name(originPrefix.toStdString()); |
| 779 | m_localChatPrefix = m_localPrefix; |
| 780 | m_localChatPrefix.append("%F0.").append(m_defaultIdentity); |
| 781 | m_localChatPrefix.append("chronos").append(m_chatroomPrefix.get(-1)).append(randString.toStdString()); |
| 782 | m_user.setPrefix(QString::fromStdString(m_localChatPrefix.toUri())); |
| 783 | m_scene->setCurrentPrefix(QString::fromStdString(m_localChatPrefix.toUri())); |
| 784 | needWrite = true; |
| 785 | needFresh = true; |
| 786 | } |
| 787 | |
| 788 | if (needWrite) { |
| 789 | updateLabels(); |
| 790 | } |
| 791 | |
| 792 | if (needFresh && m_sock != NULL) |
| 793 | { |
| 794 | |
| 795 | { |
| 796 | boost::recursive_mutex::scoped_lock lock(m_sceneMutex); |
| 797 | m_scene->clearAll(); |
| 798 | m_scene->plot("Empty"); |
| 799 | } |
| 800 | |
Yingdi Yu | a66183f | 2013-11-10 10:32:18 -0800 | [diff] [blame] | 801 | ui->textEdit->clear(); |
Yingdi Yu | fdb8ab8 | 2013-11-10 01:38:21 -0800 | [diff] [blame] | 802 | |
| 803 | // keep the new prefix |
| 804 | QString newPrefix = m_user.getPrefix(); |
| 805 | // send leave for the old |
| 806 | m_user.setPrefix(oldPrefix); |
| 807 | // there is no point to send leave if we haven't joined yet |
| 808 | if (m_joined) |
| 809 | { |
| 810 | sendLeave(); |
| 811 | } |
| 812 | // resume new prefix |
| 813 | m_user.setPrefix(newPrefix); |
| 814 | // Sync::CcnxWrapperPtr handle = Sync::CcnxWrapper::Create(); |
| 815 | // handle->clearInterestFilter(oldPrefix.toStdString()); |
| 816 | delete m_sock; |
| 817 | m_sock = NULL; |
| 818 | |
| 819 | try |
| 820 | { |
| 821 | usleep(100000); |
| 822 | m_sock = new Sync::SyncSocket(m_chatroomPrefix.toUri(), |
Yingdi Yu | 93adb1a | 2014-01-16 10:30:26 -0800 | [diff] [blame] | 823 | m_syncPolicy, |
Yingdi Yu | 6eabbd7 | 2013-12-27 08:44:12 +0800 | [diff] [blame] | 824 | m_face, |
Yingdi Yu | fdb8ab8 | 2013-11-10 01:38:21 -0800 | [diff] [blame] | 825 | bind(&ChatDialog::processTreeUpdateWrapper, this, _1, _2), |
| 826 | bind(&ChatDialog::processRemoveWrapper, this, _1)); |
| 827 | usleep(100000); |
| 828 | // Sync::CcnxWrapperPtr handle = boost::make_shared<Sync::CcnxWrapper> (); |
| 829 | // handle->setInterestFilter(m_user.getPrefix().toStdString(), bind(&ChatDialog::respondHistoryRequest, this, _1)); |
Yingdi Yu | a66183f | 2013-11-10 10:32:18 -0800 | [diff] [blame] | 830 | QTimer::singleShot(600, this, SLOT(sendJoin())); |
Yingdi Yu | fdb8ab8 | 2013-11-10 01:38:21 -0800 | [diff] [blame] | 831 | m_timer->start(FRESHNESS * 1000); |
| 832 | disableTreeDisplay(); |
Yingdi Yu | a66183f | 2013-11-10 10:32:18 -0800 | [diff] [blame] | 833 | QTimer::singleShot(2200, this, SLOT(enableTreeDisplay())); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 834 | }catch(std::exception& e){ |
Yingdi Yu | fdb8ab8 | 2013-11-10 01:38:21 -0800 | [diff] [blame] | 835 | emit noNdnConnection(QString::fromStdString("Cannot conect to ndnd!\n Have you started your ndnd?")); |
| 836 | } |
| 837 | } |
| 838 | else if (needFresh && m_sock == NULL) |
| 839 | { |
| 840 | initializeSync(); |
| 841 | } |
| 842 | else if (m_sock == NULL) |
| 843 | { |
| 844 | initializeSync(); |
| 845 | } |
| 846 | else |
| 847 | { |
| 848 | // #ifdef __DEBUG |
| 849 | // std::cout << "Just changing nicks, we're good. " << std::endl; |
| 850 | // #endif |
| 851 | } |
| 852 | |
| 853 | fitView(); |
| 854 | } |
| 855 | |
| 856 | void |
Yingdi Yu | 83eae84 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 857 | ChatDialog::iconActivated(QSystemTrayIcon::ActivationReason reason) |
| 858 | { |
| 859 | switch (reason) |
| 860 | { |
| 861 | case QSystemTrayIcon::Trigger: |
| 862 | case QSystemTrayIcon::DoubleClick: |
| 863 | break; |
| 864 | case QSystemTrayIcon::MiddleClick: |
| 865 | // showMessage(); |
| 866 | break; |
| 867 | default:; |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | |
| 872 | void |
| 873 | ChatDialog::messageClicked() |
| 874 | { |
| 875 | this->showMaximized(); |
| 876 | } |
| 877 | |
| 878 | |
| 879 | void |
| 880 | ChatDialog::createActions() |
| 881 | { |
Yingdi Yu | 702d6f1 | 2013-11-07 17:00:54 -0800 | [diff] [blame] | 882 | minimizeAction = new QAction(tr("Mi&nimize"), this); |
| 883 | connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide())); |
Yingdi Yu | 83eae84 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 884 | |
Yingdi Yu | 702d6f1 | 2013-11-07 17:00:54 -0800 | [diff] [blame] | 885 | maximizeAction = new QAction(tr("Ma&ximize"), this); |
| 886 | connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized())); |
Yingdi Yu | 83eae84 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 887 | |
Yingdi Yu | 702d6f1 | 2013-11-07 17:00:54 -0800 | [diff] [blame] | 888 | restoreAction = new QAction(tr("&Restore"), this); |
| 889 | connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal())); |
Yingdi Yu | 83eae84 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 890 | |
| 891 | // settingsAction = new QAction(tr("Settings"), this); |
| 892 | // connect (settingsAction, SIGNAL(triggered()), this, SLOT(buttonPressed())); |
| 893 | |
| 894 | // settingsAction->setMenuRole (QAction::PreferencesRole); |
| 895 | |
Yingdi Yu | fdb8ab8 | 2013-11-10 01:38:21 -0800 | [diff] [blame] | 896 | updateLocalPrefixAction = new QAction(tr("Update local prefix"), this); |
| 897 | connect (updateLocalPrefixAction, SIGNAL(triggered()), this, SLOT(updateLocalPrefix())); |
Yingdi Yu | 83eae84 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 898 | |
Yingdi Yu | 702d6f1 | 2013-11-07 17:00:54 -0800 | [diff] [blame] | 899 | quitAction = new QAction(tr("Quit"), this); |
| 900 | connect(quitAction, SIGNAL(triggered()), this, SLOT(quit())); |
Yingdi Yu | 83eae84 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | void |
| 904 | ChatDialog::createTrayIcon() |
| 905 | { |
Yingdi Yu | 702d6f1 | 2013-11-07 17:00:54 -0800 | [diff] [blame] | 906 | trayIconMenu = new QMenu(this); |
| 907 | trayIconMenu->addAction(minimizeAction); |
| 908 | trayIconMenu->addAction(maximizeAction); |
| 909 | trayIconMenu->addAction(restoreAction); |
Yingdi Yu | 83eae84 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 910 | // trayIconMenu->addSeparator(); |
| 911 | // trayIconMenu->addAction(settingsAction); |
Yingdi Yu | fdb8ab8 | 2013-11-10 01:38:21 -0800 | [diff] [blame] | 912 | trayIconMenu->addSeparator(); |
| 913 | trayIconMenu->addAction(updateLocalPrefixAction); |
Yingdi Yu | 702d6f1 | 2013-11-07 17:00:54 -0800 | [diff] [blame] | 914 | trayIconMenu->addSeparator(); |
| 915 | trayIconMenu->addAction(quitAction); |
Yingdi Yu | 83eae84 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 916 | |
| 917 | trayIcon = new QSystemTrayIcon(this); |
Yingdi Yu | 702d6f1 | 2013-11-07 17:00:54 -0800 | [diff] [blame] | 918 | trayIcon->setContextMenu(trayIconMenu); |
Yingdi Yu | 83eae84 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 919 | |
| 920 | QIcon icon(":/images/icon_small.png"); |
| 921 | trayIcon->setIcon(icon); |
| 922 | setWindowIcon(icon); |
| 923 | trayIcon->setToolTip("ChronoChat System Tray Icon"); |
| 924 | trayIcon->setVisible(true); |
Yingdi Yu | 83eae84 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | |
| 928 | void |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 929 | ChatDialog::resizeEvent(QResizeEvent *e) |
| 930 | { |
| 931 | fitView(); |
| 932 | } |
| 933 | |
| 934 | void |
| 935 | ChatDialog::showEvent(QShowEvent *e) |
| 936 | { |
| 937 | fitView(); |
| 938 | } |
| 939 | |
| 940 | void |
| 941 | ChatDialog::fitView() |
| 942 | { |
| 943 | boost::recursive_mutex::scoped_lock lock(m_sceneMutex); |
| 944 | QRectF rect = m_scene->itemsBoundingRect(); |
| 945 | m_scene->setSceneRect(rect); |
| 946 | ui->treeViewer->fitInView(m_scene->itemsBoundingRect(), Qt::KeepAspectRatio); |
| 947 | } |
| 948 | |
| 949 | void |
| 950 | ChatDialog::formChatMessage(const QString &text, SyncDemo::ChatMessage &msg) { |
| 951 | msg.set_from(m_user.getNick().toStdString()); |
| 952 | msg.set_to(m_user.getChatroom().toStdString()); |
| 953 | msg.set_data(text.toUtf8().constData()); |
| 954 | time_t seconds = time(NULL); |
| 955 | msg.set_timestamp(seconds); |
| 956 | msg.set_type(SyncDemo::ChatMessage::CHAT); |
| 957 | } |
| 958 | |
| 959 | void |
| 960 | ChatDialog::formControlMessage(SyncDemo::ChatMessage &msg, SyncDemo::ChatMessage::ChatMessageType type) |
| 961 | { |
| 962 | msg.set_from(m_user.getNick().toStdString()); |
| 963 | msg.set_to(m_user.getChatroom().toStdString()); |
| 964 | time_t seconds = time(NULL); |
| 965 | msg.set_timestamp(seconds); |
| 966 | msg.set_type(type); |
| 967 | } |
| 968 | |
| 969 | void |
Yingdi Yu | fdb8ab8 | 2013-11-10 01:38:21 -0800 | [diff] [blame] | 970 | ChatDialog::updateLocalPrefix() |
| 971 | { |
Yingdi Yu | a66183f | 2013-11-10 10:32:18 -0800 | [diff] [blame] | 972 | m_newLocalPrefixReady = false; |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 973 | ndn::Name interestName("/local/ndn/prefix"); |
| 974 | ndn::Interest interest(interestName); |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 975 | interest.setInterestLifetime(1000); |
Yingdi Yu | fdb8ab8 | 2013-11-10 01:38:21 -0800 | [diff] [blame] | 976 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 977 | m_face->expressInterest(interest, |
| 978 | bind(&ChatDialog::onLocalPrefix, this, _1, _2), |
| 979 | bind(&ChatDialog::onLocalPrefixTimeout, this, _1)); |
Yingdi Yu | fdb8ab8 | 2013-11-10 01:38:21 -0800 | [diff] [blame] | 980 | |
Yingdi Yu | a66183f | 2013-11-10 10:32:18 -0800 | [diff] [blame] | 981 | while(m_newLocalPrefixReady == false) |
| 982 | { |
| 983 | #if BOOST_VERSION >= 1050000 |
| 984 | boost::this_thread::sleep_for(boost::chrono::milliseconds(100)); |
| 985 | #else |
| 986 | boost::this_thread::sleep(boost::posix_time::milliseconds(100)); |
| 987 | #endif |
| 988 | } |
Yingdi Yu | 1ecf81f | 2013-11-10 12:25:43 -0800 | [diff] [blame] | 989 | _LOG_DEBUG("now the prefix is " << m_newLocalPrefix.toUri()); |
Yingdi Yu | af305d7 | 2013-11-10 11:54:02 -0800 | [diff] [blame] | 990 | _LOG_DEBUG("in use prefix is " << m_user.getOriginPrefix().toStdString()); |
Yingdi Yu | a66183f | 2013-11-10 10:32:18 -0800 | [diff] [blame] | 991 | QString originPrefix = QString::fromStdString(m_newLocalPrefix.toUri()); |
| 992 | |
| 993 | if (originPrefix != "" && m_user.getOriginPrefix () != originPrefix) |
| 994 | emit settingUpdated(m_user.getNick (), m_user.getChatroom (), originPrefix); |
Yingdi Yu | fdb8ab8 | 2013-11-10 01:38:21 -0800 | [diff] [blame] | 995 | } |
| 996 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 997 | |
| 998 | void |
| 999 | ChatDialog::onLocalPrefix(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest, |
| 1000 | const ndn::ptr_lib::shared_ptr<ndn::Data>& data) |
| 1001 | { |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 1002 | string dataString(reinterpret_cast<const char*>(data->getContent().value()), data->getContent().value_size()); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 1003 | QString originPrefix = QString::fromStdString (dataString).trimmed (); |
| 1004 | string trimmedString = originPrefix.toStdString(); |
| 1005 | m_newLocalPrefix = ndn::Name(trimmedString); |
| 1006 | m_newLocalPrefixReady = true; |
| 1007 | } |
| 1008 | |
| 1009 | void |
| 1010 | ChatDialog::onLocalPrefixTimeout(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest) |
| 1011 | { |
| 1012 | m_newLocalPrefix = m_localPrefix; |
| 1013 | m_newLocalPrefixReady = true; |
| 1014 | } |
| 1015 | |
Yingdi Yu | fdb8ab8 | 2013-11-10 01:38:21 -0800 | [diff] [blame] | 1016 | static std::string chars2("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789"); |
| 1017 | |
| 1018 | QString |
| 1019 | ChatDialog::getRandomString() |
| 1020 | { |
| 1021 | std::string randStr; |
| 1022 | boost::random::random_device rng; |
| 1023 | boost::random::uniform_int_distribution<> index_dist(0, chars2.size() - 1); |
| 1024 | for (int i = 0; i < 10; i ++) |
| 1025 | { |
| 1026 | randStr += chars2[index_dist(rng)]; |
| 1027 | } |
| 1028 | return randStr.c_str(); |
| 1029 | } |
| 1030 | |
| 1031 | void |
Yingdi Yu | 83eae84 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 1032 | ChatDialog::changeEvent(QEvent *e) |
| 1033 | { |
| 1034 | switch(e->type()) |
| 1035 | { |
| 1036 | case QEvent::ActivationChange: |
| 1037 | if (isActiveWindow()) |
| 1038 | { |
| 1039 | trayIcon->setIcon(QIcon(":/images/icon_small.png")); |
| 1040 | } |
| 1041 | break; |
| 1042 | default: |
| 1043 | break; |
| 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | void |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 1048 | ChatDialog::appendMessage(const SyncDemo::ChatMessage msg, bool isHistory) |
| 1049 | { |
| 1050 | boost::recursive_mutex::scoped_lock lock(m_msgMutex); |
| 1051 | |
| 1052 | if (msg.type() == SyncDemo::ChatMessage::CHAT) |
| 1053 | { |
| 1054 | |
| 1055 | if (!msg.has_data()) |
| 1056 | { |
| 1057 | return; |
| 1058 | } |
| 1059 | |
| 1060 | if (msg.from().empty() || msg.data().empty()) |
| 1061 | { |
| 1062 | return; |
| 1063 | } |
| 1064 | |
| 1065 | if (!msg.has_timestamp()) |
| 1066 | { |
| 1067 | return; |
| 1068 | } |
| 1069 | |
| 1070 | // if (m_history.size() == MAX_HISTORY_ENTRY) |
| 1071 | // { |
| 1072 | // m_history.dequeue(); |
| 1073 | // } |
| 1074 | |
| 1075 | // m_history.enqueue(msg); |
| 1076 | |
| 1077 | QTextCharFormat nickFormat; |
| 1078 | nickFormat.setForeground(Qt::darkGreen); |
| 1079 | nickFormat.setFontWeight(QFont::Bold); |
| 1080 | nickFormat.setFontUnderline(true); |
| 1081 | nickFormat.setUnderlineColor(Qt::gray); |
| 1082 | |
| 1083 | QTextCursor cursor(ui->textEdit->textCursor()); |
| 1084 | cursor.movePosition(QTextCursor::End); |
| 1085 | QTextTableFormat tableFormat; |
| 1086 | tableFormat.setBorder(0); |
| 1087 | QTextTable *table = cursor.insertTable(1, 2, tableFormat); |
| 1088 | QString from = QString("%1 ").arg(msg.from().c_str()); |
| 1089 | QTextTableCell fromCell = table->cellAt(0, 0); |
| 1090 | fromCell.setFormat(nickFormat); |
| 1091 | fromCell.firstCursorPosition().insertText(from); |
| 1092 | |
| 1093 | time_t timestamp = msg.timestamp(); |
| 1094 | printTimeInCell(table, timestamp); |
| 1095 | |
| 1096 | QTextCursor nextCursor(ui->textEdit->textCursor()); |
| 1097 | nextCursor.movePosition(QTextCursor::End); |
| 1098 | table = nextCursor.insertTable(1, 1, tableFormat); |
| 1099 | table->cellAt(0, 0).firstCursorPosition().insertText(QString::fromUtf8(msg.data().c_str())); |
| 1100 | if (!isHistory) |
| 1101 | { |
| 1102 | showMessage(from, QString::fromUtf8(msg.data().c_str())); |
| 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | if (msg.type() == SyncDemo::ChatMessage::JOIN || msg.type() == SyncDemo::ChatMessage::LEAVE) |
| 1107 | { |
| 1108 | QTextCharFormat nickFormat; |
| 1109 | nickFormat.setForeground(Qt::gray); |
| 1110 | nickFormat.setFontWeight(QFont::Bold); |
| 1111 | nickFormat.setFontUnderline(true); |
| 1112 | nickFormat.setUnderlineColor(Qt::gray); |
| 1113 | |
| 1114 | QTextCursor cursor(ui->textEdit->textCursor()); |
| 1115 | cursor.movePosition(QTextCursor::End); |
| 1116 | QTextTableFormat tableFormat; |
| 1117 | tableFormat.setBorder(0); |
| 1118 | QTextTable *table = cursor.insertTable(1, 2, tableFormat); |
| 1119 | QString action; |
| 1120 | if (msg.type() == SyncDemo::ChatMessage::JOIN) |
| 1121 | { |
| 1122 | action = "enters room"; |
| 1123 | } |
| 1124 | else |
| 1125 | { |
| 1126 | action = "leaves room"; |
| 1127 | } |
| 1128 | |
| 1129 | QString from = QString("%1 %2 ").arg(msg.from().c_str()).arg(action); |
| 1130 | QTextTableCell fromCell = table->cellAt(0, 0); |
| 1131 | fromCell.setFormat(nickFormat); |
| 1132 | fromCell.firstCursorPosition().insertText(from); |
| 1133 | |
| 1134 | time_t timestamp = msg.timestamp(); |
| 1135 | printTimeInCell(table, timestamp); |
| 1136 | } |
| 1137 | |
| 1138 | QScrollBar *bar = ui->textEdit->verticalScrollBar(); |
| 1139 | bar->setValue(bar->maximum()); |
| 1140 | } |
| 1141 | |
| 1142 | QString |
| 1143 | ChatDialog::formatTime(time_t timestamp) |
| 1144 | { |
| 1145 | struct tm *tm_time = localtime(×tamp); |
| 1146 | int hour = tm_time->tm_hour; |
| 1147 | QString amOrPM; |
| 1148 | if (hour > 12) |
| 1149 | { |
| 1150 | hour -= 12; |
| 1151 | amOrPM = "PM"; |
| 1152 | } |
| 1153 | else |
| 1154 | { |
| 1155 | amOrPM = "AM"; |
| 1156 | if (hour == 0) |
| 1157 | { |
| 1158 | hour = 12; |
| 1159 | } |
| 1160 | } |
| 1161 | |
| 1162 | char textTime[12]; |
| 1163 | sprintf(textTime, "%d:%02d:%02d %s", hour, tm_time->tm_min, tm_time->tm_sec, amOrPM.toStdString().c_str()); |
| 1164 | return QString(textTime); |
| 1165 | } |
| 1166 | |
| 1167 | void |
| 1168 | ChatDialog::printTimeInCell(QTextTable *table, time_t timestamp) |
| 1169 | { |
| 1170 | QTextCharFormat timeFormat; |
| 1171 | timeFormat.setForeground(Qt::gray); |
| 1172 | timeFormat.setFontUnderline(true); |
| 1173 | timeFormat.setUnderlineColor(Qt::gray); |
| 1174 | QTextTableCell timeCell = table->cellAt(0, 1); |
| 1175 | timeCell.setFormat(timeFormat); |
| 1176 | timeCell.firstCursorPosition().insertText(formatTime(timestamp)); |
| 1177 | } |
| 1178 | |
| 1179 | void |
| 1180 | ChatDialog::showMessage(QString from, QString data) |
| 1181 | { |
| 1182 | if (!isActiveWindow()) |
| 1183 | { |
Yingdi Yu | 83eae84 | 2013-11-06 22:07:38 -0800 | [diff] [blame] | 1184 | trayIcon->showMessage(QString("Chatroom %1 has a new message").arg(m_user.getChatroom()), QString("<%1>: %2").arg(from).arg(data), QSystemTrayIcon::Information, 20000); |
| 1185 | trayIcon->setIcon(QIcon(":/images/note.png")); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 1186 | } |
| 1187 | } |
| 1188 | |
| 1189 | void |
| 1190 | ChatDialog::sendMsg(SyncDemo::ChatMessage &msg) |
| 1191 | { |
| 1192 | // send msg |
| 1193 | size_t size = msg.ByteSize(); |
| 1194 | char *buf = new char[size]; |
| 1195 | msg.SerializeToArray(buf, size); |
| 1196 | if (!msg.IsInitialized()) |
| 1197 | { |
| 1198 | _LOG_DEBUG("Errrrr.. msg was not probally initialized "<<__FILE__ <<":"<<__LINE__<<". what is happening?"); |
| 1199 | abort(); |
| 1200 | } |
| 1201 | m_sock->publishData(m_user.getPrefix().toStdString(), m_session, buf, size, FRESHNESS); |
| 1202 | |
| 1203 | delete buf; |
| 1204 | |
| 1205 | m_lastMsgTime = time(NULL); |
| 1206 | |
| 1207 | int nextSequence = m_sock->getNextSeq(m_user.getPrefix().toStdString(), m_session); |
| 1208 | Sync::MissingDataInfo mdi = {m_user.getPrefix().toStdString(), Sync::SeqNo(0), Sync::SeqNo(nextSequence - 1)}; |
| 1209 | std::vector<Sync::MissingDataInfo> v; |
| 1210 | v.push_back(mdi); |
| 1211 | { |
| 1212 | boost::recursive_mutex::scoped_lock lock(m_sceneMutex); |
| 1213 | m_scene->processUpdate(v, m_sock->getRootDigest().c_str()); |
| 1214 | m_scene->msgReceived(m_user.getPrefix(), m_user.getNick()); |
| 1215 | } |
| 1216 | } |
| 1217 | |
| 1218 | void |
| 1219 | ChatDialog::openInviteListDialog() |
| 1220 | { |
| 1221 | m_inviteListDialog->setInviteLabel(m_chatroomPrefix.toUri()); |
| 1222 | m_inviteListDialog->show(); |
| 1223 | } |
| 1224 | |
| 1225 | void |
| 1226 | ChatDialog::sendInvitationWrapper(QString invitee, bool isIntroducer) |
| 1227 | { |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 1228 | ndn::Name inviteeNamespace(invitee.toStdString()); |
| 1229 | ndn::ptr_lib::shared_ptr<ContactItem> inviteeItem = m_contactManager->getContact(inviteeNamespace); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 1230 | sendInvitation(inviteeItem, isIntroducer); |
| 1231 | } |
| 1232 | |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 1233 | void |
| 1234 | ChatDialog::closeEvent(QCloseEvent *e) |
| 1235 | { |
Yingdi Yu | 702d6f1 | 2013-11-07 17:00:54 -0800 | [diff] [blame] | 1236 | if (trayIcon->isVisible()) |
| 1237 | { |
| 1238 | QMessageBox::information(this, tr("Chronos"), |
| 1239 | tr("The program will keep running in the " |
| 1240 | "system tray. To terminate the program" |
| 1241 | "choose <b>Quit</b> in the context memu" |
| 1242 | "of the system tray entry.")); |
| 1243 | hide(); |
| 1244 | e->ignore(); |
| 1245 | } |
| 1246 | } |
| 1247 | |
| 1248 | void |
| 1249 | ChatDialog::quit() |
| 1250 | { |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 1251 | hide(); |
Yingdi Yu | 4694828 | 2013-11-06 18:43:31 -0800 | [diff] [blame] | 1252 | emit closeChatDialog(m_chatroomPrefix); |
| 1253 | } |
| 1254 | |
| 1255 | |
| 1256 | |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 1257 | |
Alexander Afanasyev | b4b9229 | 2013-07-09 13:54:59 -0700 | [diff] [blame] | 1258 | #if WAF |
| 1259 | #include "chatdialog.moc" |
| 1260 | #include "chatdialog.cpp.moc" |
| 1261 | #endif |