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