partially working new chronochat
diff --git a/src/chat-policy-rule.cpp b/src/chat-policy-rule.cpp
index 1aa8b24..201cf70 100644
--- a/src/chat-policy-rule.cpp
+++ b/src/chat-policy-rule.cpp
@@ -8,33 +8,37 @@
  * Author: Yingdi Yu <yingdi@cs.ucla.edu>
  */
 
-#include "chat-policy-rule.cpp"
+#include "chat-policy-rule.h"
+#include <ndn.cxx/fields/signature-sha256-with-rsa.h>
 
 using namespace ndn;
 using namespace std;
 using namespace ndn::security;
 
 
-ChatPolicyRule::ChatPolicyRule(Ptr<Regex> dataRegex, const Name& dataRef,
-			       Ptr<Regex> signerRegex, const Name& signerRef,
-			       bool isPositive)
-  : PolicyRule(PolicyRule::IDENTITY_POLICY, isPositive)
+ChatPolicyRule::ChatPolicyRule(Ptr<Regex> dataRegex,
+			       Ptr<Regex> signerRegex)
+  : PolicyRule(PolicyRule::IDENTITY_POLICY, true)
   , m_dataRegex(dataRegex)
   , m_signerRegex(signerRegex)
-  , m_dataRef(dataRef)
-  , m_signerRef(signerRef)
+{}
+
+ChatPolicyRule::ChatPolicyRule(const ChatPolicyRule& rule)
+  : PolicyRule(PolicyRule::IDENTITY_POLICY, true)
+  , m_dataRegex(rule.m_dataRegex)
+  , m_signerRegex(rule.m_signerRegex)
 {}
 
 bool 
 ChatPolicyRule::matchDataName(const Data & data)
-{ return (m_dataRegex.match(data.getName()) && m_dataRegex.expand() == m_dataRef) ? true : false; }
+{ return m_dataRegex->match(data.getName()); }
 
 bool 
 ChatPolicyRule::matchSignerName(const Data & data)
 { 
   Ptr<const signature::Sha256WithRsa> sigPtr = DynamicCast<const signature::Sha256WithRsa> (data.getSignature());
   Name signerName = sigPtr->getKeyLocator ().getKeyName ();
-  return (m_signerRegex.match(signerName) && m_signerRegex.expand() == m_signerRef) ? true : false; 
+  return m_signerRegex->match(signerName); 
 }
 
 bool
@@ -43,20 +47,4 @@
 
 bool
 ChatPolicyRule::satisfy(const Name & dataName, const Name & signerName)
-{
-  if (m_dataRegex.match(data.getName()) 
-      && m_dataRegex.expand() == m_dataRef
-      && m_signerRegex.match(signerName) 
-      && m_signerRegex.expand() == m_signerRef)
-    return true;
-  else
-    return false;
-}
-  
-TiXmlElement *
-ChatPolicyRule::toXmlElement()
-{
-  //TODO:
-  return NULL;
-}
-};
+{ return (m_dataRegex->match(dataName) && m_signerRegex->match(signerName)); }
diff --git a/src/chat-policy-rule.h b/src/chat-policy-rule.h
index dfb434d..4534bab 100644
--- a/src/chat-policy-rule.h
+++ b/src/chat-policy-rule.h
@@ -12,36 +12,35 @@
 #define CHAT_POLICY_RULE_H
 
 #include <ndn.cxx/security/policy/policy-rule.h>
+#include <ndn.cxx/regex/regex.h>
 
 class ChatPolicyRule : public ndn::security::PolicyRule
 {
   
 public:
-  ChatPolicyRule();
+  ChatPolicyRule(ndn::Ptr<ndn::Regex> dataRegex,
+                 ndn::Ptr<ndn::Regex> signerRegex);
+
+  ChatPolicyRule(const ChatPolicyRule& rule);
 
   virtual
-  ~ChatPolicyRyle() {};
+  ~ChatPolicyRule() {};
 
   bool 
-  matchDataName(const Data & data);
+  matchDataName(const ndn::Data & data);
 
   bool 
-  matchSignerName(const Data & data);
+  matchSignerName(const ndn::Data & data);
 
   bool
-  satisfy(const Data & data);
+  satisfy(const ndn::Data & data);
 
   bool
-  satisfy(const Name & dataName, const Name & signerName);
+  satisfy(const ndn::Name & dataName, const ndn::Name & signerName);
   
-  TiXmlElement *
-  toXmlElement();
-
 private:
   ndn::Ptr<ndn::Regex> m_dataRegex;
   ndn::Ptr<ndn::Regex> m_signerRegex;
-  ndn::Name m_dataRef;
-  ndn::Name m_signerRef;
 };
 
 #endif //CHAT_POLICY_RULE_H
diff --git a/src/chatdialog.cpp b/src/chatdialog.cpp
index 5f1d3eb..fe7d7b5 100644
--- a/src/chatdialog.cpp
+++ b/src/chatdialog.cpp
@@ -5,67 +5,247 @@
  *
  * BSD license, See the LICENSE file for more information
  *
- * Author: Yingdi Yu <yingdi@cs.ucla.edu>
+ * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
+ *         Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ *         Yingdi Yu <yingdi@cs.ucla.edu>
  */
 
 #include "chatdialog.h"
 #include "ui_chatdialog.h"
 
+#include <QScrollBar>
+
 #ifndef Q_MOC_RUN
 #include <ndn.cxx/security/identity/identity-manager.h>
-#include <ndn.cxx/security/identity/basic-identity-storage.h>
-#include <ndn.cxx/security/identity/osx-privatekey-storage.h>
 #include <ndn.cxx/security/encryption/basic-encryption-manager.h>
+#include <sync-intro-certificate.h>
+#include <boost/random/random_device.hpp>
+#include <boost/random/uniform_int_distribution.hpp>
 #include "logging.h"
 #endif
 
 using namespace std;
-using namespace ndn;
 
 INIT_LOGGER("ChatDialog");
 
