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