separate controller backend frontend

Change-Id: Ic4ba9c8154a600c978e75ea82ff36569270dd656
diff --git a/src/controller.cpp b/src/controller.cpp
index b52c188..12e31b8 100644
--- a/src/controller.cpp
+++ b/src/controller.cpp
@@ -19,14 +19,13 @@
 #include <boost/filesystem.hpp>
 #include <boost/lexical_cast.hpp>
 #include <ndn-cxx/util/random.hpp>
-#include "invitation.hpp"
 #include "cryptopp.hpp"
 #include "config.pb.h"
 #include "endorse-info.pb.h"
 #include "logging.h"
 #endif
 
-// INIT_LOGGER("chronos.Controller");
+INIT_LOGGER("chronos.Controller");
 
 Q_DECLARE_METATYPE(ndn::Name)
 Q_DECLARE_METATYPE(ndn::IdentityCertificate)
@@ -34,28 +33,16 @@
 Q_DECLARE_METATYPE(ndn::Interest)
 Q_DECLARE_METATYPE(size_t)
 Q_DECLARE_METATYPE(chronos::ChatroomInfo)
+Q_DECLARE_METATYPE(chronos::Invitation)
 
 namespace chronos {
 
 using std::string;
 
-using ndn::Face;
-using ndn::IdentityCertificate;
-using ndn::OnInterestValidated;
-using ndn::OnInterestValidationFailed;
-
-static const uint8_t ROUTING_PREFIX_SEPARATOR[2] = {0xF0, 0x2E};
-
 // constructor & destructor
-Controller::Controller(shared_ptr<Face> face,
-                       QWidget* parent)
+Controller::Controller(QWidget* parent)
   : QDialog(parent)
-  , m_face(face)
   , m_localPrefixDetected(false)
-  , m_invitationListenerId(0)
-  , m_contactManager(m_face)
-  , m_discoveryLogic(m_face,
-                     bind(&Controller::updateDiscoveryList, this, _1, _2))
   , m_settingDialog(new SettingDialog)
   , m_startChatDialog(new StartChatDialog)
   , m_profileEditor(new ProfileEditor)
@@ -63,7 +50,6 @@
   , m_contactPanel(new ContactPanel)
   , m_browseContactDialog(new BrowseContactDialog)
   , m_addContactPanel(new AddContactPanel)
-  , m_chatroomDiscoveryDialog(new ChatroomDiscoveryDialog)
 {
   qRegisterMetaType<ndn::Name>("ndn.Name");
   qRegisterMetaType<ndn::IdentityCertificate>("ndn.IdentityCertificate");
@@ -71,23 +57,15 @@
   qRegisterMetaType<ndn::Interest>("ndn.Interest");
   qRegisterMetaType<size_t>("size_t");
   qRegisterMetaType<chronos::ChatroomInfo>("chronos.Chatroom");
-
-  connect(this, SIGNAL(localPrefixUpdated(const QString&)),
-          this, SLOT(onLocalPrefixUpdated(const QString&)));
-  connect(this, SIGNAL(invitationInterest(const ndn::Name&, const ndn::Interest&, size_t)),
-          this, SLOT(onInvitationInterest(const ndn::Name&, const ndn::Interest&, size_t)));
+  qRegisterMetaType<chronos::Invitation>("chronos.Invitation");
 
   // Connection to ContactManager
-  connect(this, SIGNAL(identityUpdated(const QString&)),
-          &m_contactManager, SLOT(onIdentityUpdated(const QString&)));
-  connect(&m_contactManager, SIGNAL(warning(const QString&)),
+  connect(m_backend.getContactManager(), SIGNAL(warning(const QString&)),
           this, SLOT(onWarning(const QString&)));
   connect(this, SIGNAL(refreshBrowseContact()),
-          &m_contactManager, SLOT(onRefreshBrowseContact()));
-  connect(&m_contactManager, SIGNAL(contactInfoFetchFailed(const QString&)),
+          m_backend.getContactManager(), SLOT(onRefreshBrowseContact()));
+  connect(m_backend.getContactManager(), SIGNAL(contactInfoFetchFailed(const QString&)),
           this, SLOT(onWarning(const QString&)));
-  connect(&m_contactManager, SIGNAL(contactIdListReady(const QStringList&)),
-          this, SLOT(onContactIdListReady(const QStringList&)));
 
   // Connection to SettingDialog
   connect(this, SIGNAL(identityUpdated(const QString&)),
@@ -96,8 +74,6 @@
           this, SLOT(onIdentityUpdated(const QString&)));
   connect(m_settingDialog, SIGNAL(nickUpdated(const QString&)),
           this, SLOT(onNickUpdated(const QString&)));
-  connect(this, SIGNAL(localPrefixUpdated(const QString&)),
-          m_settingDialog, SLOT(onLocalPrefixUpdated(const QString&)));
   connect(m_settingDialog, SIGNAL(prefixUpdated(const QString&)),
           this, SLOT(onLocalPrefixConfigured(const QString&)));
 
@@ -107,7 +83,7 @@
   connect(this, SIGNAL(identityUpdated(const QString&)),
           m_profileEditor, SLOT(onIdentityUpdated(const QString&)));
   connect(m_profileEditor, SIGNAL(updateProfile()),
-          &m_contactManager, SLOT(onUpdateProfile()));
+          m_backend.getContactManager(), SLOT(onUpdateProfile()));
 
   // Connection to StartChatDialog
   connect(m_startChatDialog, SIGNAL(startChatroom(const QString&, bool)),
@@ -115,72 +91,95 @@
 
   // Connection to InvitationDialog
   connect(m_invitationDialog, SIGNAL(invitationResponded(const ndn::Name&, bool)),
-          this, SLOT(onInvitationResponded(const ndn::Name&, bool)));
+          &m_backend, SLOT(onInvitationResponded(const ndn::Name&, bool)));
 
   // Connection to AddContactPanel
   connect(m_addContactPanel, SIGNAL(fetchInfo(const QString&)),
-          &m_contactManager, SLOT(onFetchContactInfo(const QString&)));
+          m_backend.getContactManager(), SLOT(onFetchContactInfo(const QString&)));
   connect(m_addContactPanel, SIGNAL(addContact(const QString&)),
-          &m_contactManager, SLOT(onAddFetchedContact(const QString&)));
-  connect(&m_contactManager, SIGNAL(contactEndorseInfoReady(const Chronos::EndorseInfo&)),
-          m_addContactPanel, SLOT(onContactEndorseInfoReady(const Chronos::EndorseInfo&)));
+          m_backend.getContactManager(), SLOT(onAddFetchedContact(const QString&)));
+  connect(m_backend.getContactManager(),
+          SIGNAL(contactEndorseInfoReady(const Chronos::EndorseInfo&)),
+          m_addContactPanel,
+          SLOT(onContactEndorseInfoReady(const Chronos::EndorseInfo&)));
 
 
   // Connection to BrowseContactDialog
   connect(m_browseContactDialog, SIGNAL(directAddClicked()),
           this, SLOT(onDirectAdd()));
   connect(m_browseContactDialog, SIGNAL(fetchIdCert(const QString&)),
-          &m_contactManager, SLOT(onFetchIdCert(const QString&)));
+          m_backend.getContactManager(), SLOT(onFetchIdCert(const QString&)));
   connect(m_browseContactDialog, SIGNAL(addContact(const QString&)),
-          &m_contactManager, SLOT(onAddFetchedContactIdCert(const QString&)));
-  connect(&m_contactManager, SIGNAL(idCertNameListReady(const QStringList&)),
+          m_backend.getContactManager(), SLOT(onAddFetchedContactIdCert(const QString&)));
+  connect(m_backend.getContactManager(), SIGNAL(idCertNameListReady(const QStringList&)),
           m_browseContactDialog, SLOT(onIdCertNameListReady(const QStringList&)));
