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