-ChatDialog::ChatDialog(const Name& chatroomPrefix,
-		       const Name& localPrefix,
-                       const Name& defaultIdentity,
+static const int HELLO_INTERVAL = FRESHNESS * 3 / 4;
+
+Q_DECLARE_METATYPE(std::vector<Sync::MissingDataInfo> )
+Q_DECLARE_METATYPE(size_t)
+
+ChatDialog::ChatDialog(ndn::Ptr<ContactManager> contactManager,
+                       const ndn::Name& chatroomPrefix,
+		       const ndn::Name& localPrefix,
+                       const ndn::Name& defaultIdentity,
 		       QWidget *parent) 
-    : QDialog(parent)
-    , m_chatroomPrefix(chatroomPrefix)
-    , m_localPrefix(localPrefix)
-    , m_defaultIdentity(defaultIdentity)
-    , m_policyManager(Ptr<ChatroomPolicyManager>(new ChatroomPolicyManager))
-    , ui(new Ui::ChatDialog)
+: QDialog(parent)
+  , ui(new Ui::ChatDialog)
+  , m_contactManager(contactManager)
+  , m_chatroomPrefix(chatroomPrefix)
+  , m_localPrefix(localPrefix)
+  , m_defaultIdentity(defaultIdentity)
+  , m_invitationPolicyManager(ndn::Ptr<InvitationPolicyManager>(new InvitationPolicyManager(m_chatroomPrefix.get(-1).toUri())))
+  , m_sock(NULL)
+  , m_lastMsgTime(0)
+  // , m_historyInitialized(false)
+  , m_joined(false)
+  , m_inviteListDialog(new InviteListDialog(m_contactManager))
 {
+  qRegisterMetaType<std::vector<Sync::MissingDataInfo> >("std::vector<Sync::MissingDataInfo>");
+  qRegisterMetaType<size_t>("size_t");
+
   ui->setupUi(this);
 
+  m_localChatPrefix = m_localPrefix;
+  m_localChatPrefix.append("FH").append(m_defaultIdentity);
+  m_localChatPrefix.append("chronos").append(m_chatroomPrefix.get(-1));
+
+  m_session = time(NULL);
+  m_scene = new DigestTreeScene(this);
+
+  initializeSetting();
+  updateLabels();
+
+  ui->treeViewer->setScene(m_scene);
+  ui->treeViewer->hide();
+  m_scene->plot("Empty");
+  QRectF rect = m_scene->itemsBoundingRect();
+  m_scene->setSceneRect(rect);
+
+  m_rosterModel = new QStringListModel(this);
+  ui->listView->setModel(m_rosterModel);
+
+  m_timer = new QTimer(this);
+
   setWrapper();
+
+  connect(ui->inviteButton, SIGNAL(clicked()),
+          this, SLOT(openInviteListDialog()));
+  connect(m_inviteListDialog, SIGNAL(invitionDetermined(QString, bool)),
+          this, SLOT(sendInvitationWrapper(QString, bool)));
+  connect(ui->lineEdit, SIGNAL(returnPressed()), 
+          this, SLOT(returnPressed()));
+  connect(ui->treeButton, SIGNAL(pressed()), 
+          this, SLOT(treeButtonPressed()));
+  connect(this, SIGNAL(dataReceived(QString, const char *, size_t, bool, bool)), 
+          this, SLOT(processData(QString, const char *, size_t, bool, bool)));
+  connect(this, SIGNAL(treeUpdated(const std::vector<Sync::MissingDataInfo>)), 
+          this, SLOT(processTreeUpdate(const std::vector<Sync::MissingDataInfo>)));
+  connect(m_timer, SIGNAL(timeout()), 
+          this, SLOT(replot()));
+  connect(m_scene, SIGNAL(replot()), 
+          this, SLOT(replot()));
+  // TODO: TrayIcon
+  // connect(trayIcon, SIGNAL(messageClicked()), 
+  //         this, SLOT(showNormal()));
+  // connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), 
+  //         this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
+  connect(m_scene, SIGNAL(rosterChanged(QStringList)), 
+          this, SLOT(updateRosterList(QStringList)));
+
+  // m_identityManager = ndn::Ptr<ndn::security::IdentityManager>::Create();
+  // // ndn::Ptr<ndn::security::EncryptionManager> encryptionManager = ndn::Ptr<ndn::security::EncryptionManager>(new ndn::security::BasicEncryptionManager(privateStorage, "/tmp/encryption.db"));
+
+  // ndn::Name certificateName = m_identityManager->getDefaultCertificateNameByIdentity(m_defaultIdentity);
+  // m_syncPolicyManager = ndn::Ptr<SyncPolicyManager>(new SyncPolicyManager(m_defaultIdentity, certificateName, m_chatroomPrefix));
+
+  initializeSync();
 }
 
+// ChatDialog::ChatDialog(const ndn::Name& chatroomPrefix,
+// 		       const ndn::Name& localPrefix,
+//                        const ndn::Name& defaultIdentity,
+//                        const ndn::security::IdentityCertificate& identityCertificate,
+// 		       QWidget *parent) 
+//     : QDialog(parent)
+//     , ui(new Ui::ChatDialog)
+//     , m_chatroomPrefix(chatroomPrefix)
+//     , m_localPrefix(localPrefix)
+//     , m_defaultIdentity(defaultIdentity)
+//     , m_invitationPolicyManager(ndn::Ptr<InvitationPolicyManager>(new InvitationPolicyManager(m_chatroomPrefix.get(-1).toUri())))
+
+//     , m_sock(NULL)
+//     , m_lastMsgTime(0)
+//     // , m_historyInitialized(false)
+//     , m_joined(false)
+// {
+//   qRegisterMetaType<std::vector<Sync::MissingDataInfo> >("std::vector<Sync::MissingDataInfo>");
+//   qRegisterMetaType<size_t>("size_t");
+
+//   ui->setupUi(this);
+
+//   m_localChatPrefix = m_localPrefix;
+//   m_localChatPrefix.append("FH").append(m_defaultIdentity);
+//   m_localChatPrefix.append("chronos").append(m_chatroomPrefix.get(-1));
+
+//   m_session = time(NULL);
+//   m_scene = new DigestTreeScene(this);
+
+//   initializeSetting();
+//   updateLabels();
+
+//   ui->treeViewer->setScene(m_scene);
+//   ui->treeViewer->hide();
+//   m_scene->plot("Empty");
+//   QRectF rect = m_scene->itemsBoundingRect();
+//   m_scene->setSceneRect(rect);
+
+//   m_rosterModel = new QStringListModel(this);
+//   ui->listView->setModel(m_rosterModel);
+
+//   m_timer = new QTimer(this);
+
+//   setWrapper();
+
+//   connect(ui->lineEdit, SIGNAL(returnPressed()), 
+//           this, SLOT(returnPressed()));
+//   connect(ui->treeButton, SIGNAL(pressed()), 
+//           this, SLOT(treeButtonPressed()));
+//   connect(this, SIGNAL(dataReceived(QString, const char *, size_t, bool, bool)), 
+//           this, SLOT(processData(QString, const char *, size_t, bool, bool)));
+//   connect(this, SIGNAL(treeUpdated(const std::vector<Sync::MissingDataInfo>)), 
+//           this, SLOT(processTreeUpdate(const std::vector<Sync::MissingDataInfo>)));
+//   connect(m_timer, SIGNAL(timeout()), 
+//           this, SLOT(replot()));
+//   connect(m_scene, SIGNAL(replot()), 
+//           this, SLOT(replot()));
+//   // TODO: TrayIcon
+//   // connect(trayIcon, SIGNAL(messageClicked()), 
+//   //         this, SLOT(showNormal()));
+//   // connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), 
+//   //         this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
+//   connect(m_scene, SIGNAL(rosterChanged(QStringList)), 
+//           this, SLOT(updateRosterList(QStringList)));
+
+//   // m_identityManager = ndn::Ptr<ndn::security::IdentityManager>::Create();
+//   // // ndn::Ptr<ndn::security::EncryptionManager> encryptionManager = ndn::Ptr<ndn::security::EncryptionManager>(new ndn::security::BasicEncryptionManager(privateStorage, "/tmp/encryption.db"));
+
+//   // ndn::Name certificateName = m_identityManager->getDefaultCertificateNameByIdentity(m_defaultIdentity);
+//   // m_syncPolicyManager = ndn::Ptr<SyncPolicyManager>(new SyncPolicyManager(m_defaultIdentity, certificateName, m_chatroomPrefix));
+
+//   initializeSync();
+// }
+
 ChatDialog::~ChatDialog()
 {
   delete ui;
+  sendLeave();
   m_handler->shutdown();
 }
 
 void
 ChatDialog::setWrapper()
 {
-  Ptr<security::OSXPrivatekeyStorage> privateStorage = Ptr<security::OSXPrivatekeyStorage>::Create();
-  m_identityManager = Ptr<security::IdentityManager>(new security::IdentityManager(Ptr<security::BasicIdentityStorage>::Create(), privateStorage));
-  Ptr<security::EncryptionManager> encryptionManager = Ptr<security::EncryptionManager>(new security::BasicEncryptionManager(privateStorage, "/tmp/encryption.db"));
+  m_identityManager = ndn::Ptr<ndn::security::IdentityManager>::Create();
+  // ndn::Ptr<ndn::security::EncryptionManager> encryptionManager = ndn::Ptr<ndn::security::EncryptionManager>(new ndn::security::BasicEncryptionManager(privateStorage, "/tmp/encryption.db"));
 
-  m_keychain = Ptr<security::Keychain>(new security::Keychain(m_identityManager, m_policyManager, encryptionManager));
+  ndn::Name certificateName = m_identityManager->getDefaultCertificateNameByIdentity(m_defaultIdentity);
+  m_syncPolicyManager = ndn::Ptr<SyncPolicyManager>(new SyncPolicyManager(m_defaultIdentity, certificateName, m_chatroomPrefix));
 
-  m_handler = Ptr<Wrapper>(new Wrapper(m_keychain));
+  m_keychain = ndn::Ptr<ndn::security::Keychain>(new ndn::security::Keychain(m_identityManager, m_invitationPolicyManager, NULL));
+
+  m_handler = ndn::Ptr<ndn::Wrapper>(new ndn::Wrapper(m_keychain));
 }
 
 void
-ChatDialog::sendInvitation(Ptr<ContactItem> contact)
+ChatDialog::initializeSetting()
 {
-  m_policyManager->addTrustAnchor(contact->getSelfEndorseCertificate());
+  // TODO: nick name may be changed.
+  m_user.setNick(QString::fromStdString(m_defaultIdentity.get(-1).toUri()));
+  m_user.setChatroom(QString::fromStdString(m_chatroomPrefix.get(-1).toUri()));
+  m_user.setOriginPrefix(QString::fromStdString(m_localPrefix.toUri()));
+  m_user.setPrefix(QString::fromStdString(m_localChatPrefix.toUri()));
+  m_scene->setCurrentPrefix(QString::fromStdString(m_localChatPrefix.toUri()));
+}
 
-  Name certificateName = m_identityManager->getDefaultCertificateNameByIdentity(m_defaultIdentity);
+void
+ChatDialog::updateLabels()
+{
+  QString settingDisp = QString("Chatroom: %1").arg(m_user.getChatroom());
+  ui->infoLabel->setStyleSheet("QLabel {color: #630; font-size: 16px; font: bold \"Verdana\";}");
+  ui->infoLabel->setText(settingDisp);
+  QString prefixDisp;
+  if (m_user.getPrefix().startsWith("/private/local"))
+  {
+    prefixDisp = QString("<Warning: no connection to hub or hub does not support prefix autoconfig.>\n <Prefix = %1>").arg(m_user.getPrefix());
+    ui->prefixLabel->setStyleSheet("QLabel {color: red; font-size: 12px; font: bold \"Verdana\";}");
+  }
+  else
+  {
+    prefixDisp = QString("<Prefix = %1>").arg(m_user.getPrefix());
+    ui->prefixLabel->setStyleSheet("QLabel {color: Green; font-size: 12px; font: bold \"Verdana\";}");
+  }
+  ui->prefixLabel->setText(prefixDisp);
+}
 
-  Name interestName("/ndn/broadcast/chronos/invitation");
+void
+ChatDialog::sendInvitation(ndn::Ptr<ContactItem> contact, bool isIntroducer)
+{
+  m_invitationPolicyManager->addTrustAnchor(contact->getSelfEndorseCertificate());
+
+  ndn::Name certificateName = m_identityManager->getDefaultCertificateNameByIdentity(m_defaultIdentity);
+
+  ndn::Name interestName("/ndn/broadcast/chronos/invitation");
   interestName.append(contact->getNameSpace());
   interestName.append("chatroom");
   interestName.append(m_chatroomPrefix.get(-1));
@@ -75,68 +255,100 @@
   interestName.append(certificateName);
 
   string signedUri = interestName.toUri();
-  Blob signedBlob(signedUri.c_str(), signedUri.size());
+  ndn::Blob signedBlob(signedUri.c_str(), signedUri.size());
 
-  Ptr<const signature::Sha256WithRsa> sha256sig = DynamicCast<const signature::Sha256WithRsa>(m_identityManager->signByCertificate(signedBlob, certificateName));
-  const Blob& sigBits = sha256sig->getSignatureBits();
+  ndn::Ptr<const ndn::signature::Sha256WithRsa> sha256sig = ndn::DynamicCast<const ndn::signature::Sha256WithRsa>(m_identityManager->signByCertificate(signedBlob, certificateName));
+  const ndn::Blob& sigBits = sha256sig->getSignatureBits();
 
   interestName.append(sigBits.buf(), sigBits.size());
 
-  Ptr<Interest> interest = Ptr<Interest>(new Interest(interestName));
-  Ptr<Closure> closure = Ptr<Closure>(new Closure(boost::bind(&ChatDialog::onInviteReplyVerified,
-                                                              this,
-                                                              _1,
-                                                              contact->getNameSpace()),
-                                                  boost::bind(&ChatDialog::onInviteTimeout,
-                                                              this,
-                                                              _1,
-                                                              _2,
-                                                              contact->getNameSpace(),
-                                                              7),
-                                                  boost::bind(&ChatDialog::onUnverified,
-                                                              this,
-                                                              _1)));
+  ndn::Ptr<ndn::Interest> interest = ndn::Ptr<ndn::Interest>(new ndn::Interest(interestName));
+  ndn::Ptr<ndn::Closure> closure = ndn::Ptr<ndn::Closure>(new ndn::Closure(boost::bind(&ChatDialog::onInviteReplyVerified,
+                                                                                       this,
+                                                                                       _1,
+                                                                                       contact->getNameSpace(),
+                                                                                       isIntroducer),
+                                                                           boost::bind(&ChatDialog::onInviteTimeout,
+                                                                                       this,
+                                                                                       _1,
+                                                                                       _2,
+                                                                                       contact->getNameSpace(),
+                                                                                       7),
+                                                                           boost::bind(&ChatDialog::onUnverified,
+                                                                                       this,
+                                                                                       _1)));
 
   m_handler->sendInterest(interest, closure);
 }
 
 void
-ChatDialog::invitationRejected(const Name& identity)
+ChatDialog::addTrustAnchor(const EndorseCertificate& selfEndorseCertificate)
+{ m_invitationPolicyManager->addTrustAnchor(selfEndorseCertificate); }
+
+void
+ChatDialog::addChatDataRule(const ndn::Name& prefix, 
+                            const ndn::security::IdentityCertificate& identityCertificate,
+                            bool isIntroducer)
+{ m_syncPolicyManager->addChatDataRule(prefix, identityCertificate, isIntroducer); }
+
+void
+ChatDialog::publishIntroCert(ndn::Ptr<ndn::security::IdentityCertificate> dskCertificate, bool isIntroducer)
 {
-  _LOG_DEBUG(" " << identity.toUri() << " rejected your invitation!");
+  SyncIntroCertificate syncIntroCertificate(m_chatroomPrefix,
+                                            dskCertificate->getPublicKeyName(),
+                                            m_identityManager->getDefaultKeyNameForIdentity(m_defaultIdentity),
+                                            dskCertificate->getNotBefore(),
+                                            dskCertificate->getNotAfter(),
+                                            dskCertificate->getPublicKeyInfo(),
+                                            (isIntroducer ? SyncIntroCertificate::INTRODUCER : SyncIntroCertificate::PRODUCER));
+  ndn::Name certName = m_identityManager->getDefaultCertificateNameByIdentity(m_defaultIdentity);
+  m_identityManager->signByCertificate(syncIntroCertificate, certName);
+  m_handler->putToNdnd(*syncIntroCertificate.encodeToWire());
 }
 
 void
-ChatDialog::invitationAccepted(const Name& identity)
+ChatDialog::invitationRejected(const ndn::Name& identity)
+{
+  _LOG_DEBUG(" " << identity.toUri() << " rejected your invitation!");
+  //TODO:
+}
+
+void
+ChatDialog::invitationAccepted(const ndn::Name& identity, ndn::Ptr<ndn::Data> data, const string& inviteePrefix, bool isIntroducer)
 {
   _LOG_DEBUG(" " << identity.toUri() << " accepted your invitation!");
+  ndn::Ptr<const ndn::signature::Sha256WithRsa> sha256sig = boost::dynamic_pointer_cast<const ndn::signature::Sha256WithRsa> (data->getSignature());
+  const ndn::Name & keyLocatorName = sha256sig->getKeyLocator().getKeyName();
+  ndn::Ptr<ndn::security::IdentityCertificate> dskCertificate = m_invitationPolicyManager->getValidatedDskCertificate(keyLocatorName);
+  m_syncPolicyManager->addChatDataRule(inviteePrefix, *dskCertificate, isIntroducer);
+  publishIntroCert(dskCertificate, isIntroducer);
 }
 
 void 
-ChatDialog::onInviteReplyVerified(Ptr<Data> data, const Name& identity)
+ChatDialog::onInviteReplyVerified(ndn::Ptr<ndn::Data> data, const ndn::Name& identity, bool isIntroducer)
 {
   string content(data->content().buf(), data->content().size());
   if(content.empty())
     invitationRejected(identity);
   else
-    invitationAccepted(identity);
+    invitationAccepted(identity, data, content, isIntroducer);
 }
 
 void 
-ChatDialog::onInviteTimeout(Ptr<Closure> closure, Ptr<Interest> interest, const Name& identity, int retry)
+ChatDialog::onInviteTimeout(ndn::Ptr<ndn::Closure> closure, ndn::Ptr<ndn::Interest> interest, const ndn::Name& identity, int retry)
 {
   if(retry > 0)
     {
-      Ptr<Closure> newClosure = Ptr<Closure>(new Closure(closure->m_dataCallback,
-                                                         boost::bind(&ChatDialog::onInviteTimeout, 
-                                                                     this, 
-                                                                     _1, 
-                                                                     _2, 
-                                                                     identity,
-                                                                     retry - 1),
-                                                         closure->m_unverifiedCallback,
-                                                         closure->m_stepCount)
-                                             );
+      ndn::Ptr<ndn::Closure> newClosure = ndn::Ptr<ndn::Closure>(new ndn::Closure(closure->m_dataCallback,
+                                                                                  boost::bind(&ChatDialog::onInviteTimeout, 
+                                                                                              this, 
+                                                                                              _1, 
+                                                                                              _2, 
+                                                                                              identity,
+                                                                                              retry - 1),
+                                                                                  closure->m_unverifiedCallback,
+                                                                                  closure->m_stepCount)
+                                                                 );
       m_handler->sendInterest(interest, newClosure);
     }
   else
@@ -144,9 +356,620 @@
 }
  
 void
-ChatDialog::onUnverified(Ptr<Data> data)
+ChatDialog::onUnverified(ndn::Ptr<ndn::Data> data)
 {}
 
+void
+ChatDialog::initializeSync()
+{
+  
+  m_sock = new Sync::SyncSocket(m_chatroomPrefix.toUri(),
+                                m_syncPolicyManager,
+                                bind(&ChatDialog::processTreeUpdateWrapper, this, _1, _2),
+                                bind(&ChatDialog::processRemoveWrapper, this, _1));
+  
+  usleep(100000);
+
+  QTimer::singleShot(600, this, SLOT(sendJoin()));
+  m_timer->start(FRESHNESS * 1000);
+  disableTreeDisplay();
+  QTimer::singleShot(2200, this, SLOT(enableTreeDisplay()));
+  // Sync::CcnxWrapperPtr handle = boost::make_shared<Sync::CcnxWrapper> ();
+  // handle->setInterestFilter(m_user.getPrefix().toStdString(), bind(&ChatDialog::respondHistoryRequest, this, _1));
+  // _LOG_DEBUG("initializeSync is done!");
+}
+
+void
+ChatDialog::returnPressed()
+{
+  QString text = ui->lineEdit->text();
+  if (text.isEmpty())
+    return;
+
+  ui->lineEdit->clear();
+
+  if (text.startsWith("boruoboluomi"))
+  {
+    summonReaper ();
+    // reapButton->show();
+    fitView();
+    return;
+  }
+
+  if (text.startsWith("minimanihong"))
+  {
+    // reapButton->hide();
+    fitView();
+    return;
+  }
+
+  SyncDemo::ChatMessage msg;
+  formChatMessage(text, msg);
+
+  appendMessage(msg);
+
+  sendMsg(msg);
+
+  fitView();
+}
+
+void 
+ChatDialog::treeButtonPressed()
+{
+  if (ui->treeViewer->isVisible())
+  {
+    ui->treeViewer->hide();
+    ui->treeButton->setText("Show ChronoSync Tree");
+  }
+  else
+  {
+    ui->treeViewer->show();
+    ui->treeButton->setText("Hide ChronoSync Tree");
+  }
+
+  fitView();
+}
+
+void ChatDialog::disableTreeDisplay()
+{
+  ui->treeButton->setEnabled(false);
+  ui->treeViewer->hide();
+  fitView();
+}
+
+void ChatDialog::enableTreeDisplay()
+{
+  ui->treeButton->setEnabled(true);
+  // treeViewer->show();
+  // fitView();
+}
+
+void
+ChatDialog::processTreeUpdateWrapper(const std::vector<Sync::MissingDataInfo> v, Sync::SyncSocket *sock)
+{
+  emit treeUpdated(v);
+  _LOG_DEBUG("<<< Tree update signal emitted");
+}
+
+void
+ChatDialog::processRemoveWrapper(std::string prefix)
+{
+  _LOG_DEBUG("Sync REMOVE signal received for prefix: " << prefix);
+}
+
+void
+ChatDialog::processTreeUpdate(const std::vector<Sync::MissingDataInfo> v)
+{
+  _LOG_DEBUG("<<< processing Tree Update");
+
+  if (v.empty())
+  {
+    return;
+  }
+
+  // reflect the changes on digest tree
+  {
+    boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
+    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;
+  }
+
+  for (int i = 0; i < n; i++)
+  {
+    if (totalMissingPackets < 4)
+    {
+      for (Sync::SeqNo seq = v[i].low; seq <= v[i].high; ++seq)
+      {
+        m_sock->fetchData(v[i].prefix, seq, bind(&ChatDialog::processDataWrapper, this, _1), 2);
+        _LOG_DEBUG("<<< Fetching " << v[i].prefix << "/" <<seq.getSession() <<"/" << seq.getSeq());
+      }
+    }
+    else
+    {
+        m_sock->fetchData(v[i].prefix, v[i].high, bind(&ChatDialog::processDataNoShowWrapper, this, _1), 2);
+    }
+  }
+
+  // adjust the view
+  fitView();
+
+}
+
+void
+ChatDialog::processDataWrapper(ndn::Ptr<ndn::Data> data)
+{
+  string name = data->getName().toUri();
+  const char* buf = data->content().buf();
+  size_t len = data->content().size();
+
+  char *tempBuf = new char[len];
+  memcpy(tempBuf, buf, len);
+  emit dataReceived(name.c_str(), tempBuf, len, true, false);
+  _LOG_DEBUG("<<< " << name << " fetched");
+}
+
+void
+ChatDialog::processDataNoShowWrapper(ndn::Ptr<ndn::Data> data)
+{
+  string name = data->getName().toUri();
+  const char* buf = data->content().buf();
+  size_t len = data->content().size();
+
+  char *tempBuf = new char[len];
+  memcpy(tempBuf, buf, len);
+  emit dataReceived(name.c_str(), tempBuf, len, false, false);
+
+  // if (!m_historyInitialized)
+  // {
+  //   fetchHistory(name);
+  //   m_historyInitialized = true;
+  // }
+}
+
+// void
+// ChatDialog::fetchHistory(std::string name)
+// {
+
+//   /****************************/
+//   /* TODO: fix following part */
+//   /****************************/
+//   string nameWithoutSeq = name.substr(0, name.find_last_of('/'));
+//   string prefix = nameWithoutSeq.substr(0, nameWithoutSeq.find_last_of('/'));
+//   prefix += "/history";
+//   // Ptr<Wrapper>CcnxWrapperPtr handle = boost::make_shared<Sync::CcnxWrapper> ();
+//   // QString randomString = getRandomString();
+//   // for (int i = 0; i < MAX_HISTORY_ENTRY; i++)
+//   // {
+//   //   QString interest = QString("%1/%2/%3").arg(prefix.c_str()).arg(randomString).arg(i);
+//   //   handle->sendInterest(interest.toStdString(), bind(&ChatDialog::processDataHistoryWrapper, this, _1, _2, _3));
+//   // }
+// }
+
+void
+ChatDialog::processData(QString name, const char *buf, size_t len, bool show, bool isHistory)
+{
+  SyncDemo::ChatMessage msg;
+  bool corrupted = false;
+  if (!msg.ParseFromArray(buf, len))
+  {
+    _LOG_DEBUG("Errrrr.. Can not parse msg with name: " << name.toStdString() << ". what is happening?");
+    // nasty stuff: as a remedy, we'll form some standard msg for inparsable msgs
+    msg.set_from("inconnu");
+    msg.set_type(SyncDemo::ChatMessage::OTHER);
+    corrupted = true;
+  }
+
+  delete [] buf;
+  buf = NULL;
+
+  // display msg received from network
+  // we have to do so; this function is called by ccnd thread
+  // so if we call appendMsg directly
+  // Qt crash as "QObject: Cannot create children for a parent that is in a different thread"
+  // the "cannonical" way to is use signal-slot
+  if (show && !corrupted)
+  {
+    appendMessage(msg, isHistory);
+  }
+
+  if (!isHistory)
+  {
+    // update the tree view
+    std::string stdStrName = name.toStdString();
+    std::string stdStrNameWithoutSeq = stdStrName.substr(0, stdStrName.find_last_of('/'));
+    std::string prefix = stdStrNameWithoutSeq.substr(0, stdStrNameWithoutSeq.find_last_of('/'));
+    _LOG_DEBUG("<<< updating scene for" << prefix << ": " << msg.from());
+    if (msg.type() == SyncDemo::ChatMessage::LEAVE)
+    {
+      processRemove(prefix.c_str());
+    }
+    else
+    {
+      boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
+      m_scene->msgReceived(prefix.c_str(), msg.from().c_str());
+    }
+  }
+  fitView();
+}
+
+void
+ChatDialog::processRemove(QString prefix)
+{
+  _LOG_DEBUG("<<< remove node for prefix" << prefix.toStdString());
+
+  bool removed = m_scene->removeNode(prefix);
+  if (removed)
+  {
+    boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
+    m_scene->plot(m_sock->getRootDigest().c_str());
+  }
+}
+
+void
+ChatDialog::sendJoin()
+{
+  m_joined = true;
+  SyncDemo::ChatMessage msg;
+  formControlMessage(msg, SyncDemo::ChatMessage::JOIN);
+  sendMsg(msg);
+  boost::random::random_device rng;
+  boost::random::uniform_int_distribution<> uniform(1, FRESHNESS / 5 * 1000);
+  m_randomizedInterval = HELLO_INTERVAL * 1000 + uniform(rng);
+  QTimer::singleShot(m_randomizedInterval, this, SLOT(sendHello()));
+}
+
+void
+ChatDialog::sendHello()
+{
+  time_t now = time(NULL);
+  int elapsed = now - m_lastMsgTime;
+  if (elapsed >= m_randomizedInterval / 1000)
+  {
+    SyncDemo::ChatMessage msg;
+    formControlMessage(msg, SyncDemo::ChatMessage::HELLO);
+    sendMsg(msg);
+    boost::random::random_device rng;
+    boost::random::uniform_int_distribution<> uniform(1, FRESHNESS / 5 * 1000);
+    m_randomizedInterval = HELLO_INTERVAL * 1000 + uniform(rng);
+    QTimer::singleShot(m_randomizedInterval, this, SLOT(sendHello()));
+  }
+  else
+  {
+    QTimer::singleShot((m_randomizedInterval - elapsed * 1000), this, SLOT(sendHello()));
+  }
+}
+
+void
+ChatDialog::sendLeave()
+{
+  SyncDemo::ChatMessage msg;
+  formControlMessage(msg, SyncDemo::ChatMessage::LEAVE);
+  sendMsg(msg);
+  usleep(500000);
+  m_sock->remove(m_user.getPrefix().toStdString());
+  usleep(5000);
+  m_joined = false;
+  _LOG_DEBUG("Sync REMOVE signal sent");
+}
+
+void
+ChatDialog::replot()
+{
+  boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
+  m_scene->plot(m_sock->getRootDigest().c_str());
+  fitView();
+}
+
+void
+ChatDialog::summonReaper()
+{
+  Sync::SyncLogic &logic = m_sock->getLogic ();
+  map<string, bool> branches = logic.getBranchPrefixes();
+  QMap<QString, DisplayUserPtr> roster = m_scene->getRosterFull();
+
+  m_zombieList.clear();
+
+  QMapIterator<QString, DisplayUserPtr> it(roster);
+  map<string, bool>::iterator mapIt;
+  while(it.hasNext())
+  {
+    it.next();
+    DisplayUserPtr p = it.value();
+    if (p != DisplayUserNullPtr)
+    {
+      mapIt = branches.find(p->getPrefix().toStdString());
+      if (mapIt != branches.end())
+      {
+        mapIt->second = true;
+      }
+    }
+  }
+
+  for (mapIt = branches.begin(); mapIt != branches.end(); ++mapIt)
+  {
+    // this is zombie. all active users should have been marked true
+    if (! mapIt->second)
+    {
+      m_zombieList.append(mapIt->first.c_str());
+    }
+  }
+
+  m_zombieIndex = 0;
+
+  // start reaping
+  reap();
+}
+
+void
+ChatDialog::reap()
+{
+  if (m_zombieIndex < m_zombieList.size())
+  {
+    string prefix = m_zombieList.at(m_zombieIndex).toStdString();
+    m_sock->remove(prefix);
+    _LOG_DEBUG("Reaped: prefix = " << prefix);
+    m_zombieIndex++;
+    // reap again in 10 seconds
+    QTimer::singleShot(10000, this, SLOT(reap()));
+  }
+}
+
+void
+ChatDialog::updateRosterList(QStringList staleUserList)
+{
+  boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
+  QStringList rosterList = m_scene->getRosterList();
+  m_rosterModel->setStringList(rosterList);
+  QString user;
+  QStringListIterator it(staleUserList);
+  while(it.hasNext())
+  {
+    std::string nick = it.next().toStdString();
+    if (nick.empty())
+      continue;
+
+    SyncDemo::ChatMessage msg;
+    formControlMessage(msg, SyncDemo::ChatMessage::LEAVE);
+    msg.set_from(nick);
+    appendMessage(msg);
+  }
+}
+
+void
+ChatDialog::resizeEvent(QResizeEvent *e)
+{
+  fitView();
+}
+
+void
+ChatDialog::showEvent(QShowEvent *e)
+{
+  fitView();
+}
+
+void
+ChatDialog::fitView()
+{
+  boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
+  QRectF rect = m_scene->itemsBoundingRect();
+  m_scene->setSceneRect(rect);
+  ui->treeViewer->fitInView(m_scene->itemsBoundingRect(), Qt::KeepAspectRatio);
+}
+
+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.toUtf8().constData());
+  time_t seconds = time(NULL);
+  msg.set_timestamp(seconds);
+  msg.set_type(SyncDemo::ChatMessage::CHAT);
+}
+
+void
+ChatDialog::formControlMessage(SyncDemo::ChatMessage &msg, SyncDemo::ChatMessage::ChatMessageType type)
+{
+  msg.set_from(m_user.getNick().toStdString());
+  msg.set_to(m_user.getChatroom().toStdString());
+  time_t seconds = time(NULL);
+  msg.set_timestamp(seconds);
+  msg.set_type(type);
+}
+
+void
+ChatDialog::appendMessage(const SyncDemo::ChatMessage msg, bool isHistory)
+{
+  boost::recursive_mutex::scoped_lock lock(m_msgMutex);
+
+  if (msg.type() == SyncDemo::ChatMessage::CHAT)
+  {
+
+    if (!msg.has_data())
+    {
+      return;
+    }
+
+    if (msg.from().empty() || msg.data().empty())
+    {
+      return;
+    }
+
+    if (!msg.has_timestamp())
+    {
+      return;
+    }
+
+    // if (m_history.size() == MAX_HISTORY_ENTRY)
+    // {
+    //   m_history.dequeue();
+    // }
+
+    // m_history.enqueue(msg);
+
+    QTextCharFormat nickFormat;
+    nickFormat.setForeground(Qt::darkGreen);
+    nickFormat.setFontWeight(QFont::Bold);
+    nickFormat.setFontUnderline(true);
+    nickFormat.setUnderlineColor(Qt::gray);
+
+    QTextCursor cursor(ui->textEdit->textCursor());
+    cursor.movePosition(QTextCursor::End);
+    QTextTableFormat tableFormat;
+    tableFormat.setBorder(0);
+    QTextTable *table = cursor.insertTable(1, 2, tableFormat);
+    QString from = QString("%1 ").arg(msg.from().c_str());
+    QTextTableCell fromCell = table->cellAt(0, 0);
+    fromCell.setFormat(nickFormat);
+    fromCell.firstCursorPosition().insertText(from);
+
+    time_t timestamp = msg.timestamp();
+    printTimeInCell(table, timestamp);
+
+    QTextCursor nextCursor(ui->textEdit->textCursor());
+    nextCursor.movePosition(QTextCursor::End);
+    table = nextCursor.insertTable(1, 1, tableFormat);
+    table->cellAt(0, 0).firstCursorPosition().insertText(QString::fromUtf8(msg.data().c_str()));
+    if (!isHistory)
+    {
+      showMessage(from, QString::fromUtf8(msg.data().c_str()));
+    }
+  }
+
+  if (msg.type() == SyncDemo::ChatMessage::JOIN || msg.type() == SyncDemo::ChatMessage::LEAVE)
+  {
+    QTextCharFormat nickFormat;
+    nickFormat.setForeground(Qt::gray);
+    nickFormat.setFontWeight(QFont::Bold);
+    nickFormat.setFontUnderline(true);
+    nickFormat.setUnderlineColor(Qt::gray);
+
+    QTextCursor cursor(ui->textEdit->textCursor());
+    cursor.movePosition(QTextCursor::End);
+    QTextTableFormat tableFormat;
+    tableFormat.setBorder(0);
+    QTextTable *table = cursor.insertTable(1, 2, tableFormat);
+    QString action;
+    if (msg.type() == SyncDemo::ChatMessage::JOIN)
+    {
+      action = "enters room";
+    }
+    else
+    {
+      action = "leaves room";
+    }
+
+    QString from = QString("%1 %2  ").arg(msg.from().c_str()).arg(action);
+    QTextTableCell fromCell = table->cellAt(0, 0);
+    fromCell.setFormat(nickFormat);
+    fromCell.firstCursorPosition().insertText(from);
+
+    time_t timestamp = msg.timestamp();
+    printTimeInCell(table, timestamp);
+  }
+
+  QScrollBar *bar = ui->textEdit->verticalScrollBar();
+  bar->setValue(bar->maximum());
+}
+
+QString
+ChatDialog::formatTime(time_t timestamp)
+{
+  struct tm *tm_time = localtime(&timestamp);
+  int hour = tm_time->tm_hour;
+  QString amOrPM;
+  if (hour > 12)
+  {
+    hour -= 12;
+    amOrPM = "PM";
+  }
+  else
+  {
+    amOrPM = "AM";
+    if (hour == 0)
+    {
+      hour = 12;
+    }
+  }
+
+  char textTime[12];
+  sprintf(textTime, "%d:%02d:%02d %s", hour, tm_time->tm_min, tm_time->tm_sec, amOrPM.toStdString().c_str());
+  return QString(textTime);
+}
+
+void
+ChatDialog::printTimeInCell(QTextTable *table, time_t timestamp)
+{
+  QTextCharFormat timeFormat;
+  timeFormat.setForeground(Qt::gray);
+  timeFormat.setFontUnderline(true);
+  timeFormat.setUnderlineColor(Qt::gray);
+  QTextTableCell timeCell = table->cellAt(0, 1);
+  timeCell.setFormat(timeFormat);
+  timeCell.firstCursorPosition().insertText(formatTime(timestamp));
+}
+
+void
+ChatDialog::showMessage(QString from, QString data)
+{
+  if (!isActiveWindow())
+  {
+    //TODO: Notification to be done
+    // trayIcon->showMessage(QString("Chatroom %1 has a new message").arg(m_user.getChatroom()), QString("<%1>: %2").arg(from).arg(data), QSystemTrayIcon::Information, 20000);
+    // trayIcon->setIcon(QIcon(":/images/note.png"));
+  }
+}
+
+void
+ChatDialog::sendMsg(SyncDemo::ChatMessage &msg)
+{
+  // send msg
+  size_t size = msg.ByteSize();
+  char *buf = new char[size];
+  msg.SerializeToArray(buf, size);
+  if (!msg.IsInitialized())
+  {
+    _LOG_DEBUG("Errrrr.. msg was not probally initialized "<<__FILE__ <<":"<<__LINE__<<". what is happening?");
+    abort();
+  }
+  m_sock->publishData(m_user.getPrefix().toStdString(), m_session, buf, size, FRESHNESS);
+
+  delete buf;
+
+  m_lastMsgTime = time(NULL);
+
+  int nextSequence = m_sock->getNextSeq(m_user.getPrefix().toStdString(), m_session);
+  Sync::MissingDataInfo mdi = {m_user.getPrefix().toStdString(), Sync::SeqNo(0), Sync::SeqNo(nextSequence - 1)};
+  std::vector<Sync::MissingDataInfo> v;
+  v.push_back(mdi);
+  {
+    boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
+    m_scene->processUpdate(v, m_sock->getRootDigest().c_str());
+    m_scene->msgReceived(m_user.getPrefix(), m_user.getNick());
+  }
+}
+
+void
+ChatDialog::openInviteListDialog()
+{
+  m_inviteListDialog->setInviteLabel(m_chatroomPrefix.toUri());
+  m_inviteListDialog->show();
+}
+
+void
+ChatDialog::sendInvitationWrapper(QString invitee, bool isIntroducer)
+{
+  ndn::Name inviteeNamespace(invitee.toUtf8().constData());
+  ndn::Ptr<ContactItem> inviteeItem = m_contactManager->getContact(inviteeNamespace);
+  sendInvitation(inviteeItem, isIntroducer);
+}
+
+
 #if WAF
 #include "chatdialog.moc"
 #include "chatdialog.cpp.moc"
