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