Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Varun Patil | b3d03b9 | 2021-01-08 17:09:32 +0530 | [diff] [blame] | 3 | * Copyright (c) 2013-2021, Regents of the University of California |
Junxiao Shi | 9887fd1 | 2016-07-23 02:09:00 +0000 | [diff] [blame] | 4 | * Yingdi Yu |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 5 | * |
| 6 | * BSD license, See the LICENSE file for more information |
| 7 | * |
| 8 | * Author: Yingdi Yu <yingdi@cs.ucla.edu> |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 9 | * Qiuhan Ding <qiuhanding@cs.ucla.edu> |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include "controller-backend.hpp" |
| 13 | |
| 14 | #ifndef Q_MOC_RUN |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 15 | #include <iostream> |
| 16 | #include <boost/asio.hpp> |
| 17 | |
Yingdi Yu | 7ff31f0 | 2015-02-05 11:21:07 -0800 | [diff] [blame] | 18 | #include <ndn-cxx/util/segment-fetcher.hpp> |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 19 | #include <ndn-cxx/security/signing-helpers.hpp> |
| 20 | #include <ndn-cxx/security/certificate-fetcher-offline.hpp> |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 21 | |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 22 | #include "invitation.hpp" |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 23 | #endif |
| 24 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 25 | namespace chronochat { |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 26 | |
| 27 | using std::string; |
| 28 | |
| 29 | using ndn::Face; |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 30 | |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 31 | static const ndn::Name::Component ROUTING_HINT_SEPARATOR = |
| 32 | ndn::name::Component::fromEscapedString("%F0%2E"); |
| 33 | static const int MAXIMUM_REQUEST = 3; |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 34 | static const int CONNECTION_RETRY_TIMER = 3; |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 35 | |
| 36 | ControllerBackend::ControllerBackend(QObject* parent) |
| 37 | : QThread(parent) |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 38 | , m_shouldResume(false) |
Varun Patil | 074e38d | 2021-01-05 17:38:53 +0530 | [diff] [blame^] | 39 | , m_face(nullptr, m_keyChain) |
| 40 | , m_contactManager(m_face, m_keyChain) |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 41 | { |
| 42 | // connection to contact manager |
| 43 | connect(this, SIGNAL(identityUpdated(const QString&)), |
| 44 | &m_contactManager, SLOT(onIdentityUpdated(const QString&))); |
| 45 | |
| 46 | connect(&m_contactManager, SIGNAL(contactIdListReady(const QStringList&)), |
| 47 | this, SLOT(onContactIdListReady(const QStringList&))); |
| 48 | |
Varun Patil | b3d03b9 | 2021-01-08 17:09:32 +0530 | [diff] [blame] | 49 | m_validator = std::make_shared<ndn::security::ValidatorNull>(); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Varun Patil | b3d03b9 | 2021-01-08 17:09:32 +0530 | [diff] [blame] | 52 | ControllerBackend::~ControllerBackend() = default; |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 53 | |
| 54 | void |
| 55 | ControllerBackend::run() |
| 56 | { |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 57 | bool shouldResume = false; |
| 58 | do { |
| 59 | try { |
| 60 | setInvitationListener(); |
| 61 | m_face.processEvents(); |
| 62 | } |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 63 | catch (const std::runtime_error& e) { |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 64 | { |
| 65 | std::lock_guard<std::mutex>lock(m_nfdConnectionMutex); |
| 66 | m_isNfdConnected = false; |
| 67 | } |
| 68 | emit nfdError(); |
| 69 | { |
| 70 | std::lock_guard<std::mutex>lock(m_resumeMutex); |
| 71 | m_shouldResume = true; |
| 72 | } |
| 73 | #ifdef BOOST_THREAD_USES_CHRONO |
| 74 | time::seconds reconnectTimer = time::seconds(CONNECTION_RETRY_TIMER); |
| 75 | #else |
| 76 | boost::posix_time::time_duration reconnectTimer; |
| 77 | reconnectTimer = boost::posix_time::seconds(CONNECTION_RETRY_TIMER); |
| 78 | #endif |
| 79 | while (!m_isNfdConnected) { |
| 80 | #ifdef BOOST_THREAD_USES_CHRONO |
| 81 | boost::this_thread::sleep_for(reconnectTimer); |
| 82 | #else |
| 83 | boost::this_thread::sleep(reconnectTimer); |
| 84 | #endif |
| 85 | } |
| 86 | } |
| 87 | { |
| 88 | std::lock_guard<std::mutex>lock(m_resumeMutex); |
| 89 | shouldResume = m_shouldResume; |
| 90 | m_shouldResume = false; |
| 91 | } |
| 92 | } while (shouldResume); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 93 | std::cerr << "Bye!" << std::endl; |
| 94 | } |
| 95 | |
| 96 | // private methods: |
| 97 | |
| 98 | void |
| 99 | tmpOnInvitationInterest(const ndn::Name& prefix, |
| 100 | const ndn::Interest& interest) |
| 101 | { |
| 102 | std::cerr << "tmpOnInvitationInterest" << std::endl; |
| 103 | } |
| 104 | |
| 105 | void |
| 106 | tmpOnInvitationRegisterFailed(const Name& prefix, |
| 107 | const string& failInfo) |
| 108 | { |
| 109 | std::cerr << "tmpOnInvitationRegisterFailed" << std::endl; |
| 110 | } |
| 111 | |
| 112 | void |
| 113 | tmpUnregisterPrefixSuccessCallback() |
| 114 | { |
| 115 | std::cerr << "tmpUnregisterPrefixSuccessCallback" << std::endl; |
| 116 | } |
| 117 | |
| 118 | void |
| 119 | tmpUnregisterPrefixFailureCallback(const string& failInfo) |
| 120 | { |
| 121 | std::cerr << "tmpUnregisterPrefixSuccessCallback" << std::endl; |
| 122 | } |
| 123 | |
| 124 | void |
| 125 | ControllerBackend::setInvitationListener() |
| 126 | { |
| 127 | QMutexLocker locker(&m_mutex); |
| 128 | |
| 129 | Name invitationPrefix; |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 130 | Name requestPrefix; |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 131 | Name routingPrefix = getInvitationRoutingPrefix(); |
| 132 | size_t offset = 0; |
| 133 | if (!routingPrefix.isPrefixOf(m_identity)) { |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 134 | invitationPrefix.append(routingPrefix).append(ROUTING_HINT_SEPARATOR); |
| 135 | requestPrefix.append(routingPrefix).append(ROUTING_HINT_SEPARATOR); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 136 | offset = routingPrefix.size() + 1; |
| 137 | } |
| 138 | invitationPrefix.append(m_identity).append("CHRONOCHAT-INVITATION"); |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 139 | requestPrefix.append(m_identity).append("CHRONOCHAT-INVITATION-REQUEST"); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 140 | |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 141 | m_invitationListenerHandle = m_face.setInterestFilter(invitationPrefix, |
| 142 | bind(&ControllerBackend::onInvitationInterest, this, _1, _2, offset), |
| 143 | bind(&ControllerBackend::onInvitationRegisterFailed, this, _1, _2)); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 144 | |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 145 | m_requestListenerHandle = m_face.setInterestFilter(requestPrefix, |
| 146 | bind(&ControllerBackend::onInvitationRequestInterest, this, _1, _2, offset), |
| 147 | [] (const Name& prefix, const std::string& failInfo) {}); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | ndn::Name |
| 151 | ControllerBackend::getInvitationRoutingPrefix() |
| 152 | { |
| 153 | return Name("/ndn/broadcast"); |
| 154 | } |
| 155 | |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 156 | |
| 157 | void |
| 158 | ControllerBackend::onInvitationInterest(const ndn::Name& prefix, |
| 159 | const ndn::Interest& interest, |
| 160 | size_t routingPrefixOffset) |
| 161 | { |
Varun Patil | b3d03b9 | 2021-01-08 17:09:32 +0530 | [diff] [blame] | 162 | auto invitationInterest = std::make_shared<Interest>(interest.getName().getSubName(routingPrefixOffset)); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 163 | |
| 164 | // check if the chatroom already exists; |
| 165 | try { |
| 166 | Invitation invitation(invitationInterest->getName()); |
| 167 | if (m_chatDialogList.contains(QString::fromStdString(invitation.getChatroom()))) |
| 168 | return; |
| 169 | } |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 170 | catch (const Invitation::Error& e) { |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 171 | // Cannot parse the invitation; |
| 172 | return; |
| 173 | } |
| 174 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 175 | m_validator->validate( |
| 176 | *invitationInterest, |
| 177 | bind(&ControllerBackend::onInvitationValidated, this, _1), |
| 178 | bind(&ControllerBackend::onInvitationValidationFailed, this, _1, _2)); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | void |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 182 | ControllerBackend::onInvitationRegisterFailed(const Name& prefix, const std::string& failInfo) |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 183 | { |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | void |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 187 | ControllerBackend::onInvitationRequestInterest(const ndn::Name& prefix, |
| 188 | const ndn::Interest& interest, |
| 189 | size_t routingPrefixOffset) |
| 190 | { |
| 191 | shared_ptr<const Data> data = m_ims.find(interest); |
| 192 | if (data != nullptr) { |
| 193 | m_face.put(*data); |
| 194 | return; |
| 195 | } |
| 196 | Name interestName = interest.getName(); |
| 197 | size_t i; |
| 198 | for (i = 0; i < interestName.size(); i++) |
| 199 | if (interestName.at(i) == Name::Component("CHRONOCHAT-INVITATION-REQUEST")) |
| 200 | break; |
| 201 | if (i < interestName.size()) { |
| 202 | string chatroom = interestName.at(i+1).toUri(); |
| 203 | string alias = interestName.getSubName(i+2).getPrefix(-1).toUri(); |
| 204 | emit invitationRequestReceived(QString::fromStdString(alias), |
| 205 | QString::fromStdString(chatroom), |
| 206 | interestName); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | void |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 211 | ControllerBackend::onInvitationValidated(const Interest& interest) |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 212 | { |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 213 | Invitation invitation(interest.getName()); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 214 | // Should be obtained via a method of ContactManager. |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 215 | string alias = invitation.getInviterCertificate().getKeyName().getPrefix(-1).toUri(); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 216 | |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 217 | emit invitationValidated(QString::fromStdString(alias), |
| 218 | QString::fromStdString(invitation.getChatroom()), |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 219 | interest.getName()); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | void |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 223 | ControllerBackend::onInvitationValidationFailed(const Interest& interest, |
| 224 | const ndn::security::ValidationError& failureInfo) |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 225 | { |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | void |
Yingdi Yu | 7ff31f0 | 2015-02-05 11:21:07 -0800 | [diff] [blame] | 229 | ControllerBackend::onLocalPrefix(const ndn::ConstBufferPtr& data) |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 230 | { |
| 231 | Name prefix; |
| 232 | |
Yingdi Yu | 7ff31f0 | 2015-02-05 11:21:07 -0800 | [diff] [blame] | 233 | Block contentBlock(tlv::Content, data); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 234 | try { |
| 235 | contentBlock.parse(); |
| 236 | |
| 237 | for (Block::element_const_iterator it = contentBlock.elements_begin(); |
| 238 | it != contentBlock.elements_end(); it++) { |
| 239 | Name candidate; |
| 240 | candidate.wireDecode(*it); |
| 241 | if (candidate.isPrefixOf(m_identity)) { |
| 242 | prefix = candidate; |
| 243 | break; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | if (prefix.empty()) { |
| 248 | if (contentBlock.elements_begin() != contentBlock.elements_end()) |
| 249 | prefix.wireDecode(*contentBlock.elements_begin()); |
| 250 | else |
| 251 | prefix = Name("/private/local"); |
| 252 | } |
| 253 | } |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 254 | catch (const Block::Error& e) { |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 255 | prefix = Name("/private/local"); |
| 256 | } |
| 257 | |
| 258 | updateLocalPrefix(prefix); |
| 259 | } |
| 260 | |
| 261 | void |
Yingdi Yu | 7ff31f0 | 2015-02-05 11:21:07 -0800 | [diff] [blame] | 262 | ControllerBackend::onLocalPrefixError(uint32_t code, const std::string& msg) |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 263 | { |
| 264 | Name localPrefix("/private/local"); |
| 265 | updateLocalPrefix(localPrefix); |
| 266 | } |
| 267 | |
| 268 | void |
| 269 | ControllerBackend::updateLocalPrefix(const Name& localPrefix) |
| 270 | { |
| 271 | if (m_localPrefix.empty() || m_localPrefix != localPrefix) { |
| 272 | m_localPrefix = localPrefix; |
| 273 | emit localPrefixUpdated(QString::fromStdString(localPrefix.toUri())); |
| 274 | } |
| 275 | } |
| 276 | |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 277 | void |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 278 | ControllerBackend::onRequestResponse(const Interest& interest, const Data& data) |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 279 | { |
| 280 | size_t i; |
| 281 | Name interestName = interest.getName(); |
| 282 | for (i = 0; i < interestName.size(); i++) { |
| 283 | if (interestName.at(i) == Name::Component("CHRONOCHAT-INVITATION-REQUEST")) |
| 284 | break; |
| 285 | } |
| 286 | Name::Component chatroomName = interestName.at(i+1); |
| 287 | Block contentBlock = data.getContent(); |
| 288 | int res = ndn::readNonNegativeInteger(contentBlock); |
Qiuhan Ding | 59c4543 | 2015-04-01 18:15:03 -0700 | [diff] [blame] | 289 | if (m_chatDialogList.contains(QString::fromStdString(chatroomName.toUri()))) |
| 290 | return; |
| 291 | |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 292 | // if data is true, |
| 293 | if (res == 1) |
| 294 | emit startChatroom(QString::fromStdString(chatroomName.toUri()), false); |
| 295 | else |
| 296 | emit invitationRequestResult("You are rejected to enter chatroom: " + chatroomName.toUri()); |
| 297 | } |
| 298 | |
| 299 | void |
| 300 | ControllerBackend::onRequestTimeout(const Interest& interest, int& resendTimes) |
| 301 | { |
| 302 | if (resendTimes < MAXIMUM_REQUEST) |
| 303 | m_face.expressInterest(interest, |
| 304 | bind(&ControllerBackend::onRequestResponse, this, _1, _2), |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 305 | bind(&ControllerBackend::onRequestTimeout, this, _1, resendTimes + 1), |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 306 | bind(&ControllerBackend::onRequestTimeout, this, _1, resendTimes + 1)); |
| 307 | else |
| 308 | emit invitationRequestResult("Invitation request times out."); |
| 309 | } |
| 310 | |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 311 | // public slots: |
| 312 | void |
| 313 | ControllerBackend::shutdown() |
| 314 | { |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 315 | { |
| 316 | std::lock_guard<std::mutex>lock(m_resumeMutex); |
| 317 | m_shouldResume = false; |
| 318 | } |
| 319 | { |
| 320 | // In this case, we just stop checking the nfd connection and exit |
| 321 | std::lock_guard<std::mutex>lock(m_nfdConnectionMutex); |
| 322 | m_isNfdConnected = true; |
| 323 | } |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 324 | m_face.getIoService().stop(); |
| 325 | } |
| 326 | |
| 327 | void |
| 328 | ControllerBackend::addChatroom(QString chatroom) |
| 329 | { |
| 330 | m_chatDialogList.append(chatroom); |
| 331 | } |
| 332 | |
| 333 | void |
| 334 | ControllerBackend::removeChatroom(QString chatroom) |
| 335 | { |
| 336 | m_chatDialogList.removeAll(chatroom); |
| 337 | } |
| 338 | |
| 339 | void |
| 340 | ControllerBackend::onUpdateLocalPrefixAction() |
| 341 | { |
Yingdi Yu | 7ff31f0 | 2015-02-05 11:21:07 -0800 | [diff] [blame] | 342 | Interest interest("/localhop/nfd/rib/routable-prefixes"); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 343 | interest.setInterestLifetime(time::milliseconds(1000)); |
| 344 | interest.setMustBeFresh(true); |
| 345 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 346 | auto fetcher = ndn::util::SegmentFetcher::start(m_face, |
| 347 | interest, |
| 348 | m_nullValidator); |
| 349 | fetcher->onComplete.connect(bind(&ControllerBackend::onLocalPrefix, this, _1)); |
| 350 | fetcher->onError.connect(bind(&ControllerBackend::onLocalPrefixError, this, _1, _2)); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | void |
| 354 | ControllerBackend::onIdentityChanged(const QString& identity) |
| 355 | { |
| 356 | m_chatDialogList.clear(); |
| 357 | |
| 358 | m_identity = Name(identity.toStdString()); |
| 359 | |
| 360 | std::cerr << "ControllerBackend::onIdentityChanged: " << m_identity << std::endl; |
| 361 | |
| 362 | m_keyChain.createIdentity(m_identity); |
| 363 | |
| 364 | setInvitationListener(); |
| 365 | |
| 366 | emit identityUpdated(identity); |
| 367 | } |
| 368 | |
| 369 | void |
| 370 | ControllerBackend::onInvitationResponded(const ndn::Name& invitationName, bool accepted) |
| 371 | { |
Varun Patil | b3d03b9 | 2021-01-08 17:09:32 +0530 | [diff] [blame] | 372 | auto response = std::make_shared<Data>(); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 373 | shared_ptr<ndn::security::Certificate> chatroomCert; |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 374 | |
| 375 | // generate reply; |
| 376 | if (accepted) { |
| 377 | Name responseName = invitationName; |
| 378 | responseName.append(m_localPrefix.wireEncode()); |
| 379 | |
| 380 | response->setName(responseName); |
| 381 | |
| 382 | // We should create a particular certificate for this chatroom, |
| 383 | //but let's use default one for now. |
Varun Patil | b3d03b9 | 2021-01-08 17:09:32 +0530 | [diff] [blame] | 384 | chatroomCert = std::make_shared<ndn::security::Certificate>( |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 385 | m_keyChain.createIdentity(m_identity).getDefaultKey().getDefaultCertificate()); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 386 | |
| 387 | response->setContent(chatroomCert->wireEncode()); |
| 388 | response->setFreshnessPeriod(time::milliseconds(1000)); |
| 389 | } |
| 390 | else { |
| 391 | response->setName(invitationName); |
| 392 | response->setFreshnessPeriod(time::milliseconds(1000)); |
| 393 | } |
| 394 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 395 | m_keyChain.sign(*response, ndn::security::signingByIdentity(m_identity)); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 396 | |
| 397 | // Check if we need a wrapper |
| 398 | Name invitationRoutingPrefix = getInvitationRoutingPrefix(); |
| 399 | if (invitationRoutingPrefix.isPrefixOf(m_identity)) |
| 400 | m_face.put(*response); |
| 401 | else { |
| 402 | Name wrappedName; |
| 403 | wrappedName.append(invitationRoutingPrefix) |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 404 | .append(ROUTING_HINT_SEPARATOR) |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 405 | .append(response->getName()); |
| 406 | |
Varun Patil | b3d03b9 | 2021-01-08 17:09:32 +0530 | [diff] [blame] | 407 | auto wrappedData = std::make_shared<Data>(wrappedName); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 408 | wrappedData->setContent(response->wireEncode()); |
| 409 | wrappedData->setFreshnessPeriod(time::milliseconds(1000)); |
| 410 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 411 | m_keyChain.sign(*wrappedData, ndn::security::signingByIdentity(m_identity)); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 412 | m_face.put(*wrappedData); |
| 413 | } |
| 414 | |
| 415 | Invitation invitation(invitationName); |
| 416 | emit startChatroomOnInvitation(invitation, true); |
| 417 | } |
| 418 | |
| 419 | void |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 420 | ControllerBackend::onInvitationRequestResponded(const ndn::Name& invitationResponseName, |
| 421 | bool accepted) |
| 422 | { |
Varun Patil | b3d03b9 | 2021-01-08 17:09:32 +0530 | [diff] [blame] | 423 | auto response = std::make_shared<Data>(invitationResponseName); |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 424 | if (accepted) |
Junxiao Shi | 9887fd1 | 2016-07-23 02:09:00 +0000 | [diff] [blame] | 425 | response->setContent(ndn::makeNonNegativeIntegerBlock(tlv::Content, 1)); |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 426 | else |
Junxiao Shi | 9887fd1 | 2016-07-23 02:09:00 +0000 | [diff] [blame] | 427 | response->setContent(ndn::makeNonNegativeIntegerBlock(tlv::Content, 0)); |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 428 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 429 | response->setFreshnessPeriod(time::milliseconds(1000)); |
| 430 | m_keyChain.sign(*response, ndn::security::signingByIdentity(m_identity)); |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 431 | m_ims.insert(*response); |
| 432 | m_face.put(*response); |
| 433 | } |
| 434 | |
| 435 | void |
| 436 | ControllerBackend::onSendInvitationRequest(const QString& chatroomName, const QString& prefix) |
| 437 | { |
| 438 | if (prefix.length() == 0) |
| 439 | return; |
| 440 | Name interestName = getInvitationRoutingPrefix(); |
| 441 | interestName.append(ROUTING_HINT_SEPARATOR).append(prefix.toStdString()); |
| 442 | interestName.append("CHRONOCHAT-INVITATION-REQUEST"); |
| 443 | interestName.append(chatroomName.toStdString()); |
| 444 | interestName.append(m_identity); |
| 445 | interestName.appendTimestamp(); |
| 446 | Interest interest(interestName); |
| 447 | interest.setInterestLifetime(time::milliseconds(10000)); |
| 448 | interest.setMustBeFresh(true); |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 449 | interest.setCanBePrefix(true); |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 450 | interest.getNonce(); |
| 451 | m_face.expressInterest(interest, |
| 452 | bind(&ControllerBackend::onRequestResponse, this, _1, _2), |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 453 | bind(&ControllerBackend::onRequestTimeout, this, _1, 0), |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 454 | bind(&ControllerBackend::onRequestTimeout, this, _1, 0)); |
| 455 | } |
| 456 | |
| 457 | void |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 458 | ControllerBackend::onContactIdListReady(const QStringList& list) |
| 459 | { |
| 460 | ContactList contactList; |
| 461 | |
| 462 | m_contactManager.getContactList(contactList); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 463 | // m_validator.cleanTrustAnchor(); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 464 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 465 | // for (ContactList::const_iterator it = contactList.begin(); it != contactList.end(); it++) |
| 466 | // m_validator.addTrustAnchor((*it)->getPublicKeyName(), (*it)->getPublicKey()); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 467 | } |
| 468 | |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 469 | void |
| 470 | ControllerBackend::onNfdReconnect() |
| 471 | { |
| 472 | std::lock_guard<std::mutex>lock(m_nfdConnectionMutex); |
| 473 | m_isNfdConnected = true; |
| 474 | } |
| 475 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 476 | } // namespace chronochat |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 477 | |
| 478 | #if WAF |
| 479 | #include "controller-backend.moc" |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 480 | #endif |