-  connect(&m_contactManager, SIGNAL(nameListReady(const QStringList&)),
+  connect(m_backend.getContactManager(), SIGNAL(nameListReady(const QStringList&)),
           m_browseContactDialog, SLOT(onNameListReady(const QStringList&)));
-  connect(&m_contactManager, SIGNAL(idCertReady(const ndn::IdentityCertificate&)),
+  connect(m_backend.getContactManager(), SIGNAL(idCertReady(const ndn::IdentityCertificate&)),
           m_browseContactDialog, SLOT(onIdCertReady(const ndn::IdentityCertificate&)));
 
   // Connection to ContactPanel
   connect(m_contactPanel, SIGNAL(waitForContactList()),
-          &m_contactManager, SLOT(onWaitForContactList()));
+          m_backend.getContactManager(), SLOT(onWaitForContactList()));
   connect(m_contactPanel, SIGNAL(waitForContactInfo(const QString&)),
-          &m_contactManager, SLOT(onWaitForContactInfo(const QString&)));
+          m_backend.getContactManager(), SLOT(onWaitForContactInfo(const QString&)));
   connect(m_contactPanel, SIGNAL(removeContact(const QString&)),
-          &m_contactManager, SLOT(onRemoveContact(const QString&)));
+          m_backend.getContactManager(), SLOT(onRemoveContact(const QString&)));
   connect(m_contactPanel, SIGNAL(updateAlias(const QString&, const QString&)),
-          &m_contactManager, SLOT(onUpdateAlias(const QString&, const QString&)));
+          m_backend.getContactManager(), SLOT(onUpdateAlias(const QString&, const QString&)));
   connect(m_contactPanel, SIGNAL(updateIsIntroducer(const QString&, bool)),
-          &m_contactManager, SLOT(onUpdateIsIntroducer(const QString&, bool)));
+          m_backend.getContactManager(), SLOT(onUpdateIsIntroducer(const QString&, bool)));
   connect(m_contactPanel, SIGNAL(updateEndorseCertificate(const QString&)),
-          &m_contactManager, SLOT(onUpdateEndorseCertificate(const QString&)));
+          m_backend.getContactManager(), SLOT(onUpdateEndorseCertificate(const QString&)));
   connect(m_contactPanel, SIGNAL(warning(const QString&)),
           this, SLOT(onWarning(const QString&)));
   connect(this, SIGNAL(closeDBModule()),
           m_contactPanel, SLOT(onCloseDBModule()));
   connect(this, SIGNAL(identityUpdated(const QString&)),
           m_contactPanel, SLOT(onIdentityUpdated(const QString&)));
