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