Skip to content

Commit

Permalink
Adding local prefix updating
Browse files Browse the repository at this point in the history
  • Loading branch information
bruinfish authored and cawka committed May 9, 2014
1 parent e433a50 commit fdb8ab8
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 30 deletions.
172 changes: 149 additions & 23 deletions src/chatdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#ifndef Q_MOC_RUN
#include <ndn.cxx/security/identity/identity-manager.h>
#include <ndn.cxx/security/policy/no-verify-policy-manager.h>
#include <ndn.cxx/security/encryption/basic-encryption-manager.h>
#include <sync-intro-certificate.h>
#include <boost/random/random_device.hpp>
Expand Down Expand Up @@ -61,9 +62,10 @@ ChatDialog::ChatDialog(ndn::Ptr<ContactManager> contactManager,

ui->setupUi(this);

QString randString = getRandomString();
m_localChatPrefix = m_localPrefix;
m_localChatPrefix.append("%F0.").append(m_defaultIdentity);
m_localChatPrefix.append("chronos").append(m_chatroomPrefix.get(-1));
m_localChatPrefix.append("chronos").append(m_chatroomPrefix.get(-1)).append(randString.toStdString());

m_session = time(NULL);
m_scene = new DigestTreeScene(this);
Expand Down Expand Up @@ -136,29 +138,14 @@ ChatDialog::setWrapper(bool trial)

m_keychain = ndn::Ptr<ndn::security::Keychain>(new ndn::security::Keychain(m_identityManager, m_invitationPolicyManager, NULL));

ndn::Ptr<ndn::security::Keychain> noVerifyKeychain = ndn::Ptr<ndn::security::Keychain>(new ndn::security::Keychain(m_identityManager,
ndn::Ptr<ndn::security::NoVerifyPolicyManager>::Create(), NULL));
try{
m_handler = ndn::Ptr<ndn::Wrapper>(new ndn::Wrapper(m_keychain));
m_localPrefixHandler = ndn::Ptr<ndn::Wrapper>(new ndn::Wrapper(noVerifyKeychain));
}catch(ndn::Error::ndnOperation& e){
emit noNdnConnection(QString::fromStdString("Cannot conect to ndnd!\n Have you started your ndnd?"));
}


if(trial == true)
{
ndn::Ptr<ndn::Interest> interest = ndn::Ptr<ndn::Interest>(new ndn::Interest(ndn::Name("/local/ndn/prefix")));
ndn::Ptr<ndn::Closure> closure = ndn::Ptr<ndn::Closure>(new ndn::Closure(boost::bind(&ChatDialog::onUnverified,
this,
_1),
boost::bind(&ChatDialog::onTimeout,
this,
_1,
_2),
boost::bind(&ChatDialog::onUnverified,
this,
_1)));

m_handler->sendInterest(interest, closure);
}
}

void
Expand Down Expand Up @@ -699,6 +686,94 @@ ChatDialog::updateRosterList(QStringList staleUserList)
}
}

