blob: fe5e211f47ef285cf9b19606537df6816f7f93ee [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
14#include <ndn-cxx/util/scheduler.hpp>
15#include <boost/random.hpp>
16#include <QThread>
17#include <QMutex>
18
19#ifndef Q_MOC_RUN
20#include "common.hpp"
21#include "chatroom-info.hpp"
22#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
122 */
123 void
124 chatroomInfoReady(const ChatroomInfo& info);
125
126public slots:
127
128 /**
129 * @brief update routing prefix
130 *
131 * @param routingPrefix the new routing prefix
132 */
133 void
134 updateRoutingPrefix(const QString& routingPrefix);
135
136 /**
137 * @brief erase a participant in a chatroom's roster
138 *
139 * This slot is called by chat dialog will erase a participant from a chatroom.
140 * If the user is the manager of this chatroom, he will publish an update.
141 *
142 * @param sessionPrefix the prefix of the participant to erase
143 * @param chatroomName the name of the chatroom
144 */
145 void
146 onEraseInRoster(ndn::Name sessionPrefix, ndn::Name::Component chatroomName);
147
148 /**
149 * @brief add a participant in a chatroom's roster
150 *
151 * This slot is called by chat dialog and will add a participant from a chatroom.
152 * If the user is the manager of this chatroom, he will publish an update.
153 *
154 * @param sessionPrefix the prefix of the participant to erase
155 * @param chatroomName the name of the chatroom
156 */
157 void
158 onAddInRoster(ndn::Name sessionPrefix, ndn::Name::Component chatroomName);
159
160 /**
161 * @brief is called when user himself join a chatroom
162 *
163 * This slot is called by controller and will modify the chatroom list in discovery
164 *
165 * @param chatroomName the name of chatroom the user join
166 */
167 void
168 onNewChatroomForDiscovery(Name::Component chatroomName);
169
170 /**
171 * @brief get chatroom info from chat dialog
172 *
173 * This slot is called by controller. It get the chatroom info of a chatroom and
174 * if the user is the manager, he will publish an update
175 *
176 * @param chatroomInfo chatroom info
177 * @param isManager whether the user is the manager of the chatroom
178 */
179 void
180 onRespondChatroomInfoRequest(ChatroomInfo chatroomInfo, bool isManager);
181
182 /**
183 * @brief reset when the identity updates
184 *
185 * This slot is called when the identity is updated, it will update the identity and
186 * reset the socket
187 *
188 * @param identity new identity
189 */
190 void
191 onIdentityUpdated(const QString& identity);
192
193 /**
194 * @brief prepare chatroom info for discovery panel
195 *
196 * This slot is called by discovery panel when it wants to display the info of a chatroom
197 *
198 * @param chatroomName the name of chatroom the discovery panel requested
199 */
200 void
201 onWaitForChatroomInfo(const QString& chatroomName);
202
203 void
204 shutdown();
205
206private:
207
208 typedef std::map<ndn::Name::Component, ChatroomInfoBackend> ChatroomList;
209
210 Name m_discoveryPrefix;
211 Name m_routableUserDiscoveryPrefix;
212 Name m_routingPrefix;
213 Name m_userDiscoveryPrefix;
214 Name m_identity;
215
216 boost::mt19937 m_randomGenerator;
217 boost::variate_generator<boost::mt19937&, boost::uniform_int<> > m_rangeUniformRandom;
218
219 shared_ptr<ndn::Face> m_face;
220
221 unique_ptr<ndn::Scheduler> m_scheduler; // scheduler
222 ndn::EventId m_refreshPanelId;
223 shared_ptr<chronosync::Socket> m_sock; // SyncSocket
224
225 ChatroomList m_chatroomList;
226 QMutex m_mutex;
227
228 bool m_shouldResume;
229};
230
231} // namespace chronochat
232
233#endif // CHRONOCHAT_CHATROOM_DISCOVERY_BACKEND_HPP