blob: 4d7c358bf454b882451f1766fd5a627c76d36fc4 [file] [log] [blame]
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -07001#include <QtGui>
2#include "chatdialog.h"
Zhenkai Zhu85845d22012-06-01 23:10:43 -07003#include "settingdialog.h"
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -07004#include <ctime>
Zhenkai Zhub6338822012-05-31 13:27:24 -07005#include <iostream>
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -07006#include <QTimer>
Zhenkai Zhu10ccb5a2012-06-04 21:55:14 -07007#include <QMetaType>
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -07008
Zhenkai Zhu82a62752012-06-04 17:11:04 -07009#define BROADCAST_PREFIX_FOR_SYNC_DEMO "/ndn/broadcast/sync-demo"
10
Zhenkai Zhu21d75f92012-06-04 21:23:34 -070011void
12ChatDialog::testDraw()
13{
14 std::string prefix[5] = {"/ndn/1", "/ndn/2", "/ndn/3", "/ndn/4", "/ndn/5"};
15 std::string nick[5] = {"tom", "jerry", "jason", "michael", "hurry"};
16 std::vector<Sync::MissingDataInfo> v;
17 for (int i = 0; i < 5; i++)
18 {
19 Sync::MissingDataInfo mdi = {prefix[i], Sync::SeqNo(0), Sync::SeqNo(i * (2 << i) )};
20 v.push_back(mdi);
21 }
22
23 m_scene->processUpdate(v, "12341234@!#%!@");
24
25 for (int i = 0; i < 5; i++)
26 {
27 m_scene-> msgReceived(prefix[i].c_str(), nick[i].c_str());
28 }
29
30 fitView();
31}
32
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -070033ChatDialog::ChatDialog(QWidget *parent)
Zhenkai Zhu82a62752012-06-04 17:11:04 -070034 : QDialog(parent), m_sock(NULL)
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -070035{
Zhenkai Zhu10ccb5a2012-06-04 21:55:14 -070036 // have to register this, otherwise
37 // the signal-slot system won't recognize this type
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -070038 qRegisterMetaType<std::vector<Sync::MissingDataInfo> >("std::vector<Sync::MissingDataInfo>");
39 qRegisterMetaType<size_t>("size_t");
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -070040 setupUi(this);
Zhenkai Zhu82a62752012-06-04 17:11:04 -070041 m_session = time(NULL);
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -070042
43 readSettings();
Zhenkai Zhu82a62752012-06-04 17:11:04 -070044
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -070045 updateLabels();
46
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -070047 lineEdit->setFocusPolicy(Qt::StrongFocus);
Zhenkai Zhu82a62752012-06-04 17:11:04 -070048 m_scene = new DigestTreeScene(this);
Zhenkai Zhub45e38a2012-06-01 15:44:36 -070049
Zhenkai Zhu82a62752012-06-04 17:11:04 -070050 treeViewer->setScene(m_scene);
51 m_scene->plot("Empty");
52 QRectF rect = m_scene->itemsBoundingRect();
53 m_scene->setSceneRect(rect);
54
55 // create sync socket
56 if(!m_user.getChatroom().isEmpty()) {
57 std::string syncPrefix = BROADCAST_PREFIX_FOR_SYNC_DEMO;
58 syncPrefix += "/";
59 syncPrefix += m_user.getChatroom().toStdString();
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -070060 m_sock = new Sync::SyncAppSocket(syncPrefix, bind(&ChatDialog::processTreeUpdateWrapper, this, _1, _2), bind(&ChatDialog::processRemove, this, _1));
Zhenkai Zhu82a62752012-06-04 17:11:04 -070061 }
Zhenkai Zhub45e38a2012-06-01 15:44:36 -070062
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -070063 connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(returnPressed()));
Zhenkai Zhu85845d22012-06-01 23:10:43 -070064 connect(setButton, SIGNAL(pressed()), this, SLOT(buttonPressed()));
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -070065 connect(this, SIGNAL(dataReceived(QString, const char *, size_t)), this, SLOT(processData(QString, const char *, size_t)));
66 connect(this, SIGNAL(treeUpdated(const std::vector<Sync::MissingDataInfo>)), this, SLOT(processTreeUpdate(const std::vector<Sync::MissingDataInfo>)));
Zhenkai Zhu10ccb5a2012-06-04 21:55:14 -070067 //testDraw();
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -070068}
69
Zhenkai Zhu82a62752012-06-04 17:11:04 -070070ChatDialog::~ChatDialog()
71{
72 if (m_sock != NULL)
73 {
74 delete m_sock;
75 m_sock = NULL;
76 }
77}
78
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -070079void
Zhenkai Zhu10ccb5a2012-06-04 21:55:14 -070080ChatDialog::appendMessage(const SyncDemo::ChatMessage msg)
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -070081{
Zhenkai Zhub6338822012-05-31 13:27:24 -070082
83 if (msg.type() != SyncDemo::ChatMessage::CHAT) {
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -070084 return;
Zhenkai Zhub6338822012-05-31 13:27:24 -070085 }
86
87 if (!msg.has_data()) {
88 return;
89 }
90
91 if (msg.from().empty() || msg.data().empty()) {
92 return;
93 }
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -070094
95 QTextCursor cursor(textEdit->textCursor());
96 cursor.movePosition(QTextCursor::End);
97 QTextTableFormat tableFormat;
98 tableFormat.setBorder(0);
99 QTextTable *table = cursor.insertTable(1, 2, tableFormat);
Zhenkai Zhub6338822012-05-31 13:27:24 -0700100 QString from = QString("<%1>: ").arg(msg.from().c_str());
101 table->cellAt(0, 0).firstCursorPosition().insertText(from);
102 table->cellAt(0, 1).firstCursorPosition().insertText(msg.data().c_str());
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700103 QScrollBar *bar = textEdit->verticalScrollBar();
104 bar->setValue(bar->maximum());
105}
106
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700107void
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700108ChatDialog::processTreeUpdateWrapper(const std::vector<Sync::MissingDataInfo> v, Sync::SyncAppSocket *sock)
109{
110 emit treeUpdated(v);
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700111#ifdef __DEBUG
112 std::cout << "<<< Tree update signal emitted" << std::endl;
113#endif
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700114}
115
116void
117ChatDialog::processTreeUpdate(const std::vector<Sync::MissingDataInfo> v)
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700118{
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700119#ifdef __DEBUG
120 std::cout << "<<< processing Tree Update" << std::endl;
121#endif
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700122 if (v.empty())
123 {
124 return;
125 }
126
127 // reflect the changes on digest tree
128 m_scene->processUpdate(v, m_sock->getRootDigest().c_str());
129
130 int n = v.size();
131 int totalMissingPackets = 0;
132 for (int i = 0; i < n; i++)
133 {
134 totalMissingPackets += v[i].high.getSeq() - v[i].low.getSeq() + 1;
135 }
136
137 if (totalMissingPackets < 10) {
138 for (int i = 0; i < n; i++)
139 {
140 for (Sync::SeqNo seq = v[i].low; seq <= v[i].high; ++seq)
141 {
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700142 m_sock->fetchRaw(v[i].prefix, seq, bind(&ChatDialog::processDataWrapper, this, _1, _2, _3), 2);
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700143#ifdef __DEBUG
144 std::cout << "<<< Fetching " << v[i].prefix << "/" <<seq.getSession() <<"/" << seq.getSeq() << std::endl;
145#endif
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700146 }
147 }
148 }
149 else
150 {
151 // too bad; too many missing packets
152 // we may just join a new chatroom
153 // or some network patition just healed
154 // we don't try to fetch any data in this case (for now)
155 }
156
157 // adjust the view
158 fitView();
159
160}
161
162void
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700163ChatDialog::processDataWrapper(std::string name, const char *buf, size_t len)
164{
165 emit dataReceived(name.c_str(), buf, len);
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700166#ifdef __DEBUG
167 std::cout <<"<<< " << name << " fetched" << std::endl;
168#endif
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700169}
170
171void
172ChatDialog::processData(QString name, const char *buf, size_t len)
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700173{
174 SyncDemo::ChatMessage msg;
175 if (!msg.ParseFromArray(buf, len))
176 {
177 std::cerr << "Errrrr.. Can not parse msg at "<<__FILE__ <<":"<<__LINE__<<". what is happening?" << std::endl;
178 abort();
179 }
180
Zhenkai Zhu10ccb5a2012-06-04 21:55:14 -0700181 // display msg received from network
182 // we have to do so; this function is called by ccnd thread
183 // so if we call appendMsg directly
184 // Qt crash as "QObject: Cannot create children for a parent that is in a different thread"
185 // the "cannonical" way to is use signal-slot
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700186 appendMessage(msg);
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700187
188 // update the tree view
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700189 std::string prefix = name.toStdString().substr(0, name.toStdString().find_last_of('/'));
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700190 m_scene->msgReceived(prefix.c_str(), msg.from().c_str());
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700191 fitView();
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700192}
193
194void
195ChatDialog::processRemove(std::string prefix)
196{
197}
198
199void
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700200ChatDialog::formChatMessage(const QString &text, SyncDemo::ChatMessage &msg) {
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700201 msg.set_from(m_user.getNick().toStdString());
202 msg.set_to(m_user.getChatroom().toStdString());
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700203 msg.set_data(text.toStdString());
204 time_t seconds = time(NULL);
205 msg.set_timestamp(seconds);
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700206 msg.set_type(SyncDemo::ChatMessage::CHAT);
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700207}
208
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700209bool
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700210ChatDialog::readSettings()
211{
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700212#ifndef __DEBUG
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700213 QSettings s(ORGANIZATION, APPLICATION);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700214 QString nick = s.value("nick", "").toString();
215 QString chatroom = s.value("chatroom", "").toString();
216 QString prefix = s.value("prefix", "").toString();
217 if (nick == "" || chatroom == "" || prefix == "") {
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700218 QTimer::singleShot(500, this, SLOT(buttonPressed()));
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700219 return false;
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700220 }
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700221 else {
222 m_user.setNick(nick);
223 m_user.setChatroom(chatroom);
224 m_user.setPrefix(prefix);
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700225 return true;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700226 }
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700227#else
228 QTimer::singleShot(500, this, SLOT(buttonPressed()));
229 return false;
230#endif
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700231}
232
233void
234ChatDialog::writeSettings()
235{
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700236#ifndef __DEBUG
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700237 QSettings s(ORGANIZATION, APPLICATION);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700238 s.setValue("nick", m_user.getNick());
239 s.setValue("chatroom", m_user.getChatroom());
240 s.setValue("prefix", m_user.getPrefix());
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700241#endif
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700242}
243
244void
245ChatDialog::updateLabels()
246{
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700247 QString settingDisp = QString("<User: %1>, <Chatroom: %2>").arg(m_user.getNick()).arg(m_user.getChatroom());
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700248 infoLabel->setText(settingDisp);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700249 QString prefixDisp = QString("<Prefix: %1>").arg(m_user.getPrefix());
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700250 prefixLabel->setText(prefixDisp);
251}
252
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700253void
254ChatDialog::returnPressed()
255{
256 QString text = lineEdit->text();
257 if (text.isEmpty())
258 return;
259
Zhenkai Zhub6338822012-05-31 13:27:24 -0700260 lineEdit->clear();
261
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700262 SyncDemo::ChatMessage msg;
263 formChatMessage(text, msg);
264
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700265 appendMessage(msg);
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700266
267 // send msg
268 size_t size = msg.ByteSize();
269 char *buf = new char[size];
270 msg.SerializeToArray(buf, size);
271 if (!msg.IsInitialized())
272 {
273 std::cerr << "Errrrr.. msg was not probally initialized "<<__FILE__ <<":"<<__LINE__<<". what is happening?" << std::endl;
274 abort();
275 }
276 m_sock->publishRaw(m_user.getPrefix().toStdString(), m_session, buf, size, 60);
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700277
278 int nextSequence = m_sock->getNextSeq(m_user.getPrefix().toStdString(), m_session);
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700279 Sync::MissingDataInfo mdi = {m_user.getPrefix().toStdString(), Sync::SeqNo(0), Sync::SeqNo(nextSequence - 1)};
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700280 std::vector<Sync::MissingDataInfo> v;
281 v.push_back(mdi);
282 m_scene->processUpdate(v, m_sock->getRootDigest().c_str());
283 m_scene->msgReceived(m_user.getPrefix(), m_user.getNick());
284 fitView();
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700285}
286
287void
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700288ChatDialog::buttonPressed()
289{
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700290 SettingDialog dialog(this, m_user.getNick(), m_user.getChatroom(), m_user.getPrefix());
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700291 connect(&dialog, SIGNAL(updated(QString, QString, QString)), this, SLOT(settingUpdated(QString, QString, QString)));
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700292 dialog.exec();
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -0700293}
Zhenkai Zhue08afe02012-05-31 15:49:07 -0700294
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700295void
296ChatDialog::settingUpdated(QString nick, QString chatroom, QString prefix)
297{
298 bool needWrite = false;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700299 if (!nick.isEmpty() && nick != m_user.getNick()) {
300 m_user.setNick(nick);
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700301 needWrite = true;
302 }
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700303 if (!prefix.isEmpty() && prefix != m_user.getPrefix()) {
304 m_user.setPrefix(prefix);
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700305 needWrite = true;
306 // TODO: set the previous prefix as left?
307 }
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700308 if (!chatroom.isEmpty() && chatroom != m_user.getChatroom()) {
309 m_user.setChatroom(chatroom);
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700310 needWrite = true;
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700311
312 m_scene->clearAll();
313 m_scene->plot("Empty");
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700314 // TODO: perhaps need to do a lot. e.g. use a new SyncAppSokcet
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700315 if (m_sock != NULL)
316 {
317 delete m_sock;
318 m_sock = NULL;
319 }
320 std::string syncPrefix = BROADCAST_PREFIX_FOR_SYNC_DEMO;
321 syncPrefix += "/";
322 syncPrefix += m_user.getChatroom().toStdString();
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700323 m_sock = new Sync::SyncAppSocket(syncPrefix, bind(&ChatDialog::processTreeUpdateWrapper, this, _1, _2), bind(&ChatDialog::processRemove, this, _1));
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700324
325 fitView();
326
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700327 }
328 if (needWrite) {
329 writeSettings();
330 updateLabels();
331 }
332}
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700333
334void
335ChatDialog::resizeEvent(QResizeEvent *e)
336{
337 fitView();
338}
339
340void
341ChatDialog::showEvent(QShowEvent *e)
342{
343 fitView();
344}
345
346void
347ChatDialog::fitView()
348{
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700349 QRectF rect = m_scene->itemsBoundingRect();
350 m_scene->setSceneRect(rect);
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700351 treeViewer->fitInView(m_scene->itemsBoundingRect(), Qt::KeepAspectRatio);
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700352}