void
ChatDialog::settingUpdated(QString nick, QString chatroom, QString originPrefix)
{
QString randString = getRandomString();
bool needWrite = false;
bool needFresh = false;

QString oldPrefix = m_user.getPrefix();
if (!originPrefix.isEmpty() && originPrefix != m_user.getOriginPrefix()) {
m_user.setOriginPrefix(originPrefix);

m_localPrefix = ndn::Name(originPrefix.toStdString());
m_localChatPrefix = m_localPrefix;
m_localChatPrefix.append("%F0.").append(m_defaultIdentity);
m_localChatPrefix.append("chronos").append(m_chatroomPrefix.get(-1)).append(randString.toStdString());
m_user.setPrefix(QString::fromStdString(m_localChatPrefix.toUri()));
m_scene->setCurrentPrefix(QString::fromStdString(m_localChatPrefix.toUri()));
needWrite = true;
needFresh = true;
}

if (needWrite) {
updateLabels();
}

if (needFresh && m_sock != NULL)
{

{
boost::recursive_mutex::scoped_lock lock(m_sceneMutex);
m_scene->clearAll();
m_scene->plot("Empty");
}

// ui->textEdit->clear();

// keep the new prefix
QString newPrefix = m_user.getPrefix();
// send leave for the old
m_user.setPrefix(oldPrefix);
// there is no point to send leave if we haven't joined yet
if (m_joined)
{
sendLeave();
}
// resume new prefix
m_user.setPrefix(newPrefix);
// Sync::CcnxWrapperPtr handle = Sync::CcnxWrapper::Create();
// handle->clearInterestFilter(oldPrefix.toStdString());
delete m_sock;
m_sock = NULL;

try
{
usleep(100000);
m_sock = new Sync::SyncSocket(m_chatroomPrefix.toUri(),
m_syncPolicyManager,
bind(&ChatDialog::processTreeUpdateWrapper, this, _1, _2),
bind(&ChatDialog::processRemoveWrapper, this, _1));
usleep(100000);
// Sync::CcnxWrapperPtr handle = boost::make_shared<Sync::CcnxWrapper> ();
// handle->setInterestFilter(m_user.getPrefix().toStdString(), bind(&ChatDialog::respondHistoryRequest, this, _1));
sendJoin();
m_timer->start(FRESHNESS * 1000);
disableTreeDisplay();
enableTreeDisplay();
}catch(ndn::Error::ndnOperation& e){
emit noNdnConnection(QString::fromStdString("Cannot conect to ndnd!\n Have you started your ndnd?"));
}
}
else if (needFresh && m_sock == NULL)
{
initializeSync();
}
else if (m_sock == NULL)
{
initializeSync();
}
else
{
// #ifdef __DEBUG
// std::cout << "Just changing nicks, we're good. " << std::endl;
// #endif
}

fitView();
}

void
ChatDialog::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
Expand Down Expand Up @@ -739,8 +814,8 @@ ChatDialog::createActions()

// settingsAction->setMenuRole (QAction::PreferencesRole);

// updateLocalPrefixAction = new QAction(tr("Update local prefix"), this);
// connect (updateLocalPrefixAction, SIGNAL(triggered()), this, SLOT(updateLocalPrefix()));
updateLocalPrefixAction = new QAction(tr("Update local prefix"), this);
connect (updateLocalPrefixAction, SIGNAL(triggered()), this, SLOT(updateLocalPrefix()));

quitAction = new QAction(tr("Quit"), this);
connect(quitAction, SIGNAL(triggered()), this, SLOT(quit()));
Expand All @@ -755,8 +830,8 @@ ChatDialog::createTrayIcon()
trayIconMenu->addAction(restoreAction);
// trayIconMenu->addSeparator();
// trayIconMenu->addAction(settingsAction);
// trayIconMenu->addSeparator();
// trayIconMenu->addAction(updateLocalPrefixAction);
trayIconMenu->addSeparator();
trayIconMenu->addAction(updateLocalPrefixAction);
trayIconMenu->addSeparator();
trayIconMenu->addAction(quitAction);

Expand Down Expand Up @@ -812,6 +887,57 @@ ChatDialog::formControlMessage(SyncDemo::ChatMessage &msg, SyncDemo::ChatMessage
msg.set_type(type);
}

void
ChatDialog::updateLocalPrefix()
{
ndn::Ptr<ndn::Interest> interest = ndn::Ptr<ndn::Interest>(new ndn::Interest(ndn::Name("/local/ndn/prefix")));
interest->setChildSelector(ndn::Interest::CHILD_RIGHT);

ndn::Ptr<ndn::Closure> closure = ndn::Ptr<ndn::Closure>(new ndn::Closure(boost::bind(&ChatDialog::getLocalPrefix,
this,
_1),
boost::bind(&ChatDialog::getLocalPrefixTimeout,
this,
_1,
_2),
boost::bind(&ChatDialog::getLocalPrefix,
this,
_1)));

m_localPrefixHandler->sendInterest(interest, closure);

}

static std::string chars2("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789");

QString
ChatDialog::getRandomString()
{
std::string randStr;
boost::random::random_device rng;
boost::random::uniform_int_distribution<> index_dist(0, chars2.size() - 1);
for (int i = 0; i < 10; i ++)
{
randStr += chars2[index_dist(rng)];
}
return randStr.c_str();
}

