blob: e9543bb5aadb22b2b2e9a82de52ee108892d9e82 [file] [log] [blame]
Yingdi Yud45777b2014-10-16 23:54:11 -07001/* -*- 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 Yuf3401182015-02-02 20:21:07 -08008 * Qiuhan Ding <qiuhanding@cs.ucla.edu>
Yingdi Yud45777b2014-10-16 23:54:11 -07009 */
10
11#ifndef CHRONOCHAT_CHAT_DIALOG_BACKEND_HPP
12#define CHRONOCHAT_CHAT_DIALOG_BACKEND_HPP
13
14#include <QThread>
Yingdi Yud45777b2014-10-16 23:54:11 -070015
16#ifndef Q_MOC_RUN
17#include "common.hpp"
18#include "chatroom-info.hpp"
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080019#include "chat-message.hpp"
Qiuhan Dingf22c41b2015-03-11 13:19:01 -070020#include <mutex>
Varun Patil3d850902020-11-23 12:19:14 +053021#include <ChronoSync/socket.hpp>
Yingdi Yuf3401182015-02-02 20:21:07 -080022#include <boost/thread.hpp>
Varun Patil3d850902020-11-23 12:19:14 +053023#include <ndn-cxx/security/validator-config.hpp>
Yingdi Yud45777b2014-10-16 23:54:11 -070024#endif
25
Yingdi Yueb692ac2015-02-10 18:46:18 -080026namespace chronochat {
Yingdi Yud45777b2014-10-16 23:54:11 -070027
28class NodeInfo {
29public:
30 QString sessionPrefix;
31 chronosync::SeqNo seqNo;
32};
33
34class UserInfo {
35public:
36 ndn::Name sessionPrefix;
37 bool hasNick;
38 std::string userNick;
Varun Patil3d850902020-11-23 12:19:14 +053039 ndn::scheduler::EventId timeoutEventId;
Yingdi Yud45777b2014-10-16 23:54:11 -070040};
41
42class ChatDialogBackend : public QThread
43{
44 Q_OBJECT
45
46public:
47 ChatDialogBackend(const Name& chatroomPrefix,
48 const Name& userChatPrefix,
49 const Name& routingPrefix,
50 const std::string& chatroomName,
51 const std::string& nick,
Yingdi Yu45da92a2015-02-02 13:17:03 -080052 const Name& signingId = Name(),
Yingdi Yuf3401182015-02-02 20:21:07 -080053 QObject* parent = nullptr);
Yingdi Yud45777b2014-10-16 23:54:11 -070054
55 ~ChatDialogBackend();
56
57protected:
58 void
59 run();
60
61private:
62 void
63 initializeSync();
64
65 void
Yingdi Yuf3401182015-02-02 20:21:07 -080066 exitChatroom();
67
68 void
Yingdi Yu4647f022015-02-01 00:26:38 -080069 close();
70
71 void
Yingdi Yud45777b2014-10-16 23:54:11 -070072 processSyncUpdate(const std::vector<chronosync::MissingDataInfo>& updates);
73
74 void
Varun Patil3d850902020-11-23 12:19:14 +053075 processChatData(const ndn::Data& data,
Yingdi Yu45da92a2015-02-02 13:17:03 -080076 bool needDisplay,
77 bool isValidated);
Yingdi Yud45777b2014-10-16 23:54:11 -070078
79 void
80 remoteSessionTimeout(const Name& sessionPrefix);
81
82 void
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080083 sendMsg(ChatMessage& msg);
Yingdi Yud45777b2014-10-16 23:54:11 -070084
85 void
86 sendJoin();
87
88 void
89 sendHello();
90
91 void
92 sendLeave();
93
94 void
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080095 prepareControlMessage(ChatMessage& msg,
96 ChatMessage::ChatMessageType type);
Yingdi Yud45777b2014-10-16 23:54:11 -070097
98 void
99 prepareChatMessage(const QString& text,
100 time_t timestamp,
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800101 ChatMessage &msg);
Yingdi Yud45777b2014-10-16 23:54:11 -0700102
103 void
104 updatePrefixes();
105
106 std::string
107 getHexEncodedDigest(ndn::ConstBufferPtr digest);
108
109signals:
110 void
Yingdi Yueb692ac2015-02-10 18:46:18 -0800111 syncTreeUpdated(std::vector<chronochat::NodeInfo> updates, QString digest);
Yingdi Yud45777b2014-10-16 23:54:11 -0700112
113 void
114 chatMessageReceived(QString nick, QString text, time_t timestamp);
115
116 void
Yingdi Yud45777b2014-10-16 23:54:11 -0700117 sessionRemoved(QString sessionPrefix, QString nick, time_t timestamp);
118
119 void
Qiuhan Ding7a4e7ef2015-02-03 20:25:50 -0800120 messageReceived(QString sessionPrefix, QString nick, uint64_t seqNo, time_t timestamp,
121 bool addSession);
Yingdi Yud45777b2014-10-16 23:54:11 -0700122
123 void
124 chatPrefixChanged(ndn::Name newChatPrefix);
125
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800126 void
Yingdi Yuf3401182015-02-02 20:21:07 -0800127 refreshChatDialog(ndn::Name chatPrefix);
128
129 void
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800130 eraseInRoster(ndn::Name sessionPrefix, ndn::Name::Component chatroomName);
131
132 void
133 addInRoster(ndn::Name sessionPrefix, ndn::Name::Component chatroomName);
134
Yingdi Yuf3401182015-02-02 20:21:07 -0800135 void
136 newChatroomForDiscovery(ndn::Name::Component chatroomName);
137
138 void
139 nfdError();
140
Yingdi Yud45777b2014-10-16 23:54:11 -0700141public 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 Yuf3401182015-02-02 20:21:07 -0800151 void
152 onNfdReconnect();
153
Yingdi Yud45777b2014-10-16 23:54:11 -0700154private:
155 typedef std::map<ndn::Name, UserInfo> BackendRoster;
156
Yingdi Yuf3401182015-02-02 20:21:07 -0800157 bool m_shouldResume;
158 bool m_isNfdConnected;
Yingdi Yu45da92a2015-02-02 13:17:03 -0800159 shared_ptr<ndn::Face> m_face;
Yingdi Yud45777b2014-10-16 23:54:11 -0700160
Varun Patil3d850902020-11-23 12:19:14 +0530161 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 Yud45777b2014-10-16 23:54:11 -0700165
Varun Patil3d850902020-11-23 12:19:14 +0530166 std::string m_chatroomName; // chatroom name
167 std::string m_nick; // user nick
Yingdi Yud45777b2014-10-16 23:54:11 -0700168
Varun Patil3d850902020-11-23 12:19:14 +0530169 Name m_signingId; // signing identity
170 shared_ptr<ndn::security::ValidatorConfig> m_validator; // validator
171 shared_ptr<chronosync::Socket> m_sock; // SyncSocket
Yingdi Yud45777b2014-10-16 23:54:11 -0700172
Varun Patil3d850902020-11-23 12:19:14 +0530173 unique_ptr<ndn::Scheduler> m_scheduler; // scheduler
174 ndn::scheduler::EventId m_helloEventId; // event id of timeout
Yingdi Yud45777b2014-10-16 23:54:11 -0700175
Varun Patil3d850902020-11-23 12:19:14 +0530176 bool m_joined; // true if in a chatroom
Yingdi Yud45777b2014-10-16 23:54:11 -0700177
Varun Patil3d850902020-11-23 12:19:14 +0530178 BackendRoster m_roster; // User roster
Yingdi Yu4647f022015-02-01 00:26:38 -0800179
Yingdi Yuf3401182015-02-02 20:21:07 -0800180 std::mutex m_resumeMutex;
181 std::mutex m_nfdConnectionMutex;
Yingdi Yud45777b2014-10-16 23:54:11 -0700182};
183
Yingdi Yueb692ac2015-02-10 18:46:18 -0800184} // namespace chronochat
Yingdi Yud45777b2014-10-16 23:54:11 -0700185
186#endif // CHRONOCHAT_CHAT_DIALOG_BACKEND_HPP