blob: 5ef79833fdcb7045e8384baa7e54f4f4af1d473a [file] [log] [blame]
Yingdi Yu0b0a7362014-08-05 16:31:30 -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>
Qiuhan Ding0cfc1512015-02-17 17:44:11 -08009 * Qiuhan Ding <qiuhanding@cs.ucla.edu>
Yingdi Yu0b0a7362014-08-05 16:31:30 -070010 */
11
Yingdi Yueb692ac2015-02-10 18:46:18 -080012#ifndef CHRONOCHAT_CONTACT_MANAGER_HPP
13#define CHRONOCHAT_CONTACT_MANAGER_HPP
Yingdi Yu0b0a7362014-08-05 16:31:30 -070014
15#include <QObject>
16
17#ifndef Q_MOC_RUN
18#include "common.hpp"
19#include "contact-storage.hpp"
20#include "endorse-certificate.hpp"
21#include "profile.hpp"
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080022#include "endorse-info.hpp"
23#include "endorse-collection.hpp"
Yingdi Yu0b0a7362014-08-05 16:31:30 -070024#include <ndn-cxx/security/key-chain.hpp>
25#include <ndn-cxx/security/validator.hpp>
26#include <boost/thread/locks.hpp>
27#include <boost/thread/recursive_mutex.hpp>
28#endif
29
Yingdi Yueb692ac2015-02-10 18:46:18 -080030namespace chronochat {
Yingdi Yu0b0a7362014-08-05 16:31:30 -070031
32typedef function<void(const Interest&)> TimeoutNotify;
33typedef std::vector<shared_ptr<Contact> > ContactList;
34
35class ContactManager : public QObject
36{
37 Q_OBJECT
38
39public:
Yingdi Yu2c9e7712014-10-20 11:55:05 -070040 ContactManager(ndn::Face& m_face, QObject* parent = 0);
Yingdi Yu0b0a7362014-08-05 16:31:30 -070041
42 ~ContactManager();
43
44 shared_ptr<Contact>
45 getContact(const Name& identity)
46 {
47 return m_contactStorage->getContact(identity);
48 }
49
50 void
51 getContactList(ContactList& contactList)
52 {
53 contactList.clear();
54 contactList.insert(contactList.end(), m_contactList.begin(), m_contactList.end());
55 }
56private:
57 shared_ptr<ndn::IdentityCertificate>
58 loadTrustAnchor();
59
60 void
61 initializeSecurity();
62
63 void
64 fetchCollectEndorse(const Name& identity);
65
66 void
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080067 fetchEndorseCertificateInternal(const Name& identity, size_t certIndex);
Yingdi Yu0b0a7362014-08-05 16:31:30 -070068
69 void
70 prepareEndorseInfo(const Name& identity);
71
72 // PROFILE: self-endorse-certificate
73 void
74 onDnsSelfEndorseCertValidated(const shared_ptr<const Data>& selfEndorseCertificate,
75 const Name& identity);
76
77 void
78 onDnsSelfEndorseCertValidationFailed(const shared_ptr<const Data>& selfEndorseCertificate,
79 const std::string& failInfo,
80 const Name& identity);
81
82 void
83 onDnsSelfEndorseCertTimeoutNotify(const Interest& interest, const Name& identity);
84
85 // ENDORSED: endorse-collection
86 void
87 onDnsCollectEndorseValidated(const shared_ptr<const Data>& data, const Name& identity);
88
89 void
90 onDnsCollectEndorseValidationFailed(const shared_ptr<const Data>& data,
91 const std::string& failInfo,
92 const Name& identity);
93
94 void
95 onDnsCollectEndorseTimeoutNotify(const Interest& interest,
96 const Name& identity);
97
98 // PROFILE-CERT: endorse-certificate
99 void
100 onEndorseCertificateInternal(const Interest& interest, Data& data,
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800101 const Name& identity, size_t certIndex,
102 std::string hash);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700103
104 void
105 onEndorseCertificateInternalTimeout(const Interest& interest,
106 const Name& identity,
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800107 size_t certIndex);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700108
109 // Collect endorsement
110 void
111 collectEndorsement();
112
113 void
114 onDnsEndorseeValidated(const shared_ptr<const Data>& data);
115
116 void
117 onDnsEndorseeValidationFailed(const shared_ptr<const Data>& data,
118 const std::string& failInfo);
119
120 void
121 onDnsEndorseeTimeoutNotify(const Interest& interest);
122
123 void
124 decreaseCollectStatus();
125
126 void
127 publishCollectEndorsedDataInDNS();
128
129 // Identity certificate
130 void
131 onIdentityCertValidated(const shared_ptr<const Data>& data);
132
133 void
134 onIdentityCertValidationFailed(const shared_ptr<const Data>& data, const std::string& failInfo);
135
136 void
137 onIdentityCertTimeoutNotify(const Interest& interest);
138
139 void
140 decreaseIdCertCount();
141
142 // Publish self-endorse certificate
143 shared_ptr<EndorseCertificate>
144 getSignedSelfEndorseCertificate(const Profile& profile);
145
146 void
147 publishSelfEndorseCertificateInDNS(const EndorseCertificate& selfEndorseCertificate);
148
149 // Publish endorse certificate
150 shared_ptr<EndorseCertificate>
151 generateEndorseCertificate(const Name& identity);
152
153 void
154 publishEndorseCertificateInDNS(const EndorseCertificate& endorseCertificate);
155
156 // Communication
157 void
158 sendInterest(const Interest& interest,
159 const ndn::OnDataValidated& onValidated,
160 const ndn::OnDataValidationFailed& onValidationFailed,
161 const TimeoutNotify& timeoutNotify,
162 int retry = 1);
163
164 void
165 onTargetData(const Interest& interest,
166 const Data& data,
167 const ndn::OnDataValidated& onValidated,
168 const ndn::OnDataValidationFailed& onValidationFailed);
169
170 void
171 onTargetTimeout(const Interest& interest,
172 int retry,
173 const ndn::OnDataValidated& onValidated,
174 const ndn::OnDataValidationFailed& onValidationFailed,
175 const TimeoutNotify& timeoutNotify);
176
177 // DNS listener
178 void
179 onDnsInterest(const Name& prefix, const Interest& interest);
180
181 void
182 onDnsRegisterFailed(const Name& prefix, const std::string& failInfo);
183
184signals:
185 void
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800186 contactEndorseInfoReady(const EndorseInfo& endorseInfo);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700187
188 void
189 contactInfoFetchFailed(const QString& identity);
190
191 void
192 idCertNameListReady(const QStringList& certNameList);
193
194 void
195 nameListReady(const QStringList& certNameList);
196
197 void
198 idCertReady(const ndn::IdentityCertificate& idCert);
199
200 void
201 contactAliasListReady(const QStringList& aliasList);
202
203 void
204 contactIdListReady(const QStringList& idList);
205
206 void
207 contactInfoReady(const QString& identity,
208 const QString& name,
209 const QString& institute,
210 bool isIntro);
211
212 void
213 warning(const QString& msg);
214
215public slots:
216 void
217 onIdentityUpdated(const QString& identity);
218
219 void
220 onFetchContactInfo(const QString& identity);
221
222 void
223 onAddFetchedContact(const QString& identity);
224
225 void
226 onUpdateProfile();
227
228 void
229 onRefreshBrowseContact();
230
231 void
232 onFetchIdCert(const QString& certName);
233
234 void
235 onAddFetchedContactIdCert(const QString& identity);
236
237 void
238 onWaitForContactList();
239
240 void
241 onWaitForContactInfo(const QString& identity);
242
243 void
244 onRemoveContact(const QString& identity);
245
246 void
247 onUpdateAlias(const QString& identity, const QString& alias);
248
249 void
250 onUpdateIsIntroducer(const QString& identity, bool isIntro);
251
252 void
253 onUpdateEndorseCertificate(const QString& identity);
254
255private:
256
257 class FetchedInfo {
258 public:
259 shared_ptr<EndorseCertificate> m_selfEndorseCert;
260 shared_ptr<EndorseCollection> m_endorseCollection;
261 std::vector<shared_ptr<EndorseCertificate> > m_endorseCertList;
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800262 shared_ptr<EndorseInfo> m_endorseInfo;
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700263 };
264
265 typedef std::map<Name, FetchedInfo> BufferedContacts;
266 typedef std::map<Name, shared_ptr<ndn::IdentityCertificate> > BufferedIdCerts;
267
268 typedef boost::recursive_mutex RecLock;
269 typedef boost::unique_lock<RecLock> UniqueRecLock;
270
271 // Conf
272 shared_ptr<ContactStorage> m_contactStorage;
273 shared_ptr<ndn::Validator> m_validator;
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700274 ndn::Face& m_face;
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700275 ndn::KeyChain m_keyChain;
276 Name m_identity;
277 ContactList m_contactList;
278
279 // Buffer
280 BufferedContacts m_bufferedContacts;
281 BufferedIdCerts m_bufferedIdCerts;
282
283 // Tmp Dns
284 const ndn::RegisteredPrefixId* m_dnsListenerId;
285
286 RecLock m_collectCountMutex;
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800287 size_t m_collectCount;
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700288
289 RecLock m_idCertCountMutex;
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800290 size_t m_idCertCount;
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700291};
292
Yingdi Yueb692ac2015-02-10 18:46:18 -0800293} // namespace chronochat
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700294
Yingdi Yueb692ac2015-02-10 18:46:18 -0800295#endif // CHRONOCHAT_CONTACT_MANAGER_HPP