diff --git a/src/chatdialog.h b/src/chatdialog.h
index e482169..a4792bc 100644
--- a/src/chatdialog.h
+++ b/src/chatdialog.h
@@ -5,22 +5,36 @@
  *
  * BSD license, See the LICENSE file for more information
  *
- * Author: Yingdi Yu <yingdi@cs.ucla.edu>
+ * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
+ *         Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ *         Yingdi Yu <yingdi@cs.ucla.edu>
  */
 
 #ifndef CHATDIALOG_H
 #define CHATDIALOG_H
 
 #include <QDialog>
+#include <QTextTable>
+#include <QStringListModel>
+#include <QTimer>
+
+#include "invitelistdialog.h"
 
 #ifndef Q_MOC_RUN
 #include <ndn.cxx/data.h>
 #include <ndn.cxx/security/keychain.h>
 #include <ndn.cxx/wrapper/wrapper.h>
-#include "chatroom-policy-manager.h"
+#include "invitation-policy-manager.h"
 #include "contact-item.h"
+
+#include <ccnx/sync-socket.h>
+#include <sync-seq-no.h>
+#include "chatbuf.pb.h"
+#include "digesttreescene.h"
 #endif
 
+#define MAX_HISTORY_ENTRY   20
+
 namespace Ui {
 class ChatDialog;
 }
@@ -30,10 +44,18 @@
   Q_OBJECT
 
 public:
