blob: f136b8230c9232f93c4dee2df23d8f0ad54ca5c1 [file] [log] [blame]
Yingdi Yu348f5ea2014-03-01 14:47:25 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013, Regents of the University of California
4 * Yingdi Yu
5 *
6 * BSD license, See the LICENSE file for more information
7 *
8 * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
9 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
10 * Yingdi Yu <yingdi@cs.ucla.edu>
11 */
12
13#ifndef CHAT_DIALOG_H
14#define CHAT_DIALOG_H
15
16#include <QDialog>
17#include <QTextTable>
18#include <QStringListModel>
19#include <QSystemTrayIcon>
20#include <QMenu>
21#include <QTimer>
22
23#include "invite-list-dialog.h"
24
25#ifndef Q_MOC_RUN
26#include "contact-manager.h"
27#include "invitation.h"
28#include "contact.h"
29#include "chatbuf.pb.h"
30#include "intro-cert-list.pb.h"
31#include "digesttreescene.h"
32#include <sync-socket.h>
33#include <sync-seq-no.h>
34#include <ndn-cpp-dev/security/key-chain.hpp>
35#include "validator-invitation.h"
36
37#include <boost/thread/locks.hpp>
38#include <boost/thread/recursive_mutex.hpp>
39#include <boost/thread/thread.hpp>
40#endif
41
42#define MAX_HISTORY_ENTRY 20
43
44namespace Ui {
45class ChatDialog;
46}
47
48class ChatDialog : public QDialog
49{
50 Q_OBJECT
51
52public:
53 explicit
54 ChatDialog(chronos::ContactManager* contactManager,
55 ndn::shared_ptr<ndn::Face> face,
56 const ndn::IdentityCertificate& myCertificate,
57 const ndn::Name& chatroomPrefix,
58 const ndn::Name& localPrefix,
59 const std::string& nick,
60 bool witSecurity,
61 QWidget* parent = 0);
62
63 ~ChatDialog();
64
65 void
66 addSyncAnchor(const chronos::Invitation& invitation);
67
68 void
69 processTreeUpdateWrapper(const std::vector<Sync::MissingDataInfo>&, Sync::SyncSocket *);
70
71 void
72 processDataWrapper(const ndn::shared_ptr<const ndn::Data>& data);
73
74 void
75 processDataNoShowWrapper(const ndn::shared_ptr<const ndn::Data>& data);
76
77 void
78 processRemoveWrapper(std::string);
79
80protected:
81 void
82 closeEvent(QCloseEvent *e);
83
84 void
85 resizeEvent(QResizeEvent *);
86
87 void
88 showEvent(QShowEvent *);
89
90private:
91 void
92 updatePrefix();
93
94 void
95 updateLabels();
96
97 void
98 initializeSync();
99
100 void
101 sendInvitation(ndn::shared_ptr<chronos::Contact> contact, bool isIntroducer);
102
103 void
104 replyWrapper(const ndn::Interest& interest,
105 ndn::Data& data,
106 size_t routablePrefixOffset,
107 bool isIntroducer);
108
109 void
110 replyTimeoutWrapper(const ndn::Interest& interest,
111 size_t routablePrefixOffset);
112
113 void
114 onReplyValidated(const ndn::shared_ptr<const ndn::Data>& data,
115 size_t inviteeRoutablePrefixOffset,
116 bool isIntroduce);
117
118 void
119 onReplyValidationFailed(const ndn::shared_ptr<const ndn::Data>& data,
120 const std::string& failureInfo);
121
122 void
123 invitationRejected(const ndn::Name& identity);
124
125 void
126 invitationAccepted(const ndn::IdentityCertificate& inviteeCert,
127 const ndn::Name& inviteePrefix,
128 bool isIntroducer);
129
130 void
131 fetchIntroCert(const ndn::Name& identity, const ndn::Name& prefix);
132
133 void
134 onIntroCertList(const ndn::Interest& interest, const ndn::Data& data);
135
136 void
137 onIntroCertListTimeout(const ndn::Interest& interest, int retry, const std::string& msg);
138
139 void
140 introCertWrapper(const ndn::Interest& interest, ndn::Data& data);
141
142 void
143 introCertTimeoutWrapper(const ndn::Interest& interest, int retry, const QString& msg);
144
145 void
146 onCertListInterest(const ndn::Name& prefix, const ndn::Interest& interest);
147
148 void
149 onCertListRegisterFailed(const ndn::Name& prefix, const std::string& msg);
150
151 void
152 onCertSingleInterest(const ndn::Name& prefix, const ndn::Interest& interest);
153
154 void
155 onCertSingleRegisterFailed(const ndn::Name& prefix, const std::string& msg);
156
157
158 void
159 sendMsg(SyncDemo::ChatMessage &msg);
160
161 void
162 disableTreeDisplay();
163
164 void
165 appendMessage(const SyncDemo::ChatMessage msg, bool isHistory = false);
166
167 void
168 processRemove(QString prefix);
169
170 ndn::Name
171 getInviteeRoutablePrefix(const ndn::Name& invitee);
172
173 void
174 formChatMessage(const QString &text, SyncDemo::ChatMessage &msg);
175
176 void
177 formControlMessage(SyncDemo::ChatMessage &msg, SyncDemo::ChatMessage::ChatMessageType type);
178
179 QString
180 formatTime(time_t);
181
182 void
183 printTimeInCell(QTextTable *, time_t);
184
185 std::string
186 getRandomString();
187
188 void
189 showMessage(const QString&, const QString&);
190
191 void
192 fitView();
193
194 void
195 summonReaper();
196
197signals:
198 void
199 processData(const ndn::Data& data, bool show, bool isHistory);
200
201 void
202 processTreeUpdate(const std::vector<Sync::MissingDataInfo>);
203
204 void
205 closeChatDialog(const QString& chatroomName);
206
207 void
208 inivationRejection(const QString& msg);
209
210 void
211 showChatMessage(const QString& chatroomName, const QString& from, const QString& data);
212
213 void
214 reply(const ndn::Interest& interest, const ndn::Data& data, size_t routablePrefixOffset, bool isIntroducer);
215
216 void
217 replyTimeout(const ndn::Interest& interest, size_t routablePrefixOffset);
218
219 void
220 introCert(const ndn::Interest& interest, const ndn::Data& data);
221
222 void
223 introCertTimeout(const ndn::Interest& interest, int retry, const QString& msg);
224
225
226public slots:
227 void
228 onLocalPrefixUpdated(const QString& localPrefix);
229
230 void
231 onClose();
232
233private slots:
234 void
235 onReturnPressed();
236
237 void
238 onTreeButtonPressed();
239
240 void
241 onProcessData(const ndn::Data& data, bool show, bool isHistory);
242
243 void
244 onProcessTreeUpdate(const std::vector<Sync::MissingDataInfo>&);
245
246 void
247 onReplot();
248
249 void
250 onRosterChanged(QStringList);
251
252 void
253 onInviteListDialogRequested();
254
255 void
256 sendJoin();
257
258 void
259 sendHello();
260
261 void
262 sendLeave();
263
264 void
265 enableTreeDisplay();
266
267 void
268 reap();
269
270 void
271 onSendInvitation(QString);
272
273 void
274 onReply(const ndn::Interest& interest, const ndn::Data& data, size_t routablePrefixOffset, bool isIntroducer);
275
276 void
277 onReplyTimeout(const ndn::Interest& interest, size_t routablePrefixOffset);
278
279 void
280 onIntroCert(const ndn::Interest& interest, const ndn::Data& data);
281
282 void
283 onIntroCertTimeout(const ndn::Interest& interest, int retry, const QString& msg);
284
285private:
286 Ui::ChatDialog *ui;
287 ndn::KeyChain m_keyChain;
288
289 chronos::ContactManager* m_contactManager;
290 ndn::shared_ptr<ndn::Face> m_face;
291
292 ndn::IdentityCertificate m_myCertificate;
293 ndn::Name m_identity;
294
295 ndn::Name m_certListPrefix;
296 const ndn::RegisteredPrefixId* m_certListPrefixId;
297 ndn::Name m_certSinglePrefix;
298 const ndn::RegisteredPrefixId* m_certSinglePrefixId;
299
300 std::string m_chatroomName;
301 ndn::Name m_chatroomPrefix;
302 ndn::Name m_localPrefix;
303 bool m_useRoutablePrefix;
304 ndn::Name m_chatPrefix;
305 ndn::Name m_localChatPrefix;
306 std::string m_nick;
307 DigestTreeScene *m_scene;
308 QStringListModel *m_rosterModel;
309 QTimer* m_timer;
310
311
312 int64_t m_lastMsgTime;
313 int m_randomizedInterval;
314 bool m_joined;
315
316 Sync::SyncSocket *m_sock;
317 uint64_t m_session;
318 ndn::shared_ptr<ndn::SecRuleRelative> m_dataRule;
319
320 InviteListDialog* m_inviteListDialog;
321 ndn::shared_ptr<chronos::ValidatorInvitation> m_invitationValidator;
322
323 boost::recursive_mutex m_msgMutex;
324 boost::recursive_mutex m_sceneMutex;
325 QList<QString> m_zombieList;
326 int m_zombieIndex;
327};
328
329#endif // CHAT_DIALOG_H