blob: c61a72020397cab00a35aa4a905fafecd69ccdb2 [file] [log] [blame]
Yingdi Yu0b82a4e2013-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 Yua1a688f2014-02-06 18:09:22 -080011#ifndef CHRONOS_CONTACT_MANAGER_H
12#define CHRONOS_CONTACT_MANAGER_H
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070013
Yingdi Yuaa8d7692013-10-18 17:05:02 -070014#include <QObject>
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070015
Yingdi Yuaa8d7692013-10-18 17:05:02 -070016#ifndef Q_MOC_RUN
17#include "contact-storage.h"
18#include "dns-storage.h"
19#include "endorse-certificate.h"
Yingdi Yu79c25a22013-10-21 13:38:38 -070020#include "profile.h"
Yingdi Yu6df61252014-01-21 11:05:11 -080021#include <ndn-cpp-dev/face.hpp>
22#include <ndn-cpp-dev/security/key-chain.hpp>
Yingdi Yua1a688f2014-02-06 18:09:22 -080023#include <ndn-cpp-dev/security/validator.hpp>
Yingdi Yuaa8d7692013-10-18 17:05:02 -070024#endif
25
Yingdi Yua1a688f2014-02-06 18:09:22 -080026namespace chronos{
27
28typedef ndn::function<void()> TimeoutNotify;
Yingdi Yuaa8d7692013-10-18 17:05:02 -070029
30class ContactManager : public QObject
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070031{
Yingdi Yuaa8d7692013-10-18 17:05:02 -070032 Q_OBJECT
33
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070034public:
Yingdi Yua1a688f2014-02-06 18:09:22 -080035 ContactManager(ndn::shared_ptr<ndn::Face> m_face,
Yingdi Yu64206112013-12-24 11:16:32 +080036 QObject* parent = 0);
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070037
38 ~ContactManager();
39
Yingdi Yuaa8d7692013-10-18 17:05:02 -070040 void
41 fetchSelfEndorseCertificate(const ndn::Name& identity);
42
43 void
Yingdi Yu8dacdf22013-11-05 23:06:43 -080044 fetchKey(const ndn::Name& identity);
45
46 void
47 fetchCollectEndorse(const ndn::Name& identity);
48
49 void
Yingdi Yu908f8412013-11-09 00:03:26 -080050 fetchIdCertificate(const ndn::Name& certName);
51
52 void
Yingdi Yuaa8d7692013-10-18 17:05:02 -070053 updateProfileData(const ndn::Name& identity);
54
Yingdi Yu8dacdf22013-11-05 23:06:43 -080055 void
56 updateEndorseCertificate(const ndn::Name& identity, const ndn::Name& signerIdentity);
57
Yingdi Yua1a688f2014-02-06 18:09:22 -080058 inline void
59 getContactItemList(std::vector<ndn::shared_ptr<ContactItem> >& contacts);
Yingdi Yu79c25a22013-10-21 13:38:38 -070060
Yingdi Yua1a688f2014-02-06 18:09:22 -080061 ndn::shared_ptr<ContactStorage>
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070062 getContactStorage()
63 { return m_contactStorage; }
64
Yingdi Yua1a688f2014-02-06 18:09:22 -080065 inline ndn::shared_ptr<ContactItem>
Yingdi Yud40226b2013-10-23 14:05:12 -070066 getContact(const ndn::Name& contactNamespace);
67
Yingdi Yua1a688f2014-02-06 18:09:22 -080068 ndn::shared_ptr<DnsStorage>
Yingdi Yuaa8d7692013-10-18 17:05:02 -070069 getDnsStorage()
70 { return m_dnsStorage; }
71
Yingdi Yu64206112013-12-24 11:16:32 +080072 ndn::Name
Yingdi Yuaa8d7692013-10-18 17:05:02 -070073 getDefaultIdentity()
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -080074 { return m_keyChain->getDefaultIdentity(); }
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070075
Yingdi Yu8dacdf22013-11-05 23:06:43 -080076 void
Yingdi Yua1a688f2014-02-06 18:09:22 -080077 publishCollectEndorsedDataInDNS(const ndn::Name& identity);
Yingdi Yu8dacdf22013-11-05 23:06:43 -080078
Yingdi Yu64206112013-12-24 11:16:32 +080079 void
Yingdi Yu908f8412013-11-09 00:03:26 -080080 setDefaultIdentity(const ndn::Name& identity)
81 { m_defaultIdentity = identity; }
82
83 void
Yingdi Yu64206112013-12-24 11:16:32 +080084 addContact(const ndn::IdentityCertificate& idCert, const Profile& profile);
Yingdi Yu908f8412013-11-09 00:03:26 -080085
Yingdi Yu6ea54e42013-11-12 17:50:21 -080086 void
87 removeContact(const ndn::Name& contactNameSpace);
88
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -080089private:
Yingdi Yu64206112013-12-24 11:16:32 +080090 void
91 initializeSecurity();
92
Yingdi Yua1a688f2014-02-06 18:09:22 -080093 ndn::shared_ptr<EndorseCertificate>
Yingdi Yuaa8d7692013-10-18 17:05:02 -070094 getSignedSelfEndorseCertificate(const ndn::Name& identity, const Profile& profile);
95
Yingdi Yua1a688f2014-02-06 18:09:22 -080096 ndn::shared_ptr<EndorseCertificate>
Yingdi Yu8dacdf22013-11-05 23:06:43 -080097 generateEndorseCertificate(const ndn::Name& identity, const ndn::Name& signerIdentity);
98
Yingdi Yuaa8d7692013-10-18 17:05:02 -070099 void
Yingdi Yu64206112013-12-24 11:16:32 +0800100 publishSelfEndorseCertificateInDNS(const EndorseCertificate& selfEndorseCertificate);
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700101
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800102 void
Yingdi Yu64206112013-12-24 11:16:32 +0800103 publishEndorseCertificateInDNS(const EndorseCertificate& endorseCertificate, const ndn::Name& signerIdentity);
104
Yingdi Yua1a688f2014-02-06 18:09:22 -0800105 inline void
Yingdi Yu64206112013-12-24 11:16:32 +0800106 sendInterest(const ndn::Interest& interest,
Yingdi Yua1a688f2014-02-06 18:09:22 -0800107 const ndn::OnDataValidated& onValidated,
108 const ndn::OnDataValidationFailed& onValidationFailed,
Yingdi Yu64206112013-12-24 11:16:32 +0800109 const TimeoutNotify& timeoutNotify,
Yingdi Yu7630f642014-01-16 19:13:03 -0800110 int retry = 1);
Yingdi Yu64206112013-12-24 11:16:32 +0800111
Yingdi Yua1a688f2014-02-06 18:09:22 -0800112 inline void
113 onTargetData(const ndn::Interest& interest,
114 const ndn::Data& data,
115 const ndn::OnDataValidated& onValidated,
116 const ndn::OnDataValidationFailed& onValidationFailed);
Yingdi Yu64206112013-12-24 11:16:32 +0800117
Yingdi Yua1a688f2014-02-06 18:09:22 -0800118 inline void
119 onTargetTimeout(const ndn::Interest& interest,
Yingdi Yu64206112013-12-24 11:16:32 +0800120 int retry,
Yingdi Yua1a688f2014-02-06 18:09:22 -0800121 const ndn::OnDataValidated& onValidated,
122 const ndn::OnDataValidationFailed& onValidationFailed,
Yingdi Yu64206112013-12-24 11:16:32 +0800123 const TimeoutNotify& timeoutNotify);
124
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700125 void
Yingdi Yua1a688f2014-02-06 18:09:22 -0800126 onDnsSelfEndorseCertValidated(const ndn::shared_ptr<const ndn::Data>& selfEndorseCertificate, const ndn::Name& identity);
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700127
Yingdi Yua1a688f2014-02-06 18:09:22 -0800128 inline void
129 onDnsSelfEndorseCertValidationFailed(const ndn::shared_ptr<const ndn::Data>& selfEndorseCertificate, const ndn::Name& identity);
130
131 inline void
132 onDnsSelfEndorseCertTimeoutNotify(const ndn::Name& identity);
Yingdi Yu64206112013-12-24 11:16:32 +0800133
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700134
Yingdi Yua1a688f2014-02-06 18:09:22 -0800135 inline void
136 onDnsCollectEndorseValidated(const ndn::shared_ptr<const ndn::Data>& data, const ndn::Name& identity);
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700137
Yingdi Yua1a688f2014-02-06 18:09:22 -0800138 inline void
139 onDnsCollectEndorseValidationFailed(const ndn::shared_ptr<const ndn::Data>& data, const ndn::Name& identity);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800140
Yingdi Yua1a688f2014-02-06 18:09:22 -0800141 inline void
Yingdi Yu64206112013-12-24 11:16:32 +0800142 onDnsCollectEndorseTimeoutNotify(const ndn::Name& identity);
143
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800144
145 void
Yingdi Yua1a688f2014-02-06 18:09:22 -0800146 onKeyValidated(const ndn::shared_ptr<const ndn::Data>& data, const ndn::Name& identity);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800147
Yingdi Yua1a688f2014-02-06 18:09:22 -0800148 inline void
149 onKeyValidationFailed(const ndn::shared_ptr<const ndn::Data>& data, const ndn::Name& identity);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800150
Yingdi Yua1a688f2014-02-06 18:09:22 -0800151 inline void
Yingdi Yu64206112013-12-24 11:16:32 +0800152 onKeyTimeoutNotify(const ndn::Name& identity);
153
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800154
Yingdi Yua1a688f2014-02-06 18:09:22 -0800155 inline void
156 onIdCertValidated(const ndn::shared_ptr<const ndn::Data>& data, const ndn::Name& identity);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800157
Yingdi Yua1a688f2014-02-06 18:09:22 -0800158 inline void
159 onIdCertValidationFailed(const ndn::shared_ptr<const ndn::Data>& data, const ndn::Name& identity);
Yingdi Yu908f8412013-11-09 00:03:26 -0800160
Yingdi Yua1a688f2014-02-06 18:09:22 -0800161 inline void
162 onIdCertTimeoutNotify(const ndn::Name& identity);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800163
164
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700165signals:
Yingdi Yub29f78c2013-11-09 20:12:31 -0800166 void
167 noNdnConnection(const QString& msg);
168
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700169 void
Yingdi Yua1a688f2014-02-06 18:09:22 -0800170 contactFetched(const chronos::EndorseCertificate& endorseCertificate);
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700171
172 void
173 contactFetchFailed(const ndn::Name& identity);
174
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800175 void
Yingdi Yua1a688f2014-02-06 18:09:22 -0800176 contactKeyFetched(const chronos::EndorseCertificate& endorseCertificate);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800177
178 void
179 contactKeyFetchFailed(const ndn::Name& identity);
180
181 void
Yingdi Yu64206112013-12-24 11:16:32 +0800182 contactCertificateFetched(const ndn::IdentityCertificate& identityCertificate);
Yingdi Yu908f8412013-11-09 00:03:26 -0800183
184 void
185 contactCertificateFetchFailed(const ndn::Name& identity);
186
187 void
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800188 collectEndorseFetched(const ndn::Data& data);
189
190 void
191 collectEndorseFetchFailed(const ndn::Name& identity);
192
Yingdi Yu908f8412013-11-09 00:03:26 -0800193 void
194 warning(QString msg);
195
Yingdi Yu6ea54e42013-11-12 17:50:21 -0800196 void
197 contactRemoved(const ndn::Name& identity);
198
199 void
200 contactAdded(const ndn::Name& identity);
201
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700202private slots:
203
Yingdi Yu0b82a4e2013-10-18 11:29:25 -0700204
205private:
Yingdi Yueaa84e22014-01-16 10:30:26 -0800206
Yingdi Yua1a688f2014-02-06 18:09:22 -0800207 ndn::shared_ptr<ContactStorage> m_contactStorage;
208 ndn::shared_ptr<DnsStorage> m_dnsStorage;
209 ndn::shared_ptr<ndn::Validator> m_validator;
210 ndn::shared_ptr<ndn::Face> m_face;
211 ndn::shared_ptr<ndn::KeyChain> m_keyChain;
Yingdi Yu908f8412013-11-09 00:03:26 -0800212 ndn::Name m_defaultIdentity;
Yingdi Yu0b82a4e2013-10-18 11:29:25 -0700213};
214
Yingdi Yua1a688f2014-02-06 18:09:22 -0800215void
216ContactManager::sendInterest(const ndn::Interest& interest,
217 const ndn::OnDataValidated& onValidated,
218 const ndn::OnDataValidationFailed& onValidationFailed,
219 const TimeoutNotify& timeoutNotify,
220 int retry /* = 1 */)
221{
222 m_face->expressInterest(interest,
223 bind(&ContactManager::onTargetData,
224 this, _1, _2, onValidated, onValidationFailed),
225 bind(&ContactManager::onTargetTimeout,
226 this, _1, retry, onValidated, onValidationFailed, timeoutNotify));
227}
228
229void
230ContactManager::onTargetData(const ndn::Interest& interest,
231 const ndn::Data& data,
232 const ndn::OnDataValidated& onValidated,
233 const ndn::OnDataValidationFailed& onValidationFailed)
234{ m_validator->validate(data, onValidated, onValidationFailed); }
235
236void
237ContactManager::onTargetTimeout(const ndn::Interest& interest,
238 int retry,
239 const ndn::OnDataValidated& onValidated,
240 const ndn::OnDataValidationFailed& onValidationFailed,
241 const TimeoutNotify& timeoutNotify)
242{
243 if(retry > 0)
244 sendInterest(interest, onValidated, onValidationFailed, timeoutNotify, retry-1);
245 else
246 timeoutNotify();
247}
248
249
250void
251ContactManager::onDnsSelfEndorseCertValidationFailed(const ndn::shared_ptr<const ndn::Data>& data,
252 const ndn::Name& identity)
253{ emit contactFetchFailed (identity); }
254
255void
256ContactManager::onDnsSelfEndorseCertTimeoutNotify(const ndn::Name& identity)
257{ emit contactFetchFailed(identity); }
258
259void
260ContactManager::onDnsCollectEndorseValidated(const ndn::shared_ptr<const ndn::Data>& data,
261 const ndn::Name& identity)
262{ emit collectEndorseFetched (*data); }
263
264void
265ContactManager::onDnsCollectEndorseValidationFailed(const ndn::shared_ptr<const ndn::Data>& data,
266 const ndn::Name& identity)
267{ emit collectEndorseFetchFailed (identity); }
268
269void
270ContactManager::onDnsCollectEndorseTimeoutNotify(const ndn::Name& identity)
271{ emit collectEndorseFetchFailed (identity); }
272
273void
274ContactManager::onKeyValidationFailed(const ndn::shared_ptr<const ndn::Data>& data,
275 const ndn::Name& identity)
276{ emit contactKeyFetchFailed (identity); }
277
278void
279ContactManager::onKeyTimeoutNotify(const ndn::Name& identity)
280{ emit contactKeyFetchFailed(identity); }
281
282void
283ContactManager::onIdCertValidated(const ndn::shared_ptr<const ndn::Data>& data,
284 const ndn::Name& identity)
285{
286 ndn::IdentityCertificate identityCertificate(*data);
287 emit contactCertificateFetched(identityCertificate);
288}
289
290void
291ContactManager::onIdCertValidationFailed(const ndn::shared_ptr<const ndn::Data>& data,
292 const ndn::Name& identity)
293{ emit contactCertificateFetchFailed (identity); }
294
295void
296ContactManager::onIdCertTimeoutNotify(const ndn::Name& identity)
297{ emit contactCertificateFetchFailed (identity); }
298
299ndn::shared_ptr<ContactItem>
300ContactManager::getContact(const ndn::Name& contactNamespace)
301{ return m_contactStorage->getContact(contactNamespace); }
302
303void
304ContactManager::getContactItemList(std::vector<ndn::shared_ptr<ContactItem> >& contacts)
305{ return m_contactStorage->getAllContacts(contacts); }
306
307}//chronos
308
Yingdi Yu0b82a4e2013-10-18 11:29:25 -0700309#endif