blob: c95aabf46a5313054ed430d935b29fdf8bcec01b [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"
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -070031#include "digest-tree-scene.h"
32#include "trust-tree-scene.h"
33#include "trust-tree-node.h"
Yingdi Yu348f5ea2014-03-01 14:47:25 -080034#include <sync-socket.h>
35#include <sync-seq-no.h>
36#include <ndn-cpp-dev/security/key-chain.hpp>
37#include "validator-invitation.h"
Yingdi Yu348f5ea2014-03-01 14:47:25 -080038#include <boost/thread/locks.hpp>
39#include <boost/thread/recursive_mutex.hpp>
40#include <boost/thread/thread.hpp>
41#endif
42
43#define MAX_HISTORY_ENTRY 20
44
45namespace Ui {
46class ChatDialog;
47}
48
49class ChatDialog : public QDialog
50{
51 Q_OBJECT
52
53public:
54 explicit
55 ChatDialog(chronos::ContactManager* contactManager,
56 ndn::shared_ptr<ndn::Face> face,
57 const ndn::IdentityCertificate& myCertificate,
58 const ndn::Name& chatroomPrefix,
59 const ndn::Name& localPrefix,
60 const std::string& nick,
61 bool witSecurity,
62 QWidget* parent = 0);
63
64 ~ChatDialog();
65
66 void
67 addSyncAnchor(const chronos::Invitation& invitation);
68
69 void
70 processTreeUpdateWrapper(const std::vector<Sync::MissingDataInfo>&, Sync::SyncSocket *);
71
72 void
73 processDataWrapper(const ndn::shared_ptr<const ndn::Data>& data);
74
75 void
76 processDataNoShowWrapper(const ndn::shared_ptr<const ndn::Data>& data);
77
78 void
79 processRemoveWrapper(std::string);
80
81protected:
82 void
83 closeEvent(QCloseEvent *e);
84
Yingdi Yu233a9722014-03-07 15:47:09 -080085 void
86 changeEvent(QEvent *e);
87
Yingdi Yu348f5ea2014-03-01 14:47:25 -080088 void
89 resizeEvent(QResizeEvent *);
90
91 void
92 showEvent(QShowEvent *);
93
94private:
95 void
96 updatePrefix();
97
98 void
99 updateLabels();
100
101 void
102 initializeSync();
103
104 void
105 sendInvitation(ndn::shared_ptr<chronos::Contact> contact, bool isIntroducer);
106
107 void
108 replyWrapper(const ndn::Interest& interest,
109 ndn::Data& data,
110 size_t routablePrefixOffset,
111 bool isIntroducer);
112
113 void
114 replyTimeoutWrapper(const ndn::Interest& interest,
115 size_t routablePrefixOffset);
116
117 void
118 onReplyValidated(const ndn::shared_ptr<const ndn::Data>& data,
119 size_t inviteeRoutablePrefixOffset,
120 bool isIntroduce);
121
122 void
123 onReplyValidationFailed(const ndn::shared_ptr<const ndn::Data>& data,
124 const std::string& failureInfo);
125
126 void
127 invitationRejected(const ndn::Name& identity);
128
129 void
130 invitationAccepted(const ndn::IdentityCertificate& inviteeCert,
131 const ndn::Name& inviteePrefix,
132 bool isIntroducer);
133
134 void
135 fetchIntroCert(const ndn::Name& identity, const ndn::Name& prefix);
136
137 void
138 onIntroCertList(const ndn::Interest& interest, const ndn::Data& data);
139
140 void
141 onIntroCertListTimeout(const ndn::Interest& interest, int retry, const std::string& msg);
142
143 void
144 introCertWrapper(const ndn::Interest& interest, ndn::Data& data);
145
146 void
147 introCertTimeoutWrapper(const ndn::Interest& interest, int retry, const QString& msg);
148
149 void
150 onCertListInterest(const ndn::Name& prefix, const ndn::Interest& interest);
151
152 void
153 onCertListRegisterFailed(const ndn::Name& prefix, const std::string& msg);
154
155 void
156 onCertSingleInterest(const ndn::Name& prefix, const ndn::Interest& interest);
157
158 void
159 onCertSingleRegisterFailed(const ndn::Name& prefix, const std::string& msg);
160
161
162 void
163 sendMsg(SyncDemo::ChatMessage &msg);
164
165 void
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -0700166 disableSyncTreeDisplay();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800167
168 void
169 appendMessage(const SyncDemo::ChatMessage msg, bool isHistory = false);
170
171 void
172 processRemove(QString prefix);
173
174 ndn::Name
175 getInviteeRoutablePrefix(const ndn::Name& invitee);
176
177 void
178 formChatMessage(const QString &text, SyncDemo::ChatMessage &msg);
179
180 void
181 formControlMessage(SyncDemo::ChatMessage &msg, SyncDemo::ChatMessage::ChatMessageType type);
182
183 QString
184 formatTime(time_t);
185
186 void
187 printTimeInCell(QTextTable *, time_t);
188
189 std::string
190 getRandomString();
191
192 void
193 showMessage(const QString&, const QString&);
194
195 void
196 fitView();
197
198 void
199 summonReaper();
200
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -0700201 void
202 getTree(TrustTreeNodeList& nodeList);
203
204 void
205 plotTrustTree();
206
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800207signals:
208 void
Yingdi Yu233a9722014-03-07 15:47:09 -0800209 processData(const ndn::shared_ptr<const ndn::Data>& data, bool show, bool isHistory);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800210
211 void
212 processTreeUpdate(const std::vector<Sync::MissingDataInfo>);
213
214 void
215 closeChatDialog(const QString& chatroomName);
216
217 void
218 inivationRejection(const QString& msg);
219
220 void
221 showChatMessage(const QString& chatroomName, const QString& from, const QString& data);
222
223 void
Yingdi Yu233a9722014-03-07 15:47:09 -0800224 resetIcon();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800225
226 void
Yingdi Yu233a9722014-03-07 15:47:09 -0800227 reply(const ndn::Interest& interest,
228 const ndn::shared_ptr<const ndn::Data>& data,
229 size_t routablePrefixOffset, bool isIntroducer);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800230
231 void
Yingdi Yu233a9722014-03-07 15:47:09 -0800232 replyTimeout(const ndn::Interest& interest,
233 size_t routablePrefixOffset);
234
235 void
236 introCert(const ndn::Interest& interest,
237 const ndn::shared_ptr<const ndn::Data>& data);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800238
239 void
Yingdi Yu233a9722014-03-07 15:47:09 -0800240 introCertTimeout(const ndn::Interest& interest,
241 int retry, const QString& msg);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800242
Yingdi Yu233a9722014-03-07 15:47:09 -0800243 void
244 waitForContactList();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800245
246public slots:
247 void
248 onLocalPrefixUpdated(const QString& localPrefix);
249
250 void
251 onClose();
252
253private slots:
254 void
255 onReturnPressed();
256
257 void
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -0700258 onSyncTreeButtonPressed();
259
260 void
261 onTrustTreeButtonPressed();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800262
263 void
Yingdi Yu233a9722014-03-07 15:47:09 -0800264 onProcessData(const ndn::shared_ptr<const ndn::Data>& data,
265 bool show, bool isHistory);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800266
267 void
268 onProcessTreeUpdate(const std::vector<Sync::MissingDataInfo>&);
269
270 void
271 onReplot();
272
273 void
274 onRosterChanged(QStringList);
275
276 void
277 onInviteListDialogRequested();
278
279 void
280 sendJoin();
281
282 void
283 sendHello();
284
285 void
286 sendLeave();
287
288 void
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -0700289 enableSyncTreeDisplay();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800290
291 void
292 reap();
293
294 void
295 onSendInvitation(QString);
296
297 void
Yingdi Yu233a9722014-03-07 15:47:09 -0800298 onReply(const ndn::Interest& interest,
299 const ndn::shared_ptr<const ndn::Data>& data,
300 size_t routablePrefixOffset, bool isIntroducer);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800301
302 void
Yingdi Yu233a9722014-03-07 15:47:09 -0800303 onReplyTimeout(const ndn::Interest& interest,
304 size_t routablePrefixOffset);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800305
306 void
Yingdi Yu233a9722014-03-07 15:47:09 -0800307 onIntroCert(const ndn::Interest& interest,
308 const ndn::shared_ptr<const ndn::Data>& data);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800309
310 void
Yingdi Yu233a9722014-03-07 15:47:09 -0800311 onIntroCertTimeout(const ndn::Interest& interest,
312 int retry, const QString& msg);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800313
314private:
315 Ui::ChatDialog *ui;
316 ndn::KeyChain m_keyChain;
317
318 chronos::ContactManager* m_contactManager;
319 ndn::shared_ptr<ndn::Face> m_face;
320
321 ndn::IdentityCertificate m_myCertificate;
322 ndn::Name m_identity;
323
324 ndn::Name m_certListPrefix;
325 const ndn::RegisteredPrefixId* m_certListPrefixId;
326 ndn::Name m_certSinglePrefix;
327 const ndn::RegisteredPrefixId* m_certSinglePrefixId;
328
329 std::string m_chatroomName;
330 ndn::Name m_chatroomPrefix;
331 ndn::Name m_localPrefix;
332 bool m_useRoutablePrefix;
333 ndn::Name m_chatPrefix;
334 ndn::Name m_localChatPrefix;
335 std::string m_nick;
336 DigestTreeScene *m_scene;
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -0700337 TrustTreeScene *m_trustScene;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800338 QStringListModel *m_rosterModel;
339 QTimer* m_timer;
340
341
342 int64_t m_lastMsgTime;
343 int m_randomizedInterval;
344 bool m_joined;
345
346 Sync::SyncSocket *m_sock;
347 uint64_t m_session;
348 ndn::shared_ptr<ndn::SecRuleRelative> m_dataRule;
349
350 InviteListDialog* m_inviteListDialog;
351 ndn::shared_ptr<chronos::ValidatorInvitation> m_invitationValidator;
352
353 boost::recursive_mutex m_msgMutex;
354 boost::recursive_mutex m_sceneMutex;
355 QList<QString> m_zombieList;
356 int m_zombieIndex;
357};
358
359#endif // CHAT_DIALOG_H