void
ChatDialog::getLocalPrefix(ndn::Ptr<ndn::Data> data)
{
string dataString(data->content().buf(), data->content().size());
QString originPrefix = QString::fromStdString (dataString).trimmed ();

if (originPrefix != "" && m_user.getOriginPrefix () != originPrefix)
emit settingUpdated(m_user.getNick (), m_user.getChatroom (), originPrefix);
}

void
ChatDialog::getLocalPrefixTimeout(ndn::Ptr<ndn::Closure> closure, ndn::Ptr<ndn::Interest> interest)
{
}

void
ChatDialog::changeEvent(QEvent *e)
{
Expand Down
17 changes: 17 additions & 0 deletions src/chatdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ class ChatDialog : public QDialog
void
initializeSetting();

QString
getRandomString();

void
updateLabels();

Expand Down Expand Up @@ -145,6 +148,12 @@ class ChatDialog : public QDialog
onTimeout(ndn::Ptr<ndn::Closure> closure,
ndn::Ptr<ndn::Interest> interest);

void
getLocalPrefix(ndn::Ptr<ndn::Data> data);

void
getLocalPrefixTimeout(ndn::Ptr<ndn::Closure> closure,
ndn::Ptr<ndn::Interest> interest);

// void
// fetchHistory(std::string name);
Expand Down Expand Up @@ -215,6 +224,9 @@ private slots:
void
treeButtonPressed();

void
settingUpdated(QString, QString, QString);

void
sendJoin();

Expand All @@ -233,6 +245,9 @@ private slots:
void
enableTreeDisplay();

void
updateLocalPrefix();

void
summonReaper();

Expand Down Expand Up @@ -269,6 +284,7 @@ private slots:
ndn::Ptr<ndn::security::IdentityManager> m_identityManager;
ndn::Ptr<ndn::security::Keychain> m_keychain;
ndn::Ptr<ndn::Wrapper> m_handler;
ndn::Ptr<ndn::Wrapper> m_localPrefixHandler;

User m_user;
std::string m_nick;
Expand All @@ -286,6 +302,7 @@ private slots:
QAction *minimizeAction;
QAction *maximizeAction;
QAction *restoreAction;
QAction *updateLocalPrefixAction;
QAction *quitAction;
QMenu *trayIconMenu;

Expand Down
11 changes: 6 additions & 5 deletions src/contactpanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ ContactPanel::setKeychain()
}

void
ContactPanel::setLocalPrefix()
ContactPanel::setLocalPrefix(int retry)
{
Ptr<Interest> interest = Ptr<Interest>(new Interest(Name("/local/ndn/prefix")));
interest->setChildSelector(Interest::CHILD_RIGHT);
Expand All @@ -237,7 +237,8 @@ ContactPanel::setLocalPrefix()
boost::bind(&ContactPanel::onLocalPrefixTimeout,
this,
_1,
_2),
_2,
10),
boost::bind(&ContactPanel::onLocalPrefixVerified,
this,
_1)));
Expand All @@ -256,10 +257,10 @@ ContactPanel::onLocalPrefixVerified(Ptr<Data> data)
}

void
ContactPanel::onLocalPrefixTimeout(Ptr<Closure> closure, Ptr<Interest> interest)
ContactPanel::onLocalPrefixTimeout(Ptr<Closure> closure, Ptr<Interest> interest, int retry)
{
string randomSuffix = getRandomString();
m_localPrefix = Name("/private/local");
if(retry > 0)
setLocalPrefix(retry - 1);
}

void
Expand Down
5 changes: 3 additions & 2 deletions src/contactpanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,15 @@ class ContactPanel : public QDialog
setKeychain();

void
setLocalPrefix();
setLocalPrefix(int retry = 10);

void
onLocalPrefixVerified(ndn::Ptr<ndn::Data> data);

void
onLocalPrefixTimeout(ndn::Ptr<ndn::Closure> closure,
ndn::Ptr<ndn::Interest> interest);
ndn::Ptr<ndn::Interest> interest,
int retry);

void
setInvitationListener();
Expand Down

0 comments on commit fdb8ab8

Please sign in to comment.