-  connect(&m_contactManager, SIGNAL(contactAliasListReady(const QStringList&)),
+  connect(m_backend.getContactManager(), SIGNAL(contactAliasListReady(const QStringList&)),
           m_contactPanel, SLOT(onContactAliasListReady(const QStringList&)));
-  connect(&m_contactManager, SIGNAL(contactIdListReady(const QStringList&)),
+  connect(m_backend.getContactManager(), SIGNAL(contactIdListReady(const QStringList&)),
           m_contactPanel, SLOT(onContactIdListReady(const QStringList&)));
-  connect(&m_contactManager, SIGNAL(contactInfoReady(const QString&, const QString&,
-                                                     const QString&, bool)),
+  connect(m_backend.getContactManager(), SIGNAL(contactInfoReady(const QString&, const QString&,
+                                                                 const QString&, bool)),
           m_contactPanel, SLOT(onContactInfoReady(const QString&, const QString&,
                                                   const QString&, bool)));
 
-  // Connection to DiscoveryDialog
-  connect(this,
-          SIGNAL(discoverChatroomChanged(const chronos::ChatroomInfo&, bool)),
-          m_chatroomDiscoveryDialog,
-          SLOT(onDiscoverChatroomChanged(const chronos::ChatroomInfo&, bool)));
-  connect(m_chatroomDiscoveryDialog, SIGNAL(startChatroom(const QString&, bool)),
-          this, SLOT(onStartChatroom(const QString&, bool)));
+  // Connection to backend thread
+  connect(this, SIGNAL(shutdownBackend()),
+          &m_backend, SLOT(shutdown()));
+  connect(this, SIGNAL(updateLocalPrefix()),
+          &m_backend, SLOT(onUpdateLocalPrefixAction()));
+  connect(this, SIGNAL(identityUpdated(const QString&)),
+          &m_backend, SLOT(onIdentityChanged(const QString&)));
+  connect(this, SIGNAL(addChatroom(QString)),
+          &m_backend, SLOT(addChatroom(QString)));
+  connect(this, SIGNAL(removeChatroom(QString)),
+          &m_backend, SLOT(removeChatroom(QString)));
+
+  // Thread notifications:
+  // on local prefix udpated:
+  connect(&m_backend, SIGNAL(localPrefixUpdated(const QString&)),
+          this, SLOT(onLocalPrefixUpdated(const QString&)));
+  connect(&m_backend, SIGNAL(localPrefixUpdated(const QString&)),
+          m_settingDialog, SLOT(onLocalPrefixUpdated(const QString&)));
+
+  // on invitation validated:
+  connect(&m_backend, SIGNAL(invitaionValidated(QString, QString, ndn::Name)),
+          m_invitationDialog, SLOT(onInvitationReceived(QString, QString, ndn::Name)));
+
+  // on invitation accepted:
+  connect(&m_backend, SIGNAL(startChatroomOnInvitation(chronos::Invitation, bool)),
+          this, SLOT(onStartChatroom2(chronos::Invitation, bool)));
+
+  m_backend.start();
 
   initialize();
 
   createTrayIcon();
 
-  onUpdateLocalPrefixAction();
+  emit updateLocalPrefix();
 }
 
 Controller::~Controller()
@@ -232,37 +231,9 @@
 {
   loadConf();
 
-  m_keyChain.createIdentity(m_identity);
-
   openDB();
 
   emit identityUpdated(QString(m_identity.toUri().c_str()));
-
-  setInvitationListener();
-
-  m_discoveryLogic.sendDiscoveryInterest();
-}
-
-void
-Controller::setInvitationListener()
-{
-  if (m_invitationListenerId != 0)
-    m_face->unsetInterestFilter(m_invitationListenerId);
-
-  Name invitationPrefix;
-  Name routingPrefix = getInvitationRoutingPrefix();
-  size_t offset = 0;
-  if (!routingPrefix.isPrefixOf(m_identity)) {
-    invitationPrefix.append(routingPrefix).append(ROUTING_PREFIX_SEPARATOR, 2);
-    offset = routingPrefix.size() + 1;
-  }
-  invitationPrefix.append(m_identity).append("CHRONOCHAT-INVITATION");
-
-  m_invitationListenerId = m_face->setInterestFilter(invitationPrefix,
-                                                     bind(&Controller::onInvitationInterestWrapper,
-                                                          this, _1, _2, offset),
-                                                     bind(&Controller::onInvitationRegisterFailed,
-                                                          this, _1, _2));
 }
 
 void
@@ -333,7 +304,8 @@
   connect(m_addContactAction, SIGNAL(triggered()), this, SLOT(onAddContactAction()));
 
   m_updateLocalPrefixAction = new QAction(tr("Update local prefix"), this);
-  connect(m_updateLocalPrefixAction, SIGNAL(triggered()), this, SLOT(onUpdateLocalPrefixAction()));
+  connect(m_updateLocalPrefixAction, SIGNAL(triggered()),
+          &m_backend, SLOT(onUpdateLocalPrefixAction()));
 
   m_minimizeAction = new QAction(tr("Mi&nimize"), this);
   connect(m_minimizeAction, SIGNAL(triggered()), this, SLOT(onMinimizeAction()));
@@ -350,7 +322,7 @@
 
   m_trayIconMenu = new QMenu(this);
   m_trayIconMenu->addAction(m_startChatroom);
-  m_trayIconMenu->addAction(m_discoveryAction);
+  // m_trayIconMenu->addAction(m_discoveryAction); // disable discovery temporarily
 
   m_trayIconMenu->addSeparator();
   m_trayIconMenu->addAction(m_settingsAction);
@@ -382,7 +354,7 @@
   QMenu* closeMenu = 0;
 
   menu->addAction(m_startChatroom);
-  menu->addAction(m_discoveryAction);
+  // menu->addAction(m_discoveryAction);
 
   menu->addSeparator();
   menu->addAction(m_settingsAction);
@@ -422,88 +394,6 @@
   m_closeMenu = closeMenu;
 }
 
-void
-Controller::onLocalPrefix(const Interest& interest, Data& data)
-{
-  QString localPrefixStr("/private/local");
-  Name prefix;
-
-  Block contentBlock = data.getContent();
-  try {
-    contentBlock.parse();
-
-    for (Block::element_const_iterator it = contentBlock.elements_begin();
-         it != contentBlock.elements_end(); it++) {
-      Name candidate;
-      candidate.wireDecode(*it);
-      if (candidate.isPrefixOf(m_identity)) {
-        prefix = candidate;
-        break;
-      }
-    }
-
-    if (prefix.empty()) {
-      if (contentBlock.elements_begin() != contentBlock.elements_end())
-        prefix.wireDecode(*contentBlock.elements_begin());
-      else
-        prefix = Name("/private/local");
-    }
-
-    localPrefixStr = QString::fromStdString(prefix.toUri());
-  }
-  catch (Block::Error& e) {
-    prefix = Name("/private/local");
-  }
-
-  if (m_localPrefix.empty() || m_localPrefix != prefix) {
-    emit localPrefixUpdated(localPrefixStr);
-  }
-}
-
-void
-Controller::onLocalPrefixTimeout(const Interest& interest)
-{
-  QString localPrefixStr("/private/local");
-
-  Name localPrefix(localPrefixStr.toStdString());
-  if (m_localPrefix.empty() || m_localPrefix != localPrefix) {
-    emit localPrefixUpdated(localPrefixStr);
-  }
-}
-
-void
-Controller::onInvitationInterestWrapper(const Name& prefix,
-                                        const Interest& interest,
-                                        size_t routingPrefixOffset)
-{
-  emit invitationInterest(prefix, interest, routingPrefixOffset);
-}
-
-void
-Controller::onInvitationRegisterFailed(const Name& prefix, const string& failInfo)
-{
-  // _LOG_DEBUG("Controller::onInvitationRegisterFailed: " << failInfo);
-}
-
-void
-Controller::onInvitationValidated(const shared_ptr<const Interest>& interest)
-{
-  Invitation invitation(interest->getName());
-  // Should be obtained via a method of ContactManager.
-  string alias = invitation.getInviterCertificate().getPublicKeyName().getPrefix(-1).toUri();
-
-  m_invitationDialog->setInvitation(alias, invitation.getChatroom(), interest->getName());
-  m_invitationDialog->show();
-}
-
-void
-Controller::onInvitationValidationFailed(const shared_ptr<const Interest>& interest,
-                                         string failureInfo)
-{
-  // _LOG_DEBUG("Invitation: " << interest->getName() <<
-  //            " cannot not be validated due to: " << failureInfo);
-}
-
 string
 Controller::getRandomString()
 {
@@ -528,17 +418,10 @@
   return ss.str();
 }
 
-ndn::Name
-Controller::getInvitationRoutingPrefix()
-{
-  return Name("/ndn/broadcast");
-}
-
 void
 Controller::addChatDialog(const QString& chatroomName, ChatDialog* chatDialog)
 {
   m_chatDialogList[chatroomName.toStdString()] = chatDialog;
-  m_discoveryLogic.addLocalChatroom(*chatDialog->getChatroomInfo());
   connect(chatDialog, SIGNAL(closeChatDialog(const QString&)),
           this, SLOT(onRemoveChatDialog(const QString&)));
   connect(chatDialog, SIGNAL(showChatMessage(const QString&, const QString&, const QString&)),
@@ -547,7 +430,7 @@
           this, SLOT(onResetIcon()));
   connect(chatDialog, SIGNAL(rosterChanged(const chronos::ChatroomInfo&)),
           this, SLOT(onRosterChanged(const chronos::ChatroomInfo&)));
-  connect(this, SIGNAL(localPrefixUpdated(const QString&)),
+  connect(&m_backend, SIGNAL(localPrefixUpdated(const QString&)),
           chatDialog->getBackend(), SLOT(updateRoutingPrefix(const QString&)));
   connect(this, SIGNAL(localPrefixConfigured(const QString&)),
           chatDialog->getBackend(), SLOT(updateRoutingPrefix(const QString&)));
@@ -574,21 +457,17 @@
 void
 Controller::onIdentityUpdated(const QString& identity)
 {
-  Name identityName(identity.toStdString());
-
   while (!m_chatDialogList.empty()) {
     ChatDialogList::const_iterator it = m_chatDialogList.begin();
     onRemoveChatDialog(QString::fromStdString(it->first));
   }
 
-  m_identity = identityName;
-  m_keyChain.createIdentity(m_identity);
-  setInvitationListener();
-
   emit closeDBModule();
 
-  QTimer::singleShot(500, this, SLOT(onIdentityUpdatedContinued()));
+  Name identityName(identity.toStdString());
+  m_identity = identityName;
 
+  QTimer::singleShot(500, this, SLOT(onIdentityUpdatedContinued()));
 }
 
 void
@@ -605,19 +484,6 @@
 }
 
 void
-Controller::onContactIdListReady(const QStringList& list)
-{
-  ContactList contactList;
-
-  m_contactManager.getContactList(contactList);
-  m_validator.cleanTrustAnchor();
-
-  for (ContactList::const_iterator it  = contactList.begin(); it != contactList.end(); it++)
-    m_validator.addTrustAnchor((*it)->getPublicKeyName(), (*it)->getPublicKey());
-
-}
-
-void
 Controller::onNickUpdated(const QString& nick)
 {
   m_nick = nick.toStdString();
@@ -628,12 +494,12 @@
 {
   QString privateLocalPrefix("/private/local");
 
-  m_localPrefix = Name(localPrefix.toStdString());
-
   if (privateLocalPrefix != localPrefix)
     m_localPrefixDetected = true;
   else
     m_localPrefixDetected = false;
+
+  m_localPrefix = Name(localPrefix.toStdString());
 }
 
 void
@@ -658,11 +524,6 @@
 void
 Controller::onDiscoveryAction()
 {
-  m_discoveryLogic.sendDiscoveryInterest();
-
-  m_chatroomDiscoveryDialog->updateChatroomList();
-  m_chatroomDiscoveryDialog->show();
-  m_chatroomDiscoveryDialog->raise();
 }
 
 
@@ -704,19 +565,6 @@
 }
 
 void
-Controller::onUpdateLocalPrefixAction()
-{
-  // Name interestName();
-  Interest interest("/localhop/ndn-autoconf/routable-prefixes");
-  interest.setInterestLifetime(time::milliseconds(1000));
-  interest.setMustBeFresh(true);
-
-  m_face->expressInterest(interest,
-                          bind(&Controller::onLocalPrefix, this, _1, _2),
-                          bind(&Controller::onLocalPrefixTimeout, this, _1));
-}
-
-void
 Controller::onMinimizeAction()
 {
   m_settingDialog->hide();
@@ -739,9 +587,6 @@
     it->second->shutdown();
   }
 
-  if (m_invitationListenerId != 0)
-    m_face->unsetInterestFilter(m_invitationListenerId);
-
   delete m_settingDialog;
   delete m_startChatDialog;
   delete m_profileEditor;
@@ -749,7 +594,10 @@
   delete m_browseContactDialog;
   delete m_addContactPanel;
 
-  m_face->getIoService().stop();
+  if (m_backend.isRunning()) {
+    emit shutdownBackend();
+    m_backend.wait();
+  }
 
   QApplication::quit();
 }
@@ -774,10 +622,6 @@
   // TODO: We should create a chatroom specific key/cert
   //(which should be created in the first half of this method
   //, but let's use the default one for now.
-  // std::cout << "start chat room localprefix: " << m_localPrefix.toUri() << std::endl;
-  shared_ptr<IdentityCertificate> idCert
-    = m_keyChain.getCertificate(m_keyChain.getDefaultCertificateNameForIdentity(m_identity));
-
   Name chatPrefix;
   chatPrefix.append(m_identity).append("CHRONOCHAT-DATA").append(chatroomName.toStdString());
 
@@ -790,89 +634,23 @@
 
   addChatDialog(chatroomName, chatDialog);
   chatDialog->show();
+
+  emit addChatroom(chatroomName);
 }
 
 void
