Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -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 | * |
| 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" |
| 23 | #include "validator-invitation.hpp" |
| 24 | #include <ndn-cxx/security/key-chain.hpp> |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 25 | #include <ndn-cxx/util/in-memory-storage-persistent.hpp> |
Muktadir R Chowdhury | 5119f3d | 2015-11-10 15:15:40 -0600 | [diff] [blame^] | 26 | #include <ndn-cxx/security/validator-null.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 |
| 60 | onInvitationPrefixReset(); |
| 61 | |
| 62 | void |
| 63 | onInvitationPrefixResetFailed(const std::string& failInfo); |
| 64 | |
| 65 | void |
| 66 | onInvitationInterest(const ndn::Name& prefix, const ndn::Interest& interest, |
| 67 | size_t routingPrefixOffset); |
| 68 | |
| 69 | void |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 70 | onInvitationRequestInterest(const ndn::Name& prefix, const ndn::Interest& interest, |
| 71 | size_t routingPrefixOffset); |
| 72 | |
| 73 | void |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 74 | onInvitationRegisterFailed(const Name& prefix, const std::string& failInfo); |
| 75 | |
| 76 | void |
| 77 | onInvitationValidated(const shared_ptr<const Interest>& interest); |
| 78 | |
| 79 | void |
| 80 | onInvitationValidationFailed(const shared_ptr<const Interest>& interest, |
| 81 | std::string failureInfo); |
| 82 | |
| 83 | void |
Yingdi Yu | 7ff31f0 | 2015-02-05 11:21:07 -0800 | [diff] [blame] | 84 | onLocalPrefix(const ndn::ConstBufferPtr& data); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 85 | |
| 86 | void |
Yingdi Yu | 7ff31f0 | 2015-02-05 11:21:07 -0800 | [diff] [blame] | 87 | onLocalPrefixError(uint32_t code, const std::string& msg); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 88 | |
| 89 | void |
| 90 | updateLocalPrefix(const Name& localPrefix); |
| 91 | |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 92 | void |
| 93 | onRequestResponse(const Interest& interest, Data& data); |
| 94 | |
| 95 | void |
| 96 | onRequestTimeout(const Interest& interest, int& resendTimes); |
| 97 | |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 98 | signals: |
| 99 | void |
| 100 | identityUpdated(const QString& identity); |
| 101 | |
| 102 | void |
| 103 | localPrefixUpdated(const QString& localPrefix); |
| 104 | |
| 105 | void |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 106 | invitationValidated(QString alias, QString chatroom, ndn::Name invitationINterest); |
| 107 | |
| 108 | void |
| 109 | invitationRequestReceived(QString alias, QString chatroom, ndn::Name invitationRequestInterest); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 110 | |
| 111 | void |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 112 | startChatroomOnInvitation(chronochat::Invitation invitation, bool secured); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 113 | |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 114 | void |
| 115 | startChatroom(const QString& chatroomName, bool secured); |
| 116 | |
| 117 | void |
| 118 | invitationRequestResult(const std::string& msg); |
| 119 | |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 120 | void |
| 121 | nfdError(); |
| 122 | |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 123 | public slots: |
| 124 | void |
| 125 | shutdown(); |
| 126 | |
| 127 | void |
| 128 | addChatroom(QString chatroom); |
| 129 | |
| 130 | void |
| 131 | removeChatroom(QString chatroom); |
| 132 | |
| 133 | void |
| 134 | onUpdateLocalPrefixAction(); |
| 135 | |
| 136 | void |
| 137 | onIdentityChanged(const QString& identity); |
| 138 | |
| 139 | void |
| 140 | onInvitationResponded(const ndn::Name& invitationName, bool accepted); |
| 141 | |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 142 | void |
| 143 | onInvitationRequestResponded(const ndn::Name& invitationName, bool accepted); |
| 144 | |
| 145 | void |
| 146 | onSendInvitationRequest(const QString& chatroomName, const QString& prefix); |
| 147 | |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 148 | void |
| 149 | onNfdReconnect(); |
| 150 | |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 151 | private slots: |
| 152 | void |
| 153 | onContactIdListReady(const QStringList& list); |
| 154 | |
| 155 | private: |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 156 | bool m_isNfdConnected; |
| 157 | bool m_shouldResume; |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 158 | ndn::Face m_face; |
| 159 | |
| 160 | Name m_identity; //TODO: set/get |
| 161 | |
| 162 | Name m_localPrefix; |
| 163 | |
| 164 | // Contact Manager |
| 165 | ContactManager m_contactManager; |
| 166 | |
| 167 | // Security related; |
| 168 | ndn::KeyChain m_keyChain; |
| 169 | ValidatorInvitation m_validator; |
Muktadir R Chowdhury | 5119f3d | 2015-11-10 15:15:40 -0600 | [diff] [blame^] | 170 | ndn::ValidatorNull m_nullValidator; |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 171 | |
| 172 | // RegisteredPrefixId |
| 173 | const ndn::RegisteredPrefixId* m_invitationListenerId; |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 174 | const ndn::RegisteredPrefixId* m_requestListenerId; |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 175 | |
| 176 | // ChatRoomList |
| 177 | QStringList m_chatDialogList; |
| 178 | |
| 179 | QMutex m_mutex; |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 180 | std::mutex m_resumeMutex; |
| 181 | std::mutex m_nfdConnectionMutex; |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 182 | |
| 183 | ndn::util::InMemoryStoragePersistent m_ims; |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 184 | }; |
| 185 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 186 | } // namespace chronochat |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 187 | |
| 188 | #endif // CHRONOCHAT_CONTROLLER_BACKEND_HPP |