blob: 5f1d3eb92e34bb547abbfc1f38d3eaa2c12d2dc9 [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 *
8 * Author: Yingdi Yu <yingdi@cs.ucla.edu>
9 */
10
11#include "chatdialog.h"
12#include "ui_chatdialog.h"
13
Yingdi Yuc4d08d22013-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 Yu04842232013-10-23 14:03:09 -070022using namespace std;
23using namespace ndn;
24
Yingdi Yuc4d08d22013-10-23 23:07:29 -070025INIT_LOGGER("ChatDialog");
26
Yingdi Yu04842232013-10-23 14:03:09 -070027ChatDialog::ChatDialog(const Name& chatroomPrefix,
28 const Name& localPrefix,
Yingdi Yuc4d08d22013-10-23 23:07:29 -070029 const Name& defaultIdentity,
Yingdi Yu04842232013-10-23 14:03:09 -070030 QWidget *parent)
31 : QDialog(parent)
32 , m_chatroomPrefix(chatroomPrefix)
33 , m_localPrefix(localPrefix)
Yingdi Yuc4d08d22013-10-23 23:07:29 -070034 , m_defaultIdentity(defaultIdentity)
35 , m_policyManager(Ptr<ChatroomPolicyManager>(new ChatroomPolicyManager))
Yingdi Yu04842232013-10-23 14:03:09 -070036 , ui(new Ui::ChatDialog)
37{
Yingdi Yuc4d08d22013-10-23 23:07:29 -070038 ui->setupUi(this);
39
40 setWrapper();
Yingdi Yu04842232013-10-23 14:03:09 -070041}
42
43ChatDialog::~ChatDialog()
44{
Yingdi Yuc4d08d22013-10-23 23:07:29 -070045 delete ui;
46 m_handler->shutdown();
Yingdi Yu04842232013-10-23 14:03:09 -070047}
48
49void
Yingdi Yuc4d08d22013-10-23 23:07:29 -070050ChatDialog::setWrapper()
Yingdi Yu04842232013-10-23 14:03:09 -070051{
Yingdi Yuc4d08d22013-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));
Yingdi Yu04842232013-10-23 14:03:09 -070059}
60
Yingdi Yuc4d08d22013-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
Yingdi Yu04842232013-10-23 14:03:09 -0700150#if WAF
151#include "chatdialog.moc"
152#include "chatdialog.cpp.moc"
153#endif