blob: 7902e09f0c321e3994e2d1c1743a96b078bb4c46 [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>
Yingdi Yufa0b6a02014-04-30 14:26:42 -070036#include <ndn-cxx/security/key-chain.hpp>
Yingdi Yu348f5ea2014-03-01 14:47:25 -080037#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:
Yingdi Yufa0b6a02014-04-30 14:26:42 -070054 explicit
Yingdi Yu348f5ea2014-03-01 14:47:25 -080055 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();
Yingdi Yufa0b6a02014-04-30 14:26:42 -070065
66 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -080067 addSyncAnchor(const chronos::Invitation& invitation);
68
Yingdi Yufa0b6a02014-04-30 14:26:42 -070069 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -080070 processTreeUpdateWrapper(const std::vector<Sync::MissingDataInfo>&, Sync::SyncSocket *);
71
Yingdi Yufa0b6a02014-04-30 14:26:42 -070072 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -080073 processDataWrapper(const ndn::shared_ptr<const ndn::Data>& data);
74
Yingdi Yufa0b6a02014-04-30 14:26:42 -070075 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -080076 processDataNoShowWrapper(const ndn::shared_ptr<const ndn::Data>& data);
77
Yingdi Yufa0b6a02014-04-30 14:26:42 -070078 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -080079 processRemoveWrapper(std::string);
80
81protected:
Yingdi Yufa0b6a02014-04-30 14:26:42 -070082 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -080083 closeEvent(QCloseEvent *e);
84
Yingdi Yu233a9722014-03-07 15:47:09 -080085 void
86 changeEvent(QEvent *e);
87
Yingdi Yufa0b6a02014-04-30 14:26:42 -070088 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -080089 resizeEvent(QResizeEvent *);
Yingdi Yufa0b6a02014-04-30 14:26:42 -070090
91 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -080092 showEvent(QShowEvent *);
93
94private:
95 void
96 updatePrefix();
Yingdi Yufa0b6a02014-04-30 14:26:42 -070097
98 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -080099 updateLabels();
100
101 void
102 initializeSync();
103
104 void
105 sendInvitation(ndn::shared_ptr<chronos::Contact> contact, bool isIntroducer);
106
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700107 void
108 replyWrapper(const ndn::Interest& interest,
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800109 ndn::Data& data,
110 size_t routablePrefixOffset,
111 bool isIntroducer);
112
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700113 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800114 replyTimeoutWrapper(const ndn::Interest& interest,
115 size_t routablePrefixOffset);
116
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700117 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800118 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);
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700128
129 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800130 invitationAccepted(const ndn::IdentityCertificate& inviteeCert,
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700131 const ndn::Name& inviteePrefix,
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800132 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);
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700145
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800146 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
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700162 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800163 sendMsg(SyncDemo::ChatMessage &msg);
164
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700165 void
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -0700166 disableSyncTreeDisplay();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800167
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700168 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800169 appendMessage(const SyncDemo::ChatMessage msg, bool isHistory = false);
170
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700171 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800172 processRemove(QString prefix);
173
174 ndn::Name
175 getInviteeRoutablePrefix(const ndn::Name& invitee);
176
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700177 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800178 formChatMessage(const QString &text, SyncDemo::ChatMessage &msg);
179
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700180 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800181 formControlMessage(SyncDemo::ChatMessage &msg, SyncDemo::ChatMessage::ChatMessageType type);
182
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700183 QString
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800184 formatTime(time_t);
185
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700186 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800187 printTimeInCell(QTextTable *, time_t);
188
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700189 std::string
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800190 getRandomString();
191
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700192 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800193 showMessage(const QString&, const QString&);
194
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700195 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800196 fitView();
197
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700198 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800199 summonReaper();
200
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -0700201 void
202 getTree(TrustTreeNodeList& nodeList);
203
204 void
205 plotTrustTree();
206
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700207signals:
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800208 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
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700211 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800212 processTreeUpdate(const std::vector<Sync::MissingDataInfo>);
213
214 void
215 closeChatDialog(const QString& chatroomName);
216
217 void
218 inivationRejection(const QString& msg);
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700219
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800220 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 Yufa0b6a02014-04-30 14:26:42 -0700232 replyTimeout(const ndn::Interest& interest,
Yingdi Yu233a9722014-03-07 15:47:09 -0800233 size_t routablePrefixOffset);
234
235 void
236 introCert(const ndn::Interest& interest,
237 const ndn::shared_ptr<const ndn::Data>& data);
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700238
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800239 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
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700257 void
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -0700258 onSyncTreeButtonPressed();
259
260 void
261 onTrustTreeButtonPressed();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800262
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700263 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
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700267 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800268 onProcessTreeUpdate(const std::vector<Sync::MissingDataInfo>&);
269
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700270 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800271 onReplot();
272
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700273 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800274 onRosterChanged(QStringList);
275
276 void
277 onInviteListDialogRequested();
278
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700279 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800280 sendJoin();
281
282 void
283 sendHello();
284
285 void
286 sendLeave();
287
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700288 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