-Controller::onInvitationResponded(const ndn::Name& invitationName, bool accepted)
+Controller::onStartChatroom2(chronos::Invitation invitation, bool secured)
 {
-  shared_ptr<Data> response = make_shared<Data>();
-  shared_ptr<IdentityCertificate> chatroomCert;
+  QString chatroomName = QString::fromStdString(invitation.getChatroom());
+  onStartChatroom(chatroomName, secured);
 
-  // generate reply;
-  if (accepted) {
-    Name responseName = invitationName;
-    responseName.append(m_localPrefix.wireEncode());
+  ChatDialogList::iterator it = m_chatDialogList.find(chatroomName.toStdString());
 
-    response->setName(responseName);
-
-    // We should create a particular certificate for this chatroom,
-    //but let's use default one for now.
-    chatroomCert
-      = m_keyChain.getCertificate(m_keyChain.getDefaultCertificateNameForIdentity(m_identity));
-
-    response->setContent(chatroomCert->wireEncode());
-    response->setFreshnessPeriod(time::milliseconds(1000));
-  }
-  else {
-    response->setName(invitationName);
-    response->setFreshnessPeriod(time::milliseconds(1000));
-  }
-
-  m_keyChain.signByIdentity(*response, m_identity);
-
-  // Check if we need a wrapper
-  Name invitationRoutingPrefix = getInvitationRoutingPrefix();
-  if (invitationRoutingPrefix.isPrefixOf(m_identity))
-    m_face->put(*response);
-  else {
-    Name wrappedName;
-    wrappedName.append(invitationRoutingPrefix)
-      .append(ROUTING_PREFIX_SEPARATOR, 2)
-      .append(response->getName());
-
-    // _LOG_DEBUG("onInvitationResponded: prepare reply " << wrappedName);
-
-    shared_ptr<Data> wrappedData = make_shared<Data>(wrappedName);
-    wrappedData->setContent(response->wireEncode());
-    wrappedData->setFreshnessPeriod(time::milliseconds(1000));
-
-    m_keyChain.signByIdentity(*wrappedData, m_identity);
-    m_face->put(*wrappedData);
-  }
-
-  // create chatroom
-  if (accepted) {
-    Invitation invitation(invitationName);
-    Name chatroomPrefix;
-    chatroomPrefix.append("ndn")
-      .append("broadcast")
-      .append("ChronoChat")
-      .append(invitation.getChatroom());
-
-    //We should create a chatroom specific key/cert
-    //(which should be created in the first half of this method,
-    //but let's use the default one for now.
-    shared_ptr<IdentityCertificate> idCert
-      = m_keyChain.getCertificate(m_keyChain.getDefaultCertificateNameForIdentity(m_identity));
-
-    Name chatPrefix;
-    chatPrefix.append(m_identity).append("CHRONOCHAT-DATA").append(invitation.getChatroom());
-
-    ChatDialog* chatDialog
-      = new ChatDialog(chatroomPrefix,
-                       chatPrefix,
-                       m_localPrefix,
-                       invitation.getChatroom(),
-                       m_nick,
-                       true);
-
-    chatDialog->addSyncAnchor(invitation);
-
-    addChatDialog(QString::fromStdString(invitation.getChatroom()), chatDialog);
-    chatDialog->show();
-  }
+  BOOST_ASSERT(it != m_chatDialogList.end());
+  it->second->addSyncAnchor(invitation);
 }
 
