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 | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame^] | 3 | * Copyright (c) 2020, Regents of the University of California |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 4 | * |
| 5 | * BSD license, See the LICENSE file for more information |
| 6 | * |
| 7 | * Author: Yingdi Yu <yingdi@cs.ucla.edu> |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 8 | * Qiuhan Ding <qiuhanding@cs.ucla.edu> |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #ifndef CHRONOCHAT_CONTROLLER_BACKEND_HPP |
| 12 | #define CHRONOCHAT_CONTROLLER_BACKEND_HPP |
| 13 | |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 14 | #include <QString> |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 15 | #include <QThread> |
| 16 | #include <QStringList> |
| 17 | #include <QMutex> |
| 18 | |
| 19 | #ifndef Q_MOC_RUN |
| 20 | #include "common.hpp" |
| 21 | #include "contact-manager.hpp" |
| 22 | #include "invitation.hpp" |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 23 | #include <ndn-cxx/security/key-chain.hpp> |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 24 | #include <ndn-cxx/ims/in-memory-storage-persistent.hpp> |
Muktadir R Chowdhury | 5119f3d | 2015-11-10 15:15:40 -0600 | [diff] [blame] | 25 | #include <ndn-cxx/security/validator-null.hpp> |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 26 | #include <ndn-cxx/face.hpp> |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 27 | #include <boost/thread.hpp> |
| 28 | #include <mutex> |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 29 | #endif |
| 30 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 31 | namespace chronochat { |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 32 | |
| 33 | class ControllerBackend : public QThread |
| 34 | { |
| 35 | Q_OBJECT |
| 36 | |
| 37 | public: |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 38 | ControllerBackend(QObject* parent = nullptr); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 39 | |
| 40 | ~ControllerBackend(); |
| 41 | |
| 42 | ContactManager* |
| 43 | getContactManager() |
| 44 | { |
| 45 | return &m_contactManager; |
| 46 | } |
| 47 | |
| 48 | protected: |
| 49 | void |
| 50 | run(); |
| 51 | |
| 52 | private: |
| 53 | void |
| 54 | setInvitationListener(); |
| 55 | |
| 56 | ndn::Name |
| 57 | getInvitationRoutingPrefix(); |
| 58 | |
| 59 | void |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 60 | onInvitationInterest(const ndn::Name& prefix, const ndn::Interest& interest, |
| 61 | size_t routingPrefixOffset); |
| 62 | |
| 63 | void |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 64 | onInvitationRequestInterest(const ndn::Name& prefix, const ndn::Interest& interest, |
| 65 | size_t routingPrefixOffset); |
| 66 | |
| 67 | void |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 68 | onInvitationRegisterFailed(const Name& prefix, const std::string& failInfo); |
| 69 | |
| 70 | void |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 71 | onInvitationValidated(const Interest& interest); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 72 | |
| 73 | void |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 74 | onInvitationValidationFailed(const Interest& interest, |
| 75 | const ndn::security::ValidationError& failureInfo); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 76 | |
| 77 | void |
Yingdi Yu | 7ff31f0 | 2015-02-05 11:21:07 -0800 | [diff] [blame] | 78 | onLocalPrefix(const ndn::ConstBufferPtr& data); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 79 | |
| 80 | void |
Yingdi Yu | 7ff31f0 | 2015-02-05 11:21:07 -0800 | [diff] [blame] | 81 | onLocalPrefixError(uint32_t code, const std::string& msg); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 82 | |
| 83 | void |
| 84 | updateLocalPrefix(const Name& localPrefix); |
| 85 | |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 86 | void |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 87 | onRequestResponse(const Interest& interest, const Data& data); |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 88 | |
| 89 | void |
| 90 | onRequestTimeout(const Interest& interest, int& resendTimes); |
| 91 | |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 92 | signals: |
| 93 | void |
| 94 | identityUpdated(const QString& identity); |
| 95 | |
| 96 | void |
| 97 | localPrefixUpdated(const QString& localPrefix); |
| 98 | |
| 99 | void |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 100 | invitationValidated(QString alias, QString chatroom, ndn::Name invitationINterest); |
| 101 | |
| 102 | void |
| 103 | invitationRequestReceived(QString alias, QString chatroom, ndn::Name invitationRequestInterest); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 104 | |
| 105 | void |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 106 | startChatroomOnInvitation(chronochat::Invitation invitation, bool secured); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 107 | |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 108 | void |
| 109 | startChatroom(const QString& chatroomName, bool secured); |
| 110 | |
| 111 | void |
| 112 | invitationRequestResult(const std::string& msg); |
| 113 | |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 114 | void |
| 115 | nfdError(); |
| 116 | |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 117 | public slots: |
| 118 | void |
| 119 | shutdown(); |
| 120 | |
| 121 | void |
| 122 | addChatroom(QString chatroom); |
| 123 | |
| 124 | void |
| 125 | removeChatroom(QString chatroom); |
| 126 | |
| 127 | void |
| 128 | onUpdateLocalPrefixAction(); |
| 129 | |
| 130 | void |
| 131 | onIdentityChanged(const QString& identity); |
| 132 | |
| 133 | void |
| 134 | onInvitationResponded(const ndn::Name& invitationName, bool accepted); |
| 135 | |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 136 | void |
| 137 | onInvitationRequestResponded(const ndn::Name& invitationName, bool accepted); |
| 138 | |
| 139 | void |
| 140 | onSendInvitationRequest(const QString& chatroomName, const QString& prefix); |
| 141 | |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 142 | void |
| 143 | onNfdReconnect(); |
| 144 | |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 145 | private slots: |
| 146 | void |
| 147 | onContactIdListReady(const QStringList& list); |
| 148 | |
| 149 | private: |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 150 | bool m_isNfdConnected; |
| 151 | bool m_shouldResume; |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 152 | ndn::Face m_face; |
| 153 | |
| 154 | Name m_identity; //TODO: set/get |
| 155 | |
| 156 | Name m_localPrefix; |
| 157 | |
| 158 | // Contact Manager |
| 159 | ContactManager m_contactManager; |
| 160 | |
| 161 | // Security related; |
| 162 | ndn::KeyChain m_keyChain; |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 163 | shared_ptr<ndn::security::Validator> m_validator; |
| 164 | ndn::security::ValidatorNull m_nullValidator; |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 165 | |
| 166 | // RegisteredPrefixId |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame^] | 167 | ndn::ScopedRegisteredPrefixHandle m_invitationListenerHandle; |
| 168 | ndn::ScopedRegisteredPrefixHandle m_requestListenerHandle; |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 169 | |
| 170 | // ChatRoomList |
| 171 | QStringList m_chatDialogList; |
| 172 | |
| 173 | QMutex m_mutex; |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 174 | std::mutex m_resumeMutex; |
| 175 | std::mutex m_nfdConnectionMutex; |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 176 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 177 | ndn::InMemoryStoragePersistent m_ims; |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 178 | }; |
| 179 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 180 | } // namespace chronochat |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 181 | |
| 182 | #endif // CHRONOCHAT_CONTROLLER_BACKEND_HPP |