Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 1 | /* -*- 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 Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 11 | #ifndef CHRONOS_CONTACT_MANAGER_H |
| 12 | #define CHRONOS_CONTACT_MANAGER_H |
Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 13 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 14 | #include <QObject> |
Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 15 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 16 | #ifndef Q_MOC_RUN |
| 17 | #include "contact-storage.h" |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 18 | #include "endorse-certificate.h" |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 19 | #include "profile.h" |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 20 | #include "endorse-info.pb.h" |
| 21 | #include "endorse-collection.pb.h" |
Yingdi Yu | b6fb030 | 2014-01-21 11:05:11 -0800 | [diff] [blame] | 22 | #include <ndn-cpp-dev/face.hpp> |
| 23 | #include <ndn-cpp-dev/security/key-chain.hpp> |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 24 | #include <ndn-cpp-dev/security/validator.hpp> |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 25 | #include <boost/thread/locks.hpp> |
| 26 | #include <boost/thread/recursive_mutex.hpp> |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 27 | #endif |
| 28 | |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 29 | namespace chronos{ |
| 30 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 31 | typedef ndn::function<void(const ndn::Interest&)> TimeoutNotify; |
| 32 | typedef std::vector<ndn::shared_ptr<Contact> > ContactList; |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 33 | |
| 34 | class ContactManager : public QObject |
Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 35 | { |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 36 | Q_OBJECT |
| 37 | |
Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 38 | public: |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 39 | ContactManager(ndn::shared_ptr<ndn::Face> m_face, |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 40 | QObject* parent = 0); |
Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 41 | |
| 42 | ~ContactManager(); |
| 43 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 44 | ndn::shared_ptr<Contact> |
| 45 | getContact(const ndn::Name& identity) |
| 46 | { |
| 47 | return m_contactStorage->getContact(identity); |
| 48 | } |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 49 | |
| 50 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 51 | getContactList(ContactList& contactList) |
| 52 | { |
| 53 | contactList.clear(); |
| 54 | contactList.insert(contactList.end(), m_contactList.begin(), m_contactList.end()); |
| 55 | } |
| 56 | private: |
| 57 | void |
| 58 | initializeSecurity(); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 59 | |
| 60 | void |
| 61 | fetchCollectEndorse(const ndn::Name& identity); |
| 62 | |
| 63 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 64 | fetchEndorseCertificateInternal(const ndn::Name& identity, int certIndex); |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 65 | |
| 66 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 67 | 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 Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 73 | |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 74 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 75 | onDnsSelfEndorseCertValidationFailed(const ndn::shared_ptr<const ndn::Data>& selfEndorseCertificate, |
| 76 | const ndn::Name& identity); |
Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 77 | |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 78 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 79 | 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 Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 86 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 87 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 88 | onDnsCollectEndorseValidationFailed(const ndn::shared_ptr<const ndn::Data>& data, |
| 89 | const ndn::Name& identity); |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 90 | |
| 91 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 92 | 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 Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 102 | |
Yingdi Yu | 7223269 | 2013-11-12 17:50:21 -0800 | [diff] [blame] | 103 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 104 | 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 Yu | 7223269 | 2013-11-12 17:50:21 -0800 | [diff] [blame] | 130 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 131 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 132 | onIdentityCertValidationFailed(const ndn::shared_ptr<const ndn::Data>& data); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 133 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 134 | void |
| 135 | onIdentityCertTimeoutNotify(const ndn::Interest& interest); |
| 136 | |
| 137 | void |
| 138 | decreaseIdCertCount(); |
| 139 | |
| 140 | // Publish self-endorse certificate |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 141 | ndn::shared_ptr<EndorseCertificate> |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 142 | getSignedSelfEndorseCertificate(const Profile& profile); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 143 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 144 | void |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 145 | publishSelfEndorseCertificateInDNS(const EndorseCertificate& selfEndorseCertificate); |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 146 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 147 | // Publish endorse certificate |
| 148 | ndn::shared_ptr<EndorseCertificate> |
| 149 | generateEndorseCertificate(const ndn::Name& identity); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 150 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 151 | void |
| 152 | publishEndorseCertificateInDNS(const EndorseCertificate& endorseCertificate); |
| 153 | |
| 154 | // Communication |
| 155 | void |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 156 | sendInterest(const ndn::Interest& interest, |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 157 | const ndn::OnDataValidated& onValidated, |
| 158 | const ndn::OnDataValidationFailed& onValidationFailed, |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 159 | const TimeoutNotify& timeoutNotify, |
Yingdi Yu | 68de61d | 2014-01-16 19:13:03 -0800 | [diff] [blame] | 160 | int retry = 1); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 161 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 162 | void |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 163 | onTargetData(const ndn::Interest& interest, |
| 164 | const ndn::Data& data, |
| 165 | const ndn::OnDataValidated& onValidated, |
| 166 | const ndn::OnDataValidationFailed& onValidationFailed); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 167 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 168 | void |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 169 | onTargetTimeout(const ndn::Interest& interest, |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 170 | int retry, |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 171 | const ndn::OnDataValidated& onValidated, |
| 172 | const ndn::OnDataValidationFailed& onValidationFailed, |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 173 | const TimeoutNotify& timeoutNotify); |
| 174 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 175 | // DNS listener |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 176 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 177 | onDnsInterest(const ndn::Name& prefix, const ndn::Interest& interest); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 178 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 179 | void |
| 180 | onDnsRegisterFailed(const ndn::Name& prefix, const std::string& failInfo); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 181 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 182 | signals: |
Yingdi Yu | 8e13583 | 2013-11-09 20:12:31 -0800 | [diff] [blame] | 183 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 184 | contactEndorseInfoReady(const chronos::EndorseInfo& endorseInfo); |
| 185 | |
| 186 | void |
| 187 | contactInfoFetchFailed(const QString& identity); |
| 188 | |
| 189 | void |
| 190 | idCertNameListReady(const QStringList& certNameList); |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 191 | |
| 192 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 193 | nameListReady(const QStringList& certNameList); |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 194 | |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 195 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 196 | idCertReady(const ndn::IdentityCertificate& idCert); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 197 | |
| 198 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 199 | contactAliasListReady(const QStringList& aliasList); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 200 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 201 | 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 | |
| 213 | public slots: |
| 214 | void |
| 215 | onIdentityUpdated(const QString& identity); |
| 216 | |
| 217 | void |
| 218 | onFetchContactInfo(const QString& identity); |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 219 | |
| 220 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 221 | onAddFetchedContact(const QString& identity); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 222 | |
| 223 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 224 | onUpdateProfile(); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 225 | |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 226 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 227 | onRefreshBrowseContact(); |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 228 | |
Yingdi Yu | 7223269 | 2013-11-12 17:50:21 -0800 | [diff] [blame] | 229 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 230 | onFetchIdCert(const QString& certName); |
Yingdi Yu | 7223269 | 2013-11-12 17:50:21 -0800 | [diff] [blame] | 231 | |
| 232 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 233 | onAddFetchedContactIdCert(const QString& identity); |
Yingdi Yu | 7223269 | 2013-11-12 17:50:21 -0800 | [diff] [blame] | 234 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 235 | void |
| 236 | onWaitForContactList(); |
| 237 | |
| 238 | void |
| 239 | onWaitForContactInfo(const QString& identity); |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 240 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 241 | 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 Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 253 | private: |
Yingdi Yu | 93adb1a | 2014-01-16 10:30:26 -0800 | [diff] [blame] | 254 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 255 | 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 Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 270 | ndn::shared_ptr<ContactStorage> m_contactStorage; |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 271 | ndn::shared_ptr<ndn::Validator> m_validator; |
| 272 | ndn::shared_ptr<ndn::Face> m_face; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 273 | 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 Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 289 | }; |
| 290 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 291 | } // namespace chronos |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 292 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 293 | #endif //CHRONOS_CONTACT_MANAGER_H |