blob: fe7d7b5a49ea0123566807fbbae6d2058dfe535a [file] [log] [blame]
Yingdi Yu04842232013-10-23 14:03:09 -07001/* -*- 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 *
Yingdi Yu42f66462013-10-31 17:38:22 -07008 * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
9 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
10 * Yingdi Yu <yingdi@cs.ucla.edu>
Yingdi Yu04842232013-10-23 14:03:09 -070011 */
12
13#include "chatdialog.h"
14#include "ui_chatdialog.h"
15
Yingdi Yu42f66462013-10-31 17:38:22 -070016#include <QScrollBar>
17
Yingdi Yuc4d08d22013-10-23 23:07:29 -070018#ifndef Q_MOC_RUN
19#include <ndn.cxx/security/identity/identity-manager.h>
Yingdi Yuc4d08d22013-10-23 23:07:29 -070020#include <ndn.cxx/security/encryption/basic-encryption-manager.h>
Yingdi Yu42f66462013-10-31 17:38:22 -070021#include <sync-intro-certificate.h>
22#include <boost/random/random_device.hpp>
23#include <boost/random/uniform_int_distribution.hpp>
Yingdi Yuc4d08d22013-10-23 23:07:29 -070024#include "logging.h"
25#endif
26
Yingdi Yu04842232013-10-23 14:03:09 -070027using namespace std;
Yingdi Yu04842232013-10-23 14:03:09 -070028
Yingdi Yuc4d08d22013-10-23 23:07:29 -070029INIT_LOGGER("ChatDialog");
30
Yingdi Yu42f66462013-10-31 17:38:22 -070031static const int HELLO_INTERVAL = FRESHNESS * 3 / 4;
32
33Q_DECLARE_METATYPE(std::vector<Sync::MissingDataInfo> )
34Q_DECLARE_METATYPE(size_t)
35
36ChatDialog::ChatDialog(ndn::Ptr<ContactManager> contactManager,
37 const ndn::Name& chatroomPrefix,
38 const ndn::Name& localPrefix,
39 const ndn::Name& defaultIdentity,
Yingdi Yu04842232013-10-23 14:03:09 -070040 QWidget *parent)
Yingdi Yu42f66462013-10-31 17:38:22 -070041: QDialog(parent)
42 , ui(new Ui::ChatDialog)
43 , m_contactManager(contactManager)
44 , m_chatroomPrefix(chatroomPrefix)
45 , m_localPrefix(localPrefix)
46 , m_defaultIdentity(defaultIdentity)
47 , m_invitationPolicyManager(ndn::Ptr<InvitationPolicyManager>(new InvitationPolicyManager(m_chatroomPrefix.get(-1).toUri())))
48 , m_sock(NULL)
49 , m_lastMsgTime(0)
50 // , m_historyInitialized(false)
51 , m_joined(false)
52 , m_inviteListDialog(new InviteListDialog(m_contactManager))
Yingdi Yu04842232013-10-23 14:03:09 -070053{
Yingdi Yu42f66462013-10-31 17:38:22 -070054 qRegisterMetaType<std::vector<Sync::MissingDataInfo> >("std::vector<Sync::MissingDataInfo>");
55 qRegisterMetaType<size_t>("size_t");
56
Yingdi Yuc4d08d22013-10-23 23:07:29 -070057 ui->setupUi(this);
58
Yingdi Yu42f66462013-10-31 17:38:22 -070059 m_localChatPrefix = m_localPrefix;
60 m_localChatPrefix.append("FH").append(m_defaultIdentity);
61 m_localChatPrefix.append("chronos").append(m_chatroomPrefix.get(-1));
62
63 m_session = time(NULL);
64 m_scene = new DigestTreeScene(this);
65
66 initializeSetting();
67 updateLabels();
68
69 ui->treeViewer->setScene(m_scene);
70 ui->treeViewer->hide();
71 m_scene->plot("Empty");
72 QRectF rect = m_scene->itemsBoundingRect();
73 m_scene->setSceneRect(rect);
74
75 m_rosterModel = new QStringListModel(this);
76 ui->listView->setModel(m_rosterModel);
77
78 m_timer = new QTimer(this);
79
Yingdi Yuc4d08d22013-10-23 23:07:29 -070080 setWrapper();
Yingdi Yu42f66462013-10-31 17:38:22 -070081
82 connect(ui->inviteButton, SIGNAL(clicked()),
83 this, SLOT(openInviteListDialog()));
84 connect(m_inviteListDialog, SIGNAL(invitionDetermined(QString, bool)),
85 this, SLOT(sendInvitationWrapper(QString, bool)));
86 connect(ui->lineEdit, SIGNAL(returnPressed()),
87 this, SLOT(returnPressed()));
88 connect(ui->treeButton, SIGNAL(pressed()),
89 this, SLOT(treeButtonPressed()));
90 connect(this, SIGNAL(dataReceived(QString, const char *, size_t, bool, bool)),
91 this, SLOT(processData(QString, const char *, size_t, bool, bool)));
92 connect(this, SIGNAL(treeUpdated(const std::vector<Sync::MissingDataInfo>)),
93 this, SLOT(processTreeUpdate(const std::vector<Sync::MissingDataInfo>)));
94 connect(m_timer, SIGNAL(timeout()),
95 this, SLOT(replot()));
96 connect(m_scene, SIGNAL(replot()),
97 this, SLOT(replot()));
98 // TODO: TrayIcon
99 // connect(trayIcon, SIGNAL(messageClicked()),
100 // this, SLOT(showNormal()));
101 // connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
102 // this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
103 connect(m_scene, SIGNAL(rosterChanged(QStringList)),
104 this, SLOT(updateRosterList(QStringList)));
105
106 // m_identityManager = ndn::Ptr<ndn::security::IdentityManager>::Create();
107 // // ndn::Ptr<ndn::security::EncryptionManager> encryptionManager = ndn::Ptr<ndn::security::EncryptionManager>(new ndn::security::BasicEncryptionManager(privateStorage, "/tmp/encryption.db"));
108
109 // ndn::Name certificateName = m_identityManager->getDefaultCertificateNameByIdentity(m_defaultIdentity);
110 // m_syncPolicyManager = ndn::Ptr<SyncPolicyManager>(new SyncPolicyManager(m_defaultIdentity, certificateName, m_chatroomPrefix));
111
112 initializeSync();
Yingdi Yu04842232013-10-23 14:03:09 -0700113}
114
Yingdi Yu42f66462013-10-31 17:38:22 -0700115// ChatDialog::ChatDialog(const ndn::Name& chatroomPrefix,
116// const ndn::Name& localPrefix,
117// const ndn::Name& defaultIdentity,
118// const ndn::security::IdentityCertificate& identityCertificate,
119// QWidget *parent)
120// : QDialog(parent)
121// , ui(new Ui::ChatDialog)
122// , m_chatroomPrefix(chatroomPrefix)
123// , m_localPrefix(localPrefix)
124// , m_defaultIdentity(defaultIdentity)
125// , m_invitationPolicyManager(ndn::Ptr<InvitationPolicyManager>(new InvitationPolicyManager(m_chatroomPrefix.get(-1).toUri())))
126
127// , m_sock(NULL)
128// , m_lastMsgTime(0)
129// // , m_historyInitialized(false)
130// , m_joined(false)
131// {
132// qRegisterMetaType<std::vector<Sync::MissingDataInfo> >("std::vector<Sync::MissingDataInfo>");
133// qRegisterMetaType<size_t>("size_t");
134
135// ui->setupUi(this);
136
137// m_localChatPrefix = m_localPrefix;
138// m_localChatPrefix.append("FH").append(m_defaultIdentity);
139// m_localChatPrefix.append("chronos").append(m_chatroomPrefix.get(-1));
140
141// m_session = time(NULL);
142// m_scene = new DigestTreeScene(this);
143
144// initializeSetting();
145// updateLabels();
146
147// ui->treeViewer->setScene(m_scene);
148// ui->treeViewer->hide();
149// m_scene->plot("Empty");
150// QRectF rect = m_scene->itemsBoundingRect();
151// m_scene->setSceneRect(rect);
152
153// m_rosterModel = new QStringListModel(this);
154// ui->listView->setModel(m_rosterModel);
155
156// m_timer = new QTimer(this);
157
158// setWrapper();
159
160// connect(ui->lineEdit, SIGNAL(returnPressed()),
161// this, SLOT(returnPressed()));
162// connect(ui->treeButton, SIGNAL(pressed()),
163// this, SLOT(treeButtonPressed()));
164// connect(this, SIGNAL(dataReceived(QString, const char *, size_t, bool, bool)),
165// this, SLOT(processData(QString, const char *, size_t, bool, bool)));
166// connect(this, SIGNAL(treeUpdated(const std::vector<Sync::MissingDataInfo>)),
167// this, SLOT(processTreeUpdate(const std::vector<Sync::MissingDataInfo>)));
168// connect(m_timer, SIGNAL(timeout()),
169// this, SLOT(replot()));
170// connect(m_scene, SIGNAL(replot()),
171// this, SLOT(replot()));
172// // TODO: TrayIcon
173// // connect(trayIcon, SIGNAL(messageClicked()),
174// // this, SLOT(showNormal()));
175// // connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
176// // this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
177// connect(m_scene, SIGNAL(rosterChanged(QStringList)),
178// this, SLOT(updateRosterList(QStringList)));
179
180// // m_identityManager = ndn::Ptr<ndn::security::IdentityManager>::Create();
181// // // ndn::Ptr<ndn::security::EncryptionManager> encryptionManager = ndn::Ptr<ndn::security::EncryptionManager>(new ndn::security::BasicEncryptionManager(privateStorage, "/tmp/encryption.db"));
182
183// // ndn::Name certificateName = m_identityManager->getDefaultCertificateNameByIdentity(m_defaultIdentity);
184// // m_syncPolicyManager = ndn::Ptr<SyncPolicyManager>(new SyncPolicyManager(m_defaultIdentity, certificateName, m_chatroomPrefix));
185
186// initializeSync();
187// }
188
Yingdi Yu04842232013-10-23 14:03:09 -0700189ChatDialog::~ChatDialog()
190{
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700191 delete ui;
Yingdi Yu42f66462013-10-31 17:38:22 -0700192 sendLeave();
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700193 m_handler->shutdown();
Yingdi Yu04842232013-10-23 14:03:09 -0700194}
195
196void
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700197ChatDialog::setWrapper()
Yingdi Yu04842232013-10-23 14:03:09 -0700198{
Yingdi Yu42f66462013-10-31 17:38:22 -0700199 m_identityManager = ndn::Ptr<ndn::security::IdentityManager>::Create();
200 // ndn::Ptr<ndn::security::EncryptionManager> encryptionManager = ndn::Ptr<ndn::security::EncryptionManager>(new ndn::security::BasicEncryptionManager(privateStorage, "/tmp/encryption.db"));
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700201
Yingdi Yu42f66462013-10-31 17:38:22 -0700202 ndn::Name certificateName = m_identityManager->getDefaultCertificateNameByIdentity(m_defaultIdentity);
203 m_syncPolicyManager = ndn::Ptr<SyncPolicyManager>(new SyncPolicyManager(m_defaultIdentity, certificateName, m_chatroomPrefix));
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700204
Yingdi Yu42f66462013-10-31 17:38:22 -0700205 m_keychain = ndn::Ptr<ndn::security::Keychain>(new ndn::security::Keychain(m_identityManager, m_invitationPolicyManager, NULL));
206
207 m_handler = ndn::Ptr<ndn::Wrapper>(new ndn::Wrapper(m_keychain));
Yingdi Yu04842232013-10-23 14:03:09 -0700208}
209
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700210void
Yingdi Yu42f66462013-10-31 17:38:22 -0700211ChatDialog::initializeSetting()
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700212{
Yingdi Yu42f66462013-10-31 17:38:22 -0700213 // TODO: nick name may be changed.
214 m_user.setNick(QString::fromStdString(m_defaultIdentity.get(-1).toUri()));
215 m_user.setChatroom(QString::fromStdString(m_chatroomPrefix.get(-1).toUri()));
216 m_user.setOriginPrefix(QString::fromStdString(m_localPrefix.toUri()));
217 m_user.setPrefix(QString::fromStdString(m_localChatPrefix.toUri()));
218 m_scene->setCurrentPrefix(QString::fromStdString(m_localChatPrefix.toUri()));
219}
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700220
Yingdi Yu42f66462013-10-31 17:38:22 -0700221void
222ChatDialog::updateLabels()
223{
224 QString settingDisp = QString("Chatroom: %1").arg(m_user.getChatroom());
225 ui->infoLabel->setStyleSheet("QLabel {color: #630; font-size: 16px; font: bold \"Verdana\";}");
226 ui->infoLabel->setText(settingDisp);
227 QString prefixDisp;
228 if (m_user.getPrefix().startsWith("/private/local"))
229 {
230 prefixDisp = QString("<Warning: no connection to hub or hub does not support prefix autoconfig.>\n <Prefix = %1>").arg(m_user.getPrefix());
231 ui->prefixLabel->setStyleSheet("QLabel {color: red; font-size: 12px; font: bold \"Verdana\";}");
232 }
233 else
234 {
235 prefixDisp = QString("<Prefix = %1>").arg(m_user.getPrefix());
236 ui->prefixLabel->setStyleSheet("QLabel {color: Green; font-size: 12px; font: bold \"Verdana\";}");
237 }
238 ui->prefixLabel->setText(prefixDisp);
239}
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700240
Yingdi Yu42f66462013-10-31 17:38:22 -0700241void
242ChatDialog::sendInvitation(ndn::Ptr<ContactItem> contact, bool isIntroducer)
243{
244 m_invitationPolicyManager->addTrustAnchor(contact->getSelfEndorseCertificate());
245
246 ndn::Name certificateName = m_identityManager->getDefaultCertificateNameByIdentity(m_defaultIdentity);
247
248 ndn::Name interestName("/ndn/broadcast/chronos/invitation");
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700249 interestName.append(contact->getNameSpace());
250 interestName.append("chatroom");
251 interestName.append(m_chatroomPrefix.get(-1));
252 interestName.append("inviter-prefix");
253 interestName.append(m_localPrefix);
254 interestName.append("inviter");
255 interestName.append(certificateName);
256
257 string signedUri = interestName.toUri();
Yingdi Yu42f66462013-10-31 17:38:22 -0700258 ndn::Blob signedBlob(signedUri.c_str(), signedUri.size());
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700259
Yingdi Yu42f66462013-10-31 17:38:22 -0700260 ndn::Ptr<const ndn::signature::Sha256WithRsa> sha256sig = ndn::DynamicCast<const ndn::signature::Sha256WithRsa>(m_identityManager->signByCertificate(signedBlob, certificateName));
261 const ndn::Blob& sigBits = sha256sig->getSignatureBits();
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700262
263 interestName.append(sigBits.buf(), sigBits.size());
264
Yingdi Yu42f66462013-10-31 17:38:22 -0700265 ndn::Ptr<ndn::Interest> interest = ndn::Ptr<ndn::Interest>(new ndn::Interest(interestName));
266 ndn::Ptr<ndn::Closure> closure = ndn::Ptr<ndn::Closure>(new ndn::Closure(boost::bind(&ChatDialog::onInviteReplyVerified,
267 this,
268 _1,
269 contact->getNameSpace(),
270 isIntroducer),
271 boost::bind(&ChatDialog::onInviteTimeout,
272 this,
273 _1,
274 _2,
275 contact->getNameSpace(),
276 7),
277 boost::bind(&ChatDialog::onUnverified,
278 this,
279 _1)));
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700280
281 m_handler->sendInterest(interest, closure);
282}
283
284void
Yingdi Yu42f66462013-10-31 17:38:22 -0700285ChatDialog::addTrustAnchor(const EndorseCertificate& selfEndorseCertificate)
286{ m_invitationPolicyManager->addTrustAnchor(selfEndorseCertificate); }
287
288void
289ChatDialog::addChatDataRule(const ndn::Name& prefix,
290 const ndn::security::IdentityCertificate& identityCertificate,
291 bool isIntroducer)
292{ m_syncPolicyManager->addChatDataRule(prefix, identityCertificate, isIntroducer); }
293
294void
295ChatDialog::publishIntroCert(ndn::Ptr<ndn::security::IdentityCertificate> dskCertificate, bool isIntroducer)
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700296{
Yingdi Yu42f66462013-10-31 17:38:22 -0700297 SyncIntroCertificate syncIntroCertificate(m_chatroomPrefix,
298 dskCertificate->getPublicKeyName(),
299 m_identityManager->getDefaultKeyNameForIdentity(m_defaultIdentity),
300 dskCertificate->getNotBefore(),
301 dskCertificate->getNotAfter(),
302 dskCertificate->getPublicKeyInfo(),
303 (isIntroducer ? SyncIntroCertificate::INTRODUCER : SyncIntroCertificate::PRODUCER));
304 ndn::Name certName = m_identityManager->getDefaultCertificateNameByIdentity(m_defaultIdentity);
305 m_identityManager->signByCertificate(syncIntroCertificate, certName);
306 m_handler->putToNdnd(*syncIntroCertificate.encodeToWire());
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700307}
308
309void
Yingdi Yu42f66462013-10-31 17:38:22 -0700310ChatDialog::invitationRejected(const ndn::Name& identity)
311{
312 _LOG_DEBUG(" " << identity.toUri() << " rejected your invitation!");
313 //TODO:
314}
315
316void
317ChatDialog::invitationAccepted(const ndn::Name& identity, ndn::Ptr<ndn::Data> data, const string& inviteePrefix, bool isIntroducer)
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700318{
319 _LOG_DEBUG(" " << identity.toUri() << " accepted your invitation!");
Yingdi Yu42f66462013-10-31 17:38:22 -0700320 ndn::Ptr<const ndn::signature::Sha256WithRsa> sha256sig = boost::dynamic_pointer_cast<const ndn::signature::Sha256WithRsa> (data->getSignature());
321 const ndn::Name & keyLocatorName = sha256sig->getKeyLocator().getKeyName();
322 ndn::Ptr<ndn::security::IdentityCertificate> dskCertificate = m_invitationPolicyManager->getValidatedDskCertificate(keyLocatorName);
323 m_syncPolicyManager->addChatDataRule(inviteePrefix, *dskCertificate, isIntroducer);
324 publishIntroCert(dskCertificate, isIntroducer);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700325}
326
327void
Yingdi Yu42f66462013-10-31 17:38:22 -0700328ChatDialog::onInviteReplyVerified(ndn::Ptr<ndn::Data> data, const ndn::Name& identity, bool isIntroducer)
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700329{
330 string content(data->content().buf(), data->content().size());
331 if(content.empty())
332 invitationRejected(identity);
333 else
Yingdi Yu42f66462013-10-31 17:38:22 -0700334 invitationAccepted(identity, data, content, isIntroducer);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700335}
336
337void
Yingdi Yu42f66462013-10-31 17:38:22 -0700338ChatDialog::onInviteTimeout(ndn::Ptr<ndn::Closure> closure, ndn::Ptr<ndn::Interest> interest, const ndn::Name& identity, int retry)
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700339{
340 if(retry > 0)
341 {
Yingdi Yu42f66462013-10-31 17:38:22 -0700342 ndn::Ptr<ndn::Closure> newClosure = ndn::Ptr<ndn::Closure>(new ndn::Closure(closure->m_dataCallback,
343 boost::bind(&ChatDialog::onInviteTimeout,
344 this,
345 _1,
346 _2,
347 identity,
348 retry - 1),
349 closure->m_unverifiedCallback,
350 closure->m_stepCount)
351 );
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700352 m_handler->sendInterest(interest, newClosure);
353 }
354 else
355 invitationRejected(identity);
356}
357
358void
Yingdi Yu42f66462013-10-31 17:38:22 -0700359ChatDialog::onUnverified(ndn::Ptr<ndn::Data> data)
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700360{}
361
Yingdi Yu42f66462013-10-31 17:38:22 -0700362void
363ChatDialog::initializeSync()
364{
365
366 m_sock = new Sync::SyncSocket(m_chatroomPrefix.toUri(),
367 m_syncPolicyManager,
368 bind(&ChatDialog::processTreeUpdateWrapper, this, _1, _2),
369 bind(&ChatDialog::processRemoveWrapper, this, _1));
370
371 usleep(100000);
372
373 QTimer::singleShot(600, this, SLOT(sendJoin()));
374 m_timer->start(FRESHNESS * 1000);
375 disableTreeDisplay();
376 QTimer::singleShot(2200, this, SLOT(enableTreeDisplay()));
377 // Sync::CcnxWrapperPtr handle = boost::make_shared<Sync::CcnxWrapper> ();
378 // handle->setInterestFilter(m_user.getPrefix().toStdString(), bind(&ChatDialog::respondHistoryRequest, this, _1));
379 // _LOG_DEBUG("initializeSync is done!");
380}
381
382void
383ChatDialog::returnPressed()
384{
385 QString text = ui->lineEdit->text();
386 if (text.isEmpty())
387 return;
388
389 ui->lineEdit->clear();
390
391 if (text.startsWith("boruoboluomi"))
392 {
393 summonReaper ();
394 // reapButton->show();
395 fitView();
396 return;
397 }
398
399 if (text.startsWith("minimanihong"))
400 {
401 // reapButton->hide();
402 fitView();
403 return;
404 }
405
406 SyncDemo::ChatMessage msg;
407 formChatMessage(text, msg);
408
409 appendMessage(msg);
410
411 sendMsg(msg);
412
413 fitView();
414}
415
416void
417ChatDialog::treeButtonPressed()
418{
419 if (ui->treeViewer->isVisible())
420 {
421 ui->treeViewer->hide();
422 ui->treeButton->setText("Show ChronoSync Tree");
423 }
424 else
425 {
426 ui->treeViewer->show();
427 ui->treeButton->setText("Hide ChronoSync Tree");
428 }
429
430 fitView();
431}
432
433void ChatDialog::disableTreeDisplay()
434{
435 ui->treeButton->setEnabled(false);
436 ui->treeViewer->hide();
437 fitView();
438}
439
440void ChatDialog::enableTreeDisplay()
441{
442 ui->treeButton->setEnabled(true);
443 // treeViewer->show();
444 // fitView();
445}
446
447void
448ChatDialog::processTreeUpdateWrapper(const std::vector<Sync::MissingDataInfo> v, Sync::SyncSocket *sock)
449{
450 emit treeUpdated(v);
451 _LOG_DEBUG("<<< Tree update signal emitted");
452}
453
454void
455ChatDialog::processRemoveWrapper(std::string prefix)
456{
457 _LOG_DEBUG("Sync REMOVE signal received for prefix: " << prefix);
458}
459
460void
461ChatDialog::processTreeUpdate(const std::vector<Sync::MissingDataInfo> v)
462{
463 _LOG_DEBUG("<<< processing Tree Update");
464
465 if (v.empty())
466 {
467 return;
468 }
469
470 // reflect the changes on digest tree
471 {
472 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
473 m_scene->processUpdate(v, m_sock->getRootDigest().c_str());
474 }
475
476 int n = v.size();
477 int totalMissingPackets = 0;
478 for (int i = 0; i < n; i++)
479 {
480 totalMissingPackets += v[i].high.getSeq() - v[i].low.getSeq() + 1;
481 }
482
483 for (int i = 0; i < n; i++)
484 {
485 if (totalMissingPackets < 4)
486 {
487 for (Sync::SeqNo seq = v[i].low; seq <= v[i].high; ++seq)
488 {
489 m_sock->fetchData(v[i].prefix, seq, bind(&ChatDialog::processDataWrapper, this, _1), 2);
490 _LOG_DEBUG("<<< Fetching " << v[i].prefix << "/" <<seq.getSession() <<"/" << seq.getSeq());
491 }
492 }
493 else
494 {
495 m_sock->fetchData(v[i].prefix, v[i].high, bind(&ChatDialog::processDataNoShowWrapper, this, _1), 2);
496 }
497 }
498
499 // adjust the view
500 fitView();
501
502}
503
504void
505ChatDialog::processDataWrapper(ndn::Ptr<ndn::Data> data)
506{
507 string name = data->getName().toUri();
508 const char* buf = data->content().buf();
509 size_t len = data->content().size();
510
511 char *tempBuf = new char[len];
512 memcpy(tempBuf, buf, len);
513 emit dataReceived(name.c_str(), tempBuf, len, true, false);
514 _LOG_DEBUG("<<< " << name << " fetched");
515}
516
517void
518ChatDialog::processDataNoShowWrapper(ndn::Ptr<ndn::Data> data)
519{
520 string name = data->getName().toUri();
521 const char* buf = data->content().buf();
522 size_t len = data->content().size();
523
524 char *tempBuf = new char[len];
525 memcpy(tempBuf, buf, len);
526 emit dataReceived(name.c_str(), tempBuf, len, false, false);
527
528 // if (!m_historyInitialized)
529 // {
530 // fetchHistory(name);
531 // m_historyInitialized = true;
532 // }
533}
534
535// void
536// ChatDialog::fetchHistory(std::string name)
537// {
538
539// /****************************/
540// /* TODO: fix following part */
541// /****************************/
542// string nameWithoutSeq = name.substr(0, name.find_last_of('/'));
543// string prefix = nameWithoutSeq.substr(0, nameWithoutSeq.find_last_of('/'));
544// prefix += "/history";
545// // Ptr<Wrapper>CcnxWrapperPtr handle = boost::make_shared<Sync::CcnxWrapper> ();
546// // QString randomString = getRandomString();
547// // for (int i = 0; i < MAX_HISTORY_ENTRY; i++)
548// // {
549// // QString interest = QString("%1/%2/%3").arg(prefix.c_str()).arg(randomString).arg(i);
550// // handle->sendInterest(interest.toStdString(), bind(&ChatDialog::processDataHistoryWrapper, this, _1, _2, _3));
551// // }
552// }
553
554void
555ChatDialog::processData(QString name, const char *buf, size_t len, bool show, bool isHistory)
556{
557 SyncDemo::ChatMessage msg;
558 bool corrupted = false;
559 if (!msg.ParseFromArray(buf, len))
560 {
561 _LOG_DEBUG("Errrrr.. Can not parse msg with name: " << name.toStdString() << ". what is happening?");
562 // nasty stuff: as a remedy, we'll form some standard msg for inparsable msgs
563 msg.set_from("inconnu");
564 msg.set_type(SyncDemo::ChatMessage::OTHER);
565 corrupted = true;
566 }
567
568 delete [] buf;
569 buf = NULL;
570
571 // display msg received from network
572 // we have to do so; this function is called by ccnd thread
573 // so if we call appendMsg directly
574 // Qt crash as "QObject: Cannot create children for a parent that is in a different thread"
575 // the "cannonical" way to is use signal-slot
576 if (show && !corrupted)
577 {
578 appendMessage(msg, isHistory);
579 }
580
581 if (!isHistory)
582 {
583 // update the tree view
584 std::string stdStrName = name.toStdString();
585 std::string stdStrNameWithoutSeq = stdStrName.substr(0, stdStrName.find_last_of('/'));
586 std::string prefix = stdStrNameWithoutSeq.substr(0, stdStrNameWithoutSeq.find_last_of('/'));
587 _LOG_DEBUG("<<< updating scene for" << prefix << ": " << msg.from());
588 if (msg.type() == SyncDemo::ChatMessage::LEAVE)
589 {
590 processRemove(prefix.c_str());
591 }
592 else
593 {
594 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
595 m_scene->msgReceived(prefix.c_str(), msg.from().c_str());
596 }
597 }
598 fitView();
599}
600
601void
602ChatDialog::processRemove(QString prefix)
603{
604 _LOG_DEBUG("<<< remove node for prefix" << prefix.toStdString());
605
606 bool removed = m_scene->removeNode(prefix);
607 if (removed)
608 {
609 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
610 m_scene->plot(m_sock->getRootDigest().c_str());
611 }
612}
613
614void
615ChatDialog::sendJoin()
616{
617 m_joined = true;
618 SyncDemo::ChatMessage msg;
619 formControlMessage(msg, SyncDemo::ChatMessage::JOIN);
620 sendMsg(msg);
621 boost::random::random_device rng;
622 boost::random::uniform_int_distribution<> uniform(1, FRESHNESS / 5 * 1000);
623 m_randomizedInterval = HELLO_INTERVAL * 1000 + uniform(rng);
624 QTimer::singleShot(m_randomizedInterval, this, SLOT(sendHello()));
625}
626
627void
628ChatDialog::sendHello()
629{
630 time_t now = time(NULL);
631 int elapsed = now - m_lastMsgTime;
632 if (elapsed >= m_randomizedInterval / 1000)
633 {
634 SyncDemo::ChatMessage msg;
635 formControlMessage(msg, SyncDemo::ChatMessage::HELLO);
636 sendMsg(msg);
637 boost::random::random_device rng;
638 boost::random::uniform_int_distribution<> uniform(1, FRESHNESS / 5 * 1000);
639 m_randomizedInterval = HELLO_INTERVAL * 1000 + uniform(rng);
640 QTimer::singleShot(m_randomizedInterval, this, SLOT(sendHello()));
641 }
642 else
643 {
644 QTimer::singleShot((m_randomizedInterval - elapsed * 1000), this, SLOT(sendHello()));
645 }
646}
647
648void
649ChatDialog::sendLeave()
650{
651 SyncDemo::ChatMessage msg;
652 formControlMessage(msg, SyncDemo::ChatMessage::LEAVE);
653 sendMsg(msg);
654 usleep(500000);
655 m_sock->remove(m_user.getPrefix().toStdString());
656 usleep(5000);
657 m_joined = false;
658 _LOG_DEBUG("Sync REMOVE signal sent");
659}
660
661void
662ChatDialog::replot()
663{
664 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
665 m_scene->plot(m_sock->getRootDigest().c_str());
666 fitView();
667}
668
669void
670ChatDialog::summonReaper()
671{
672 Sync::SyncLogic &logic = m_sock->getLogic ();
673 map<string, bool> branches = logic.getBranchPrefixes();
674 QMap<QString, DisplayUserPtr> roster = m_scene->getRosterFull();
675
676 m_zombieList.clear();
677
678 QMapIterator<QString, DisplayUserPtr> it(roster);
679 map<string, bool>::iterator mapIt;
680 while(it.hasNext())
681 {
682 it.next();
683 DisplayUserPtr p = it.value();
684 if (p != DisplayUserNullPtr)
685 {
686 mapIt = branches.find(p->getPrefix().toStdString());
687 if (mapIt != branches.end())
688 {
689 mapIt->second = true;
690 }
691 }
692 }
693
694 for (mapIt = branches.begin(); mapIt != branches.end(); ++mapIt)
695 {
696 // this is zombie. all active users should have been marked true
697 if (! mapIt->second)
698 {
699 m_zombieList.append(mapIt->first.c_str());
700 }
701 }
702
703 m_zombieIndex = 0;
704
705 // start reaping
706 reap();
707}
708
709void
710ChatDialog::reap()
711{
712 if (m_zombieIndex < m_zombieList.size())
713 {
714 string prefix = m_zombieList.at(m_zombieIndex).toStdString();
715 m_sock->remove(prefix);
716 _LOG_DEBUG("Reaped: prefix = " << prefix);
717 m_zombieIndex++;
718 // reap again in 10 seconds
719 QTimer::singleShot(10000, this, SLOT(reap()));
720 }
721}
722
723void
724ChatDialog::updateRosterList(QStringList staleUserList)
725{
726 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
727 QStringList rosterList = m_scene->getRosterList();
728 m_rosterModel->setStringList(rosterList);
729 QString user;
730 QStringListIterator it(staleUserList);
731 while(it.hasNext())
732 {
733 std::string nick = it.next().toStdString();
734 if (nick.empty())
735 continue;
736
737 SyncDemo::ChatMessage msg;
738 formControlMessage(msg, SyncDemo::ChatMessage::LEAVE);
739 msg.set_from(nick);
740 appendMessage(msg);
741 }
742}
743
744void
745ChatDialog::resizeEvent(QResizeEvent *e)
746{
747 fitView();
748}
749
750void
751ChatDialog::showEvent(QShowEvent *e)
752{
753 fitView();
754}
755
756void
757ChatDialog::fitView()
758{
759 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
760 QRectF rect = m_scene->itemsBoundingRect();
761 m_scene->setSceneRect(rect);
762 ui->treeViewer->fitInView(m_scene->itemsBoundingRect(), Qt::KeepAspectRatio);
763}
764
765void
766ChatDialog::formChatMessage(const QString &text, SyncDemo::ChatMessage &msg) {
767 msg.set_from(m_user.getNick().toStdString());
768 msg.set_to(m_user.getChatroom().toStdString());
769 msg.set_data(text.toUtf8().constData());
770 time_t seconds = time(NULL);
771 msg.set_timestamp(seconds);
772 msg.set_type(SyncDemo::ChatMessage::CHAT);
773}
774
775void
776ChatDialog::formControlMessage(SyncDemo::ChatMessage &msg, SyncDemo::ChatMessage::ChatMessageType type)
777{
778 msg.set_from(m_user.getNick().toStdString());
779 msg.set_to(m_user.getChatroom().toStdString());
780 time_t seconds = time(NULL);
781 msg.set_timestamp(seconds);
782 msg.set_type(type);
783}
784
785void
786ChatDialog::appendMessage(const SyncDemo::ChatMessage msg, bool isHistory)
787{
788 boost::recursive_mutex::scoped_lock lock(m_msgMutex);
789
790 if (msg.type() == SyncDemo::ChatMessage::CHAT)
791 {
792
793 if (!msg.has_data())
794 {
795 return;
796 }
797
798 if (msg.from().empty() || msg.data().empty())
799 {
800 return;
801 }
802
803 if (!msg.has_timestamp())
804 {
805 return;
806 }
807
808 // if (m_history.size() == MAX_HISTORY_ENTRY)
809 // {
810 // m_history.dequeue();
811 // }
812
813 // m_history.enqueue(msg);
814
815 QTextCharFormat nickFormat;
816 nickFormat.setForeground(Qt::darkGreen);
817 nickFormat.setFontWeight(QFont::Bold);
818 nickFormat.setFontUnderline(true);
819 nickFormat.setUnderlineColor(Qt::gray);
820
821 QTextCursor cursor(ui->textEdit->textCursor());
822 cursor.movePosition(QTextCursor::End);
823 QTextTableFormat tableFormat;
824 tableFormat.setBorder(0);
825 QTextTable *table = cursor.insertTable(1, 2, tableFormat);
826 QString from = QString("%1 ").arg(msg.from().c_str());
827 QTextTableCell fromCell = table->cellAt(0, 0);
828 fromCell.setFormat(nickFormat);
829 fromCell.firstCursorPosition().insertText(from);
830
831 time_t timestamp = msg.timestamp();
832 printTimeInCell(table, timestamp);
833
834 QTextCursor nextCursor(ui->textEdit->textCursor());
835 nextCursor.movePosition(QTextCursor::End);
836 table = nextCursor.insertTable(1, 1, tableFormat);
837 table->cellAt(0, 0).firstCursorPosition().insertText(QString::fromUtf8(msg.data().c_str()));
838 if (!isHistory)
839 {
840 showMessage(from, QString::fromUtf8(msg.data().c_str()));
841 }
842 }
843
844 if (msg.type() == SyncDemo::ChatMessage::JOIN || msg.type() == SyncDemo::ChatMessage::LEAVE)
845 {
846 QTextCharFormat nickFormat;
847 nickFormat.setForeground(Qt::gray);
848 nickFormat.setFontWeight(QFont::Bold);
849 nickFormat.setFontUnderline(true);
850 nickFormat.setUnderlineColor(Qt::gray);
851
852 QTextCursor cursor(ui->textEdit->textCursor());
853 cursor.movePosition(QTextCursor::End);
854 QTextTableFormat tableFormat;
855 tableFormat.setBorder(0);
856 QTextTable *table = cursor.insertTable(1, 2, tableFormat);
857 QString action;
858 if (msg.type() == SyncDemo::ChatMessage::JOIN)
859 {
860 action = "enters room";
861 }
862 else
863 {
864 action = "leaves room";
865 }
866
867 QString from = QString("%1 %2 ").arg(msg.from().c_str()).arg(action);
868 QTextTableCell fromCell = table->cellAt(0, 0);
869 fromCell.setFormat(nickFormat);
870 fromCell.firstCursorPosition().insertText(from);
871
872 time_t timestamp = msg.timestamp();
873 printTimeInCell(table, timestamp);
874 }
875
876 QScrollBar *bar = ui->textEdit->verticalScrollBar();
877 bar->setValue(bar->maximum());
878}
879
880QString
881ChatDialog::formatTime(time_t timestamp)
882{
883 struct tm *tm_time = localtime(&timestamp);
884 int hour = tm_time->tm_hour;
885 QString amOrPM;
886 if (hour > 12)
887 {
888 hour -= 12;
889 amOrPM = "PM";
890 }
891 else
892 {
893 amOrPM = "AM";
894 if (hour == 0)
895 {
896 hour = 12;
897 }
898 }
899
900 char textTime[12];
901 sprintf(textTime, "%d:%02d:%02d %s", hour, tm_time->tm_min, tm_time->tm_sec, amOrPM.toStdString().c_str());
902 return QString(textTime);
903}
904
905void
906ChatDialog::printTimeInCell(QTextTable *table, time_t timestamp)
907{
908 QTextCharFormat timeFormat;
909 timeFormat.setForeground(Qt::gray);
910 timeFormat.setFontUnderline(true);
911 timeFormat.setUnderlineColor(Qt::gray);
912 QTextTableCell timeCell = table->cellAt(0, 1);
913 timeCell.setFormat(timeFormat);
914 timeCell.firstCursorPosition().insertText(formatTime(timestamp));
915}
916
917void
918ChatDialog::showMessage(QString from, QString data)
919{
920 if (!isActiveWindow())
921 {
922 //TODO: Notification to be done
923 // trayIcon->showMessage(QString("Chatroom %1 has a new message").arg(m_user.getChatroom()), QString("<%1>: %2").arg(from).arg(data), QSystemTrayIcon::Information, 20000);
924 // trayIcon->setIcon(QIcon(":/images/note.png"));
925 }
926}
927
928void
929ChatDialog::sendMsg(SyncDemo::ChatMessage &msg)
930{
931 // send msg
932 size_t size = msg.ByteSize();
933 char *buf = new char[size];
934 msg.SerializeToArray(buf, size);
935 if (!msg.IsInitialized())
936 {
937 _LOG_DEBUG("Errrrr.. msg was not probally initialized "<<__FILE__ <<":"<<__LINE__<<". what is happening?");
938 abort();
939 }
940 m_sock->publishData(m_user.getPrefix().toStdString(), m_session, buf, size, FRESHNESS);
941
942 delete buf;
943
944 m_lastMsgTime = time(NULL);
945
946 int nextSequence = m_sock->getNextSeq(m_user.getPrefix().toStdString(), m_session);
947 Sync::MissingDataInfo mdi = {m_user.getPrefix().toStdString(), Sync::SeqNo(0), Sync::SeqNo(nextSequence - 1)};
948 std::vector<Sync::MissingDataInfo> v;
949 v.push_back(mdi);
950 {
951 boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
952 m_scene->processUpdate(v, m_sock->getRootDigest().c_str());
953 m_scene->msgReceived(m_user.getPrefix(), m_user.getNick());
954 }
955}
956
957void
958ChatDialog::openInviteListDialog()
959{
960 m_inviteListDialog->setInviteLabel(m_chatroomPrefix.toUri());
961 m_inviteListDialog->show();
962}
963
964void
965ChatDialog::sendInvitationWrapper(QString invitee, bool isIntroducer)
966{
967 ndn::Name inviteeNamespace(invitee.toUtf8().constData());
968 ndn::Ptr<ContactItem> inviteeItem = m_contactManager->getContact(inviteeNamespace);
969 sendInvitation(inviteeItem, isIntroducer);
970}
971
972
Yingdi Yu04842232013-10-23 14:03:09 -0700973#if WAF
974#include "chatdialog.moc"
975#include "chatdialog.cpp.moc"
976#endif