blob: 20ed372dfa66dc63acab454cca7979c12e7dde16 [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 Zhua4fb1242012-06-05 20:26:05 -070060 try
61 {
62 m_sock = new Sync::SyncAppSocket(syncPrefix, bind(&ChatDialog::processTreeUpdateWrapper, this, _1, _2), bind(&ChatDialog::processRemove, this, _1));
63 }
64 catch (Sync::CcnxOperationException ex)
65 {
66 QMessageBox::critical(this, tr("Sync-Demo"), tr("Canno connect to ccnd.\n Have you started your ccnd?"), QMessageBox::Ok);
67 std::exit(1);
68 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -070069 }
Zhenkai Zhub45e38a2012-06-01 15:44:36 -070070
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -070071 connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(returnPressed()));
Zhenkai Zhu85845d22012-06-01 23:10:43 -070072 connect(setButton, SIGNAL(pressed()), this, SLOT(buttonPressed()));
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -070073 connect(this, SIGNAL(dataReceived(QString, const char *, size_t)), this, SLOT(processData(QString, const char *, size_t)));
74 connect(this, SIGNAL(treeUpdated(const std::vector<Sync::MissingDataInfo>)), this, SLOT(processTreeUpdate(const std::vector<Sync::MissingDataInfo>)));
Zhenkai Zhu10ccb5a2012-06-04 21:55:14 -070075 //testDraw();
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -070076}
77
Zhenkai Zhu82a62752012-06-04 17:11:04 -070078ChatDialog::~ChatDialog()
79{
80 if (m_sock != NULL)
81 {
82 delete m_sock;
83 m_sock = NULL;
84 }
85}
86
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -070087void
Zhenkai Zhu10ccb5a2012-06-04 21:55:14 -070088ChatDialog::appendMessage(const SyncDemo::ChatMessage msg)
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -070089{
Zhenkai Zhub6338822012-05-31 13:27:24 -070090
91 if (msg.type() != SyncDemo::ChatMessage::CHAT) {
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -070092 return;
Zhenkai Zhub6338822012-05-31 13:27:24 -070093 }
94
95 if (!msg.has_data()) {
96 return;
97 }
98
99 if (msg.from().empty() || msg.data().empty()) {
100 return;
101 }
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700102
103 QTextCursor cursor(textEdit->textCursor());
104 cursor.movePosition(QTextCursor::End);
105 QTextTableFormat tableFormat;
106 tableFormat.setBorder(0);
107 QTextTable *table = cursor.insertTable(1, 2, tableFormat);
Zhenkai Zhub6338822012-05-31 13:27:24 -0700108 QString from = QString("<%1>: ").arg(msg.from().c_str());
109 table->cellAt(0, 0).firstCursorPosition().insertText(from);
110 table->cellAt(0, 1).firstCursorPosition().insertText(msg.data().c_str());
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700111 QScrollBar *bar = textEdit->verticalScrollBar();
112 bar->setValue(bar->maximum());
113}
114
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700115void
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700116ChatDialog::processTreeUpdateWrapper(const std::vector<Sync::MissingDataInfo> v, Sync::SyncAppSocket *sock)
117{
118 emit treeUpdated(v);
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700119#ifdef __DEBUG
120 std::cout << "<<< Tree update signal emitted" << std::endl;
121#endif
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700122}
123
124void
125ChatDialog::processTreeUpdate(const std::vector<Sync::MissingDataInfo> v)
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700126{
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700127#ifdef __DEBUG
128 std::cout << "<<< processing Tree Update" << std::endl;
129#endif
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700130 if (v.empty())
131 {
132 return;
133 }
134
135 // reflect the changes on digest tree
136 m_scene->processUpdate(v, m_sock->getRootDigest().c_str());
137
138 int n = v.size();
139 int totalMissingPackets = 0;
140 for (int i = 0; i < n; i++)
141 {
142 totalMissingPackets += v[i].high.getSeq() - v[i].low.getSeq() + 1;
143 }
144
145 if (totalMissingPackets < 10) {
146 for (int i = 0; i < n; i++)
147 {
148 for (Sync::SeqNo seq = v[i].low; seq <= v[i].high; ++seq)
149 {
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700150 m_sock->fetchRaw(v[i].prefix, seq, bind(&ChatDialog::processDataWrapper, this, _1, _2, _3), 2);
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700151#ifdef __DEBUG
152 std::cout << "<<< Fetching " << v[i].prefix << "/" <<seq.getSession() <<"/" << seq.getSeq() << std::endl;
153#endif
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700154 }
155 }
156 }
157 else
158 {
159 // too bad; too many missing packets
160 // we may just join a new chatroom
161 // or some network patition just healed
162 // we don't try to fetch any data in this case (for now)
163 }
164
165 // adjust the view
166 fitView();
167
168}
169
170void
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700171ChatDialog::processDataWrapper(std::string name, const char *buf, size_t len)
172{
173 emit dataReceived(name.c_str(), buf, len);
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700174#ifdef __DEBUG
175 std::cout <<"<<< " << name << " fetched" << std::endl;
176#endif
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700177}
178
179void
180ChatDialog::processData(QString name, const char *buf, size_t len)
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700181{
182 SyncDemo::ChatMessage msg;
183 if (!msg.ParseFromArray(buf, len))
184 {
185 std::cerr << "Errrrr.. Can not parse msg at "<<__FILE__ <<":"<<__LINE__<<". what is happening?" << std::endl;
186 abort();
187 }
188
Zhenkai Zhu10ccb5a2012-06-04 21:55:14 -0700189 // display msg received from network
190 // we have to do so; this function is called by ccnd thread
191 // so if we call appendMsg directly
192 // Qt crash as "QObject: Cannot create children for a parent that is in a different thread"
193 // the "cannonical" way to is use signal-slot
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700194 appendMessage(msg);
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700195
196 // update the tree view
Zhenkai Zhu097bfe72012-06-05 14:30:17 -0700197 std::string stdStrName = name.toStdString();
198 std::string stdStrNameWithoutSeq = stdStrName.substr(0, stdStrName.find_last_of('/'));
199 std::string prefix = stdStrNameWithoutSeq.substr(0, stdStrNameWithoutSeq.find_last_of('/'));
200#ifdef __DEBUG
201 std::cout <<"<<< updating scene for" << prefix << ": " << msg.from() << std::endl;
202#endif
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700203 m_scene->msgReceived(prefix.c_str(), msg.from().c_str());
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700204 fitView();
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700205}
206
207void
208ChatDialog::processRemove(std::string prefix)
209{
210}
211
212void
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700213ChatDialog::formChatMessage(const QString &text, SyncDemo::ChatMessage &msg) {
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700214 msg.set_from(m_user.getNick().toStdString());
215 msg.set_to(m_user.getChatroom().toStdString());
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700216 msg.set_data(text.toStdString());
217 time_t seconds = time(NULL);
218 msg.set_timestamp(seconds);
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700219 msg.set_type(SyncDemo::ChatMessage::CHAT);
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700220}
221
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700222bool
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700223ChatDialog::readSettings()
224{
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700225#ifndef __DEBUG
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700226 QSettings s(ORGANIZATION, APPLICATION);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700227 QString nick = s.value("nick", "").toString();
228 QString chatroom = s.value("chatroom", "").toString();
229 QString prefix = s.value("prefix", "").toString();
230 if (nick == "" || chatroom == "" || prefix == "") {
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700231 QTimer::singleShot(500, this, SLOT(buttonPressed()));
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700232 return false;
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700233 }
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700234 else {
235 m_user.setNick(nick);
236 m_user.setChatroom(chatroom);
237 m_user.setPrefix(prefix);
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700238 return true;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700239 }
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700240#else
241 QTimer::singleShot(500, this, SLOT(buttonPressed()));
242 return false;
243#endif
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700244}
245
246void
247ChatDialog::writeSettings()
248{
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700249#ifndef __DEBUG
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700250 QSettings s(ORGANIZATION, APPLICATION);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700251 s.setValue("nick", m_user.getNick());
252 s.setValue("chatroom", m_user.getChatroom());
253 s.setValue("prefix", m_user.getPrefix());
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700254#endif
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700255}
256
257void
258ChatDialog::updateLabels()
259{
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700260 QString settingDisp = QString("<User: %1>, <Chatroom: %2>").arg(m_user.getNick()).arg(m_user.getChatroom());
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700261 infoLabel->setText(settingDisp);
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700262 QString prefixDisp = QString("<Prefix: %1>").arg(m_user.getPrefix());
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700263 prefixLabel->setText(prefixDisp);
264}
265
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700266void
267ChatDialog::returnPressed()
268{
269 QString text = lineEdit->text();
270 if (text.isEmpty())
271 return;
272
Zhenkai Zhub6338822012-05-31 13:27:24 -0700273 lineEdit->clear();
274
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700275 SyncDemo::ChatMessage msg;
276 formChatMessage(text, msg);
277
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700278 appendMessage(msg);
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700279
280 // send msg
281 size_t size = msg.ByteSize();
282 char *buf = new char[size];
283 msg.SerializeToArray(buf, size);
284 if (!msg.IsInitialized())
285 {
286 std::cerr << "Errrrr.. msg was not probally initialized "<<__FILE__ <<":"<<__LINE__<<". what is happening?" << std::endl;
287 abort();
288 }
289 m_sock->publishRaw(m_user.getPrefix().toStdString(), m_session, buf, size, 60);
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700290
291 int nextSequence = m_sock->getNextSeq(m_user.getPrefix().toStdString(), m_session);
Zhenkai Zhud1c5a972012-06-05 14:07:41 -0700292 Sync::MissingDataInfo mdi = {m_user.getPrefix().toStdString(), Sync::SeqNo(0), Sync::SeqNo(nextSequence - 1)};
Zhenkai Zhuc5470612012-06-05 12:28:59 -0700293 std::vector<Sync::MissingDataInfo> v;
294 v.push_back(mdi);
295 m_scene->processUpdate(v, m_sock->getRootDigest().c_str());
296 m_scene->msgReceived(m_user.getPrefix(), m_user.getNick());
297 fitView();
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700298}
299
300void
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700301ChatDialog::buttonPressed()
302{
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700303 SettingDialog dialog(this, m_user.getNick(), m_user.getChatroom(), m_user.getPrefix());
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700304 connect(&dialog, SIGNAL(updated(QString, QString, QString)), this, SLOT(settingUpdated(QString, QString, QString)));
Zhenkai Zhu85845d22012-06-01 23:10:43 -0700305 dialog.exec();
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -0700306}
Zhenkai Zhue08afe02012-05-31 15:49:07 -0700307
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700308void
309ChatDialog::settingUpdated(QString nick, QString chatroom, QString prefix)
310{
311 bool needWrite = false;
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700312 if (!nick.isEmpty() && nick != m_user.getNick()) {
313 m_user.setNick(nick);
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700314 needWrite = true;
315 }
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700316 if (!prefix.isEmpty() && prefix != m_user.getPrefix()) {
317 m_user.setPrefix(prefix);
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700318 needWrite = true;
319 // TODO: set the previous prefix as left?
320 }
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700321 if (!chatroom.isEmpty() && chatroom != m_user.getChatroom()) {
322 m_user.setChatroom(chatroom);
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700323 needWrite = true;
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700324
325 m_scene->clearAll();
326 m_scene->plot("Empty");
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700327 // TODO: perhaps need to do a lot. e.g. use a new SyncAppSokcet
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700328 if (m_sock != NULL)
329 {
330 delete m_sock;
331 m_sock = NULL;
332 }
333 std::string syncPrefix = BROADCAST_PREFIX_FOR_SYNC_DEMO;
334 syncPrefix += "/";
335 syncPrefix += m_user.getChatroom().toStdString();
Zhenkai Zhua4fb1242012-06-05 20:26:05 -0700336 try
337 {
338 m_sock = new Sync::SyncAppSocket(syncPrefix, bind(&ChatDialog::processTreeUpdateWrapper, this, _1, _2), bind(&ChatDialog::processRemove, this, _1));
339 }
340 catch (Sync::CcnxOperationException ex)
341 {
342 QMessageBox::critical(this, tr("Sync-Demo"), tr("Canno connect to ccnd.\n Have you started your ccnd?"), QMessageBox::Ok);
343 std::exit(1);
344 }
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700345
346 fitView();
347
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -0700348 }
349 if (needWrite) {
350 writeSettings();
351 updateLabels();
352 }
353}
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700354
355void
356ChatDialog::resizeEvent(QResizeEvent *e)
357{
358 fitView();
359}
360
361void
362ChatDialog::showEvent(QShowEvent *e)
363{
364 fitView();
365}
366
367void
368ChatDialog::fitView()
369{
Zhenkai Zhu21d75f92012-06-04 21:23:34 -0700370 QRectF rect = m_scene->itemsBoundingRect();
371 m_scene->setSceneRect(rect);
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700372 treeViewer->fitInView(m_scene->itemsBoundingRect(), Qt::KeepAspectRatio);
Zhenkai Zhud13acd02012-06-04 15:25:20 -0700373}