blob: fa28a9ec359e1dc90aaa5e7a4e43eec94fbd44f2 [file] [log] [blame]
Yingdi Yu9236c432013-10-18 11:29:25 -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
Yingdi Yufa4ce792014-02-06 18:09:22 -080011#ifndef CHRONOS_CONTACT_MANAGER_H
12#define CHRONOS_CONTACT_MANAGER_H
Yingdi Yu9236c432013-10-18 11:29:25 -070013
Yingdi Yu4685b1b2013-10-18 17:05:02 -070014#include <QObject>
Yingdi Yu9236c432013-10-18 11:29:25 -070015
Yingdi Yu4685b1b2013-10-18 17:05:02 -070016#ifndef Q_MOC_RUN
17#include "contact-storage.h"
Yingdi Yu4685b1b2013-10-18 17:05:02 -070018#include "endorse-certificate.h"
Yingdi Yu2ac40fb2013-10-21 13:38:38 -070019#include "profile.h"
Yingdi Yu348f5ea2014-03-01 14:47:25 -080020#include "endorse-info.pb.h"
21#include "endorse-collection.pb.h"
Yingdi Yub6fb0302014-01-21 11:05:11 -080022#include <ndn-cpp-dev/face.hpp>
23#include <ndn-cpp-dev/security/key-chain.hpp>
Yingdi Yufa4ce792014-02-06 18:09:22 -080024#include <ndn-cpp-dev/security/validator.hpp>
Yingdi Yu348f5ea2014-03-01 14:47:25 -080025#include <boost/thread/locks.hpp>
26#include <boost/thread/recursive_mutex.hpp>
Yingdi Yu4685b1b2013-10-18 17:05:02 -070027#endif
28
Yingdi Yufa4ce792014-02-06 18:09:22 -080029namespace chronos{
30
Yingdi Yu348f5ea2014-03-01 14:47:25 -080031typedef ndn::function<void(const ndn::Interest&)> TimeoutNotify;
32typedef std::vector<ndn::shared_ptr<Contact> > ContactList;
Yingdi Yu4685b1b2013-10-18 17:05:02 -070033
34class ContactManager : public QObject
Yingdi Yu9236c432013-10-18 11:29:25 -070035{
Yingdi Yu4685b1b2013-10-18 17:05:02 -070036 Q_OBJECT
37
Yingdi Yu9236c432013-10-18 11:29:25 -070038public:
Yingdi Yufa4ce792014-02-06 18:09:22 -080039 ContactManager(ndn::shared_ptr<ndn::Face> m_face,
Yingdi Yu76dd8002013-12-24 11:16:32 +080040 QObject* parent = 0);
Yingdi Yu9236c432013-10-18 11:29:25 -070041
42 ~ContactManager();
43
Yingdi Yu348f5ea2014-03-01 14:47:25 -080044 ndn::shared_ptr<Contact>
45 getContact(const ndn::Name& identity)
46 {
47 return m_contactStorage->getContact(identity);
48 }
Yingdi Yu4685b1b2013-10-18 17:05:02 -070049
50 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -080051 getContactList(ContactList& contactList)
52 {
53 contactList.clear();
54 contactList.insert(contactList.end(), m_contactList.begin(), m_contactList.end());
55 }
Yingdi Yu06f572b2014-03-11 15:46:10 -070056private:
57 ndn::shared_ptr<ndn::IdentityCertificate>
58 loadTrustAnchor();
59
Yingdi Yu348f5ea2014-03-01 14:47:25 -080060 void
61 initializeSecurity();
Yingdi Yub2e747d2013-11-05 23:06:43 -080062
63 void
64 fetchCollectEndorse(const ndn::Name& identity);
65
66 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -080067 fetchEndorseCertificateInternal(const ndn::Name& identity, int certIndex);
Yingdi Yuae8217c2013-11-09 00:03:26 -080068
69 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -080070 prepareEndorseInfo(const ndn::Name& identity);
71
72 // PROFILE: self-endorse-certificate
73 void
74 onDnsSelfEndorseCertValidated(const ndn::shared_ptr<const ndn::Data>& selfEndorseCertificate,
75 const ndn::Name& identity);
Yingdi Yu4685b1b2013-10-18 17:05:02 -070076
Yingdi Yub2e747d2013-11-05 23:06:43 -080077 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -080078 onDnsSelfEndorseCertValidationFailed(const ndn::shared_ptr<const ndn::Data>& selfEndorseCertificate,
79 const ndn::Name& identity);
Yingdi Yu9236c432013-10-18 11:29:25 -070080
Yingdi Yub2e747d2013-11-05 23:06:43 -080081 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -080082 onDnsSelfEndorseCertTimeoutNotify(const ndn::Interest& interest,
83 const ndn::Name& identity);
84
85 // ENDORSED: endorse-collection
86 void
87 onDnsCollectEndorseValidated(const ndn::shared_ptr<const ndn::Data>& data,
88 const ndn::Name& identity);
Yingdi Yub2e747d2013-11-05 23:06:43 -080089
Yingdi Yu76dd8002013-12-24 11:16:32 +080090 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -080091 onDnsCollectEndorseValidationFailed(const ndn::shared_ptr<const ndn::Data>& data,
92 const ndn::Name& identity);
Yingdi Yuae8217c2013-11-09 00:03:26 -080093
94 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -080095 onDnsCollectEndorseTimeoutNotify(const ndn::Interest& interest,
96 const ndn::Name& identity);
97
98 // PROFILE-CERT: endorse-certificate
99 void
100 onEndorseCertificateInternal(const ndn::Interest& interest,
101 ndn::Data& data,
102 const ndn::Name& identity,
103 int certIndex,
104 std::string hash);
Yingdi Yuae8217c2013-11-09 00:03:26 -0800105
Yingdi Yu72232692013-11-12 17:50:21 -0800106 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800107 onEndorseCertificateInternalTimeout(const ndn::Interest& interest,
108 const ndn::Name& identity,
109 int certIndex);
110
111 // Collect endorsement
112 void
113 collectEndorsement();
114
115 void
116 onDnsEndorseeValidated(const ndn::shared_ptr<const ndn::Data>& data);
117
118 void
119 onDnsEndorseeValidationFailed(const ndn::shared_ptr<const ndn::Data>& data);
120
121 void
122 onDnsEndorseeTimeoutNotify(const ndn::Interest& interest);
123
124 void
125 decreaseCollectStatus();
126
127 void
128 publishCollectEndorsedDataInDNS();
129
130 // Identity certificate
131 void
132 onIdentityCertValidated(const ndn::shared_ptr<const ndn::Data>& data);
Yingdi Yu72232692013-11-12 17:50:21 -0800133
Yingdi Yu76dd8002013-12-24 11:16:32 +0800134 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800135 onIdentityCertValidationFailed(const ndn::shared_ptr<const ndn::Data>& data);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800136
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800137 void
138 onIdentityCertTimeoutNotify(const ndn::Interest& interest);
139
140 void
141 decreaseIdCertCount();
142
143 // Publish self-endorse certificate
Yingdi Yufa4ce792014-02-06 18:09:22 -0800144 ndn::shared_ptr<EndorseCertificate>
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800145 getSignedSelfEndorseCertificate(const Profile& profile);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800146
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700147 void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800148 publishSelfEndorseCertificateInDNS(const EndorseCertificate& selfEndorseCertificate);
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700149
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800150 // Publish endorse certificate
151 ndn::shared_ptr<EndorseCertificate>
152 generateEndorseCertificate(const ndn::Name& identity);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800153
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800154 void
155 publishEndorseCertificateInDNS(const EndorseCertificate& endorseCertificate);
156
157 // Communication
158 void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800159 sendInterest(const ndn::Interest& interest,
Yingdi Yufa4ce792014-02-06 18:09:22 -0800160 const ndn::OnDataValidated& onValidated,
161 const ndn::OnDataValidationFailed& onValidationFailed,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800162 const TimeoutNotify& timeoutNotify,
Yingdi Yu68de61d2014-01-16 19:13:03 -0800163 int retry = 1);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800164
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800165 void
Yingdi Yufa4ce792014-02-06 18:09:22 -0800166 onTargetData(const ndn::Interest& interest,
167 const ndn::Data& data,
168 const ndn::OnDataValidated& onValidated,
169 const ndn::OnDataValidationFailed& onValidationFailed);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800170
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800171 void
Yingdi Yufa4ce792014-02-06 18:09:22 -0800172 onTargetTimeout(const ndn::Interest& interest,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800173 int retry,
Yingdi Yufa4ce792014-02-06 18:09:22 -0800174 const ndn::OnDataValidated& onValidated,
175 const ndn::OnDataValidationFailed& onValidationFailed,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800176 const TimeoutNotify& timeoutNotify);
177
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800178 // DNS listener
Yingdi Yub2e747d2013-11-05 23:06:43 -0800179 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800180 onDnsInterest(const ndn::Name& prefix, const ndn::Interest& interest);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800181
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800182 void
183 onDnsRegisterFailed(const ndn::Name& prefix, const std::string& failInfo);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800184
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700185signals:
Yingdi Yu8e135832013-11-09 20:12:31 -0800186 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800187 contactEndorseInfoReady(const chronos::EndorseInfo& endorseInfo);
188
189 void
190 contactInfoFetchFailed(const QString& identity);
191
192 void
193 idCertNameListReady(const QStringList& certNameList);
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700194
195 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800196 nameListReady(const QStringList& certNameList);
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700197
Yingdi Yub2e747d2013-11-05 23:06:43 -0800198 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800199 idCertReady(const ndn::IdentityCertificate& idCert);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800200
201 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800202 contactAliasListReady(const QStringList& aliasList);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800203
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800204 void
205 contactIdListReady(const QStringList& idList);
206
207 void
208 contactInfoReady(const QString& identity,
209 const QString& name,
210 const QString& institute,
211 bool isIntro);
212
213 void
214 warning(const QString& msg);
215
216public slots:
217 void
218 onIdentityUpdated(const QString& identity);
219
220 void
221 onFetchContactInfo(const QString& identity);
Yingdi Yuae8217c2013-11-09 00:03:26 -0800222
223 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800224 onAddFetchedContact(const QString& identity);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800225
226 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800227 onUpdateProfile();
Yingdi Yub2e747d2013-11-05 23:06:43 -0800228
Yingdi Yuae8217c2013-11-09 00:03:26 -0800229 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800230 onRefreshBrowseContact();
Yingdi Yuae8217c2013-11-09 00:03:26 -0800231
Yingdi Yu72232692013-11-12 17:50:21 -0800232 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800233 onFetchIdCert(const QString& certName);
Yingdi Yu72232692013-11-12 17:50:21 -0800234
235 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800236 onAddFetchedContactIdCert(const QString& identity);
Yingdi Yu72232692013-11-12 17:50:21 -0800237
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800238 void
239 onWaitForContactList();
240
241 void
242 onWaitForContactInfo(const QString& identity);
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700243
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800244 void
245 onRemoveContact(const QString& identity);
246
247 void
248 onUpdateAlias(const QString& identity, const QString& alias);
249
250 void
251 onUpdateIsIntroducer(const QString& identity, bool isIntro);
252
253 void
254 onUpdateEndorseCertificate(const QString& identity);
255
Yingdi Yu9236c432013-10-18 11:29:25 -0700256private:
Yingdi Yu93adb1a2014-01-16 10:30:26 -0800257
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800258 class FetchedInfo {
259 public:
260 ndn::shared_ptr<EndorseCertificate> m_selfEndorseCert;
261 ndn::shared_ptr<EndorseCollection> m_endorseCollection;
262 std::vector<ndn::shared_ptr<EndorseCertificate> > m_endorseCertList;
263 ndn::shared_ptr<EndorseInfo> m_endorseInfo;
264 };
265
266 typedef std::map<ndn::Name, FetchedInfo> BufferedContacts;
267 typedef std::map<ndn::Name, ndn::shared_ptr<ndn::IdentityCertificate> > BufferedIdCerts;
268
269 typedef boost::recursive_mutex RecLock;
270 typedef boost::unique_lock<RecLock> UniqueRecLock;
271
272 // Conf
Yingdi Yufa4ce792014-02-06 18:09:22 -0800273 ndn::shared_ptr<ContactStorage> m_contactStorage;
Yingdi Yufa4ce792014-02-06 18:09:22 -0800274 ndn::shared_ptr<ndn::Validator> m_validator;
275 ndn::shared_ptr<ndn::Face> m_face;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800276 ndn::KeyChain m_keyChain;
277 ndn::Name m_identity;
278 ContactList m_contactList;
279
280 // Buffer
281 BufferedContacts m_bufferedContacts;
282 BufferedIdCerts m_bufferedIdCerts;
283
284 // Tmp Dns
285 const ndn::RegisteredPrefixId* m_dnsListenerId;
286
287 RecLock m_collectCountMutex;
288 int m_collectCount;
289
290 RecLock m_idCertCountMutex;
291 int m_idCertCount;
Yingdi Yu9236c432013-10-18 11:29:25 -0700292};
293
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800294} // namespace chronos
Yingdi Yufa4ce792014-02-06 18:09:22 -0800295
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800296#endif //CHRONOS_CONTACT_MANAGER_H