-  explicit ChatDialog(const ndn::Name& chatroomPrefix,
+  explicit ChatDialog(ndn::Ptr<ContactManager> contactManager,
+                      const ndn::Name& chatroomPrefix,
                       const ndn::Name& localPrefix,
                       const ndn::Name& defaultIdentity,
                       QWidget *parent = 0);
+
+  // explicit ChatDialog(const ndn::Name& chatroomPrefix,
+  //                     const ndn::Name& localPrefix,
+  //                     const ndn::Name& defaultIdentity,
+  //                     const ndn::security::IdentityCertificate& identityCertificate,
+  //                     QWidget *parent = 0);
+
   ~ChatDialog();
 
   const ndn::Name&
@@ -45,14 +67,50 @@
   { return m_localPrefix; }
 
   void
-  sendInvitation(ndn::Ptr<ContactItem> contact);
+  sendInvitation(ndn::Ptr<ContactItem> contact, bool isIntroducer);
+
+  void
+  addTrustAnchor(const EndorseCertificate& selfEndorseCertificate);
+
+  void
+  addChatDataRule(const ndn::Name& prefix, 
+                  const ndn::security::IdentityCertificate& identityCertificate,
+                  bool isIntroducer);
+
+  void 
+  appendMessage(const SyncDemo::ChatMessage msg, bool isHistory = false);
+
+  void 
+  processTreeUpdateWrapper(const std::vector<Sync::MissingDataInfo>, Sync::SyncSocket *);
+
+  void 
+  processDataWrapper(ndn::Ptr<ndn::Data> data);
+
+  void 
+  processDataNoShowWrapper(ndn::Ptr<ndn::Data> data);
+
+  void 
+  processRemoveWrapper(std::string);
 
 private:
+
+  void
+  initializeSetting();
+
+  void 
+  updateLabels();
+
   void
   setWrapper();
+
+  void
+  initializeSync();
+
+  void
+  publishIntroCert(ndn::Ptr<ndn::security::IdentityCertificate> dskCertificate, bool isIntroducer);
   
   void 
-  onInviteReplyVerified(ndn::Ptr<ndn::Data> data, const ndn::Name& identity);
+  onInviteReplyVerified(ndn::Ptr<ndn::Data> data, const ndn::Name& identity, bool isIntroducer);
 
   void 
   onInviteTimeout(ndn::Ptr<ndn::Closure> closure, 
@@ -64,20 +122,139 @@
   invitationRejected(const ndn::Name& identity);
   
   void 
-  invitationAccepted(const ndn::Name& identity);
+  invitationAccepted(const ndn::Name& identity,
+                     ndn::Ptr<ndn::Data> data, 
+                     const std::string& inviteePrefix,
+                     bool isIntroducer);
 
   void
   onUnverified(ndn::Ptr<ndn::Data> data);
+
+
+
+  // void 
+  // fetchHistory(std::string name);
+
+  void 
+  formChatMessage(const QString &text, SyncDemo::ChatMessage &msg);
+
+  void 
+  formControlMessage(SyncDemo::ChatMessage &msg, SyncDemo::ChatMessage::ChatMessageType type);
+
+  void 
+  sendMsg(SyncDemo::ChatMessage &msg);
+
+  void 
+  resizeEvent(QResizeEvent *);
+  
+  void 
+  showEvent(QShowEvent *);
+
+  void 
+  fitView();
+
+  QString 
+  formatTime(time_t);
+
+  void 
+  printTimeInCell(QTextTable *, time_t);
+
+  void 
+  disableTreeDisplay();
+
+signals:
+  void 
+  dataReceived(QString name, const char *buf, size_t len, bool show, bool isHistory);
+  
+  void 
+  treeUpdated(const std::vector<Sync::MissingDataInfo>);
+  
+  void 
+  removeReceived(QString prefix);
+
+public slots:
+  void 
+  processTreeUpdate(const std::vector<Sync::MissingDataInfo>);
+
+  void 
+  processData(QString name, const char *buf, size_t len, bool show, bool isHistory);
+
+  void 
+  processRemove(QString prefix);
+
+private slots:
+  void
+  returnPressed();
+
+  void 
+  treeButtonPressed();
+
+  void 
+  sendJoin();
+
+  void
+  sendHello();
+
+  void
+  sendLeave();
+
+  void 
+  replot();
+
+  void 
+  updateRosterList(QStringList);
+
+  void 
+  enableTreeDisplay();
+
+  void 
+  summonReaper();
+
+  void
+  reap();
+
+  void 
+  showMessage(QString, QString);
+  
+  void
+  openInviteListDialog();
+  
+  void
+  sendInvitationWrapper(QString, bool);
     
 private:
   Ui::ChatDialog *ui;
+  ndn::Ptr<ContactManager> m_contactManager;
   ndn::Name m_chatroomPrefix;
   ndn::Name m_localPrefix;
+  ndn::Name m_localChatPrefix;
   ndn::Name m_defaultIdentity;
-  ndn::Ptr<ChatroomPolicyManager> m_policyManager;
+  ndn::Ptr<InvitationPolicyManager> m_invitationPolicyManager;
+  ndn::Ptr<SyncPolicyManager> m_syncPolicyManager; 
   ndn::Ptr<ndn::security::IdentityManager> m_identityManager;
   ndn::Ptr<ndn::security::Keychain> m_keychain;
   ndn::Ptr<ndn::Wrapper> m_handler;
+
+  User m_user; 
+  Sync::SyncSocket *m_sock;
+  uint32_t m_session;
+  DigestTreeScene *m_scene;
+  boost::recursive_mutex m_msgMutex;
+  boost::recursive_mutex m_sceneMutex;
+  time_t m_lastMsgTime;
+  int m_randomizedInterval;
+  QTimer *m_timer;
+  QStringListModel *m_rosterModel;
+  
+
+  // QQueue<SyncDemo::ChatMessage> m_history;
+  // bool m_historyInitialized;
+  bool m_joined;
+
+  QList<QString> m_zombieList;
+  int m_zombieIndex;
+
+  InviteListDialog* m_inviteListDialog;
 };
 
 #endif // ChatDIALOG_H
diff --git a/src/chatdialog.ui b/src/chatdialog.ui
index 2380f3d..256aadf 100644
--- a/src/chatdialog.ui
+++ b/src/chatdialog.ui
@@ -63,6 +63,16 @@
         <enum>QLayout::SetFixedSize</enum>
        </property>
        <item>
+        <widget class="QPushButton" name="inviteButton">
+         <property name="text">
+          <string>Invite</string>
+         </property>
+         <property name="autoDefault">
+          <bool>false</bool>
+         </property>
+        </widget>
+       </item>
+       <item>
         <widget class="QPushButton" name="treeButton">
          <property name="enabled">
           <bool>false</bool>
diff --git a/src/chatroom-policy-manager.cpp b/src/chatroom-policy-manager.cpp
deleted file mode 100644
index 394f8ee..0000000
--- a/src/chatroom-policy-manager.cpp
+++ /dev/null
@@ -1,174 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
-/*
- * Copyright (c) 2013, Regents of the University of California
- *                     Yingdi Yu
- *
- * BSD license, See the LICENSE file for more information
- *
- * Author: Yingdi Yu <yingdi@cs.ucla.edu>
- */
-
-#include "chatroom-policy-manager.h"
-
-#include <ndn.cxx/security/cache/ttl-certificate-cache.h>
-
-#include "logging.h"
-
-using namespace std;
-using namespace ndn;
-using namespace ndn::security;
-
-INIT_LOGGER("ChatroomPolicyManager");
-
-ChatroomPolicyManager::ChatroomPolicyManager(int stepLimit,
-					     Ptr<CertificateCache> certificateCache)
-  : m_stepLimit(stepLimit)
-  , m_certificateCache(certificateCache)
-{
-  if(m_certificateCache == NULL)
-    m_certificateCache = Ptr<TTLCertificateCache>(new TTLCertificateCache());
-
-  m_invitationPolicyRule = Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^<ndn><broadcast><chronos><invitation>([^<chatroom>]*)<chatroom>", 
-									  "^([^<KEY>]*)<KEY><DSK-.*><ID-CERT><>$", 
-									  "==", "\\1", "\\1", true));
-
-  m_dskRule = Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<KEY>]*)<KEY><DSK-.*><ID-CERT><>$", 
-							     "^([^<KEY>]*)<KEY>(<>*)<KSK-.*><ID-CERT><>$", 
-							     "==", "\\1", "\\1\\2", true));
-
-  m_keyNameRegex = Ptr<Regex>(new Regex("^([^<KEY>]*)<KEY>(<>*<KSK-.*>)<ID-CERT><>$", "\\1\\2"));
-} 
-
-ChatroomPolicyManager::~ChatroomPolicyManager()
-{}
-
-bool 
-ChatroomPolicyManager::skipVerifyAndTrust (const Data& data)
-{ return false; }
-
-bool
-ChatroomPolicyManager::requireVerify (const Data& data)
-{ return true; }
-
-Ptr<ValidationRequest>
-ChatroomPolicyManager::checkVerificationPolicy(Ptr<Data> data, 
-					       const int& stepCount, 
-					       const DataCallback& verifiedCallback,
-					       const UnverifiedCallback& unverifiedCallback)
-{
-  if(m_stepLimit == stepCount)
-    {
-      _LOG_DEBUG("reach the maximum steps of verification");
-      unverifiedCallback(data);
-      return NULL;
-    }
-
-  Ptr<const signature::Sha256WithRsa> sha256sig = boost::dynamic_pointer_cast<const signature::Sha256WithRsa> (data->getSignature());    
-
-  if(KeyLocator::KEYNAME != sha256sig->getKeyLocator().getType())
-    {
-      unverifiedCallback(data);
-      return NULL;
-    }
-
-  const Name & keyLocatorName = sha256sig->getKeyLocator().getKeyName();
-
-  if(m_invitationPolicyRule->satisfy(*data))
-    {
-      Ptr<const IdentityCertificate> trustedCert = m_certificateCache->getCertificate(keyLocatorName);
-      
-      if(NULL != trustedCert){
-	if(verifySignature(*data, trustedCert->getPublicKeyInfo()))
-	  verifiedCallback(data);
-	else
-	  unverifiedCallback(data);
-
-	return NULL;
-      }
-      else{
-	_LOG_DEBUG("KeyLocator has not been cached and validated!");
-
-	DataCallback recursiveVerifiedCallback = boost::bind(&ChatroomPolicyManager::onCertificateVerified, 
-							     this, 
-							     _1, 
-							     data, 
-							     verifiedCallback, 
-							     unverifiedCallback);
-
-	UnverifiedCallback recursiveUnverifiedCallback = boost::bind(&ChatroomPolicyManager::onCertificateUnverified, 
-								     this, 
-								     _1, 
-								     data, 
-								     unverifiedCallback);
-
-
-	Ptr<Interest> interest = Ptr<Interest>(new Interest(keyLocatorName));
-	
-	Ptr<ValidationRequest> nextStep = Ptr<ValidationRequest>(new ValidationRequest(interest, 
-										       recursiveVerifiedCallback,
-										       recursiveUnverifiedCallback,
-										       0,
-										       stepCount + 1)
-								 );
-	return nextStep;
-      }
-    }
-
-  if(m_dskRule->satisfy(*data))
-    {
-      m_keyNameRegex->match(keyLocatorName);
-      Name keyName = m_keyNameRegex->expand();
-
-      if(m_trustAnchors.end() != m_trustAnchors.find(keyName))
-        if(verifySignature(*data, m_trustAnchors[keyName]))
-          verifiedCallback(data);
-        else
-          unverifiedCallback(data);
-      else
-        unverifiedCallback(data);
-
-      return NULL;	
-    }
-
-  unverifiedCallback(data);
-  return NULL;
-}
-
-bool 
-ChatroomPolicyManager::checkSigningPolicy(const Name& dataName, 
-					  const Name& certificateName)
-{
-  //TODO:
-}
-    
-Name 
-ChatroomPolicyManager::inferSigningIdentity(const Name& dataName)
-{
-  //TODO:
-}
-
-void
-ChatroomPolicyManager::addTrustAnchor(const EndorseCertificate& selfEndorseCertificate)
-{ m_trustAnchors.insert(pair <Name, Publickey > (selfEndorseCertificate.getPublicKeyName(), selfEndorseCertificate.getPublicKeyInfo())); }
-
-void 
-ChatroomPolicyManager::onCertificateVerified(Ptr<Data> certData, 
-					     Ptr<Data> originalData,
-					     const DataCallback& verifiedCallback, 
-					     const UnverifiedCallback& unverifiedCallback)
-{
-  Ptr<IdentityCertificate> certificate = Ptr<IdentityCertificate>(new IdentityCertificate(*certData));
-  m_certificateCache->insertCertificate(certificate);
-
-  if(verifySignature(*originalData, certificate->getPublicKeyInfo()))
-    verifiedCallback(originalData);
-  else
-    unverifiedCallback(originalData);
-}
-
-void
-ChatroomPolicyManager::onCertificateUnverified(Ptr<Data> certData, 
-					       Ptr<Data> originalData,
-					       const UnverifiedCallback& unverifiedCallback)
-{ unverifiedCallback(originalData); }
-
diff --git a/src/chatroom-policy-manager.h b/src/chatroom-policy-manager.h
deleted file mode 100644
index 04227b8..0000000
--- a/src/chatroom-policy-manager.h
+++ /dev/null
@@ -1,78 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
-/*
- * Copyright (c) 2013, Regents of the University of California
- *                     Yingdi Yu
- *
- * BSD license, See the LICENSE file for more information
- *
- * Author: Yingdi Yu <yingdi@cs.ucla.edu>
- */
-
-#ifndef CHATROOM_POLICY_MANAGER_H
-#define CHATROOM_POLICY_MANAGER_H
-
-#include <ndn.cxx/security/policy/policy-manager.h>
-#include <ndn.cxx/security/policy/identity-policy-rule.h>
-#include <ndn.cxx/security/cache/certificate-cache.h>
-#include <ndn.cxx/regex/regex.h>
-#include <map>
-
-#include "endorse-certificate.h"
-
-class ChatroomPolicyManager : public ndn::security::PolicyManager
-{
-public:
-  ChatroomPolicyManager(int stepLimit = 10,
-                        ndn::Ptr<ndn::security::CertificateCache> certificateCache = NULL);
-  
-  virtual
-  ~ChatroomPolicyManager();
-
-  bool 
-  skipVerifyAndTrust (const ndn::Data& data);
-
-  bool
-  requireVerify (const ndn::Data& data);
-
-  ndn::Ptr<ndn::security::ValidationRequest>
-  checkVerificationPolicy(ndn::Ptr<ndn::Data> data, 
-                          const int& stepCount, 
-                          const ndn::DataCallback& verifiedCallback,
-                          const ndn::UnverifiedCallback& unverifiedCallback);
-
-  bool 
-  checkSigningPolicy(const ndn::Name& dataName, 
-                     const ndn::Name& certificateName);
-    
-  ndn::Name 
-  inferSigningIdentity(const ndn::Name& dataName);
-
-  void
-  addTrustAnchor(const EndorseCertificate& selfEndorseCertificate);
-
-private:
-  void 
-  onCertificateVerified(ndn::Ptr<ndn::Data> certData, 
-                        ndn::Ptr<ndn::Data> originalData,
-                        const ndn::DataCallback& verifiedCallback, 
-                        const ndn::UnverifiedCallback& unverifiedCallback);
-
-  void
-  onCertificateUnverified(ndn::Ptr<ndn::Data> certData, 
-                          ndn::Ptr<ndn::Data> originalData,
-                          const ndn::UnverifiedCallback& unverifiedCallback);
-
-private:
-  int m_stepLimit;
-  ndn::Ptr<ndn::security::CertificateCache> m_certificateCache;
-
-  ndn::Ptr<ndn::security::IdentityPolicyRule> m_invitationPolicyRule;
-  ndn::Ptr<ndn::security::IdentityPolicyRule> m_dskRule;
-
-  ndn::Ptr<ndn::Regex> m_keyNameRegex;
-
-  std::map<ndn::Name, ndn::security::Publickey> m_trustAnchors;
-
-};
-
-#endif //CHATROOM_POLICY_MANAGER_H
diff --git a/src/chronos-invitation.cpp b/src/chronos-invitation.cpp
new file mode 100644
index 0000000..7ec0243
--- /dev/null
+++ b/src/chronos-invitation.cpp
@@ -0,0 +1,98 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ *                     Yingdi Yu
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Yingdi Yu <yingdi@cs.ucla.edu>
+ */
+
+#include "chronos-invitation.h"
+#include "exception.h"
+#include "logging.h"
+
+using namespace std;
+using namespace ndn;
+
+INIT_LOGGER("ChronosInvitation");
+
+ChronosInvitation::ChronosInvitation(const ndn::Name& interestName)
+  : m_interestName(interestName)
+{
+  if(interestName.get(0).toUri() != string("ndn")
+     || interestName.get(1).toUri() != string("broadcast")
+     || interestName.get(2).toUri() != string("chronos")
+     || interestName.get(3).toUri() != string("invitation"))
+    throw LnException("Wrong ChronosInvitation Name");
+    
+  int i = 4;
+  int size = interestName.size();
+
+  string chatroomStr("chatroom");
+  int inviteeBegin = 4;
+  for(; i < size; i++)
+    if(interestName.get(i).toUri() == chatroomStr)
+      break;
+
+  if(i >= size)
+    throw LnException("Wrong ChronosInvitation Name, No chatroom tag");
+  m_inviteeNameSpace = interestName.getSubName(inviteeBegin, i - inviteeBegin);
+
+  _LOG_DEBUG(m_inviteeNameSpace.toUri());
+
+  string inviterPrefixStr("inviter-prefix");
+  int chatroomBegin = (++i);
+  for(; i < size;  i++)
+    if(interestName.get(i).toUri() == inviterPrefixStr)
+      break;
+
+  if(i > size)
+    throw LnException("Wrong ChronosInvitation Name, No inviter-prefix tag");
+  m_chatroom = interestName.getSubName(chatroomBegin, i - chatroomBegin);
+
+  _LOG_DEBUG(m_chatroom.toUri());
+
+  string inviterStr("inviter");
+  int inviterPrefixBegin = (++i);
+  for(; i < size; i++)
+    if(interestName.get(i).toUri() == inviterStr)
+      break;
+  
+  if(i > size)
+    throw LnException("Wrong ChronosInvitation Name, No inviter tag");
+  m_inviterPrefix = interestName.getSubName(inviterPrefixBegin, i - inviterPrefixBegin);
+
+  _LOG_DEBUG(m_inviterPrefix.toUri());
+
+  int inviterCertBegin = (++i);
+  m_inviterCertificateName = interestName.getSubName(inviterCertBegin, size - 1 - inviterCertBegin);
+
+  _LOG_DEBUG(m_inviterCertificateName.toUri());
+  
+  string signature = interestName.get(-1).toBlob();
+  m_signatureBits.insert(m_signatureBits.end(), signature.begin(), signature.end());
+
+  string keyStr("KEY");
+  int keyId = 0;
+  for(; keyId < m_inviterCertificateName.size(); keyId++)
+    if(m_inviterCertificateName.get(keyId).toUri() == keyStr)
+      break;
+  if(keyId >= m_inviterCertificateName.size())
+    throw LnException("Wrong ChronosInvitation Name, no KEY tag in inviter Certificate Name");
+  m_inviterNameSpace = m_inviterCertificateName.getSubName(0, keyId);
+
+  string signedName = interestName.getSubName(0, size - 1).toUri();
+  m_signedBlob.insert(m_signedBlob.end(), signedName.begin(), signedName.end());
+}
+
+ChronosInvitation::ChronosInvitation(const ChronosInvitation& invitation)
+  : m_interestName(invitation.m_interestName)
+  , m_inviteeNameSpace(invitation.m_inviteeNameSpace)
+  , m_chatroom(invitation.m_chatroom)
+  , m_inviterPrefix(invitation.m_inviterPrefix)
+  , m_inviterCertificateName(invitation.m_inviterCertificateName)
+  , m_signatureBits(invitation.m_signatureBits)
+  , m_inviterNameSpace(invitation.m_inviterNameSpace)
+  , m_signedBlob(invitation.m_signedBlob)
+{}
diff --git a/src/chronos-invitation.h b/src/chronos-invitation.h
new file mode 100644
index 0000000..7f660db
--- /dev/null
+++ b/src/chronos-invitation.h
@@ -0,0 +1,74 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ *                     Yingdi Yu
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Yingdi Yu <yingdi@cs.ucla.edu>
+ */
+
+#ifndef CHRONOS_INVITATION_H
+#define CHRONOS_INVITATION_H
+
+
+#include <ndn.cxx/fields/name.h>
+
+class ChronosInvitation
+{
+public:
+  ChronosInvitation() {}
+
+  ChronosInvitation(const ndn::Name& interestName);
+
+  ChronosInvitation(const ChronosInvitation& invitation);
+
+  virtual
+  ~ChronosInvitation() {};
+
+  inline const ndn::Name&
+  getInviteeNameSpace() const
+  { return m_inviteeNameSpace; }
+
+  inline const ndn::Name&
+  getChatroom() const
+  { return m_chatroom; }
+
+  inline const ndn::Name&
+  getInviterPrefix() const
+  { return m_inviterPrefix; }
+
+  inline const ndn::Name&
+  getInviterCertificateName() const
+  { return m_inviterCertificateName; }
+
+  inline const ndn::Blob&
+  getSignatureBits() const
+  { return m_signatureBits; }
+
+  inline const ndn::Name&
+  getInviterNameSpace() const
+  { return m_inviterNameSpace; }
+
+  inline const ndn::Blob&
+  getSignedBlob() const
+  { return m_signedBlob; }
+  
+  inline const ndn::Name&
+  getInterestName() const
+  { return m_interestName; }
+
+private:
+  ndn::Name m_interestName;
+
+  ndn::Name m_inviteeNameSpace;
+  ndn::Name m_chatroom;
+  ndn::Name m_inviterPrefix;
+  ndn::Name m_inviterCertificateName;
+  ndn::Blob m_signatureBits;
+  ndn::Name m_inviterNameSpace;
+
+  ndn::Blob m_signedBlob;
+};
+
+#endif
diff --git a/src/contact-manager.cpp b/src/contact-manager.cpp
index bc324b7..2f7536b 100644
--- a/src/contact-manager.cpp
+++ b/src/contact-manager.cpp
@@ -58,13 +58,13 @@
                                                                                           "^([^<KEY>]*)<KEY>(<>*)<><ID-CERT>",
                                                                                           "==", "\\1", "\\1\\2", true)));
   policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<PROFILE-CERT>]*)<PROFILE-CERT>",
