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 | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 8 | * Author: Yingdi Yu <yingdi@cs.ucla.edu> |
Alexander Afanasyev | b4b9229 | 2013-07-09 13:54:59 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 11 | #include "chatdialog.h" |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 12 | #include "ui_chatdialog.h" |
Alexander Afanasyev | f829f4d | 2013-05-07 15:59:36 -0700 | [diff] [blame] | 13 | |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame^] | 14 | #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 Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 22 | using namespace std; |
| 23 | using namespace ndn; |
Alexander Afanasyev | f829f4d | 2013-05-07 15:59:36 -0700 | [diff] [blame] | 24 | |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame^] | 25 | INIT_LOGGER("ChatDialog"); |
| 26 | |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 27 | ChatDialog::ChatDialog(const Name& chatroomPrefix, |
| 28 | const Name& localPrefix, |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame^] | 29 | const Name& defaultIdentity, |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 30 | QWidget *parent) |
| 31 | : QDialog(parent) |
| 32 | , m_chatroomPrefix(chatroomPrefix) |
| 33 | , m_localPrefix(localPrefix) |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame^] | 34 | , m_defaultIdentity(defaultIdentity) |
| 35 | , m_policyManager(Ptr<ChatroomPolicyManager>(new ChatroomPolicyManager)) |
Yingdi Yu | 5b98913 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 36 | , ui(new Ui::ChatDialog) |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 37 | { |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame^] | 38 | ui->setupUi(this); |
| 39 | |
| 40 | setWrapper(); |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 43 | ChatDialog::~ChatDialog() |
| 44 | { |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame^] | 45 | delete ui; |
| 46 | m_handler->shutdown(); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Zhenkai Zhu | 86df741 | 2012-09-27 16:30:20 -0700 | [diff] [blame] | 49 | void |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame^] | 50 | ChatDialog::setWrapper() |
Zhenkai Zhu | cf02444 | 2012-10-05 10:33:08 -0700 | [diff] [blame] | 51 | { |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame^] | 52 | 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 Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 59 | } |
Alexander Afanasyev | b4b9229 | 2013-07-09 13:54:59 -0700 | [diff] [blame] | 60 | |
Yingdi Yu | eda39aa | 2013-10-23 23:07:29 -0700 | [diff] [blame^] | 61 | void |
| 62 | ChatDialog::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 | |
| 103 | void |
| 104 | ChatDialog::invitationRejected(const Name& identity) |
| 105 | { |
| 106 | _LOG_DEBUG(" " << identity.toUri() << " rejected your invitation!"); |
| 107 | } |
| 108 | |
| 109 | void |
| 110 | ChatDialog::invitationAccepted(const Name& identity) |
| 111 | { |
| 112 | _LOG_DEBUG(" " << identity.toUri() << " accepted your invitation!"); |
| 113 | } |
| 114 | |
| 115 | void |
| 116 | ChatDialog::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 | |
| 125 | void |
| 126 | ChatDialog::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 | |
| 146 | void |
| 147 | ChatDialog::onUnverified(Ptr<Data> data) |
| 148 | {} |
| 149 | |
Alexander Afanasyev | b4b9229 | 2013-07-09 13:54:59 -0700 | [diff] [blame] | 150 | #if WAF |
| 151 | #include "chatdialog.moc" |
| 152 | #include "chatdialog.cpp.moc" |
| 153 | #endif |