Zhenkai Zhu | fd52ab7 | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 1 | #include <QtGui> |
| 2 | #include "chatdialog.h" |
Zhenkai Zhu | 275ba3a | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 3 | #include "settingdialog.h" |
Zhenkai Zhu | c71da77 | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 4 | #include <ctime> |
Zhenkai Zhu | 2c55b38 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 5 | #include <iostream> |
Zhenkai Zhu | 3472e3f | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 6 | #include <QTimer> |
Zhenkai Zhu | e8b9e8e | 2012-06-04 21:55:14 -0700 | [diff] [blame^] | 7 | #include <QMetaType> |
Zhenkai Zhu | fd52ab7 | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 8 | |
Zhenkai Zhu | 36c6b78 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 9 | #define BROADCAST_PREFIX_FOR_SYNC_DEMO "/ndn/broadcast/sync-demo" |
| 10 | |
Zhenkai Zhu | 88380c1 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 11 | void |
| 12 | ChatDialog::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 Zhu | fd52ab7 | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 33 | ChatDialog::ChatDialog(QWidget *parent) |
Zhenkai Zhu | 36c6b78 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 34 | : QDialog(parent), m_sock(NULL) |
Zhenkai Zhu | fd52ab7 | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 35 | { |
Zhenkai Zhu | e8b9e8e | 2012-06-04 21:55:14 -0700 | [diff] [blame^] | 36 | // have to register this, otherwise |
| 37 | // the signal-slot system won't recognize this type |
| 38 | qRegisterMetaType<SyncDemo::ChatMessage>("SyncDemo::ChatMessage"); |
Zhenkai Zhu | fd52ab7 | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 39 | setupUi(this); |
Zhenkai Zhu | 36c6b78 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 40 | m_session = time(NULL); |
Zhenkai Zhu | 3472e3f | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 41 | |
| 42 | readSettings(); |
Zhenkai Zhu | 36c6b78 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 43 | |
Zhenkai Zhu | 3472e3f | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 44 | updateLabels(); |
| 45 | |
Zhenkai Zhu | fd52ab7 | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 46 | lineEdit->setFocusPolicy(Qt::StrongFocus); |
Zhenkai Zhu | 36c6b78 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 47 | m_scene = new DigestTreeScene(this); |
Zhenkai Zhu | 9ec8f41 | 2012-06-01 15:44:36 -0700 | [diff] [blame] | 48 | |
Zhenkai Zhu | 36c6b78 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 49 | treeViewer->setScene(m_scene); |
| 50 | m_scene->plot("Empty"); |
| 51 | QRectF rect = m_scene->itemsBoundingRect(); |
| 52 | m_scene->setSceneRect(rect); |
| 53 | |
| 54 | // create sync socket |
| 55 | if(!m_user.getChatroom().isEmpty()) { |
| 56 | std::string syncPrefix = BROADCAST_PREFIX_FOR_SYNC_DEMO; |
| 57 | syncPrefix += "/"; |
| 58 | syncPrefix += m_user.getChatroom().toStdString(); |
| 59 | m_sock = new Sync::SyncAppSocket(syncPrefix, bind(&ChatDialog::processTreeUpdate, this, _1, _2), bind(&ChatDialog::processRemove, this, _1)); |
| 60 | } |
Zhenkai Zhu | 9ec8f41 | 2012-06-01 15:44:36 -0700 | [diff] [blame] | 61 | |
Zhenkai Zhu | c71da77 | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 62 | connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(returnPressed())); |
Zhenkai Zhu | 275ba3a | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 63 | connect(setButton, SIGNAL(pressed()), this, SLOT(buttonPressed())); |
Zhenkai Zhu | e8b9e8e | 2012-06-04 21:55:14 -0700 | [diff] [blame^] | 64 | connect(this, SIGNAL(msgReceived(const SyncDemo::ChatMessage)), this, SLOT(appendMessage(const SyncDemo::ChatMessage))); |
| 65 | //testDraw(); |
Zhenkai Zhu | c71da77 | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Zhenkai Zhu | 36c6b78 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 68 | ChatDialog::~ChatDialog() |
| 69 | { |
| 70 | if (m_sock != NULL) |
| 71 | { |
| 72 | delete m_sock; |
| 73 | m_sock = NULL; |
| 74 | } |
| 75 | } |
| 76 | |
Zhenkai Zhu | c71da77 | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 77 | void |
Zhenkai Zhu | e8b9e8e | 2012-06-04 21:55:14 -0700 | [diff] [blame^] | 78 | ChatDialog::appendMessage(const SyncDemo::ChatMessage msg) |
Zhenkai Zhu | c71da77 | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 79 | { |
Zhenkai Zhu | 2c55b38 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 80 | |
| 81 | if (msg.type() != SyncDemo::ChatMessage::CHAT) { |
Zhenkai Zhu | c71da77 | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 82 | return; |
Zhenkai Zhu | 2c55b38 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | if (!msg.has_data()) { |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | if (msg.from().empty() || msg.data().empty()) { |
| 90 | return; |
| 91 | } |
Zhenkai Zhu | c71da77 | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 92 | |
Zhenkai Zhu | e8b9e8e | 2012-06-04 21:55:14 -0700 | [diff] [blame^] | 93 | std::cout << "<<<< Received Message: " << msg.data() << std::endl; |
Zhenkai Zhu | c71da77 | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 94 | QTextCursor cursor(textEdit->textCursor()); |
| 95 | cursor.movePosition(QTextCursor::End); |
| 96 | QTextTableFormat tableFormat; |
| 97 | tableFormat.setBorder(0); |
| 98 | QTextTable *table = cursor.insertTable(1, 2, tableFormat); |
Zhenkai Zhu | 2c55b38 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 99 | QString from = QString("<%1>: ").arg(msg.from().c_str()); |
| 100 | table->cellAt(0, 0).firstCursorPosition().insertText(from); |
| 101 | table->cellAt(0, 1).firstCursorPosition().insertText(msg.data().c_str()); |
Zhenkai Zhu | c71da77 | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 102 | QScrollBar *bar = textEdit->verticalScrollBar(); |
| 103 | bar->setValue(bar->maximum()); |
Zhenkai Zhu | e8b9e8e | 2012-06-04 21:55:14 -0700 | [diff] [blame^] | 104 | std::cout << "<<<<, Message appended " << std::endl; |
Zhenkai Zhu | c71da77 | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Zhenkai Zhu | 275ba3a | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 107 | void |
Zhenkai Zhu | 36c6b78 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 108 | ChatDialog::processTreeUpdate(const std::vector<Sync::MissingDataInfo> &v, Sync::SyncAppSocket *) |
| 109 | { |
| 110 | if (v.empty()) |
| 111 | { |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | // reflect the changes on digest tree |
| 116 | m_scene->processUpdate(v, m_sock->getRootDigest().c_str()); |
| 117 | |
| 118 | int n = v.size(); |
| 119 | int totalMissingPackets = 0; |
| 120 | for (int i = 0; i < n; i++) |
| 121 | { |
| 122 | totalMissingPackets += v[i].high.getSeq() - v[i].low.getSeq() + 1; |
| 123 | } |
| 124 | |
| 125 | if (totalMissingPackets < 10) { |
| 126 | for (int i = 0; i < n; i++) |
| 127 | { |
| 128 | for (Sync::SeqNo seq = v[i].low; seq <= v[i].high; ++seq) |
| 129 | { |
| 130 | m_sock->fetchRaw(v[i].prefix, seq, bind(&ChatDialog::processData, this, _1, _2, _3), 2); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | else |
| 135 | { |
| 136 | // too bad; too many missing packets |
| 137 | // we may just join a new chatroom |
| 138 | // or some network patition just healed |
| 139 | // we don't try to fetch any data in this case (for now) |
| 140 | } |
| 141 | |
| 142 | // adjust the view |
| 143 | fitView(); |
| 144 | |
| 145 | } |
| 146 | |
| 147 | void |
| 148 | ChatDialog::processData(std::string name, const char *buf, size_t len) |
| 149 | { |
| 150 | SyncDemo::ChatMessage msg; |
| 151 | if (!msg.ParseFromArray(buf, len)) |
| 152 | { |
| 153 | std::cerr << "Errrrr.. Can not parse msg at "<<__FILE__ <<":"<<__LINE__<<". what is happening?" << std::endl; |
| 154 | abort(); |
| 155 | } |
| 156 | |
Zhenkai Zhu | e8b9e8e | 2012-06-04 21:55:14 -0700 | [diff] [blame^] | 157 | // display msg received from network |
| 158 | // we have to do so; this function is called by ccnd thread |
| 159 | // so if we call appendMsg directly |
| 160 | // Qt crash as "QObject: Cannot create children for a parent that is in a different thread" |
| 161 | // the "cannonical" way to is use signal-slot |
| 162 | emit msgReceived(msg); |
Zhenkai Zhu | 36c6b78 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 163 | |
| 164 | // update the tree view |
| 165 | std::string prefix = name.substr(0, name.find_last_of('/')); |
| 166 | m_scene->msgReceived(prefix.c_str(), msg.from().c_str()); |
| 167 | } |
| 168 | |
| 169 | void |
| 170 | ChatDialog::processRemove(std::string prefix) |
| 171 | { |
| 172 | } |
| 173 | |
| 174 | void |
Zhenkai Zhu | 275ba3a | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 175 | ChatDialog::formChatMessage(const QString &text, SyncDemo::ChatMessage &msg) { |
Zhenkai Zhu | 56a8859 | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 176 | msg.set_from(m_user.getNick().toStdString()); |
| 177 | msg.set_to(m_user.getChatroom().toStdString()); |
Zhenkai Zhu | 275ba3a | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 178 | msg.set_data(text.toStdString()); |
| 179 | time_t seconds = time(NULL); |
| 180 | msg.set_timestamp(seconds); |
Zhenkai Zhu | 36c6b78 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 181 | msg.set_type(SyncDemo::ChatMessage::CHAT); |
Zhenkai Zhu | c71da77 | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Zhenkai Zhu | 36c6b78 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 184 | bool |
Zhenkai Zhu | 3472e3f | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 185 | ChatDialog::readSettings() |
| 186 | { |
| 187 | QSettings s(ORGANIZATION, APPLICATION); |
Zhenkai Zhu | 56a8859 | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 188 | QString nick = s.value("nick", "").toString(); |
| 189 | QString chatroom = s.value("chatroom", "").toString(); |
| 190 | QString prefix = s.value("prefix", "").toString(); |
| 191 | if (nick == "" || chatroom == "" || prefix == "") { |
Zhenkai Zhu | 3472e3f | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 192 | QTimer::singleShot(500, this, SLOT(buttonPressed())); |
Zhenkai Zhu | 36c6b78 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 193 | return false; |
Zhenkai Zhu | 3472e3f | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 194 | } |
Zhenkai Zhu | 56a8859 | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 195 | else { |
| 196 | m_user.setNick(nick); |
| 197 | m_user.setChatroom(chatroom); |
| 198 | m_user.setPrefix(prefix); |
Zhenkai Zhu | 36c6b78 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 199 | return true; |
Zhenkai Zhu | 56a8859 | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 200 | } |
Zhenkai Zhu | 3472e3f | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | void |
| 204 | ChatDialog::writeSettings() |
| 205 | { |
| 206 | QSettings s(ORGANIZATION, APPLICATION); |
Zhenkai Zhu | 56a8859 | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 207 | s.setValue("nick", m_user.getNick()); |
| 208 | s.setValue("chatroom", m_user.getChatroom()); |
| 209 | s.setValue("prefix", m_user.getPrefix()); |
Zhenkai Zhu | 3472e3f | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | void |
| 213 | ChatDialog::updateLabels() |
| 214 | { |
Zhenkai Zhu | 56a8859 | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 215 | QString settingDisp = QString("<User: %1>, <Chatroom: %2>").arg(m_user.getNick()).arg(m_user.getChatroom()); |
Zhenkai Zhu | 3472e3f | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 216 | infoLabel->setText(settingDisp); |
Zhenkai Zhu | 56a8859 | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 217 | QString prefixDisp = QString("<Prefix: %1>").arg(m_user.getPrefix()); |
Zhenkai Zhu | 3472e3f | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 218 | prefixLabel->setText(prefixDisp); |
| 219 | } |
| 220 | |
Zhenkai Zhu | c71da77 | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 221 | void |
| 222 | ChatDialog::returnPressed() |
| 223 | { |
| 224 | QString text = lineEdit->text(); |
| 225 | if (text.isEmpty()) |
| 226 | return; |
| 227 | |
Zhenkai Zhu | 2c55b38 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 228 | lineEdit->clear(); |
| 229 | |
Zhenkai Zhu | c71da77 | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 230 | SyncDemo::ChatMessage msg; |
| 231 | formChatMessage(text, msg); |
| 232 | |
Zhenkai Zhu | c71da77 | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 233 | appendMessage(msg); |
Zhenkai Zhu | 36c6b78 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 234 | |
| 235 | // send msg |
| 236 | size_t size = msg.ByteSize(); |
| 237 | char *buf = new char[size]; |
| 238 | msg.SerializeToArray(buf, size); |
| 239 | if (!msg.IsInitialized()) |
| 240 | { |
| 241 | std::cerr << "Errrrr.. msg was not probally initialized "<<__FILE__ <<":"<<__LINE__<<". what is happening?" << std::endl; |
| 242 | abort(); |
| 243 | } |
| 244 | m_sock->publishRaw(m_user.getPrefix().toStdString(), m_session, buf, size, 60); |
Zhenkai Zhu | 2c55b38 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 245 | |
Zhenkai Zhu | c71da77 | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | void |
Zhenkai Zhu | 275ba3a | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 249 | ChatDialog::buttonPressed() |
| 250 | { |
Zhenkai Zhu | 56a8859 | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 251 | SettingDialog dialog(this, m_user.getNick(), m_user.getChatroom(), m_user.getPrefix()); |
Zhenkai Zhu | 3472e3f | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 252 | connect(&dialog, SIGNAL(updated(QString, QString, QString)), this, SLOT(settingUpdated(QString, QString, QString))); |
Zhenkai Zhu | 275ba3a | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 253 | dialog.exec(); |
Zhenkai Zhu | fd52ab7 | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 254 | } |
Zhenkai Zhu | 80af0e0 | 2012-05-31 15:49:07 -0700 | [diff] [blame] | 255 | |
Zhenkai Zhu | 3472e3f | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 256 | void |
| 257 | ChatDialog::settingUpdated(QString nick, QString chatroom, QString prefix) |
| 258 | { |
| 259 | bool needWrite = false; |
Zhenkai Zhu | 56a8859 | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 260 | if (!nick.isEmpty() && nick != m_user.getNick()) { |
| 261 | m_user.setNick(nick); |
Zhenkai Zhu | 3472e3f | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 262 | needWrite = true; |
| 263 | } |
Zhenkai Zhu | 56a8859 | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 264 | if (!prefix.isEmpty() && prefix != m_user.getPrefix()) { |
| 265 | m_user.setPrefix(prefix); |
Zhenkai Zhu | 3472e3f | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 266 | needWrite = true; |
| 267 | // TODO: set the previous prefix as left? |
| 268 | } |
Zhenkai Zhu | 56a8859 | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 269 | if (!chatroom.isEmpty() && chatroom != m_user.getChatroom()) { |
| 270 | m_user.setChatroom(chatroom); |
Zhenkai Zhu | 3472e3f | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 271 | needWrite = true; |
Zhenkai Zhu | 36c6b78 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 272 | |
| 273 | m_scene->clearAll(); |
| 274 | m_scene->plot("Empty"); |
Zhenkai Zhu | 3472e3f | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 275 | // TODO: perhaps need to do a lot. e.g. use a new SyncAppSokcet |
Zhenkai Zhu | 36c6b78 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 276 | if (m_sock != NULL) |
| 277 | { |
| 278 | delete m_sock; |
| 279 | m_sock = NULL; |
| 280 | } |
| 281 | std::string syncPrefix = BROADCAST_PREFIX_FOR_SYNC_DEMO; |
| 282 | syncPrefix += "/"; |
| 283 | syncPrefix += m_user.getChatroom().toStdString(); |
| 284 | m_sock = new Sync::SyncAppSocket(syncPrefix, bind(&ChatDialog::processTreeUpdate, this, _1, _2), bind(&ChatDialog::processRemove, this, _1)); |
| 285 | |
| 286 | fitView(); |
| 287 | |
Zhenkai Zhu | 3472e3f | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 288 | } |
| 289 | if (needWrite) { |
| 290 | writeSettings(); |
| 291 | updateLabels(); |
| 292 | } |
| 293 | } |
Zhenkai Zhu | e566093 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 294 | |
| 295 | void |
| 296 | ChatDialog::resizeEvent(QResizeEvent *e) |
| 297 | { |
| 298 | fitView(); |
| 299 | } |
| 300 | |
| 301 | void |
| 302 | ChatDialog::showEvent(QShowEvent *e) |
| 303 | { |
| 304 | fitView(); |
| 305 | } |
| 306 | |
| 307 | void |
| 308 | ChatDialog::fitView() |
| 309 | { |
Zhenkai Zhu | 88380c1 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 310 | QRectF rect = m_scene->itemsBoundingRect(); |
| 311 | m_scene->setSceneRect(rect); |
Zhenkai Zhu | 36c6b78 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 312 | treeViewer->fitInView(m_scene->itemsBoundingRect(), Qt::KeepAspectRatio); |
Zhenkai Zhu | e566093 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 313 | } |