-											  "^([^<KEY>]*)<KEY>(<>*<KSK-.*>)<ID-CERT>", 
+											  "^([^<KEY>]*)<KEY>(<>*<ksk-.*>)<ID-CERT>", 
 											  "==", "\\1", "\\1\\2", true)));
-  policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<KEY>]*)<KEY>(<>*)<KSK-.*><ID-CERT>",
-											  "^([^<KEY>]*)<KEY><DSK-.*><ID-CERT>",
+  policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>",
+											  "^([^<KEY>]*)<KEY><dsk-.*><ID-CERT>",
 											  ">", "\\1\\2", "\\1", true)));
-  policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<KEY>]*)<KEY><DSK-.*><ID-CERT>",
-											  "^([^<KEY>]*)<KEY>(<>*)<KSK-.*><ID-CERT>",
+  policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<KEY>]*)<KEY><dsk-.*><ID-CERT>",
+											  "^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>",
 											  "==", "\\1", "\\1\\2", true)));
 
   policyManager->addSigningPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<DNS>]*)<DNS><PROFILE>",
@@ -97,6 +97,7 @@
   interestName.append("DNS").append("PROFILE");
   
   Ptr<Interest> interestPtr = Ptr<Interest>(new Interest(interestName));
+  interestPtr->setChildSelector(Interest::CHILD_RIGHT);
   Ptr<Closure> closure = Ptr<Closure> (new Closure(boost::bind(&ContactManager::onDnsSelfEndorseCertificateVerified, 
                                                                this,
                                                                _1,
@@ -200,7 +201,12 @@
   Ptr<security::IdentityCertificate> dskCert = identityManager->getCertificate(certificateName);
   Ptr<const signature::Sha256WithRsa> dskCertSig = DynamicCast<const signature::Sha256WithRsa>(dskCert->getSignature());
   // HACK! KSK certificate should be retrieved from network.
-  Ptr<security::IdentityCertificate> kskCert = identityManager->getCertificate(dskCertSig->getKeyLocator().getKeyName());
+  _LOG_DEBUG("keyLocator: " << dskCertSig->getKeyLocator().getKeyName());
+  Name keyName = security::IdentityCertificate::certificateNameToPublicKeyName(dskCertSig->getKeyLocator().getKeyName());
+  _LOG_DEBUG("keyName: " << keyName.toUri());
+  Name kskCertName = identityManager->getPublicStorage()->getDefaultCertificateNameForKey(keyName);
+  _LOG_DEBUG("ksk cert name: " << kskCertName);
+  Ptr<security::IdentityCertificate> kskCert = identityManager->getCertificate(kskCertName);
 
   vector<string> endorseList;
   Profile::const_iterator it = profile.begin();
@@ -277,12 +283,9 @@
   Name keyName = selfEndorseCertificate->getPublicKeyName();
   Name identity = keyName.getSubName(0, keyName.size()-1);
 
-  TimeInterval ti = time::NowUnixTimestamp();
-  ostringstream oss;
-  oss << ti.total_seconds();
 
   Name dnsName = identity;
-  dnsName.append("DNS").append("PROFILE").append(oss.str());
+  dnsName.append("DNS").append("PROFILE").appendVersion();
   
   data->setName(dnsName);
   Ptr<Blob> blob = selfEndorseCertificate->encodeToWire();
diff --git a/src/contactpanel.cpp b/src/contactpanel.cpp
index 5c4cfc3..0ce0694 100644
--- a/src/contactpanel.cpp
+++ b/src/contactpanel.cpp
@@ -28,7 +28,7 @@
 #include <boost/filesystem.hpp>
 #include <boost/random/random_device.hpp>
 #include <boost/random/uniform_int_distribution.hpp>
-#include "invitation-policy-manager.h"
+#include "panel-policy-manager.h"
 #include "logging.h"
 #include "exception.h"
 #endif
@@ -41,15 +41,12 @@
 INIT_LOGGER("ContactPanel");
 
 Q_DECLARE_METATYPE(ndn::security::IdentityCertificate)
+Q_DECLARE_METATYPE(ChronosInvitation)
 
 ContactPanel::ContactPanel(Ptr<ContactManager> contactManager, QWidget *parent) 
     : QDialog(parent)
     , ui(new Ui::ContactPanel)
-    , m_contactManager(contactManager)
     , m_contactListModel(new QStringListModel)
-    , m_profileEditor(new ProfileEditor(m_contactManager))
-    , m_addContactPanel(new AddContactPanel(contactManager))
-    , m_setAliasDialog(new SetAliasDialog(contactManager))
     , m_startChatDialog(new StartChatDialog)
     , m_invitationDialog(new InvitationDialog)
     , m_settingDialog(new SettingDialog)
@@ -57,12 +54,18 @@
     , m_menuAlias(new QAction("&Set Alias", this))
 {
   qRegisterMetaType<ndn::security::IdentityCertificate>("IdentityCertificate");
+  qRegisterMetaType<ChronosInvitation>("ChronosInvitation");
   
+  openDB();    
+
+  m_contactManager = contactManager;
+  m_profileEditor = new ProfileEditor(m_contactManager);
+  m_addContactPanel = new AddContactPanel(contactManager);
+  m_setAliasDialog = new SetAliasDialog(contactManager);
+ 
   ui->setupUi(this);
   refreshContactList();
 
-  openDB();    
-
   setKeychain();
   m_handler = Ptr<Wrapper>(new Wrapper(m_keychain));
    
@@ -101,10 +104,10 @@
   connect(m_startChatDialog, SIGNAL(chatroomConfirmed(const QString&, const QString&, bool)),
           this, SLOT(startChatroom(const QString&, const QString&, bool)));
 
-  connect(m_invitationDialog, SIGNAL(invitationAccepted(const ndn::Name&, const ndn::security::IdentityCertificate&, QString, QString)),
-          this, SLOT(acceptInvitation(const ndn::Name&, const ndn::security::IdentityCertificate&, QString, QString)));
-  connect(m_invitationDialog, SIGNAL(invitationRejected(const ndn::Name&)),
-          this, SLOT(rejectInvitation(const ndn::Name&)));
+  connect(m_invitationDialog, SIGNAL(invitationAccepted(const ChronosInvitation&, const ndn::security::IdentityCertificate&)),
+          this, SLOT(acceptInvitation(const ChronosInvitation&, const ndn::security::IdentityCertificate&)));
+  connect(m_invitationDialog, SIGNAL(invitationRejected(const ChronosInvitation&)),
+          this, SLOT(rejectInvitation(const ChronosInvitation&)));
 
   connect(m_settingDialog, SIGNAL(identitySet(const QString&)),
           this, SLOT(updateDefaultIdentity(const QString&)));
@@ -138,22 +141,22 @@
   path.append(QDir::separator()).append(".chronos").append(QDir::separator()).append("chronos.db");
   db.setDatabaseName(path);
   bool ok = db.open();
+  _LOG_DEBUG("db opened: " << std::boolalpha << ok );
 }
 
 void 
 ContactPanel::setKeychain()
 {
-  Ptr<security::OSXPrivatekeyStorage> privateStorage = Ptr<security::OSXPrivatekeyStorage>::Create();
-  Ptr<security::IdentityManager> identityManager = Ptr<security::IdentityManager>(new security::IdentityManager(Ptr<security::BasicIdentityStorage>::Create(), privateStorage));
+  Ptr<security::IdentityManager> identityManager = Ptr<security::IdentityManager>::Create();
   Ptr<security::CertificateCache> certificateCache = Ptr<security::CertificateCache>(new security::TTLCertificateCache());
-  Ptr<InvitationPolicyManager> policyManager = Ptr<InvitationPolicyManager>(new InvitationPolicyManager(10, certificateCache));
-  Ptr<security::EncryptionManager> encryptionManager = Ptr<security::EncryptionManager>(new security::BasicEncryptionManager(privateStorage, "/tmp/encryption.db"));
+  Ptr<PanelPolicyManager> policyManager = Ptr<PanelPolicyManager>(new PanelPolicyManager(10, certificateCache));
+  // Ptr<security::EncryptionManager> encryptionManager = Ptr<security::EncryptionManager>(new security::BasicEncryptionManager(privateStorage, "/tmp/encryption.db"));
 
   vector<Ptr<ContactItem> >::const_iterator it = m_contactList.begin();
   for(; it != m_contactList.end(); it++)
       policyManager->addTrustAnchor((*it)->getSelfEndorseCertificate());
 
-  m_keychain = Ptr<security::Keychain>(new security::Keychain(identityManager, policyManager, encryptionManager));
+  m_keychain = Ptr<security::Keychain>(new security::Keychain(identityManager, policyManager, NULL));
 }
 
 void
@@ -188,7 +191,11 @@
 
 void
 ContactPanel::onLocalPrefixTimeout(Ptr<Closure> closure, Ptr<Interest> interest)
-{ throw LnException("No local prefix is found!"); }
+{ 
+  string randomSuffix = getRandomString();
+  m_localPrefix = Name("/private/local"); 
+  m_localPrefix.append(randomSuffix);
+}
 
 void
 ContactPanel::onUnverified(Ptr<Data> data)
@@ -204,41 +211,35 @@
                                        int inviterIndex)
 {
   Ptr<security::IdentityCertificate> certificate = Ptr<security::IdentityCertificate>(new security::IdentityCertificate(*data));
-
-  const int end = interestName.size();
-
-  string signature = interestName.get(end-1).toBlob();
-  Blob signatureBlob(signature.c_str(), signature.size());
-  string signedName = interestName.getSubName(0, end - 1).toUri();
-  Blob signedBlob(signedName.c_str(), signedName.size());
-
-  if(security::PolicyManager::verifySignature(signedBlob, signatureBlob, certificate->getPublicKeyInfo()))
+  Ptr<ChronosInvitation> invitation = Ptr<ChronosInvitation>(new ChronosInvitation(interestName));
+  
+  if(security::PolicyManager::verifySignature(invitation->getSignedBlob(), invitation->getSignatureBits(), certificate->getPublicKeyInfo()))
     {
       Name keyName = certificate->getPublicKeyName();
       Name inviterNameSpace = keyName.getSubName(0, keyName.size() - 1);
-      popChatInvitation(interestName, inviterIndex, inviterNameSpace, certificate);
+      popChatInvitation(invitation, inviterIndex, inviterNameSpace, certificate);
     }
 }
 
 void
-ContactPanel::popChatInvitation(const Name& interestName,
+ContactPanel::popChatInvitation(Ptr<ChronosInvitation> invitation,
                                 int inviterIndex,
                                 const Name& inviterNameSpace,
                                 Ptr<security::IdentityCertificate> certificate)
 {
-  string chatroomTag("chatroom");
-  int i = 0;
-  for(; i < inviterIndex; i++)
-    if(interestName.get(i).toUri() == chatroomTag)
-      break;
-  if(i+1 >= inviterIndex)
+  string chatroom = invitation->getChatroom().get(0).toUri();
+  string inviter = inviterNameSpace.toUri();
+
+  string alias;
+  vector<Ptr<ContactItem> >::iterator it = m_contactList.begin();
+  for(; it != m_contactList.end(); it++)
+    if((*it)->getNameSpace() == inviterNameSpace)
+      alias = (*it)->getAlias();
+
+  if(it != m_contactList.end())
     return;
 
-  string chatroom = interestName.get(i+1).toUri();
-  string inviter = inviterNameSpace.toUri();
-  m_invitationDialog->setMsg(inviter, chatroom);
-  m_invitationDialog->setIdentityCertificate(certificate);
-  m_invitationDialog->setInterestName(interestName);
+  m_invitationDialog->setInvitation(alias, invitation, certificate);
   emit newInvitationReady();
 }
 
@@ -427,17 +428,18 @@
 void
 ContactPanel::openStartChatDialog()
 {
-  TimeInterval ti = time::NowUnixTimestamp();
-  ostringstream oss;
-  oss << ti.total_seconds();
+  // TimeInterval ti = time::NowUnixTimestamp();
+  // ostringstream oss;
+  // oss << ti.total_seconds();
 
   Name chatroom("/ndn/broadcast/chronos");
-  chatroom.append(string("chatroom-") + oss.str());
+  chatroom.append(string("chatroom-") + getRandomString());
 
   m_startChatDialog->setInvitee(m_currentSelectedContactNamespace, chatroom.toUri());
   m_startChatDialog->show();
 }
 
+// For inviter
 void
 ContactPanel::startChatroom(const QString& chatroom, const QString& invitee, bool isIntroducer)
 {
@@ -445,48 +447,59 @@
   _LOG_DEBUG("invitee: " << invitee.toUtf8().constData());
   _LOG_DEBUG("introducer: " << std::boolalpha << isIntroducer);
 
-  Name chatroomName("/ndn/broadcast/chronos");
-  chatroomName.append(chatroom.toUtf8().constData());
+  Name chatroomName(chatroom.toUtf8().constData());
   
-  ChatDialog* chatDialog = new ChatDialog(chatroomName, m_localPrefix, m_defaultIdentity);
+  ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity);
   m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog));
   
   //TODO: send invitation
   Name inviteeNamespace(invitee.toUtf8().constData());
   Ptr<ContactItem> inviteeItem = m_contactManager->getContact(inviteeNamespace);
 
-  chatDialog->sendInvitation(inviteeItem); 
+  chatDialog->sendInvitation(inviteeItem, isIntroducer); 
   
   chatDialog->show();
 }
 
+// For Invitee
 void
-ContactPanel::startChatroom2(const QString& chatroom, const QString& inviter)
+ContactPanel::startChatroom2(const ChronosInvitation& invitation, 
+                             const security::IdentityCertificate& identityCertificate)
 {
-  _LOG_DEBUG("room: " << chatroom.toUtf8().constData());
-  _LOG_DEBUG("inviter: " << inviter.toUtf8().constData());
+  _LOG_DEBUG("room: " << invitation.getChatroom().toUri());
+  _LOG_DEBUG("inviter: " << invitation.getInviterNameSpace().toUri());
+
+  Name chatroomName("/ndn/broadcast/chronos");
+  chatroomName.append(invitation.getChatroom());
+
+  ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity);
+  chatDialog->addChatDataRule(invitation.getInviterPrefix(), identityCertificate, true);
+
+  Ptr<ContactItem> inviterItem = m_contactManager->getContact(invitation.getInviterNameSpace());
+  chatDialog->addTrustAnchor(inviterItem->getSelfEndorseCertificate());
+  
+  m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog));
+
+  chatDialog->show();
 }
 
 void
