blob: e0c8b0cd3c0af340694614ca63b72ad988d01662 [file] [log] [blame]
Yingdi Yud45777b2014-10-16 23:54:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
Varun Patila24bd3e2020-11-24 10:08:33 +05303 * Copyright (c) 2020, Regents of the University of California
Yingdi Yud45777b2014-10-16 23:54:11 -07004 *
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
Varun Patila24bd3e2020-11-24 10:08:33 +053028class NodeInfo
29{
Yingdi Yud45777b2014-10-16 23:54:11 -070030public:
31 QString sessionPrefix;
32 chronosync::SeqNo seqNo;
33};
34
Varun Patila24bd3e2020-11-24 10:08:33 +053035class UserInfo
36{
Yingdi Yud45777b2014-10-16 23:54:11 -070037public:
38 ndn::Name sessionPrefix;
39 bool hasNick;
40 std::string userNick;
Varun Patila24bd3e2020-11-24 10:08:33 +053041 ndn::scheduler::ScopedEventId timeoutEventId;
Yingdi Yud45777b2014-10-16 23:54:11 -070042};
43
44class ChatDialogBackend : public QThread
45{
46 Q_OBJECT
47
48public:
49 ChatDialogBackend(const Name& chatroomPrefix,
50 const Name& userChatPrefix,
51 const Name& routingPrefix,
52 const std::string& chatroomName,
53 const std::string& nick,
Yingdi Yu45da92a2015-02-02 13:17:03 -080054 const Name& signingId = Name(),
Yingdi Yuf3401182015-02-02 20:21:07 -080055 QObject* parent = nullptr);
Yingdi Yud45777b2014-10-16 23:54:11 -070056
57 ~ChatDialogBackend();
58
59protected:
60 void
61 run();
62
63private:
64 void
65 initializeSync();
66
67 void
Yingdi Yuf3401182015-02-02 20:21:07 -080068 exitChatroom();
69
70 void
Yingdi Yu4647f022015-02-01 00:26:38 -080071 close();
72
73 void
Yingdi Yud45777b2014-10-16 23:54:11 -070074 processSyncUpdate(const std::vector<chronosync::MissingDataInfo>& updates);
75
76 void
Varun Patil3d850902020-11-23 12:19:14 +053077 processChatData(const ndn::Data& data,
Yingdi Yu45da92a2015-02-02 13:17:03 -080078 bool needDisplay,
79 bool isValidated);
Yingdi Yud45777b2014-10-16 23:54:11 -070080
81 void
82 remoteSessionTimeout(const Name& sessionPrefix);
83
84 void
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080085 sendMsg(ChatMessage& msg);
Yingdi Yud45777b2014-10-16 23:54:11 -070086
87 void
88 sendJoin();
89
90 void
91 sendHello();
92
93 void
94 sendLeave();
95
96 void
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080097 prepareControlMessage(ChatMessage& msg,
98 ChatMessage::ChatMessageType type);
Yingdi Yud45777b2014-10-16 23:54:11 -070099
100 void
101 prepareChatMessage(const QString& text,
102 time_t timestamp,
Varun Patila24bd3e2020-11-24 10:08:33 +0530103 ChatMessage& msg);
Yingdi Yud45777b2014-10-16 23:54:11 -0700104
105 void
106 updatePrefixes();
107
Yingdi Yud45777b2014-10-16 23:54:11 -0700108signals:
109 void
Yingdi Yueb692ac2015-02-10 18:46:18 -0800110 syncTreeUpdated(std::vector<chronochat::NodeInfo> updates, QString digest);
Yingdi Yud45777b2014-10-16 23:54:11 -0700111
112 void
113 chatMessageReceived(QString nick, QString text, time_t timestamp);
114
115 void
Yingdi Yud45777b2014-10-16 23:54:11 -0700116 sessionRemoved(QString sessionPrefix, QString nick, time_t timestamp);
117
118 void
Qiuhan Ding7a4e7ef2015-02-03 20:25:50 -0800119 messageReceived(QString sessionPrefix, QString nick, uint64_t seqNo, time_t timestamp,
120 bool addSession);
Yingdi Yud45777b2014-10-16 23:54:11 -0700121
122 void
123 chatPrefixChanged(ndn::Name newChatPrefix);
124
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800125 void
Yingdi Yuf3401182015-02-02 20:21:07 -0800126 refreshChatDialog(ndn::Name chatPrefix);
127
128 void
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800129 eraseInRoster(ndn::Name sessionPrefix, ndn::Name::Component chatroomName);
130
131 void
132 addInRoster(ndn::Name sessionPrefix, ndn::Name::Component chatroomName);
133
Yingdi Yuf3401182015-02-02 20:21:07 -0800134 void
135 newChatroomForDiscovery(ndn::Name::Component chatroomName);
136
137 void
138 nfdError();
139
Yingdi Yud45777b2014-10-16 23:54:11 -0700140public slots:
141 void
142 sendChatMessage(QString text, time_t timestamp);
143
144 void
145 updateRoutingPrefix(const QString& localRoutingPrefix);
146
147 void
148 shutdown();
149
Yingdi Yuf3401182015-02-02 20:21:07 -0800150 void
151 onNfdReconnect();
152
Yingdi Yud45777b2014-10-16 23:54:11 -0700153private:
154 typedef std::map<ndn::Name, UserInfo> BackendRoster;
155
Yingdi Yuf3401182015-02-02 20:21:07 -0800156 bool m_shouldResume;
157 bool m_isNfdConnected;
Yingdi Yu45da92a2015-02-02 13:17:03 -0800158 shared_ptr<ndn::Face> m_face;
Yingdi Yud45777b2014-10-16 23:54:11 -0700159
Varun Patil3d850902020-11-23 12:19:14 +0530160 Name m_localRoutingPrefix; // routable local prefix
161 Name m_chatroomPrefix; // chatroom sync prefix
162 Name m_userChatPrefix; // user chat prefix
163 Name m_routableUserChatPrefix; // routable user chat prefix
Yingdi Yud45777b2014-10-16 23:54:11 -0700164
Varun Patil3d850902020-11-23 12:19:14 +0530165 std::string m_chatroomName; // chatroom name
166 std::string m_nick; // user nick
Yingdi Yud45777b2014-10-16 23:54:11 -0700167
Varun Patil3d850902020-11-23 12:19:14 +0530168 Name m_signingId; // signing identity
169 shared_ptr<ndn::security::ValidatorConfig> m_validator; // validator
170 shared_ptr<chronosync::Socket> m_sock; // SyncSocket
Yingdi Yud45777b2014-10-16 23:54:11 -0700171
Varun Patil3d850902020-11-23 12:19:14 +0530172 unique_ptr<ndn::Scheduler> m_scheduler; // scheduler
173 ndn::scheduler::EventId m_helloEventId; // event id of timeout
Yingdi Yud45777b2014-10-16 23:54:11 -0700174
Varun Patil3d850902020-11-23 12:19:14 +0530175 bool m_joined; // true if in a chatroom
Yingdi Yud45777b2014-10-16 23:54:11 -0700176
Varun Patil3d850902020-11-23 12:19:14 +0530177 BackendRoster m_roster; // User roster
Yingdi Yu4647f022015-02-01 00:26:38 -0800178
Yingdi Yuf3401182015-02-02 20:21:07 -0800179 std::mutex m_resumeMutex;
180 std::mutex m_nfdConnectionMutex;
Yingdi Yud45777b2014-10-16 23:54:11 -0700181};
182
Yingdi Yueb692ac2015-02-10 18:46:18 -0800183} // namespace chronochat
Yingdi Yud45777b2014-10-16 23:54:11 -0700184
185#endif // CHRONOCHAT_CHAT_DIALOG_BACKEND_HPP