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> |
| 14 | #include <QMutex> |
| 15 | |
| 16 | #ifndef Q_MOC_RUN |
| 17 | #include "common.hpp" |
| 18 | #include "chatroom-info.hpp" |
| 19 | #include "chatbuf.pb.h" |
| 20 | #include <socket.hpp> |
| 21 | #endif |
| 22 | |
| 23 | namespace chronos { |
| 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, |
| 49 | QObject* parent = 0); |
| 50 | |
| 51 | ~ChatDialogBackend(); |
| 52 | |
| 53 | protected: |
| 54 | void |
| 55 | run(); |
| 56 | |
| 57 | private: |
| 58 | void |
| 59 | initializeSync(); |
| 60 | |
| 61 | void |
Yingdi Yu | 4647f02 | 2015-02-01 00:26:38 -0800 | [diff] [blame^] | 62 | close(); |
| 63 | |
| 64 | void |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 65 | processSyncUpdate(const std::vector<chronosync::MissingDataInfo>& updates); |
| 66 | |
| 67 | void |
| 68 | processChatData(const ndn::shared_ptr<const ndn::Data>& data, bool needDisplay); |
| 69 | |
| 70 | void |
| 71 | remoteSessionTimeout(const Name& sessionPrefix); |
| 72 | |
| 73 | void |
| 74 | sendMsg(SyncDemo::ChatMessage& msg); |
| 75 | |
| 76 | void |
| 77 | sendJoin(); |
| 78 | |
| 79 | void |
| 80 | sendHello(); |
| 81 | |
| 82 | void |
| 83 | sendLeave(); |
| 84 | |
| 85 | void |
| 86 | prepareControlMessage(SyncDemo::ChatMessage& msg, |
| 87 | SyncDemo::ChatMessage::ChatMessageType type); |
| 88 | |
| 89 | void |
| 90 | prepareChatMessage(const QString& text, |
| 91 | time_t timestamp, |
| 92 | SyncDemo::ChatMessage &msg); |
| 93 | |
| 94 | void |
| 95 | updatePrefixes(); |
| 96 | |
| 97 | std::string |
| 98 | getHexEncodedDigest(ndn::ConstBufferPtr digest); |
| 99 | |
| 100 | signals: |
| 101 | void |
| 102 | syncTreeUpdated(std::vector<chronos::NodeInfo> updates, QString digest); |
| 103 | |
| 104 | void |
| 105 | chatMessageReceived(QString nick, QString text, time_t timestamp); |
| 106 | |
| 107 | void |
| 108 | sessionAdded(QString sessionPrefix, QString nick, time_t timestamp); |
| 109 | |
| 110 | void |
| 111 | sessionRemoved(QString sessionPrefix, QString nick, time_t timestamp); |
| 112 | |
| 113 | void |
| 114 | nickUpdated(QString sessionPrefix, QString nick); |
| 115 | |
| 116 | void |
| 117 | messageReceived(QString sessionPrefix); |
| 118 | |
| 119 | void |
| 120 | chatPrefixChanged(ndn::Name newChatPrefix); |
| 121 | |
| 122 | public slots: |
| 123 | void |
| 124 | sendChatMessage(QString text, time_t timestamp); |
| 125 | |
| 126 | void |
| 127 | updateRoutingPrefix(const QString& localRoutingPrefix); |
| 128 | |
| 129 | void |
| 130 | shutdown(); |
| 131 | |
| 132 | private: |
| 133 | typedef std::map<ndn::Name, UserInfo> BackendRoster; |
| 134 | |
Yingdi Yu | 4647f02 | 2015-02-01 00:26:38 -0800 | [diff] [blame^] | 135 | unique_ptr<ndn::Face> m_face; |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 136 | |
| 137 | Name m_localRoutingPrefix; // routable local prefix |
| 138 | Name m_chatroomPrefix; // chatroom sync prefix |
| 139 | Name m_userChatPrefix; // user chat prefix |
| 140 | Name m_routableUserChatPrefix; // routable user chat prefix |
| 141 | |
| 142 | std::string m_chatroomName; // chatroom name |
| 143 | std::string m_nick; // user nick |
| 144 | |
| 145 | shared_ptr<chronosync::Socket> m_sock; // SyncSocket |
| 146 | |
Yingdi Yu | 4647f02 | 2015-02-01 00:26:38 -0800 | [diff] [blame^] | 147 | unique_ptr<ndn::Scheduler> m_scheduler;// scheduler |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 148 | ndn::EventId m_helloEventId; // event id of timeout |
| 149 | |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 150 | bool m_joined; // true if in a chatroom |
| 151 | |
| 152 | BackendRoster m_roster; // User roster |
Yingdi Yu | 4647f02 | 2015-02-01 00:26:38 -0800 | [diff] [blame^] | 153 | |
| 154 | QMutex m_mutex; |
| 155 | bool m_shouldResume; |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 156 | }; |
| 157 | |
| 158 | } // namespace chronos |
| 159 | |
| 160 | #endif // CHRONOCHAT_CHAT_DIALOG_BACKEND_HPP |