-ContactPanel::acceptInvitation(const Name& interestName, 
-                               const security::IdentityCertificate& identityCertificate, 
-                               QString inviter, 
-                               QString chatroom)
+ContactPanel::acceptInvitation(const ChronosInvitation& invitation, 
+                               const security::IdentityCertificate& identityCertificate)
 {
   string prefix = m_localPrefix.toUri();
-  _LOG_DEBUG("interestName " << interestName);
-  _LOG_DEBUG("prefix " << prefix);
-  m_handler->publishDataByIdentity (interestName, prefix);
+
+  m_handler->publishDataByIdentity (invitation.getInterestName(), prefix);
   //TODO:: open chat dialog
-  _LOG_DEBUG("ok");
-  startChatroom2(chatroom, inviter);
+  _LOG_DEBUG("TO open chat dialog");
+  startChatroom2(invitation, identityCertificate);
 }
 
 void
-ContactPanel::rejectInvitation(const ndn::Name& interestName)
+ContactPanel::rejectInvitation(const ChronosInvitation& invitation)
 {
   string empty;
-  m_handler->publishDataByIdentity (interestName, empty);
+  m_handler->publishDataByIdentity (invitation.getInterestName(), empty);
 }
 
 
diff --git a/src/contactpanel.h b/src/contactpanel.h
index 2f33b49..5f595b0 100644
--- a/src/contactpanel.h
+++ b/src/contactpanel.h
@@ -26,6 +26,7 @@
 
 #ifndef Q_MOC_RUN
 #include "contact-manager.h"
+#include "chronos-invitation.h"
 #endif
 
 
@@ -79,7 +80,7 @@
   getRandomString();
 
   void
-  popChatInvitation(const ndn::Name& interestName,
+  popChatInvitation(ndn::Ptr<ChronosInvitation> invitation,
                     int inviterIndex,
                     const ndn::Name& inviterNameSpace,
                     ndn::Ptr<ndn::security::IdentityCertificate> certificate);
@@ -124,16 +125,15 @@
   startChatroom(const QString& chatroom, const QString& invitee, bool isIntroducer);
 
   void 
-  startChatroom2(const QString& chatroom, const QString& inviter);
+  startChatroom2(const ChronosInvitation& invitation, 
+                 const ndn::security::IdentityCertificate& identityCertificate);
 
   void
-  acceptInvitation(const ndn::Name& interestName, 
-                   const ndn::security::IdentityCertificate& identityCertificate, 
-                   QString inviter, 
-                   QString chatroom);
+  acceptInvitation(const ChronosInvitation& invitation, 
+                   const ndn::security::IdentityCertificate& identityCertificate);
 
   void
-  rejectInvitation(const ndn::Name& interestName);
+  rejectInvitation(const ChronosInvitation& invitation);
 
 private:
   Ui::ContactPanel *ui;
diff --git a/src/digesttreescene.cpp b/src/digesttreescene.cpp
index ba45af8..5c785e6 100644
--- a/src/digesttreescene.cpp
+++ b/src/digesttreescene.cpp
@@ -1,3 +1,14 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ *                     Yingdi Yu
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
+ *         Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
 #include "digesttreescene.h"
 
 #include <QtGui>
diff --git a/src/digesttreescene.h b/src/digesttreescene.h
index fd04828..7266ecb 100644
--- a/src/digesttreescene.h
+++ b/src/digesttreescene.h
@@ -1,3 +1,14 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ *                     Yingdi Yu
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
+ *         Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
 #ifndef DIGESTTREESCENE_H
 #define DIGESTTREESCENE_H
 
diff --git a/src/exception.h b/src/exception.h
index fbd1812..5cce5aa 100644
--- a/src/exception.h
+++ b/src/exception.h
@@ -22,9 +22,10 @@
   ~LnException() throw()
   {}
   
-  inline const std::string& 
-  msg() const 
-  {return m_errMsg;}
+  const char* what() const throw()
+  {
+    return m_errMsg.c_str();
+  }
   
 private:
   const std::string m_errMsg;
diff --git a/src/invitation-policy-manager.cpp b/src/invitation-policy-manager.cpp
index a8b75fb..10929e3 100644
--- a/src/invitation-policy-manager.cpp
+++ b/src/invitation-policy-manager.cpp
@@ -10,8 +10,7 @@
 
 #include "invitation-policy-manager.h"
 
-#include <ndn.cxx/security/certificate/identity-certificate.h>
-#include <boost/bind.hpp>
+#include <ndn.cxx/security/cache/ttl-certificate-cache.h>
 
 #include "logging.h"
 
@@ -21,51 +20,43 @@
 
 INIT_LOGGER("InvitationPolicyManager");
 
-InvitationPolicyManager::InvitationPolicyManager(const int & stepLimit,
-						 Ptr<CertificateCache> certificateCache)
-  : m_stepLimit(stepLimit)
+InvitationPolicyManager::InvitationPolicyManager(const string& chatroomName,
+                                             int stepLimit,
+					     Ptr<CertificateCache> certificateCache)
+  : m_chatroomName(chatroomName)
+  , m_stepLimit(stepLimit)
   , m_certificateCache(certificateCache)
-  , m_localPrefixRegex(Ptr<Regex>(new Regex("^<local><ndn><prefix><><>$")))
 {
-  m_invitationDataRule = Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^<ndn><broadcast><chronos><invitation>([^<chatroom>]*)<chatroom>", 
-        								"^([^<KEY>]*)<KEY><DSK-.*><ID-CERT><>$", 
-        								"==", "\\1", "\\1", true));
-  
-  m_dskRule = Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<KEY>]*)<KEY><DSK-.*><ID-CERT><>$", 
-							     "^([^<KEY>]*)<KEY>(<>*)<KSK-.*><ID-CERT><>$", 
+  if(m_certificateCache == NULL)
+    m_certificateCache = Ptr<TTLCertificateCache>(new TTLCertificateCache());
+
+  m_invitationPolicyRule = Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^<ndn><broadcast><chronos><invitation>([^<chatroom>]*)<chatroom>", 
+									  "^([^<KEY>]*)<KEY><dsk-.*><ID-CERT>$", 
+									  "==", "\\1", "\\1", true));
+
+  m_dskRule = Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<KEY>]*)<KEY><dsk-.*><ID-CERT><>$", 
+							     "^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>$", 
 							     "==", "\\1", "\\1\\2", true));
 
-  m_keyNameRegex = Ptr<Regex>(new Regex("^([^<KEY>]*)<KEY>(<>*<KSK-.*>)<ID-CERT><>$", "\\1\\2"));
+  m_keyNameRegex = Ptr<Regex>(new Regex("^([^<KEY>]*)<KEY>(<>*<ksk-.*>)<ID-CERT>$", "\\1\\2"));
+} 
 
-  m_signingCertificateRegex = Ptr<Regex>(new Regex("^<ndn><broadcast><chronos><invitation>([^<chatroom>]*)<chatroom>", "\\1"));
-}
+InvitationPolicyManager::~InvitationPolicyManager()
+{}
 
 bool 
-InvitationPolicyManager::skipVerifyAndTrust (const Data & data)
-{
-  if(m_localPrefixRegex->match(data.getName()))
-    return true;
-  
-  return false;
-}
+InvitationPolicyManager::skipVerifyAndTrust (const Data& data)
+{ return false; }
 
 bool
