blob: 5f1d3eb92e34bb547abbfc1f38d3eaa2c12d2dc9 [file] [log] [blame]
Alexander Afanasyevb4b92292013-07-09 13:54:59 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013, Regents of the University of California
Yingdi Yu5b989132013-10-23 14:03:09 -07004 * Yingdi Yu
Alexander Afanasyevb4b92292013-07-09 13:54:59 -07005 *
Yingdi Yu5b989132013-10-23 14:03:09 -07006 * BSD license, See the LICENSE file for more information
Alexander Afanasyevb4b92292013-07-09 13:54:59 -07007 *
Yingdi Yu5b989132013-10-23 14:03:09 -07008 * Author: Yingdi Yu <yingdi@cs.ucla.edu>
Alexander Afanasyevb4b92292013-07-09 13:54:59 -07009 */
10
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -070011#include "chatdialog.h"
Yingdi Yu5b989132013-10-23 14:03:09 -070012#include "ui_chatdialog.h"
Alexander Afanasyevf829f4d2013-05-07 15:59:36 -070013
Yingdi Yueda39aa2013-10-23 23:07:29 -070014#ifndef Q_MOC_RUN
15#include <ndn.cxx/security/identity/identity-manager.h>
16#include <ndn.cxx/security/identity/basic-identity-storage.h>
17#include <ndn.cxx/security/identity/osx-privatekey-storage.h>
18#include <ndn.cxx/security/encryption/basic-encryption-manager.h>
19#include "logging.h"
20#endif
21
Yingdi Yu5b989132013-10-23 14:03:09 -070022using namespace std;
23using namespace ndn;
Alexander Afanasyevf829f4d2013-05-07 15:59:36 -070024
Yingdi Yueda39aa2013-10-23 23:07:29 -070025INIT_LOGGER("ChatDialog");
26
Yingdi Yu5b989132013-10-23 14:03:09 -070027ChatDialog::ChatDialog(const Name& chatroomPrefix,
28 const Name& localPrefix,
Yingdi Yueda39aa2013-10-23 23:07:29 -070029 const Name& defaultIdentity,
Yingdi Yu5b989132013-10-23 14:03:09 -070030 QWidget *parent)
31 : QDialog(parent)
32 , m_chatroomPrefix(chatroomPrefix)
33 , m_localPrefix(localPrefix)
Yingdi Yueda39aa2013-10-23 23:07:29 -070034 , m_defaultIdentity(defaultIdentity)
35 , m_policyManager(Ptr<ChatroomPolicyManager>(new ChatroomPolicyManager))
Yingdi Yu5b989132013-10-23 14:03:09 -070036 , ui(new Ui::ChatDialog)
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -070037{
Yingdi Yueda39aa2013-10-23 23:07:29 -070038 ui->setupUi(this);
39
40 setWrapper();
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -070041}
42
Zhenkai Zhu82a62752012-06-04 17:11:04 -070043ChatDialog::~ChatDialog()
44{
Yingdi Yueda39aa2013-10-23 23:07:29 -070045 delete ui;
46 m_handler->shutdown();
Zhenkai Zhu82a62752012-06-04 17:11:04 -070047}
48
Zhenkai Zhu86df7412012-09-27 16:30:20 -070049void
Yingdi Yueda39aa2013-10-23 23:07:29 -070050ChatDialog::setWrapper()
Zhenkai Zhucf024442012-10-05 10:33:08 -070051{
Yingdi Yueda39aa2013-10-23 23:07:29 -070052 Ptr<security::OSXPrivatekeyStorage> privateStorage = Ptr<security::OSXPrivatekeyStorage>::Create();
53 m_identityManager = Ptr<security::IdentityManager>(new security::IdentityManager(Ptr<security::BasicIdentityStorage>::Create(), privateStorage));
54 Ptr<security::EncryptionManager> encryptionManager = Ptr<security::EncryptionManager>(new security::BasicEncryptionManager(privateStorage, "/tmp/encryption.db"));
55
56 m_keychain = Ptr<security::Keychain>(new security::Keychain(m_identityManager, m_policyManager, encryptionManager));
57
58 m_handler = Ptr<Wrapper>(new Wrapper(m_keychain));
Zhenkai Zhud13acd02012-06-04 15:25:20 -070059}
Alexander Afanasyevb4b92292013-07-09 13:54:59 -070060
Yingdi Yueda39aa2013-10-23 23:07:29 -070061void
62ChatDialog::sendInvitation(Ptr<ContactItem> contact)
63{
64 m_policyManager->addTrustAnchor(contact->getSelfEndorseCertificate());
65
66 Name certificateName = m_identityManager->getDefaultCertificateNameByIdentity(m_defaultIdentity);
67
68 Name interestName("/ndn/broadcast/chronos/invitation");
69 interestName.append(contact->getNameSpace());
70 interestName.append("chatroom");
71 interestName.append(m_chatroomPrefix.get(-1));
72 interestName.append("inviter-prefix");
73 interestName.append(m_localPrefix);
74 interestName.append("inviter");
75 interestName.append(certificateName);
76
77 string signedUri = interestName.toUri();
78 Blob signedBlob(signedUri.c_str(), signedUri.size());
79
80 Ptr<const signature::Sha256WithRsa> sha256sig = DynamicCast<const signature::Sha256WithRsa>(m_identityManager->signByCertificate(signedBlob, certificateName));
81 const Blob& sigBits = sha256sig->getSignatureBits();
82
83 interestName.append(sigBits.buf(), sigBits.size());
84
85 Ptr<Interest> interest = Ptr<Interest>(new Interest(interestName));
86 Ptr<Closure> closure = Ptr<Closure>(new Closure(boost::bind(&ChatDialog::onInviteReplyVerified,
87 this,
88 _1,
89 contact->getNameSpace()),
90 boost::bind(&ChatDialog::onInviteTimeout,
91 this,
92 _1,
93 _2,
94 contact->getNameSpace(),
95 7),
96 boost::bind(&ChatDialog::onUnverified,
97 this,
98 _1)));
99
100 m_handler->sendInterest(interest, closure);
101}
102
103void
104ChatDialog::invitationRejected(const Name& identity)
105{
106 _LOG_DEBUG(" " << identity.toUri() << " rejected your invitation!");
107}
108
109void
110ChatDialog::invitationAccepted(const Name& identity)
111{
112 _LOG_DEBUG(" " << identity.toUri() << " accepted your invitation!");
113}
114
115void
116ChatDialog::onInviteReplyVerified(Ptr<Data> data, const Name& identity)
117{
118 string content(data->content().buf(), data->content().size());
119 if(content.empty())
120 invitationRejected(identity);
121 else
122 invitationAccepted(identity);
123}
124
125void
126ChatDialog::onInviteTimeout(Ptr<Closure> closure, Ptr<Interest> interest, const Name& identity, int retry)
127{
128 if(retry > 0)
129 {
130 Ptr<Closure> newClosure = Ptr<Closure>(new Closure(closure->m_dataCallback,
131 boost::bind(&ChatDialog::onInviteTimeout,
132 this,
133 _1,
134 _2,
135 identity,
136 retry - 1),
137 closure->m_unverifiedCallback,
138 closure->m_stepCount)
139 );
140 m_handler->sendInterest(interest, newClosure);
141 }
142 else
143 invitationRejected(identity);
144}
145
146void
147ChatDialog::onUnverified(Ptr<Data> data)
148{}
149
Alexander Afanasyevb4b92292013-07-09 13:54:59 -0700150#if WAF
151#include "chatdialog.moc"
152#include "chatdialog.cpp.moc"
153#endif