add sync functions
diff --git a/chatdialog.cpp b/chatdialog.cpp
index 6fd93df..10c7af0 100644
--- a/chatdialog.cpp
+++ b/chatdialog.cpp
@@ -5,26 +5,47 @@
#include <iostream>
#include <QTimer>
+#define BROADCAST_PREFIX_FOR_SYNC_DEMO "/ndn/broadcast/sync-demo"
+
ChatDialog::ChatDialog(QWidget *parent)
- : QDialog(parent)
+ : QDialog(parent), m_sock(NULL)
{
setupUi(this);
+ m_session = time(NULL);
readSettings();
+
updateLabels();
lineEdit->setFocusPolicy(Qt::StrongFocus);
- DigestTreeScene *scene = new DigestTreeScene();
+ m_scene = new DigestTreeScene(this);
- treeViewer->setScene(scene);
- QRectF rect = scene->itemsBoundingRect();
- scene->setSceneRect(rect);
- //scene->plot();
+ treeViewer->setScene(m_scene);
+ m_scene->plot("Empty");
+ QRectF rect = m_scene->itemsBoundingRect();
+ m_scene->setSceneRect(rect);
+
+ // create sync socket
+ if(!m_user.getChatroom().isEmpty()) {
+ std::string syncPrefix = BROADCAST_PREFIX_FOR_SYNC_DEMO;
+ syncPrefix += "/";
+ syncPrefix += m_user.getChatroom().toStdString();
+ m_sock = new Sync::SyncAppSocket(syncPrefix, bind(&ChatDialog::processTreeUpdate, this, _1, _2), bind(&ChatDialog::processRemove, this, _1));
+ }
connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(returnPressed()));
connect(setButton, SIGNAL(pressed()), this, SLOT(buttonPressed()));
}
+ChatDialog::~ChatDialog()
+{
+ if (m_sock != NULL)
+ {
+ delete m_sock;
+ m_sock = NULL;
+ }
+}
+
void
ChatDialog::appendMessage(const SyncDemo::ChatMessage &msg)
{
@@ -54,15 +75,79 @@
}
void
+ChatDialog::processTreeUpdate(const std::vector<Sync::MissingDataInfo> &v, Sync::SyncAppSocket *)
+{
+ if (v.empty())
+ {
+ return;
+ }
+
+ // reflect the changes on digest tree
+ m_scene->processUpdate(v, m_sock->getRootDigest().c_str());
+
+ int n = v.size();
+ int totalMissingPackets = 0;
+ for (int i = 0; i < n; i++)
+ {
+ totalMissingPackets += v[i].high.getSeq() - v[i].low.getSeq() + 1;
+ }
+
+ if (totalMissingPackets < 10) {
+ for (int i = 0; i < n; i++)
+ {
+ for (Sync::SeqNo seq = v[i].low; seq <= v[i].high; ++seq)
+ {
+ m_sock->fetchRaw(v[i].prefix, seq, bind(&ChatDialog::processData, this, _1, _2, _3), 2);
+ }
+ }
+ }
+ else
+ {
+ // too bad; too many missing packets
+ // we may just join a new chatroom
+ // or some network patition just healed
+ // we don't try to fetch any data in this case (for now)
+ }
+
+ // adjust the view
+ fitView();
+
+}
+
+void
+ChatDialog::processData(std::string name, const char *buf, size_t len)
+{
+ SyncDemo::ChatMessage msg;
+ if (!msg.ParseFromArray(buf, len))
+ {
+ std::cerr << "Errrrr.. Can not parse msg at "<<__FILE__ <<":"<<__LINE__<<". what is happening?" << std::endl;
+ abort();
+ }
+
+ // display
+ appendMessage(msg);
+
+ // update the tree view
+ std::string prefix = name.substr(0, name.find_last_of('/'));
+ m_scene->msgReceived(prefix.c_str(), msg.from().c_str());
+}
+
+void
+ChatDialog::processRemove(std::string prefix)
+{
+}
+
+void
ChatDialog::formChatMessage(const QString &text, SyncDemo::ChatMessage &msg) {
msg.set_from(m_user.getNick().toStdString());
msg.set_to(m_user.getChatroom().toStdString());
msg.set_data(text.toStdString());
time_t seconds = time(NULL);
msg.set_timestamp(seconds);
+ msg.set_type(SyncDemo::ChatMessage::CHAT);
}
-void
+bool
ChatDialog::readSettings()
{
QSettings s(ORGANIZATION, APPLICATION);
@@ -71,11 +156,13 @@
QString prefix = s.value("prefix", "").toString();
if (nick == "" || chatroom == "" || prefix == "") {
QTimer::singleShot(500, this, SLOT(buttonPressed()));
+ return false;
}
else {
m_user.setNick(nick);
m_user.setChatroom(chatroom);
m_user.setPrefix(prefix);
+ return true;
}
}
@@ -109,9 +196,18 @@
SyncDemo::ChatMessage msg;
formChatMessage(text, msg);
- // TODO:
- // send message
appendMessage(msg);
+
+ // send msg
+ size_t size = msg.ByteSize();
+ char *buf = new char[size];
+ msg.SerializeToArray(buf, size);
+ if (!msg.IsInitialized())
+ {
+ std::cerr << "Errrrr.. msg was not probally initialized "<<__FILE__ <<":"<<__LINE__<<". what is happening?" << std::endl;
+ abort();
+ }
+ m_sock->publishRaw(m_user.getPrefix().toStdString(), m_session, buf, size, 60);
}
@@ -139,7 +235,22 @@
if (!chatroom.isEmpty() && chatroom != m_user.getChatroom()) {
m_user.setChatroom(chatroom);
needWrite = true;
+
+ m_scene->clearAll();
+ m_scene->plot("Empty");
// TODO: perhaps need to do a lot. e.g. use a new SyncAppSokcet
+ if (m_sock != NULL)
+ {
+ delete m_sock;
+ m_sock = NULL;
+ }
+ std::string syncPrefix = BROADCAST_PREFIX_FOR_SYNC_DEMO;
+ syncPrefix += "/";
+ syncPrefix += m_user.getChatroom().toStdString();
+ m_sock = new Sync::SyncAppSocket(syncPrefix, bind(&ChatDialog::processTreeUpdate, this, _1, _2), bind(&ChatDialog::processRemove, this, _1));
+
+ fitView();
+
}
if (needWrite) {
writeSettings();
@@ -162,5 +273,5 @@
void
ChatDialog::fitView()
{
- treeViewer->fitInView(treeViewer->scene()->itemsBoundingRect(), Qt::KeepAspectRatio);
+ treeViewer->fitInView(m_scene->itemsBoundingRect(), Qt::KeepAspectRatio);
}
diff --git a/chatdialog.h b/chatdialog.h
index 9ca77c0..252144b 100644
--- a/chatdialog.h
+++ b/chatdialog.h
@@ -18,12 +18,15 @@
public:
ChatDialog(QWidget *parent = 0);
+ ~ChatDialog();
void appendMessage(const SyncDemo::ChatMessage &msg);
- void processData(const std::vector<Sync::MissingDataInfo> &, Sync::SyncAppSocket *);
+ void processTreeUpdate(const std::vector<Sync::MissingDataInfo> &, Sync::SyncAppSocket *);
+ void processData(std::string, const char *buf, size_t len);
+ void processRemove(const std::string);
private:
void formChatMessage(const QString &text, SyncDemo::ChatMessage &msg);
- void readSettings();
+ bool readSettings();
void writeSettings();
void updateLabels();
void resizeEvent(QResizeEvent *);
@@ -38,5 +41,7 @@
private:
User m_user;
Sync::SyncAppSocket *m_sock;
+ uint32_t m_session;
+ DigestTreeScene *m_scene;
};
#endif
diff --git a/digesttreescene.cpp b/digesttreescene.cpp
index 5133e4c..0dd7a10 100644
--- a/digesttreescene.cpp
+++ b/digesttreescene.cpp
@@ -36,11 +36,11 @@
: QGraphicsScene(parent)
{
previouslyUpdatedUser = DisplayUserNullPtr;
- testDraw(this);
+// testDraw(this);
}
void
-DigestTreeScene::processUpdate(std::vector<Sync::MissingDataInfo> &v, QString digest)
+DigestTreeScene::processUpdate(const std::vector<Sync::MissingDataInfo> &v, QString digest)
{
int n = v.size();
bool rePlot = false;
@@ -81,21 +81,6 @@
}
void
-DigestTreeScene::reDrawNode(DisplayUserPtr p, QColor rimColor)
-{
- QGraphicsRectItem *rimItem = p->getRimRectItem();
- rimItem->setBrush(QBrush(rimColor));
- QGraphicsRectItem *innerItem = p->getInnerRectItem();
- innerItem->setBrush(QBrush(Qt::lightGray));
- QGraphicsTextItem *seqTextItem = p->getSeqTextItem();
- std::string s = boost::lexical_cast<std::string>(p->getSeqNo().getSeq());
- seqTextItem->setPlainText(s.c_str());
- QRectF textBR = seqTextItem->boundingRect();
- QRectF innerBR = innerItem->boundingRect();
- seqTextItem->setPos(innerBR.x() + (innerBR.width() - textBR.width())/2, innerBR.y() + (innerBR.height() - textBR.height())/2);
-}
-
-void
DigestTreeScene::msgReceived(QString prefix, QString nick)
{
Roster_iterator it = m_roster.find(prefix);
@@ -124,6 +109,14 @@
}
void
+DigestTreeScene::clearAll()
+{
+ clear();
+ m_graph.clear();
+ m_roster.clear();
+}
+
+void
DigestTreeScene::plot(QString digest)
{
clear();
@@ -250,3 +243,18 @@
}
}
+void
+DigestTreeScene::reDrawNode(DisplayUserPtr p, QColor rimColor)
+{
+ QGraphicsRectItem *rimItem = p->getRimRectItem();
+ rimItem->setBrush(QBrush(rimColor));
+ QGraphicsRectItem *innerItem = p->getInnerRectItem();
+ innerItem->setBrush(QBrush(Qt::lightGray));
+ QGraphicsTextItem *seqTextItem = p->getSeqTextItem();
+ std::string s = boost::lexical_cast<std::string>(p->getSeqNo().getSeq());
+ seqTextItem->setPlainText(s.c_str());
+ QRectF textBR = seqTextItem->boundingRect();
+ QRectF innerBR = innerItem->boundingRect();
+ seqTextItem->setPos(innerBR.x() + (innerBR.width() - textBR.width())/2, innerBR.y() + (innerBR.height() - textBR.height())/2);
+}
+
diff --git a/digesttreescene.h b/digesttreescene.h
index 08a50d0..91876e6 100644
--- a/digesttreescene.h
+++ b/digesttreescene.h
@@ -29,10 +29,11 @@
public:
DigestTreeScene(QWidget *parent = 0);
- void processUpdate(std::vector<Sync::MissingDataInfo> &v, QString digest);
+ void processUpdate(const std::vector<Sync::MissingDataInfo> &v, QString digest);
void msgReceived(QString prefix, QString nick);
-private:
+ void clearAll();
void plot(QString digest);
+private:
void plotEdge(ogdf::GraphAttributes &GA);
void plotNode(ogdf::GraphAttributes &GA, int rootIndex, QString digest);
void reDrawNode(DisplayUserPtr p, QColor rimColor);