blob: 6bcd429398de5f63846cb4c8334a586dd82c73a5 [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 }
56private:
57 void
58 initializeSecurity();
Yingdi Yub2e747d2013-11-05 23:06:43 -080059
60 void
61 fetchCollectEndorse(const ndn::Name& identity);
62
63 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -080064 fetchEndorseCertificateInternal(const ndn::Name& identity, int certIndex);
Yingdi Yuae8217c2013-11-09 00:03:26 -080065
66 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -080067 prepareEndorseInfo(const ndn::Name& identity);
68
69 // PROFILE: self-endorse-certificate
70 void
71 onDnsSelfEndorseCertValidated(const ndn::shared_ptr<const ndn::Data>& selfEndorseCertificate,
72 const ndn::Name& identity);
Yingdi Yu4685b1b2013-10-18 17:05:02 -070073
Yingdi Yub2e747d2013-11-05 23:06:43 -080074 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -080075 onDnsSelfEndorseCertValidationFailed(const ndn::shared_ptr<const ndn::Data>& selfEndorseCertificate,
76 const ndn::Name& identity);
Yingdi Yu9236c432013-10-18 11:29:25 -070077
Yingdi Yub2e747d2013-11-05 23:06:43 -080078 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -080079 onDnsSelfEndorseCertTimeoutNotify(const ndn::Interest& interest,
80 const ndn::Name& identity);
81
82 // ENDORSED: endorse-collection
83 void
84 onDnsCollectEndorseValidated(const ndn::shared_ptr<const ndn::Data>& data,
85 const ndn::Name& identity);
Yingdi Yub2e747d2013-11-05 23:06:43 -080086
Yingdi Yu76dd8002013-12-24 11:16:32 +080087 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -080088 onDnsCollectEndorseValidationFailed(const ndn::shared_ptr<const ndn::Data>& data,
89 const ndn::Name& identity);
Yingdi Yuae8217c2013-11-09 00:03:26 -080090
91 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -080092 onDnsCollectEndorseTimeoutNotify(const ndn::Interest& interest,
93 const ndn::Name& identity);
94
95 // PROFILE-CERT: endorse-certificate
96 void
97 onEndorseCertificateInternal(const ndn::Interest& interest,
98 ndn::Data& data,
99 const ndn::Name& identity,
100 int certIndex,
101 std::string hash);
Yingdi Yuae8217c2013-11-09 00:03:26 -0800102
Yingdi Yu72232692013-11-12 17:50:21 -0800103 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800104 onEndorseCertificateInternalTimeout(const ndn::Interest& interest,
105 const ndn::Name& identity,
106 int certIndex);
107
108 // Collect endorsement
109 void
110 collectEndorsement();
111
112 void
113 onDnsEndorseeValidated(const ndn::shared_ptr<const ndn::Data>& data);
114
115 void
116 onDnsEndorseeValidationFailed(const ndn::shared_ptr<const ndn::Data>& data);
117
118 void
119 onDnsEndorseeTimeoutNotify(const ndn::Interest& interest);
120
121 void
122 decreaseCollectStatus();
123
124 void
125 publishCollectEndorsedDataInDNS();
126
127 // Identity certificate
128 void
129 onIdentityCertValidated(const ndn::shared_ptr<const ndn::Data>& data);
Yingdi Yu72232692013-11-12 17:50:21 -0800130
Yingdi Yu76dd8002013-12-24 11:16:32 +0800131 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800132 onIdentityCertValidationFailed(const ndn::shared_ptr<const ndn::Data>& data);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800133
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800134 void
135 onIdentityCertTimeoutNotify(const ndn::Interest& interest);
136
137 void
138 decreaseIdCertCount();
139
140 // Publish self-endorse certificate
Yingdi Yufa4ce792014-02-06 18:09:22 -0800141 ndn::shared_ptr<EndorseCertificate>
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800142 getSignedSelfEndorseCertificate(const Profile& profile);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800143
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700144 void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800145 publishSelfEndorseCertificateInDNS(const EndorseCertificate& selfEndorseCertificate);
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700146
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800147 // Publish endorse certificate
148 ndn::shared_ptr<EndorseCertificate>
149 generateEndorseCertificate(const ndn::Name& identity);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800150
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800151 void
152 publishEndorseCertificateInDNS(const EndorseCertificate& endorseCertificate);
153
154 // Communication
155 void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800156 sendInterest(const ndn::Interest& interest,
Yingdi Yufa4ce792014-02-06 18:09:22 -0800157 const ndn::OnDataValidated& onValidated,
158 const ndn::OnDataValidationFailed& onValidationFailed,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800159 const TimeoutNotify& timeoutNotify,
Yingdi Yu68de61d2014-01-16 19:13:03 -0800160 int retry = 1);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800161
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800162 void
Yingdi Yufa4ce792014-02-06 18:09:22 -0800163 onTargetData(const ndn::Interest& interest,
164 const ndn::Data& data,
165 const ndn::OnDataValidated& onValidated,
166 const ndn::OnDataValidationFailed& onValidationFailed);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800167
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800168 void
Yingdi Yufa4ce792014-02-06 18:09:22 -0800169 onTargetTimeout(const ndn::Interest& interest,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800170 int retry,
Yingdi Yufa4ce792014-02-06 18:09:22 -0800171 const ndn::OnDataValidated& onValidated,
172 const ndn::OnDataValidationFailed& onValidationFailed,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800173 const TimeoutNotify& timeoutNotify);
174
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800175 // DNS listener
Yingdi Yub2e747d2013-11-05 23:06:43 -0800176 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800177 onDnsInterest(const ndn::Name& prefix, const ndn::Interest& interest);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800178
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800179 void
180 onDnsRegisterFailed(const ndn::Name& prefix, const std::string& failInfo);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800181
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700182signals:
Yingdi Yu8e135832013-11-09 20:12:31 -0800183 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800184 contactEndorseInfoReady(const chronos::EndorseInfo& endorseInfo);
185
186 void
187 contactInfoFetchFailed(const QString& identity);
188
189 void
190 idCertNameListReady(const QStringList& certNameList);
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700191
192 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800193 nameListReady(const QStringList& certNameList);
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700194
Yingdi Yub2e747d2013-11-05 23:06:43 -0800195 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800196 idCertReady(const ndn::IdentityCertificate& idCert);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800197
198 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800199 contactAliasListReady(const QStringList& aliasList);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800200
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800201 void
202 contactIdListReady(const QStringList& idList);
203
204 void
205 contactInfoReady(const QString& identity,
206 const QString& name,
207 const QString& institute,
208 bool isIntro);
209
210 void
211 warning(const QString& msg);
212
213public slots:
214 void
215 onIdentityUpdated(const QString& identity);
216
217 void
218 onFetchContactInfo(const QString& identity);
Yingdi Yuae8217c2013-11-09 00:03:26 -0800219
220 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800221 onAddFetchedContact(const QString& identity);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800222
223 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800224 onUpdateProfile();
Yingdi Yub2e747d2013-11-05 23:06:43 -0800225
Yingdi Yuae8217c2013-11-09 00:03:26 -0800226 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800227 onRefreshBrowseContact();
Yingdi Yuae8217c2013-11-09 00:03:26 -0800228
Yingdi Yu72232692013-11-12 17:50:21 -0800229 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800230 onFetchIdCert(const QString& certName);
Yingdi Yu72232692013-11-12 17:50:21 -0800231
232 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800233 onAddFetchedContactIdCert(const QString& identity);
Yingdi Yu72232692013-11-12 17:50:21 -0800234
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800235 void
236 onWaitForContactList();
237
238 void
239 onWaitForContactInfo(const QString& identity);
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700240
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800241 void
242 onRemoveContact(const QString& identity);
243
244 void
245 onUpdateAlias(const QString& identity, const QString& alias);
246
247 void
248 onUpdateIsIntroducer(const QString& identity, bool isIntro);
249
250 void
251 onUpdateEndorseCertificate(const QString& identity);
252
Yingdi Yu9236c432013-10-18 11:29:25 -0700253private:
Yingdi Yu93adb1a2014-01-16 10:30:26 -0800254
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800255 class FetchedInfo {
256 public:
257 ndn::shared_ptr<EndorseCertificate> m_selfEndorseCert;
258 ndn::shared_ptr<EndorseCollection> m_endorseCollection;
259 std::vector<ndn::shared_ptr<EndorseCertificate> > m_endorseCertList;
260 ndn::shared_ptr<EndorseInfo> m_endorseInfo;
261 };
262
263 typedef std::map<ndn::Name, FetchedInfo> BufferedContacts;
264 typedef std::map<ndn::Name, ndn::shared_ptr<ndn::IdentityCertificate> > BufferedIdCerts;
265
266 typedef boost::recursive_mutex RecLock;
267 typedef boost::unique_lock<RecLock> UniqueRecLock;
268
269 // Conf
Yingdi Yufa4ce792014-02-06 18:09:22 -0800270 ndn::shared_ptr<ContactStorage> m_contactStorage;
Yingdi Yufa4ce792014-02-06 18:09:22 -0800271 ndn::shared_ptr<ndn::Validator> m_validator;
272 ndn::shared_ptr<ndn::Face> m_face;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800273 ndn::KeyChain m_keyChain;
274 ndn::Name m_identity;
275 ContactList m_contactList;
276
277 // Buffer
278 BufferedContacts m_bufferedContacts;
279 BufferedIdCerts m_bufferedIdCerts;
280
281 // Tmp Dns
282 const ndn::RegisteredPrefixId* m_dnsListenerId;
283
284 RecLock m_collectCountMutex;
285 int m_collectCount;
286
287 RecLock m_idCertCountMutex;
288 int m_idCertCount;
Yingdi Yu9236c432013-10-18 11:29:25 -0700289};
290
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800291} // namespace chronos
Yingdi Yufa4ce792014-02-06 18:09:22 -0800292
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800293#endif //CHRONOS_CONTACT_MANAGER_H