Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -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 | |
| 11 | #ifndef CHRONOS_CONTACT_MANAGER_HPP |
| 12 | #define CHRONOS_CONTACT_MANAGER_HPP |
| 13 | |
| 14 | #include <QObject> |
| 15 | |
| 16 | #ifndef Q_MOC_RUN |
| 17 | #include "common.hpp" |
| 18 | #include "contact-storage.hpp" |
| 19 | #include "endorse-certificate.hpp" |
| 20 | #include "profile.hpp" |
| 21 | #include "endorse-info.pb.h" |
| 22 | #include "endorse-collection.pb.h" |
| 23 | #include <ndn-cxx/security/key-chain.hpp> |
| 24 | #include <ndn-cxx/security/validator.hpp> |
| 25 | #include <boost/thread/locks.hpp> |
| 26 | #include <boost/thread/recursive_mutex.hpp> |
| 27 | #endif |
| 28 | |
| 29 | namespace chronos{ |
| 30 | |
| 31 | typedef function<void(const Interest&)> TimeoutNotify; |
| 32 | typedef std::vector<shared_ptr<Contact> > ContactList; |
| 33 | |
| 34 | class ContactManager : public QObject |
| 35 | { |
| 36 | Q_OBJECT |
| 37 | |
| 38 | public: |
| 39 | ContactManager(shared_ptr<ndn::Face> m_face, QObject* parent = 0); |
| 40 | |
| 41 | ~ContactManager(); |
| 42 | |
| 43 | shared_ptr<Contact> |
| 44 | getContact(const Name& identity) |
| 45 | { |
| 46 | return m_contactStorage->getContact(identity); |
| 47 | } |
| 48 | |
| 49 | void |
| 50 | getContactList(ContactList& contactList) |
| 51 | { |
| 52 | contactList.clear(); |
| 53 | contactList.insert(contactList.end(), m_contactList.begin(), m_contactList.end()); |
| 54 | } |
| 55 | private: |
| 56 | shared_ptr<ndn::IdentityCertificate> |
| 57 | loadTrustAnchor(); |
| 58 | |
| 59 | void |
| 60 | initializeSecurity(); |
| 61 | |
| 62 | void |
| 63 | fetchCollectEndorse(const Name& identity); |
| 64 | |
| 65 | void |
| 66 | fetchEndorseCertificateInternal(const Name& identity, int certIndex); |
| 67 | |
| 68 | void |
| 69 | prepareEndorseInfo(const Name& identity); |
| 70 | |
| 71 | // PROFILE: self-endorse-certificate |
| 72 | void |
| 73 | onDnsSelfEndorseCertValidated(const shared_ptr<const Data>& selfEndorseCertificate, |
| 74 | const Name& identity); |
| 75 | |
| 76 | void |
| 77 | onDnsSelfEndorseCertValidationFailed(const shared_ptr<const Data>& selfEndorseCertificate, |
| 78 | const std::string& failInfo, |
| 79 | const Name& identity); |
| 80 | |
| 81 | void |
| 82 | onDnsSelfEndorseCertTimeoutNotify(const Interest& interest, const Name& identity); |
| 83 | |
| 84 | // ENDORSED: endorse-collection |
| 85 | void |
| 86 | onDnsCollectEndorseValidated(const shared_ptr<const Data>& data, const Name& identity); |
| 87 | |
| 88 | void |
| 89 | onDnsCollectEndorseValidationFailed(const shared_ptr<const Data>& data, |
| 90 | const std::string& failInfo, |
| 91 | const Name& identity); |
| 92 | |
| 93 | void |
| 94 | onDnsCollectEndorseTimeoutNotify(const Interest& interest, |
| 95 | const Name& identity); |
| 96 | |
| 97 | // PROFILE-CERT: endorse-certificate |
| 98 | void |
| 99 | onEndorseCertificateInternal(const Interest& interest, Data& data, |
| 100 | const Name& identity, int certIndex, std::string hash); |
| 101 | |
| 102 | void |
| 103 | onEndorseCertificateInternalTimeout(const Interest& interest, |
| 104 | const Name& identity, |
| 105 | int certIndex); |
| 106 | |
| 107 | // Collect endorsement |
| 108 | void |
| 109 | collectEndorsement(); |
| 110 | |
| 111 | void |
| 112 | onDnsEndorseeValidated(const shared_ptr<const Data>& data); |
| 113 | |
| 114 | void |
| 115 | onDnsEndorseeValidationFailed(const shared_ptr<const Data>& data, |
| 116 | const std::string& failInfo); |
| 117 | |
| 118 | void |
| 119 | onDnsEndorseeTimeoutNotify(const Interest& interest); |
| 120 | |
| 121 | void |
| 122 | decreaseCollectStatus(); |
| 123 | |
| 124 | void |
| 125 | publishCollectEndorsedDataInDNS(); |
| 126 | |
| 127 | // Identity certificate |
| 128 | void |
| 129 | onIdentityCertValidated(const shared_ptr<const Data>& data); |
| 130 | |
| 131 | void |
| 132 | onIdentityCertValidationFailed(const shared_ptr<const Data>& data, const std::string& failInfo); |
| 133 | |
| 134 | void |
| 135 | onIdentityCertTimeoutNotify(const Interest& interest); |
| 136 | |
| 137 | void |
| 138 | decreaseIdCertCount(); |
| 139 | |
| 140 | // Publish self-endorse certificate |
| 141 | shared_ptr<EndorseCertificate> |
| 142 | getSignedSelfEndorseCertificate(const Profile& profile); |
| 143 | |
| 144 | void |
| 145 | publishSelfEndorseCertificateInDNS(const EndorseCertificate& selfEndorseCertificate); |
| 146 | |
| 147 | // Publish endorse certificate |
| 148 | shared_ptr<EndorseCertificate> |
| 149 | generateEndorseCertificate(const Name& identity); |
| 150 | |
| 151 | void |
| 152 | publishEndorseCertificateInDNS(const EndorseCertificate& endorseCertificate); |
| 153 | |
| 154 | // Communication |
| 155 | void |
| 156 | sendInterest(const Interest& interest, |
| 157 | const ndn::OnDataValidated& onValidated, |
| 158 | const ndn::OnDataValidationFailed& onValidationFailed, |
| 159 | const TimeoutNotify& timeoutNotify, |
| 160 | int retry = 1); |
| 161 | |
| 162 | void |
| 163 | onTargetData(const Interest& interest, |
| 164 | const Data& data, |
| 165 | const ndn::OnDataValidated& onValidated, |
| 166 | const ndn::OnDataValidationFailed& onValidationFailed); |
| 167 | |
| 168 | void |
| 169 | onTargetTimeout(const Interest& interest, |
| 170 | int retry, |
| 171 | const ndn::OnDataValidated& onValidated, |
| 172 | const ndn::OnDataValidationFailed& onValidationFailed, |
| 173 | const TimeoutNotify& timeoutNotify); |
| 174 | |
| 175 | // DNS listener |
| 176 | void |
| 177 | onDnsInterest(const Name& prefix, const Interest& interest); |
| 178 | |
| 179 | void |
| 180 | onDnsRegisterFailed(const Name& prefix, const std::string& failInfo); |
| 181 | |
| 182 | signals: |
| 183 | void |
| 184 | contactEndorseInfoReady(const Chronos::EndorseInfo& endorseInfo); |
| 185 | |
| 186 | void |
| 187 | contactInfoFetchFailed(const QString& identity); |
| 188 | |
| 189 | void |
| 190 | idCertNameListReady(const QStringList& certNameList); |
| 191 | |
| 192 | void |
| 193 | nameListReady(const QStringList& certNameList); |
| 194 | |
| 195 | void |
| 196 | idCertReady(const ndn::IdentityCertificate& idCert); |
| 197 | |
| 198 | void |
| 199 | contactAliasListReady(const QStringList& aliasList); |
| 200 | |
| 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); |
| 219 | |
| 220 | void |
| 221 | onAddFetchedContact(const QString& identity); |
| 222 | |
| 223 | void |
| 224 | onUpdateProfile(); |
| 225 | |
| 226 | void |
| 227 | onRefreshBrowseContact(); |
| 228 | |
| 229 | void |
| 230 | onFetchIdCert(const QString& certName); |
| 231 | |
| 232 | void |
| 233 | onAddFetchedContactIdCert(const QString& identity); |
| 234 | |
| 235 | void |
| 236 | onWaitForContactList(); |
| 237 | |
| 238 | void |
| 239 | onWaitForContactInfo(const QString& identity); |
| 240 | |
| 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 | |
| 253 | private: |
| 254 | |
| 255 | class FetchedInfo { |
| 256 | public: |
| 257 | shared_ptr<EndorseCertificate> m_selfEndorseCert; |
| 258 | shared_ptr<EndorseCollection> m_endorseCollection; |
| 259 | std::vector<shared_ptr<EndorseCertificate> > m_endorseCertList; |
| 260 | shared_ptr<Chronos::EndorseInfo> m_endorseInfo; |
| 261 | }; |
| 262 | |
| 263 | typedef std::map<Name, FetchedInfo> BufferedContacts; |
| 264 | typedef std::map<Name, shared_ptr<ndn::IdentityCertificate> > BufferedIdCerts; |
| 265 | |
| 266 | typedef boost::recursive_mutex RecLock; |
| 267 | typedef boost::unique_lock<RecLock> UniqueRecLock; |
| 268 | |
| 269 | // Conf |
| 270 | shared_ptr<ContactStorage> m_contactStorage; |
| 271 | shared_ptr<ndn::Validator> m_validator; |
| 272 | shared_ptr<ndn::Face> m_face; |
| 273 | ndn::KeyChain m_keyChain; |
| 274 | 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; |
| 289 | }; |
| 290 | |
| 291 | } // namespace chronos |
| 292 | |
| 293 | #endif //CHRONOS_CONTACT_MANAGER_HPP |