-InvitationPolicyManager::requireVerify (const Data & data)
-{
-  // if(m_invitationDataRule->matchDataName(data))
-  //   return true;
-
-  if(m_dskRule->matchDataName(data))
-    return true;
-
-  return false;
-}
+InvitationPolicyManager::requireVerify (const Data& data)
+{ return true; }
 
 Ptr<ValidationRequest>
 InvitationPolicyManager::checkVerificationPolicy(Ptr<Data> data, 
-						 const int & stepCount, 
-						 const DataCallback& verifiedCallback,
-						 const UnverifiedCallback& unverifiedCallback)
+					       const int& stepCount, 
+					       const DataCallback& verifiedCallback,
+					       const UnverifiedCallback& unverifiedCallback)
 {
   if(m_stepLimit == stepCount)
     {
@@ -84,46 +75,46 @@
 
   const Name & keyLocatorName = sha256sig->getKeyLocator().getKeyName();
 
-  // if(m_invitationDataRule->satisfy(*data))
-  //   {
-  //     Ptr<const IdentityCertificate> trustedCert = m_certificateCache->getCertificate(keyLocatorName);
+  if(m_invitationPolicyRule->satisfy(*data))
+    {
+      Ptr<const IdentityCertificate> trustedCert = m_certificateCache->getCertificate(keyLocatorName);
       
-  //     if(NULL != trustedCert){
-  //       if(verifySignature(*data, trustedCert->getPublicKeyInfo()))
-  //         verifiedCallback(data);
-  //       else
-  //         unverifiedCallback(data);
+      if(NULL != trustedCert){
+	if(verifySignature(*data, trustedCert->getPublicKeyInfo()))
+	  verifiedCallback(data);
+	else
+	  unverifiedCallback(data);
 
-  //       return NULL;
-  //     }
-  //     else{
-  //       _LOG_DEBUG("KeyLocator has not been cached and validated!");
+	return NULL;
+      }
+      else{
+	_LOG_DEBUG("KeyLocator has not been cached and validated!");
 
-  //       DataCallback recursiveVerifiedCallback = boost::bind(&InvitationPolicyManager::onCertificateVerified, 
-  //       						     this, 
-  //       						     _1, 
-  //       						     data, 
-  //       						     verifiedCallback, 
-  //       						     unverifiedCallback);
+	DataCallback recursiveVerifiedCallback = boost::bind(&InvitationPolicyManager::onDskCertificateVerified, 
+							     this, 
+							     _1, 
+							     data, 
+							     verifiedCallback, 
+							     unverifiedCallback);
 
-  //       UnverifiedCallback recursiveUnverifiedCallback = boost::bind(&InvitationPolicyManager::onCertificateUnverified, 
-  //       							     this, 
-  //       							     _1, 
-  //       							     data, 
-  //       							     unverifiedCallback);
+	UnverifiedCallback recursiveUnverifiedCallback = boost::bind(&InvitationPolicyManager::onDskCertificateUnverified, 
+								     this, 
+								     _1, 
+								     data, 
+								     unverifiedCallback);
 
 
-  //       Ptr<Interest> interest = Ptr<Interest>(new Interest(sha256sig->getKeyLocator().getKeyName()));
+	Ptr<Interest> interest = Ptr<Interest>(new Interest(keyLocatorName));
 	
-  //       Ptr<ValidationRequest> nextStep = Ptr<ValidationRequest>(new ValidationRequest(interest, 
-  //       									       recursiveVerifiedCallback,
-  //       									       recursiveUnverifiedCallback,
-  //       									       0,
-  //       									       stepCount + 1)
-  //       							 );
-  //       return nextStep;
-  //     }
-  //   }
+	Ptr<ValidationRequest> nextStep = Ptr<ValidationRequest>(new ValidationRequest(interest, 
+										       recursiveVerifiedCallback,
+										       recursiveUnverifiedCallback,
+										       0,
+										       stepCount + 1)
+								 );
+	return nextStep;
+      }
+    }
 
   if(m_dskRule->satisfy(*data))
     {
@@ -145,41 +136,86 @@
   return NULL;
 }
 
-// void 
-// InvitationPolicyManager::onCertificateVerified(Ptr<Data> certData, 
-// 					       Ptr<Data> originalData,
-// 					       const DataCallback& verifiedCallback, 
-// 					       const UnverifiedCallback& unverifiedCallback)
-// {
-//   IdentityCertificate certificate(*certData);
-
-//   if(verifySignature(*originalData, certificate.getPublicKeyInfo()))
-//     verifiedCallback(originalData);
-//   else
-//     unverifiedCallback(originalData);
-// }
-
-// void
-// InvitationPolicyManager::onCertificateUnverified(Ptr<Data> certData, 
-// 						 Ptr<Data> originalData,
-// 						 const UnverifiedCallback& unverifiedCallback)
-// { unverifiedCallback(originalData); }
-
 bool 
-InvitationPolicyManager::checkSigningPolicy(const Name & dataName, const Name & certificateName)
+InvitationPolicyManager::checkSigningPolicy(const Name& dataName, 
+					  const Name& certificateName)
 {
-  return m_invitationDataRule->satisfy(dataName, certificateName);
+  //TODO:
+  return true;
 }
-
+    
 Name 
-InvitationPolicyManager::inferSigningIdentity(const Name & dataName)
+InvitationPolicyManager::inferSigningIdentity(const Name& dataName)
 {
-  if(m_signingCertificateRegex->match(dataName))
-    return m_signingCertificateRegex->expand();
-  else
-    return Name();
+  //TODO:
+  return Name();
 }
 
 void
 InvitationPolicyManager::addTrustAnchor(const EndorseCertificate& selfEndorseCertificate)
 { m_trustAnchors.insert(pair <Name, Publickey > (selfEndorseCertificate.getPublicKeyName(), selfEndorseCertificate.getPublicKeyInfo())); }
+
+
+// void
+// InvitationPolicyManager::addChatDataRule(const Name& prefix, 
+//                                        const IdentityCertificate identityCertificate)
+// {
+//   Name dataPrefix = prefix;
+//   dataPrefix.append("chronos").append(m_chatroomName);
+//   Ptr<Regex> dataRegex = Regex::fromName(prefix);
+//   Name certName = identityCertificate.getName();
+//   Name signerName = certName.getPrefix(certName.size()-1);
+//   Ptr<Regex> signerRegex = Regex::fromName(signerName, true);
+  
+//   ChatPolicyRule rule(dataRegex, signerRegex);
+//   map<Name, ChatPolicyRule>::iterator it = m_chatDataRules.find(dataPrefix);
+//   if(it != m_chatDataRules.end())
+//     it->second = rule;
+//   else
+//     m_chatDataRules.insert(pair <Name, ChatPolicyRule > (dataPrefix, rule));
+// }
+
+
+void 
+InvitationPolicyManager::onDskCertificateVerified(Ptr<Data> certData, 
+					     Ptr<Data> originalData,
+					     const DataCallback& verifiedCallback, 
+					     const UnverifiedCallback& unverifiedCallback)
+{
+  Ptr<IdentityCertificate> certificate = Ptr<IdentityCertificate>(new IdentityCertificate(*certData));
+
+  if(!certificate->isTooLate() && !certificate->isTooEarly())
+    {
+      Name certName = certificate->getName().getPrefix(certificate->getName().size()-1);
+      map<Name, Ptr<IdentityCertificate> >::iterator it = m_dskCertificates.find(certName);
+      if(it == m_dskCertificates.end())
+        m_dskCertificates.insert(pair <Name, Ptr<IdentityCertificate> > (certName, certificate));
+
+      if(verifySignature(*originalData, certificate->getPublicKeyInfo()))
+        {
+          verifiedCallback(originalData);
+          return;
+        }
+    }
+  else
+    {
+      unverifiedCallback(originalData);
+      return;
+    }
+}
+
+void
+InvitationPolicyManager::onDskCertificateUnverified(Ptr<Data> certData, 
+                                                  Ptr<Data> originalData,
+                                                  const UnverifiedCallback& unverifiedCallback)
+{ unverifiedCallback(originalData); }
+
+Ptr<IdentityCertificate> 
+InvitationPolicyManager::getValidatedDskCertificate(const ndn::Name& certName)
+{
+  map<Name, Ptr<IdentityCertificate> >::iterator it = m_dskCertificates.find(certName);
+  if(m_dskCertificates.end() != it)
+    return it->second;
+  else
+    return NULL;
+}
diff --git a/src/invitation-policy-manager.h b/src/invitation-policy-manager.h
index c8316cd..85afa53 100644
--- a/src/invitation-policy-manager.h
+++ b/src/invitation-policy-manager.h
@@ -14,93 +14,80 @@
 #include <ndn.cxx/security/policy/policy-manager.h>
 #include <ndn.cxx/security/policy/identity-policy-rule.h>
 #include <ndn.cxx/security/cache/certificate-cache.h>
+#include <ndn.cxx/regex/regex.h>
 #include <map>
 
 #include "endorse-certificate.h"
+#include "chat-policy-rule.h"
 
 class InvitationPolicyManager : public ndn::security::PolicyManager
 {
 public:
-  InvitationPolicyManager(const int & stepLimit = 10,                        
-                          ndn::Ptr<ndn::security::CertificateCache> certificateCache = NULL);
+  InvitationPolicyManager(const std::string& chatroomName,
+                        int stepLimit = 10,
+                        ndn::Ptr<ndn::security::CertificateCache> certificateCache = NULL);
+  
+  virtual
+  ~InvitationPolicyManager();
 
-  ~InvitationPolicyManager()
-  {}
-
-  /**
-   * @brief check if the received data packet can escape from verification
-   * @param data the received data packet
-   * @return true if the data does not need to be verified, otherwise false
-   */
   bool 
-  skipVerifyAndTrust (const ndn::Data & data);
+  skipVerifyAndTrust (const ndn::Data& data);
 
-  /**
-   * @brief check if PolicyManager has the verification rule for the received data
-   * @param data the received data packet
-   * @return true if the data must be verified, otherwise false
-   */
   bool
-  requireVerify (const ndn::Data & data);
+  requireVerify (const ndn::Data& data);
 
-  /**
-   * @brief check whether received data packet complies with the verification policy, and get the indication of next verification step
-   * @param data the received data packet
-   * @param stepCount the number of verification steps that have been done, used to track the verification progress
-   * @param verifiedCallback the callback function that will be called if the received data packet has been validated
-   * @param unverifiedCallback the callback function that will be called if the received data packet cannot be validated
-   * @return the indication of next verification step, NULL if there is no further step
-   */
   ndn::Ptr<ndn::security::ValidationRequest>
   checkVerificationPolicy(ndn::Ptr<ndn::Data> data, 
-                          const int & stepCount, 
+                          const int& stepCount, 
                           const ndn::DataCallback& verifiedCallback,
                           const ndn::UnverifiedCallback& unverifiedCallback);
 
-    
-  /**
-   * @brief check if the signing certificate name and data name satify the signing policy 
-   * @param dataName the name of data to be signed
-   * @param certificateName the name of signing certificate
-   * @return true if the signing certificate can be used to sign the data, otherwise false
-   */
   bool 
-  checkSigningPolicy(const ndn::Name & dataName, const ndn::Name & certificateName);
-  
-  /**
-   * @brief Infer signing identity name according to policy, if the signing identity cannot be inferred, it should return empty name
-   * @param dataName, the name of data to be signed
-   * @return the signing identity. 
-   */
+  checkSigningPolicy(const ndn::Name& dataName, 
+                     const ndn::Name& certificateName);
+    
   ndn::Name 
-  inferSigningIdentity(const ndn::Name & dataName);
+  inferSigningIdentity(const ndn::Name& dataName);
 
-  
   void
   addTrustAnchor(const EndorseCertificate& selfEndorseCertificate);
+  
+  // void 
+  // addChatDataRule(const ndn::Name& prefix, 
+  //                 const ndn::security::IdentityCertificate identityCertificate);
 
-// private:
-//   void 
-//   onCertificateVerified(ndn::Ptr<ndn::Data> certData, 
-//                         ndn::Ptr<ndn::Data> originalData,
-//                         const ndn::DataCallback& verifiedCallback, 
-//                         const ndn::UnverifiedCallback& unverifiedCallback);
-
-//   void
-//   onCertificateUnverified(ndn::Ptr<ndn::Data> certData, 
-//                           ndn::Ptr<ndn::Data> originalData,
-//                           const ndn::UnverifiedCallback& unverifiedCallback);
+  ndn::Ptr<ndn::security::IdentityCertificate> 
+  getValidatedDskCertificate(const ndn::Name& certName);
 
 private:
+  void 
+  onDskCertificateVerified(ndn::Ptr<ndn::Data> certData, 
+                        ndn::Ptr<ndn::Data> originalData,
+                        const ndn::DataCallback& verifiedCallback, 
+                        const ndn::UnverifiedCallback& unverifiedCallback);
+
+  void
+  onDskCertificateUnverified(ndn::Ptr<ndn::Data> certData, 
+                          ndn::Ptr<ndn::Data> originalData,
+                          const ndn::UnverifiedCallback& unverifiedCallback);
+
+private:
+  std::string m_chatroomName;
+
   int m_stepLimit;
+
   ndn::Ptr<ndn::security::CertificateCache> m_certificateCache;
-  ndn::Ptr<ndn::Regex> m_localPrefixRegex;
-  ndn::Ptr<ndn::security::IdentityPolicyRule> m_invitationDataRule;
+
+  ndn::Ptr<ndn::security::IdentityPolicyRule> m_invitationPolicyRule;
   ndn::Ptr<ndn::security::IdentityPolicyRule> m_dskRule;
+  std::map<ndn::Name, ChatPolicyRule> m_chatDataRules;
+
   ndn::Ptr<ndn::Regex> m_keyNameRegex;
-  ndn::Ptr<ndn::Regex> m_signingCertificateRegex;
+
   std::map<ndn::Name, ndn::security::Publickey> m_trustAnchors;
-  
+
+  std::map<ndn::Name, ndn::Ptr<ndn::security::IdentityCertificate> > m_dskCertificates;
+
 };
 
-#endif
+#endif //CHATROOM_POLICY_MANAGER_H
diff --git a/src/invitationdialog.cpp b/src/invitationdialog.cpp
index c00727d..9194bcc 100644
--- a/src/invitationdialog.cpp
+++ b/src/invitationdialog.cpp
@@ -33,23 +33,25 @@
 }
 
 void
-InvitationDialog::setMsg(const string& inviter, const string& chatroom)
+InvitationDialog::setInvitation(const string& alias,
+                                Ptr<ChronosInvitation> invitation, 
+                                Ptr<security::IdentityCertificate> identityCertificate)
 {
-  m_inviter = inviter;
-  m_chatroom = chatroom;
-  string msg = inviter;
+  m_inviterAlias = alias;
+  string msg = alias;
   msg.append(" invites you to join the chat room: ");
-  
   ui->msgLabel->setText(QString::fromUtf8(msg.c_str()));
-  ui->chatroomLine->setText(QString::fromUtf8(chatroom.c_str()));
+
+  m_invitation = invitation;
+  ui->chatroomLine->setText(QString::fromUtf8(invitation->getChatroom().get(0).toUri().c_str()));
+
+  m_identityCertificate = identityCertificate;
 }
 
 void
 InvitationDialog::onOkClicked()
 { 
-  QString inviter = QString::fromUtf8(m_inviter.c_str());
-  QString chatroom = QString::fromUtf8(m_chatroom.c_str());
-  emit invitationAccepted(m_interestName, *m_identityCertificate, inviter, chatroom); 
+  emit invitationAccepted(*m_invitation, *m_identityCertificate); 
   this->close();
 }
   
@@ -58,10 +60,12 @@
 { 
   ui->msgLabel->clear();
   ui->chatroomLine->clear();
-  m_interestName = Name();
-  m_inviter.clear();
-  m_chatroom.clear();
-  emit invitationRejected(m_interestName); 
+
+  m_invitation = NULL;
+  m_identityCertificate = NULL;
+  m_inviterAlias.clear();
+
+  emit invitationRejected(*m_invitation); 
   this->close();
 }
 
diff --git a/src/invitationdialog.h b/src/invitationdialog.h
index 3826ad4..d90b80e 100644
--- a/src/invitationdialog.h
+++ b/src/invitationdialog.h
@@ -16,6 +16,7 @@
 #ifndef Q_MOC_RUN
 #include <ndn.cxx/data.h>
 #include <ndn.cxx/security/certificate/identity-certificate.h>
+#include "chronos-invitation.h"
 #endif
 
 namespace Ui {
@@ -31,25 +32,17 @@
   ~InvitationDialog();
 
   void
-  setMsg(const std::string& inviter, const std::string& chatroom);
-
-  inline void
-  setInterestName(const ndn::Name& interestName)
-  { m_interestName = interestName; }
-
-  inline void
-  setIdentityCertificate(const ndn::Ptr<ndn::security::IdentityCertificate> identityCertificate)
-  { m_identityCertificate = identityCertificate; }
+  setInvitation(const std::string& alias,
+                ndn::Ptr<ChronosInvitation> invitation, 
+                ndn::Ptr<ndn::security::IdentityCertificate> identityCertificate);
 
 signals:
   void
-  invitationAccepted(const ndn::Name& interestName, 
-                     const ndn::security::IdentityCertificate& identityCertificate, 
-                     QString inviter, 
-                     QString chatroom);
+  invitationAccepted(const ChronosInvitation& invitation, 
+                     const ndn::security::IdentityCertificate& identityCertificate);
   
   void
-  invitationRejected(const ndn::Name& interestName);
+  invitationRejected(const ChronosInvitation& invitation);
 
 private slots:
   void
@@ -61,9 +54,8 @@
 
 private:
   Ui::InvitationDialog *ui;
-  std::string m_inviter;
-  std::string m_chatroom;
-  ndn::Name m_interestName;
+  std::string m_inviterAlias;
+  ndn::Ptr<ChronosInvitation> m_invitation;
   ndn::Ptr<ndn::security::IdentityCertificate> m_identityCertificate;
 };
 
diff --git a/src/invitelistdialog.cpp b/src/invitelistdialog.cpp
new file mode 100644
index 0000000..797a312
--- /dev/null
+++ b/src/invitelistdialog.cpp
@@ -0,0 +1,93 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ *                     Yingdi Yu
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Yingdi Yu <yingdi@cs.ucla.edu>
+ */
+
+#include "invitelistdialog.h"
+#include "ui_invitelistdialog.h"
+
+using namespace std;
+
+InviteListDialog::InviteListDialog(ndn::Ptr<ContactManager> contactManager,
+				   QWidget *parent) 
+  :QDialog(parent)
+  , ui(new Ui::InviteListDialog)
+  , m_contactManager(contactManager)
+  , m_contactListModel(new QStringListModel)
+{
+  ui->setupUi(this);
+
+  refreshContactList();
+
+  ui->contactListView->setModel(m_contactListModel);
+
+  connect(ui->inviteButton, SIGNAL(clicked()),
+	  this, SLOT(inviteWrapper()));
+  connect(ui->cancelButton, SIGNAL(clicked()),
+	  this, SLOT(onCancelClicked()));
+}
+
+InviteListDialog::~InviteListDialog()
+{
+  delete ui;
+  delete m_contactListModel;
+}
+
+void
+InviteListDialog::setInviteLabel(string label)
+{ 
+  string msg("invite to chatroom:\n");
+  msg += label;
+  ui->inviteLabel->setText(QString::fromUtf8(msg.c_str())); 
+  refreshContactList();
+}
+
+void
+InviteListDialog::refreshContactList()
+{
+  m_contactList = m_contactManager->getContactItemList();
+  QStringList contactNameList;
+  for(int i = 0; i < m_contactList.size(); i++)
+    {
+      contactNameList << QString::fromUtf8(m_contactList[i]->getAlias().c_str());
+    }
+
+  m_contactListModel->setStringList(contactNameList);
+}
+
+void
+InviteListDialog::inviteWrapper()
+{
+  QModelIndexList selected = ui->contactListView->selectionModel()->selectedIndexes();
+  QString text = m_contactListModel->data(selected.first(), Qt::DisplayRole).toString();
+  string alias = text.toUtf8().constData();
+
+  int i = 0;
+  for(; i < m_contactList.size(); i++)
+    {
+      if(alias == m_contactList[i]->getAlias())
+        break;
+    }
+
+  QString invitedContactNamespace = QString::fromUtf8(m_contactList[i]->getNameSpace().toUri().c_str());
+
+  bool isIntroducer = true;
+
+  emit invitionDetermined(invitedContactNamespace, isIntroducer);
+
+  this->close();
+}
+
+void
+InviteListDialog::onCancelClicked()
+{ this->close(); }
+
+#if WAF
+#include "invitelistdialog.moc"
+#include "invitelistdialog.cpp.moc"
+#endif
diff --git a/src/invitelistdialog.h b/src/invitelistdialog.h
new file mode 100644
index 0000000..cda26e5
--- /dev/null
+++ b/src/invitelistdialog.h
@@ -0,0 +1,59 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ *                     Yingdi Yu
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Yingdi Yu <yingdi@cs.ucla.edu>
+ */
+
+#ifndef INVITELISTDIALOG_H
+#define INVITELISTDIALOG_H
+
+#include <QDialog>
+#include <QStringListModel>
+
+#ifndef Q_MOC_RUN
+#include "contact-manager.h"
+#endif
+
+namespace Ui {
+class InviteListDialog;
+}
+
+class InviteListDialog : public QDialog
+{
+  Q_OBJECT
+
+public:
+  explicit InviteListDialog(ndn::Ptr<ContactManager> contactManager,
+                            QWidget *parent = 0);
+  ~InviteListDialog();
+
+  void
+  setInviteLabel(std::string label);
+  
+signals:
+  void
+  invitionDetermined(QString, bool);
+
+private slots:
+  void
+  refreshContactList();
+
+  void 
+  inviteWrapper();
+
+  void
+  onCancelClicked();
+
+private:
+  Ui::InviteListDialog *ui;
+  ndn::Ptr<ContactManager> m_contactManager;
+  QStringListModel* m_contactListModel;
+  std::vector<ndn::Ptr<ContactItem> > m_contactList;
+  std::vector<std::string> m_invitedContacts;
+};
+
+#endif // INVITELISTDIALOG_H
diff --git a/src/invitelistdialog.ui b/src/invitelistdialog.ui
new file mode 100644
index 0000000..0143de6
--- /dev/null
+++ b/src/invitelistdialog.ui
@@ -0,0 +1,93 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>InviteListDialog</class>
+ <widget class="QDialog" name="InviteListDialog">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>300</width>
+    <height>400</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>Dialog</string>
+  </property>
+  <widget class="QWidget" name="">
+   <property name="geometry">
+    <rect>
+     <x>10</x>
+     <y>10</y>
+     <width>281</width>
+     <height>381</height>
+    </rect>
+   </property>
+   <layout class="QVBoxLayout" name="verticalLayout" stretch="4,1,20,2">
+    <property name="spacing">
+     <number>10</number>
+    </property>
+    <item>
+     <widget class="QLabel" name="inviteLabel">
+      <property name="font">
+       <font>
+        <weight>75</weight>
+        <bold>true</bold>
+       </font>
+      </property>
+      <property name="text">
+       <string>TextLabel</string>
+      </property>
+      <property name="alignment">
+       <set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop</set>
+      </property>
+     </widget>
+    </item>
+    <item>
+     <widget class="QCheckBox" name="isIntroducer">
+      <property name="text">
+       <string>as introducer</string>
+      </property>
+      <property name="checkable">
+       <bool>false</bool>
+      </property>
+      <property name="checked">
+       <bool>false</bool>
+      </property>
+      <property name="tristate">
+       <bool>false</bool>
+      </property>
+     </widget>
+    </item>
+    <item>
+     <widget class="QListView" name="contactListView"/>
+    </item>
+    <item>
+     <layout class="QHBoxLayout" name="horizontalLayout">
+      <item>
+       <widget class="QPushButton" name="cancelButton">
+        <property name="text">
+         <string>Cancel</string>
+        </property>
+        <property name="autoDefault">
+         <bool>false</bool>
+        </property>
+       </widget>
+      </item>
+      <item>
+       <widget class="QPushButton" name="inviteButton">
+        <property name="text">
+         <string>Invite</string>
+        </property>
+        <property name="autoDefault">
+         <bool>false</bool>
+        </property>
+       </widget>
+      </item>
+     </layout>
+    </item>
+   </layout>
+  </widget>
+ </widget>
+ <resources/>
+ <connections/>
+</ui>
diff --git a/src/main.cpp b/src/main.cpp
index 9464195..0ae3da1 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -22,9 +22,31 @@
 
 using namespace ndn;
 
+class NewApp : public QApplication
+{
+public:
+  NewApp(int & argc, char ** argv)
+    : QApplication(argc, argv)
+  {}
+
+  bool notify(QObject * receiver, QEvent * event) 
+  {
+    try 
+      {
+        return QApplication::notify(receiver, event);
+      } 
+    catch(std::exception& e) 
+      {
+        std::cerr << "Exception thrown:" << e.what() << endl;
+        return false;
+      }
+    
+  }
+};
+
 int main(int argc, char *argv[])
 {
-  QApplication app(argc, argv);
+  NewApp app(argc, argv);
 
 
 //   app.setWindowIcon(QIcon("/Users/yuyingdi/Develop/QT/demo.icns"));
diff --git a/src/panel-policy-manager.cpp b/src/panel-policy-manager.cpp
new file mode 100644
index 0000000..d3c7260
--- /dev/null
+++ b/src/panel-policy-manager.cpp
@@ -0,0 +1,191 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ *                     Yingdi Yu
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Yingdi Yu <yingdi@cs.ucla.edu>
+ */
+
+#include "panel-policy-manager.h"
+
+#include <ndn.cxx/security/certificate/identity-certificate.h>
+#include <boost/bind.hpp>
+
+#include "logging.h"
+
+using namespace std;
+using namespace ndn;
+using namespace ndn::security;
+
+INIT_LOGGER("PanelPolicyManager");
+
+PanelPolicyManager::PanelPolicyManager(const int & stepLimit,
+						 Ptr<CertificateCache> certificateCache)
+  : m_stepLimit(stepLimit)
+  , m_certificateCache(certificateCache)
+  , m_localPrefixRegex(Ptr<Regex>(new Regex("^<local><ndn><prefix><><>$")))
+{
+  m_invitationDataSigningRule = Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^<ndn><broadcast><chronos><invitation>([^<chatroom>]*)<chatroom>", 
+        								"^([^<KEY>]*)<KEY><dsk-.*><ID-CERT><>$", 
+        								"==", "\\1", "\\1", true));
+  
+  m_dskRule = Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<KEY>]*)<KEY><dsk-.*><ID-CERT><>$", 
+							     "^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>$", 
+							     "==", "\\1", "\\1\\2", true));
+
+  m_keyNameRegex = Ptr<Regex>(new Regex("^([^<KEY>]*)<KEY>(<>*<ksk-.*>)<ID-CERT>$", "\\1\\2"));
+
+  m_signingCertificateRegex = Ptr<Regex>(new Regex("^<ndn><broadcast><chronos><invitation>([^<chatroom>]*)<chatroom>", "\\1"));
+}
+
+bool 
+PanelPolicyManager::skipVerifyAndTrust (const Data & data)
+{
+  if(m_localPrefixRegex->match(data.getName()))
+    return true;
+  
+  return false;
+}
+
+bool
+PanelPolicyManager::requireVerify (const Data & data)
+{
+  // if(m_invitationDataRule->matchDataName(data))
+  //   return true;
+
+  if(m_dskRule->matchDataName(data))
+    return true;
+
+  return false;
+}
+
+Ptr<ValidationRequest>
+PanelPolicyManager::checkVerificationPolicy(Ptr<Data> data, 
+						 const int & stepCount, 
+						 const DataCallback& verifiedCallback,
+						 const UnverifiedCallback& unverifiedCallback)
+{
+  _LOG_DEBUG("checkVerificationPolicy");
+  if(m_stepLimit == stepCount)
+    {
+      _LOG_DEBUG("reach the maximum steps of verification");
+      unverifiedCallback(data);
+      return NULL;
+    }
+
+  Ptr<const signature::Sha256WithRsa> sha256sig = boost::dynamic_pointer_cast<const signature::Sha256WithRsa> (data->getSignature());    
+
+  if(KeyLocator::KEYNAME != sha256sig->getKeyLocator().getType())
+    {
+      unverifiedCallback(data);
+      return NULL;
+    }
+
+  const Name & keyLocatorName = sha256sig->getKeyLocator().getKeyName();
+
+  // if(m_invitationDataRule->satisfy(*data))
+  //   {
+  //     Ptr<const IdentityCertificate> trustedCert = m_certificateCache->getCertificate(keyLocatorName);
+      
+  //     if(NULL != trustedCert){
+  //       if(verifySignature(*data, trustedCert->getPublicKeyInfo()))
+  //         verifiedCallback(data);
+  //       else
+  //         unverifiedCallback(data);
+
+  //       return NULL;
+  //     }
+  //     else{
+  //       _LOG_DEBUG("KeyLocator has not been cached and validated!");
+
+  //       DataCallback recursiveVerifiedCallback = boost::bind(&PanelPolicyManager::onCertificateVerified, 
+  //       						     this, 
+  //       						     _1, 
+  //       						     data, 
+  //       						     verifiedCallback, 
+  //       						     unverifiedCallback);
+
+  //       UnverifiedCallback recursiveUnverifiedCallback = boost::bind(&PanelPolicyManager::onCertificateUnverified, 
+  //       							     this, 
+  //       							     _1, 
+  //       							     data, 
+  //       							     unverifiedCallback);
+
+
+  //       Ptr<Interest> interest = Ptr<Interest>(new Interest(sha256sig->getKeyLocator().getKeyName()));
+	
+  //       Ptr<ValidationRequest> nextStep = Ptr<ValidationRequest>(new ValidationRequest(interest, 
+  //       									       recursiveVerifiedCallback,
+  //       									       recursiveUnverifiedCallback,
+  //       									       0,
+  //       									       stepCount + 1)
+  //       							 );
+  //       return nextStep;
+  //     }
+  //   }
+
+  if(m_dskRule->satisfy(*data))
+    {
+      m_keyNameRegex->match(keyLocatorName);
+      Name keyName = m_keyNameRegex->expand();
+      _LOG_DEBUG(keyName.toUri());
+
+      if(m_trustAnchors.end() != m_trustAnchors.find(keyName))
+        if(verifySignature(*data, m_trustAnchors[keyName]))
+          verifiedCallback(data);
+        else
+          unverifiedCallback(data);
+      else
+        unverifiedCallback(data);
+
+      return NULL;	
+    }
+  _LOG_DEBUG("Unverified!");
+
+  unverifiedCallback(data);
+  return NULL;
+}
+
+// void 
+// PanelPolicyManager::onCertificateVerified(Ptr<Data> certData, 
+// 					       Ptr<Data> originalData,
+// 					       const DataCallback& verifiedCallback, 
+// 					       const UnverifiedCallback& unverifiedCallback)
+// {
+//   IdentityCertificate certificate(*certData);
+
+//   if(verifySignature(*originalData, certificate.getPublicKeyInfo()))
+//     verifiedCallback(originalData);
+//   else
+//     unverifiedCallback(originalData);
+// }
+
+// void
+// PanelPolicyManager::onCertificateUnverified(Ptr<Data> certData, 
+// 						 Ptr<Data> originalData,
+// 						 const UnverifiedCallback& unverifiedCallback)
+// { unverifiedCallback(originalData); }
+
+bool 
+PanelPolicyManager::checkSigningPolicy(const Name & dataName, const Name & certificateName)
+{
+  return m_invitationDataSigningRule->satisfy(dataName, certificateName);
+}
+
+Name 
+PanelPolicyManager::inferSigningIdentity(const Name & dataName)
+{
+  if(m_signingCertificateRegex->match(dataName))
+    return m_signingCertificateRegex->expand();
+  else
+    return Name();
+}
+
+void
+PanelPolicyManager::addTrustAnchor(const EndorseCertificate& selfEndorseCertificate)
+{ 
+  _LOG_DEBUG(selfEndorseCertificate.getPublicKeyName().toUri());
+  m_trustAnchors.insert(pair <Name, Publickey > (selfEndorseCertificate.getPublicKeyName(), selfEndorseCertificate.getPublicKeyInfo())); 
+}
diff --git a/src/panel-policy-manager.h b/src/panel-policy-manager.h
new file mode 100644
index 0000000..85fd2d9
--- /dev/null
+++ b/src/panel-policy-manager.h
@@ -0,0 +1,106 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ *                     Yingdi Yu
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Yingdi Yu <yingdi@cs.ucla.edu>
+ */
+
+#ifndef PANEL_POLICY_MANAGER_H
+#define PANEL_POLICY_MANAGER_H
+
+#include <ndn.cxx/security/policy/policy-manager.h>
+#include <ndn.cxx/security/policy/identity-policy-rule.h>
+#include <ndn.cxx/security/cache/certificate-cache.h>
+#include <map>
+
+#include "endorse-certificate.h"
+
+class PanelPolicyManager : public ndn::security::PolicyManager
+{
+public:
+  PanelPolicyManager(const int & stepLimit = 10,                        
+                          ndn::Ptr<ndn::security::CertificateCache> certificateCache = NULL);
+
+  ~PanelPolicyManager()
+  {}
+
+  /**
+   * @brief check if the received data packet can escape from verification
+   * @param data the received data packet
+   * @return true if the data does not need to be verified, otherwise false
+   */
+  bool 
+  skipVerifyAndTrust (const ndn::Data & data);
+
+  /**
+   * @brief check if PolicyManager has the verification rule for the received data
+   * @param data the received data packet
+   * @return true if the data must be verified, otherwise false
+   */
+  bool
+  requireVerify (const ndn::Data & data);
+
+  /**
+   * @brief check whether received data packet complies with the verification policy, and get the indication of next verification step
+   * @param data the received data packet
+   * @param stepCount the number of verification steps that have been done, used to track the verification progress
+   * @param verifiedCallback the callback function that will be called if the received data packet has been validated
+   * @param unverifiedCallback the callback function that will be called if the received data packet cannot be validated
+   * @return the indication of next verification step, NULL if there is no further step
+   */
+  ndn::Ptr<ndn::security::ValidationRequest>
+  checkVerificationPolicy(ndn::Ptr<ndn::Data> data, 
+                          const int & stepCount, 
+                          const ndn::DataCallback& verifiedCallback,
+                          const ndn::UnverifiedCallback& unverifiedCallback);
+
+    
+  /**
+   * @brief check if the signing certificate name and data name satify the signing policy 
+   * @param dataName the name of data to be signed
+   * @param certificateName the name of signing certificate
+   * @return true if the signing certificate can be used to sign the data, otherwise false
+   */
+  bool 
+  checkSigningPolicy(const ndn::Name & dataName, const ndn::Name & certificateName);
+  
+  /**
+   * @brief Infer signing identity name according to policy, if the signing identity cannot be inferred, it should return empty name
+   * @param dataName, the name of data to be signed
+   * @return the signing identity. 
+   */
+  ndn::Name 
+  inferSigningIdentity(const ndn::Name & dataName);
+
+  
+  void
+  addTrustAnchor(const EndorseCertificate& selfEndorseCertificate);
+
+// private:
+//   void 
+//   onCertificateVerified(ndn::Ptr<ndn::Data> certData, 
+//                         ndn::Ptr<ndn::Data> originalData,
+//                         const ndn::DataCallback& verifiedCallback, 
+//                         const ndn::UnverifiedCallback& unverifiedCallback);
+
+//   void
+//   onCertificateUnverified(ndn::Ptr<ndn::Data> certData, 
+//                           ndn::Ptr<ndn::Data> originalData,
+//                           const ndn::UnverifiedCallback& unverifiedCallback);
+
+private:
+  int m_stepLimit;
+  ndn::Ptr<ndn::security::CertificateCache> m_certificateCache;
+  ndn::Ptr<ndn::Regex> m_localPrefixRegex;
+  ndn::Ptr<ndn::security::IdentityPolicyRule> m_invitationDataSigningRule;
+  ndn::Ptr<ndn::security::IdentityPolicyRule> m_dskRule;
+  ndn::Ptr<ndn::Regex> m_keyNameRegex;
+  ndn::Ptr<ndn::Regex> m_signingCertificateRegex;
+  std::map<ndn::Name, ndn::security::Publickey> m_trustAnchors;
+  
+};
+
+#endif
diff --git a/src/profile-data.cpp b/src/profile-data.cpp
index f057a6d..9a8087b 100644
--- a/src/profile-data.cpp
+++ b/src/profile-data.cpp
@@ -26,11 +26,8 @@
   , m_profile(profile)
 {
   Name dataName = identity;
-  TimeInterval ti = time::NowUnixTimestamp();
-  ostringstream oss;
-  oss << ti.total_seconds();
 
-  dataName.append("PROFILE").append(oss.str());
+  dataName.append("PROFILE").appendVersion();
   setName(dataName);
   Ptr<Blob> profileBlob = profile.toDerBlob();
   setContent(Content(profileBlob->buf(), profileBlob->size()));
diff --git a/src/profileeditor.cpp b/src/profileeditor.cpp
index dd037ca..db00a6f 100644
--- a/src/profileeditor.cpp
+++ b/src/profileeditor.cpp
@@ -94,6 +94,7 @@
   m_currentIdentity = Name(inputIdentity.toUtf8().constData());
   string filter("profile_identity = '");
   filter.append(m_currentIdentity.toUri()).append("'");
+  _LOG_DEBUG("filter: " << filter);
 
   m_tableModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
   m_tableModel->setTable("SelfProfile");
@@ -103,6 +104,8 @@
   m_tableModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Type"));
   m_tableModel->setHeaderData(2, Qt::Horizontal, QObject::tr("Value"));
 
+  _LOG_DEBUG("row count: " << m_tableModel->rowCount());
+
   ui->profileTable->setModel(m_tableModel);
   ui->profileTable->setColumnHidden(0, true);
   ui->profileTable->show();
diff --git a/src/startchatdialog.cpp b/src/startchatdialog.cpp
index 561096a..02d88f0 100644
--- a/src/startchatdialog.cpp
+++ b/src/startchatdialog.cpp
@@ -41,7 +41,8 @@
 {
   QString chatroom = ui->chatroomInput->text();
   QString invitee = QString::fromUtf8(m_invitee.c_str());
-  bool isIntroducer = ui->introCheckBox->isChecked();
+  // bool isIntroducer = ui->introCheckBox->isChecked();
+  bool isIntroducer = true;
   emit chatroomConfirmed(chatroom, invitee, isIntroducer);
   this->close();
 }
diff --git a/src/startchatdialog.ui b/src/startchatdialog.ui
index bfe6217..9451b51 100644
--- a/src/startchatdialog.ui
+++ b/src/startchatdialog.ui
@@ -80,6 +80,9 @@
    <property name="text">
     <string>Set invitee as introducer</string>
    </property>
+   <property name="checkable">
+    <bool>false</bool>
+   </property>
   </widget>
  </widget>
  <resources/>
diff --git a/src/treelayout.cpp b/src/treelayout.cpp
index 0a40484..c1fecf3 100644
--- a/src/treelayout.cpp
+++ b/src/treelayout.cpp
@@ -1,3 +1,14 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ *                     Yingdi Yu
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
+ *         Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
 #include "treelayout.h"
 
 void
diff --git a/src/treelayout.h b/src/treelayout.h
index 498210c..4942f62 100644
--- a/src/treelayout.h
+++ b/src/treelayout.h
@@ -1,3 +1,14 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ *                     Yingdi Yu
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
+ *         Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
 #ifndef TREELAYOUT_H
 #define TREELAYOUT_H
 #include <vector>
diff --git a/src/trusted-contact.cpp b/src/trusted-contact.cpp
index b3271d2..7db842a 100644
--- a/src/trusted-contact.cpp
+++ b/src/trusted-contact.cpp
@@ -9,7 +9,9 @@
  */
 
 #include "trusted-contact.h"
-#include <tinyxml.h>
+#include <boost/tokenizer.hpp>
+using boost::tokenizer;
+using boost::escaped_list_separator;
 
 using namespace std;
 using namespace ndn;
@@ -19,14 +21,15 @@
 			       const string& alias)
   : ContactItem(selfEndorseCertificate, alias)
 {
-  TiXmlDocument xmlDoc;
-  xmlDoc.Parse(trustScope.c_str());
-  
-  TiXmlNode * it = xmlDoc.FirstChild();    
-  while(it != NULL)
+  tokenizer<escaped_list_separator<char> > trustScopeItems(trustScope, escaped_list_separator<char> ("\\", " \t", "'\""));
+
+  tokenizer<escaped_list_separator<char> >::iterator it = trustScopeItems.begin();
+
+  while (it != trustScopeItems.end())
     {
-      m_trustScope.push_back(Regex::fromXmlElement(dynamic_cast<TiXmlElement *>(it)));
-      it = it->NextSibling();
+      m_trustScope.push_back(Regex::fromName(Name(*it)));
+      m_trustScopeName.push_back(Name(*it));
+      it++;
     }
 }
 
@@ -45,12 +48,12 @@
 TrustedContact::getTrustScopeBlob() const
 {
   ostringstream oss;
-  TiXmlDocument * xmlDoc = new TiXmlDocument();
 
-  vector<Ptr<Regex> >::const_iterator it = m_trustScope.begin();
-  for(; it != m_trustScope.end(); it++)
-      xmlDoc->LinkEndChild((*it)->toXmlElement());
+  vector<Name>::const_iterator it = m_trustScopeName.begin();
+  if(it != m_trustScopeName.end())
+    oss << it->toUri();
+  for(; it != m_trustScopeName.end(); it++)
+    oss << " " << it->toUri();
 
-  oss << *xmlDoc;
   return Ptr<Blob>(new Blob(oss.str().c_str(), oss.str().size()));
 }
diff --git a/src/trusted-contact.h b/src/trusted-contact.h
index 9f9121c..bd2e84a 100644
--- a/src/trusted-contact.h
+++ b/src/trusted-contact.h
@@ -35,6 +35,7 @@
 
 private:
   std::vector<ndn::Ptr<ndn::Regex> > m_trustScope;
+  std::vector<ndn::Name> m_trustScopeName;
 };
 
 #endif