Yingdi Yu | 0484223 | 2013-10-23 14:03:09 -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 |
| 4 | * Yingdi Yu |
| 5 | * |
| 6 | * BSD license, See the LICENSE file for more information |
| 7 | * |
| 8 | * Author: Yingdi Yu <yingdi@cs.ucla.edu> |
| 9 | */ |
| 10 | |
| 11 | #include "chatdialog.h" |
| 12 | #include "ui_chatdialog.h" |
| 13 | |
Yingdi Yu | c4d08d2 | 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 | 0484223 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 22 | using namespace std; |
| 23 | using namespace ndn; |
| 24 | |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 25 | INIT_LOGGER("ChatDialog"); |
| 26 | |
Yingdi Yu | 0484223 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 27 | ChatDialog::ChatDialog(const Name& chatroomPrefix, |
| 28 | const Name& localPrefix, |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 29 | const Name& defaultIdentity, |
Yingdi Yu | 0484223 | 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 | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 34 | , m_defaultIdentity(defaultIdentity) |
| 35 | , m_policyManager(Ptr<ChatroomPolicyManager>(new ChatroomPolicyManager)) |
Yingdi Yu | 0484223 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 36 | , ui(new Ui::ChatDialog) |
| 37 | { |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 38 | ui->setupUi(this); |
| 39 | |
| 40 | setWrapper(); |
Yingdi Yu | 0484223 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | ChatDialog::~ChatDialog() |
| 44 | { |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 45 | delete ui; |
| 46 | m_handler->shutdown(); |
Yingdi Yu | 0484223 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void |
Yingdi Yu | c4d08d2 | 2013-10-23 23:07:29 -0700 | [diff] [blame] | 50 | ChatDialog::setWrapper() |
Yingdi Yu | 0484223 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 51 | { |
Yingdi Yu | c4d08d2 | 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)); |
Yingdi Yu | 0484223 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Yingdi Yu | c4d08d2 | 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 | |
Yingdi Yu | 0484223 | 2013-10-23 14:03:09 -0700 | [diff] [blame] | 150 | #if WAF |
| 151 | #include "chatdialog.moc" |
| 152 | #include "chatdialog.cpp.moc" |
| 153 | #endif |