Yingdi Yu | d45777b | 2014-10-16 23:54:11 -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 | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #ifndef CHRONOCHAT_CHAT_DIALOG_BACKEND_HPP |
| 12 | #define CHRONOCHAT_CHAT_DIALOG_BACKEND_HPP |
| 13 | |
| 14 | #include <QThread> |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 15 | |
| 16 | #ifndef Q_MOC_RUN |
| 17 | #include "common.hpp" |
| 18 | #include "chatroom-info.hpp" |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 19 | #include "chat-message.hpp" |
Qiuhan Ding | f22c41b | 2015-03-11 13:19:01 -0700 | [diff] [blame] | 20 | #include <mutex> |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 21 | #include <ChronoSync/socket.hpp> |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 22 | #include <boost/thread.hpp> |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 23 | #include <ndn-cxx/security/validator-config.hpp> |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 24 | #endif |
| 25 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 26 | namespace chronochat { |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 27 | |
| 28 | class NodeInfo { |
| 29 | public: |
| 30 | QString sessionPrefix; |
| 31 | chronosync::SeqNo seqNo; |
| 32 | }; |
| 33 | |
| 34 | class UserInfo { |
| 35 | public: |
| 36 | ndn::Name sessionPrefix; |
| 37 | bool hasNick; |
| 38 | std::string userNick; |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 39 | ndn::scheduler::EventId timeoutEventId; |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | class ChatDialogBackend : public QThread |
| 43 | { |
| 44 | Q_OBJECT |
| 45 | |
| 46 | public: |
| 47 | ChatDialogBackend(const Name& chatroomPrefix, |
| 48 | const Name& userChatPrefix, |
| 49 | const Name& routingPrefix, |
| 50 | const std::string& chatroomName, |
| 51 | const std::string& nick, |
Yingdi Yu | 45da92a | 2015-02-02 13:17:03 -0800 | [diff] [blame] | 52 | const Name& signingId = Name(), |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 53 | QObject* parent = nullptr); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 54 | |
| 55 | ~ChatDialogBackend(); |
| 56 | |
| 57 | protected: |
| 58 | void |
| 59 | run(); |
| 60 | |
| 61 | private: |
| 62 | void |
| 63 | initializeSync(); |
| 64 | |
| 65 | void |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 66 | exitChatroom(); |
| 67 | |
| 68 | void |
Yingdi Yu | 4647f02 | 2015-02-01 00:26:38 -0800 | [diff] [blame] | 69 | close(); |
| 70 | |
| 71 | void |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 72 | processSyncUpdate(const std::vector<chronosync::MissingDataInfo>& updates); |
| 73 | |
| 74 | void |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 75 | processChatData(const ndn::Data& data, |
Yingdi Yu | 45da92a | 2015-02-02 13:17:03 -0800 | [diff] [blame] | 76 | bool needDisplay, |
| 77 | bool isValidated); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 78 | |
| 79 | void |
| 80 | remoteSessionTimeout(const Name& sessionPrefix); |
| 81 | |
| 82 | void |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 83 | sendMsg(ChatMessage& msg); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 84 | |
| 85 | void |
| 86 | sendJoin(); |
| 87 | |
| 88 | void |
| 89 | sendHello(); |
| 90 | |
| 91 | void |
| 92 | sendLeave(); |
| 93 | |
| 94 | void |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 95 | prepareControlMessage(ChatMessage& msg, |
| 96 | ChatMessage::ChatMessageType type); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 97 | |
| 98 | void |
| 99 | prepareChatMessage(const QString& text, |
| 100 | time_t timestamp, |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 101 | ChatMessage &msg); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 102 | |
| 103 | void |
| 104 | updatePrefixes(); |
| 105 | |
| 106 | std::string |
| 107 | getHexEncodedDigest(ndn::ConstBufferPtr digest); |
| 108 | |
| 109 | signals: |
| 110 | void |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 111 | syncTreeUpdated(std::vector<chronochat::NodeInfo> updates, QString digest); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 112 | |
| 113 | void |
| 114 | chatMessageReceived(QString nick, QString text, time_t timestamp); |
| 115 | |
| 116 | void |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 117 | sessionRemoved(QString sessionPrefix, QString nick, time_t timestamp); |
| 118 | |
| 119 | void |
Qiuhan Ding | 7a4e7ef | 2015-02-03 20:25:50 -0800 | [diff] [blame] | 120 | messageReceived(QString sessionPrefix, QString nick, uint64_t seqNo, time_t timestamp, |
| 121 | bool addSession); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 122 | |
| 123 | void |
| 124 | chatPrefixChanged(ndn::Name newChatPrefix); |
| 125 | |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 126 | void |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 127 | refreshChatDialog(ndn::Name chatPrefix); |
| 128 | |
| 129 | void |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 130 | eraseInRoster(ndn::Name sessionPrefix, ndn::Name::Component chatroomName); |
| 131 | |
| 132 | void |
| 133 | addInRoster(ndn::Name sessionPrefix, ndn::Name::Component chatroomName); |
| 134 | |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 135 | void |
| 136 | newChatroomForDiscovery(ndn::Name::Component chatroomName); |
| 137 | |
| 138 | void |
| 139 | nfdError(); |
| 140 | |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 141 | public slots: |
| 142 | void |
| 143 | sendChatMessage(QString text, time_t timestamp); |
| 144 | |
| 145 | void |
| 146 | updateRoutingPrefix(const QString& localRoutingPrefix); |
| 147 | |
| 148 | void |
| 149 | shutdown(); |
| 150 | |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 151 | void |
| 152 | onNfdReconnect(); |
| 153 | |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 154 | private: |
| 155 | typedef std::map<ndn::Name, UserInfo> BackendRoster; |
| 156 | |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 157 | bool m_shouldResume; |
| 158 | bool m_isNfdConnected; |
Yingdi Yu | 45da92a | 2015-02-02 13:17:03 -0800 | [diff] [blame] | 159 | shared_ptr<ndn::Face> m_face; |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 160 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 161 | Name m_localRoutingPrefix; // routable local prefix |
| 162 | Name m_chatroomPrefix; // chatroom sync prefix |
| 163 | Name m_userChatPrefix; // user chat prefix |
| 164 | Name m_routableUserChatPrefix; // routable user chat prefix |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 165 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 166 | std::string m_chatroomName; // chatroom name |
| 167 | std::string m_nick; // user nick |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 168 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 169 | Name m_signingId; // signing identity |
| 170 | shared_ptr<ndn::security::ValidatorConfig> m_validator; // validator |
| 171 | shared_ptr<chronosync::Socket> m_sock; // SyncSocket |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 172 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 173 | unique_ptr<ndn::Scheduler> m_scheduler; // scheduler |
| 174 | ndn::scheduler::EventId m_helloEventId; // event id of timeout |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 175 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 176 | bool m_joined; // true if in a chatroom |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 177 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame^] | 178 | BackendRoster m_roster; // User roster |
Yingdi Yu | 4647f02 | 2015-02-01 00:26:38 -0800 | [diff] [blame] | 179 | |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 180 | std::mutex m_resumeMutex; |
| 181 | std::mutex m_nfdConnectionMutex; |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 182 | }; |
| 183 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 184 | } // namespace chronochat |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 185 | |
| 186 | #endif // CHRONOCHAT_CHAT_DIALOG_BACKEND_HPP |