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 |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 114 | sessionRemoved(QString sessionPrefix, QString nick, time_t timestamp); |
| 115 | |
| 116 | void |
Qiuhan Ding | 7a4e7ef | 2015-02-03 20:25:50 -0800 | [diff] [blame^] | 117 | messageReceived(QString sessionPrefix, QString nick, uint64_t seqNo, time_t timestamp, |
| 118 | bool addSession); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 119 | |
| 120 | void |
| 121 | chatPrefixChanged(ndn::Name newChatPrefix); |
| 122 | |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 123 | void |
| 124 | eraseInRoster(ndn::Name sessionPrefix, ndn::Name::Component chatroomName); |
| 125 | |
| 126 | void |
| 127 | addInRoster(ndn::Name sessionPrefix, ndn::Name::Component chatroomName); |
| 128 | |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 129 | public slots: |
| 130 | void |
| 131 | sendChatMessage(QString text, time_t timestamp); |
| 132 | |
| 133 | void |
| 134 | updateRoutingPrefix(const QString& localRoutingPrefix); |
| 135 | |
| 136 | void |
| 137 | shutdown(); |
| 138 | |
| 139 | private: |
| 140 | typedef std::map<ndn::Name, UserInfo> BackendRoster; |
| 141 | |
Yingdi Yu | 45da92a | 2015-02-02 13:17:03 -0800 | [diff] [blame] | 142 | shared_ptr<ndn::Face> m_face; |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 143 | |
| 144 | Name m_localRoutingPrefix; // routable local prefix |
| 145 | Name m_chatroomPrefix; // chatroom sync prefix |
| 146 | Name m_userChatPrefix; // user chat prefix |
| 147 | Name m_routableUserChatPrefix; // routable user chat prefix |
| 148 | |
| 149 | std::string m_chatroomName; // chatroom name |
| 150 | std::string m_nick; // user nick |
| 151 | |
Yingdi Yu | 45da92a | 2015-02-02 13:17:03 -0800 | [diff] [blame] | 152 | Name m_signingId; // signing identity |
| 153 | shared_ptr<ndn::Validator> m_validator;// validator |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 154 | shared_ptr<chronosync::Socket> m_sock; // SyncSocket |
| 155 | |
Yingdi Yu | 4647f02 | 2015-02-01 00:26:38 -0800 | [diff] [blame] | 156 | unique_ptr<ndn::Scheduler> m_scheduler;// scheduler |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 157 | ndn::EventId m_helloEventId; // event id of timeout |
| 158 | |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 159 | bool m_joined; // true if in a chatroom |
| 160 | |
| 161 | BackendRoster m_roster; // User roster |
Yingdi Yu | 4647f02 | 2015-02-01 00:26:38 -0800 | [diff] [blame] | 162 | |
Qiuhan Ding | f22c41b | 2015-03-11 13:19:01 -0700 | [diff] [blame] | 163 | std::mutex m_mutex; |
Yingdi Yu | 4647f02 | 2015-02-01 00:26:38 -0800 | [diff] [blame] | 164 | bool m_shouldResume; |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 165 | }; |
| 166 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 167 | } // namespace chronochat |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 168 | |
| 169 | #endif // CHRONOCHAT_CHAT_DIALOG_BACKEND_HPP |