blob: b3f809fb8a3a8d24ee28060e18db55daede77bfd [file] [log] [blame]
Qiuhan Ding43c8e162015-02-02 15:16:48 -08001/* -*- 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: 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 <ndn-cxx/util/scheduler.hpp>
20#include <boost/random.hpp>
21#include <mutex>
Qiuhan Ding43c8e162015-02-02 15:16:48 -080022#include <socket.hpp>
23#endif
24
25namespace chronochat {
26
27class ChatroomInfoBackend {
28public:
29 std::string chatroomName;
30 Name chatroomPrefix;
31 ChatroomInfo info;
32 // For a chatroom's user to check whether his own chatroom is alive
33 ndn::EventId localChatroomTimeoutEventId;
34 // If the manager no longer exist, set a random timer to compete for manager
35 ndn::EventId managerSelectionTimeoutEventId;
36 // For a user to check the status of the chatroom that he is not in.
37 ndn::EventId remoteChatroomTimeoutEventId;
38 // If the user is manager, he will need the helloEventId to keep track of hello message
39 ndn::EventId helloTimeoutEventId;
40 // To tell whether the user is in this chatroom
41 bool isParticipant;
42 // To tell whether the user is the manager
43 bool isManager;
44 // Variable to tell whether another manager exists
45 int count;
46 // Variable to tell whether to print on the panel
47 bool isPrint;
48
49};
50
51class ChatroomDiscoveryBackend : public QThread
52{
53 Q_OBJECT
54
55public:
56 ChatroomDiscoveryBackend(const Name& routingPrefix,
57 const Name& identity,
58 QObject* parent = 0);
59
60 ~ChatroomDiscoveryBackend();
61
62
63protected:
64 void
65 run();
66
67private:
68 void
69 initializeSync();
70
71 void
72 close();
73
74 void
75 processSyncUpdate(const std::vector<chronosync::MissingDataInfo>& updates);
76
77 void
78 processChatroomData(const ndn::shared_ptr<const ndn::Data>& data);
79
80 void
81 localSessionTimeout(const Name::Component& chatroomName);
82
83 void
84 remoteSessionTimeout(const Name::Component& chatroomName);
85
86 void
87 randomSessionTimeout(const Name::Component& chatroomName);
88
89 void
90 sendUpdate(const Name::Component& chatroomName);
91
92 void
93 updatePrefixes();
94
95 void
96 sendChatroomList();
97
98signals:
99 /**
100 * @brief request to get chatroom info
101 *
102 * This signal will be sent to controller to get chatroom info
103 *
104 * @param chatroomName specify which chatroom's info to request
105 * @param isManager if the user who send the signal is the manager of the chatroom
106 */
107 void
108 chatroomInfoRequest(std::string chatroomName, bool isManager);
109
110 /**
111 * @brief send chatroom list to front end
112 *
113 * @param chatroomList the list of chatrooms
114 */
115 void
116 chatroomListReady(const QStringList& chatroomList);
117
118 /**
119 * @brief send chatroom info to front end
120 *
121 * @param info the chatroom info request by front end
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800122 * @param isParticipant if the user is a participant of the chatroom
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800123 */
124 void
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800125 chatroomInfoReady(const ChatroomInfo& info, bool isParticipant);
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800126
127public slots:
128
129 /**
130 * @brief update routing prefix
131 *
132 * @param routingPrefix the new routing prefix
133 */
134 void
135 updateRoutingPrefix(const QString& routingPrefix);
136
137 /**
138 * @brief erase a participant in a chatroom's roster
139 *
140 * This slot is called by chat dialog will erase a participant from a chatroom.
141 * If the user is the manager of this chatroom, he will publish an update.
142 *
143 * @param sessionPrefix the prefix of the participant to erase
144 * @param chatroomName the name of the chatroom
145 */
146 void
147 onEraseInRoster(ndn::Name sessionPrefix, ndn::Name::Component chatroomName);
148
149 /**
150 * @brief add a participant in a chatroom's roster
151 *
152 * This slot is called by chat dialog and will add a participant from a chatroom.
153 * If the user is the manager of this chatroom, he will publish an update.
154 *
155 * @param sessionPrefix the prefix of the participant to erase
156 * @param chatroomName the name of the chatroom
157 */
158 void
159 onAddInRoster(ndn::Name sessionPrefix, ndn::Name::Component chatroomName);
160
161 /**
162 * @brief is called when user himself join a chatroom
163 *
164 * This slot is called by controller and will modify the chatroom list in discovery
165 *
166 * @param chatroomName the name of chatroom the user join
167 */
168 void
169 onNewChatroomForDiscovery(Name::Component chatroomName);
170
171 /**
172 * @brief get chatroom info from chat dialog
173 *
174 * This slot is called by controller. It get the chatroom info of a chatroom and
175 * if the user is the manager, he will publish an update
176 *
177 * @param chatroomInfo chatroom info
178 * @param isManager whether the user is the manager of the chatroom
179 */
180 void
181 onRespondChatroomInfoRequest(ChatroomInfo chatroomInfo, bool isManager);
182
183 /**
184 * @brief reset when the identity updates
185 *
186 * This slot is called when the identity is updated, it will update the identity and
187 * reset the socket
188 *
189 * @param identity new identity
190 */
191 void
192 onIdentityUpdated(const QString& identity);
193
194 /**
195 * @brief prepare chatroom info for discovery panel
196 *
197 * This slot is called by discovery panel when it wants to display the info of a chatroom
198 *
199 * @param chatroomName the name of chatroom the discovery panel requested
200 */
201 void
202 onWaitForChatroomInfo(const QString& chatroomName);
203
204 void
205 shutdown();
206
207private:
208
209 typedef std::map<ndn::Name::Component, ChatroomInfoBackend> ChatroomList;
210
211 Name m_discoveryPrefix;
212 Name m_routableUserDiscoveryPrefix;
213 Name m_routingPrefix;
214 Name m_userDiscoveryPrefix;
215 Name m_identity;
216
217 boost::mt19937 m_randomGenerator;
218 boost::variate_generator<boost::mt19937&, boost::uniform_int<> > m_rangeUniformRandom;
219
220 shared_ptr<ndn::Face> m_face;
221
222 unique_ptr<ndn::Scheduler> m_scheduler; // scheduler
223 ndn::EventId m_refreshPanelId;
224 shared_ptr<chronosync::Socket> m_sock; // SyncSocket
225
226 ChatroomList m_chatroomList;
Qiuhan Dingf22c41b2015-03-11 13:19:01 -0700227 std::mutex m_mutex;
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800228
229 bool m_shouldResume;
230};
231
232} // namespace chronochat
233
234#endif // CHRONOCHAT_CHATROOM_DISCOVERY_BACKEND_HPP