blob: 0c40ee4bdf6bf7f15a4a684141a63e383cd1c3ed [file] [log] [blame]
Qiuhan Ding43c8e162015-02-02 15:16:48 -08001/* -*- 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
Qiuhan Ding43c8e162015-02-02 15:16:48 -08004 *
5 * BSD license, See the LICENSE file for more information
6 *
7 * Author: Qiuhan Ding <qiuhanding@cs.ucla.edu>
8 * Yingdi Yu <yingdi@cs.ucla.edu>
9 */
10
11#ifndef CHRONOCHAT_CHATROOM_DISCOVERY_BACKEND_HPP
12#define CHRONOCHAT_CHATROOM_DISCOVERY_BACKEND_HPP
13
Qiuhan Ding43c8e162015-02-02 15:16:48 -080014#include <QThread>
Qiuhan Ding43c8e162015-02-02 15:16:48 -080015
16#ifndef Q_MOC_RUN
17#include "common.hpp"
18#include "chatroom-info.hpp"
Qiuhan Dingf22c41b2015-03-11 13:19:01 -070019#include <boost/random.hpp>
20#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>
Qiuhan Ding43c8e162015-02-02 15:16:48 -080023#endif
24
25namespace chronochat {
26
Varun Patila24bd3e2020-11-24 10:08:33 +053027class ChatroomInfoBackend
28{
Qiuhan Ding43c8e162015-02-02 15:16:48 -080029public:
30 std::string chatroomName;
31 Name chatroomPrefix;
32 ChatroomInfo info;
33 // For a chatroom's user to check whether his own chatroom is alive
Varun Patil3d850902020-11-23 12:19:14 +053034 ndn::scheduler::EventId localChatroomTimeoutEventId;
Qiuhan Ding43c8e162015-02-02 15:16:48 -080035 // If the manager no longer exist, set a random timer to compete for manager
Varun Patil3d850902020-11-23 12:19:14 +053036 ndn::scheduler::EventId managerSelectionTimeoutEventId;
Qiuhan Ding43c8e162015-02-02 15:16:48 -080037 // For a user to check the status of the chatroom that he is not in.
Varun Patil3d850902020-11-23 12:19:14 +053038 ndn::scheduler::EventId remoteChatroomTimeoutEventId;
Qiuhan Ding43c8e162015-02-02 15:16:48 -080039 // If the user is manager, he will need the helloEventId to keep track of hello message
Varun Patila24bd3e2020-11-24 10:08:33 +053040 ndn::scheduler::ScopedEventId helloTimeoutEventId;
Qiuhan Ding43c8e162015-02-02 15:16:48 -080041 // To tell whether the user is in this chatroom
42 bool isParticipant;
43 // To tell whether the user is the manager
44 bool isManager;
45 // Variable to tell whether another manager exists
46 int count;
47 // Variable to tell whether to print on the panel
48 bool isPrint;
49
50};
51
52class ChatroomDiscoveryBackend : public QThread
53{
54 Q_OBJECT
55
56public:
57 ChatroomDiscoveryBackend(const Name& routingPrefix,
58 const Name& identity,
Yingdi Yuf3401182015-02-02 20:21:07 -080059 QObject* parent = nullptr);
Qiuhan Ding43c8e162015-02-02 15:16:48 -080060
61 ~ChatroomDiscoveryBackend();
62
63
64protected:
65 void
66 run();
67
68private:
69 void
70 initializeSync();
71
72 void
73 close();
74
75 void
76 processSyncUpdate(const std::vector<chronosync::MissingDataInfo>& updates);
77
78 void
Varun Patil3d850902020-11-23 12:19:14 +053079 processChatroomData(const ndn::Data& data);
Qiuhan Ding43c8e162015-02-02 15:16:48 -080080
81 void
82 localSessionTimeout(const Name::Component& chatroomName);
83
84 void
85 remoteSessionTimeout(const Name::Component& chatroomName);
86
87 void
88 randomSessionTimeout(const Name::Component& chatroomName);
89
90 void
91 sendUpdate(const Name::Component& chatroomName);
92
93 void
94 updatePrefixes();
95
96 void
97 sendChatroomList();
98
99signals:
100 /**
101 * @brief request to get chatroom info
102 *
103 * This signal will be sent to controller to get chatroom info
104 *
105 * @param chatroomName specify which chatroom's info to request
106 * @param isManager if the user who send the signal is the manager of the chatroom
107 */
108 void
109 chatroomInfoRequest(std::string chatroomName, bool isManager);
110
111 /**
112 * @brief send chatroom list to front end
113 *
114 * @param chatroomList the list of chatrooms
115 */
116 void
117 chatroomListReady(const QStringList& chatroomList);
118
119 /**
120 * @brief send chatroom info to front end
121 *
122 * @param info the chatroom info request by front end
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800123 * @param isParticipant if the user is a participant of the chatroom
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800124 */
125 void
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800126 chatroomInfoReady(const ChatroomInfo& info, bool isParticipant);
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800127
Yingdi Yuf3401182015-02-02 20:21:07 -0800128 void
129 nfdError();
130
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800131public slots:
132
133 /**
134 * @brief update routing prefix
135 *
136 * @param routingPrefix the new routing prefix
137 */
138 void
139 updateRoutingPrefix(const QString& routingPrefix);
140
141 /**
142 * @brief erase a participant in a chatroom's roster
143 *
144 * This slot is called by chat dialog will erase a participant from a chatroom.
145 * If the user is the manager of this chatroom, he will publish an update.
146 *
147 * @param sessionPrefix the prefix of the participant to erase
148 * @param chatroomName the name of the chatroom
149 */
150 void
151 onEraseInRoster(ndn::Name sessionPrefix, ndn::Name::Component chatroomName);
152
153 /**
154 * @brief add a participant in a chatroom's roster
155 *
156 * This slot is called by chat dialog and will add a participant from a chatroom.
157 * If the user is the manager of this chatroom, he will publish an update.
158 *
159 * @param sessionPrefix the prefix of the participant to erase
160 * @param chatroomName the name of the chatroom
161 */
162 void
163 onAddInRoster(ndn::Name sessionPrefix, ndn::Name::Component chatroomName);
164
165 /**
166 * @brief is called when user himself join a chatroom
167 *
168 * This slot is called by controller and will modify the chatroom list in discovery
169 *
170 * @param chatroomName the name of chatroom the user join
171 */
172 void
Yingdi Yuf3401182015-02-02 20:21:07 -0800173 onNewChatroomForDiscovery(ndn::Name::Component chatroomName);
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800174
175 /**
176 * @brief get chatroom info from chat dialog
177 *
178 * This slot is called by controller. It get the chatroom info of a chatroom and
179 * if the user is the manager, he will publish an update
180 *
181 * @param chatroomInfo chatroom info
182 * @param isManager whether the user is the manager of the chatroom
183 */
184 void
185 onRespondChatroomInfoRequest(ChatroomInfo chatroomInfo, bool isManager);
186
187 /**
188 * @brief reset when the identity updates
189 *
190 * This slot is called when the identity is updated, it will update the identity and
191 * reset the socket
192 *
193 * @param identity new identity
194 */
195 void
196 onIdentityUpdated(const QString& identity);
197
198 /**
199 * @brief prepare chatroom info for discovery panel
200 *
201 * This slot is called by discovery panel when it wants to display the info of a chatroom
202 *
203 * @param chatroomName the name of chatroom the discovery panel requested
204 */
205 void
206 onWaitForChatroomInfo(const QString& chatroomName);
207
208 void
209 shutdown();
210
Yingdi Yuf3401182015-02-02 20:21:07 -0800211 void
212 onNfdReconnect();
213
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800214private:
215
216 typedef std::map<ndn::Name::Component, ChatroomInfoBackend> ChatroomList;
217
Yingdi Yuf3401182015-02-02 20:21:07 -0800218 bool m_shouldResume;
219 bool m_isNfdConnected;
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800220 Name m_discoveryPrefix;
221 Name m_routableUserDiscoveryPrefix;
222 Name m_routingPrefix;
223 Name m_userDiscoveryPrefix;
224 Name m_identity;
225
226 boost::mt19937 m_randomGenerator;
227 boost::variate_generator<boost::mt19937&, boost::uniform_int<> > m_rangeUniformRandom;
228
229 shared_ptr<ndn::Face> m_face;
230
231 unique_ptr<ndn::Scheduler> m_scheduler; // scheduler
Varun Patila24bd3e2020-11-24 10:08:33 +0530232 ndn::scheduler::ScopedEventId m_refreshPanelId;
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800233 shared_ptr<chronosync::Socket> m_sock; // SyncSocket
234
235 ChatroomList m_chatroomList;
Yingdi Yuf3401182015-02-02 20:21:07 -0800236 std::mutex m_resumeMutex;
237 std::mutex m_nfdConnectionMutex;
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800238
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800239};
240
241} // namespace chronochat
242
243#endif // CHRONOCHAT_CHATROOM_DISCOVERY_BACKEND_HPP