+
 void
 Controller::onShowChatMessage(const QString& chatroomName, const QString& from, const QString& data)
 {
@@ -898,7 +676,6 @@
     if (deletedChat)
       delete deletedChat;
     m_chatDialogList.erase(it);
-    m_discoveryLogic.removeLocalChatroom(Name::Component(chatroomName.toStdString()));
 
     QAction* chatAction = m_chatActionList[chatroomName.toStdString()];
     QAction* closeAction = m_closeActionList[chatroomName.toStdString()];
@@ -928,35 +705,9 @@
 }
 
 void
-Controller::onInvitationInterest(const ndn::Name& prefix,
-                                 const ndn::Interest& interest,
-                                 size_t routingPrefixOffset)
-{
-  // _LOG_DEBUG("onInvitationInterest: " << interest.getName());
-  shared_ptr<Interest> invitationInterest =
-    make_shared<Interest>(boost::cref(interest.getName().getSubName(routingPrefixOffset)));
-
-  // check if the chatroom already exists;
-  try {
-      Invitation invitation(invitationInterest->getName());
-      if (m_chatDialogList.find(invitation.getChatroom()) != m_chatDialogList.end())
-        return;
-  }
-  catch (Invitation::Error& e) {
-    // Cannot parse the invitation;
-    return;
-  }
-
-  OnInterestValidated onValidated = bind(&Controller::onInvitationValidated, this, _1);
-  OnInterestValidationFailed onValidationFailed = bind(&Controller::onInvitationValidationFailed,
-                                                       this, _1, _2);
-  m_validator.validate(*invitationInterest, onValidated, onValidationFailed);
-}
-
-void
 Controller::onRosterChanged(const chronos::ChatroomInfo& info)
 {
-  m_discoveryLogic.addLocalChatroom(info);
+
 }
 
 } // namespace chronos