blob: c9ffe274ad2ff0a072f59ca9b75488fc43d7b5df [file] [log] [blame]
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
/*
* Copyright (c) 2012 University of California, Los Angeles
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
* Chaoyi Bian <bcy@pku.edu.cn>
* Alexander Afanasyev <alexander.afanasyev@ucla.edu>
*/
#ifndef SYNC_CCNX_WRAPPER_H
#define SYNC_CCNX_WRAPPER_H
#include <boost/exception/all.hpp>
#include <boost/function.hpp>
#include <string>
#include <ns3/ptr.h>
#include <ns3/node.h>
#include <ns3/random-variable.h>
#include <ns3/ccnx-app.h>
#include <ns3/ccnx-name-components.h>
#include <ns3/ccnx-name-components-hash-helper.h>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/mem_fun.hpp>
/**
* \defgroup sync SYNC protocol
*
* Implementation of SYNC protocol
*/
namespace Sync {
template<class Callback>
struct CcnxFilterEntry
{
public:
CcnxFilterEntry (ns3::Ptr<const ns3::CcnxNameComponents> prefix)
: m_prefix (prefix)
{ }
const ns3::CcnxNameComponents &
GetPrefix () const
{ return *m_prefix; }
void
AddCallback (Callback callback)
{
m_callback = callback;
}
void
ClearCallback ()
{
m_callback = 0;
}
public:
ns3::Ptr<const ns3::CcnxNameComponents> m_prefix; ///< \brief Prefix of the PIT entry
Callback m_callback;
};
template<class Callback>
struct CcnxFilterEntryContainer
{
typedef
boost::multi_index::multi_index_container<
CcnxFilterEntry<Callback>,
boost::multi_index::indexed_by<
// indexed by hash
boost::multi_index::hashed_unique<
boost::multi_index::const_mem_fun< CcnxFilterEntry<Callback>, const ns3::CcnxNameComponents&, &CcnxFilterEntry<Callback>::GetPrefix >,
ns3::CcnxPrefixHash
>
>
> type;
};
struct CcnxOperationException : virtual boost::exception, virtual std::exception { };
/**
* \ingroup sync
* @brief A wrapper for ccnx library; clients of this code do not need to deal
* with ccnx library
*/
class CcnxWrapper
: public ns3::CcnxApp
{
public:
typedef boost::function<void (std::string, std::string)> DataCallback;
typedef boost::function<void (std::string)> InterestCallback;
/**
* @brief initialize the wrapper; a lot of things needs to be done. 1) init
* keystore 2) init keylocator 3) start a thread to hold a loop of ccn_run
*
*/
CcnxWrapper();
~CcnxWrapper();
// from CcnxApp
/**
* @brief Should be called after Node pointer is set to create face and start application
*/
virtual void
StartApplication ();
/**
* @brief Stop application
*/
virtual void
StopApplication ();
/**
* @brief send Interest; need to grab lock m_mutex first
*
* @param strInterest the Interest name
* @param dataCallback the callback function to deal with the returned data
* @return the return code of ccn_express_interest
*/
int
sendInterest (const std::string &strInterest, const DataCallback &dataCallback);
/**
* @brief set Interest filter (specify what interest you want to receive)
*
* @param prefix the prefix of Interest
* @param interestCallback the callback function to deal with the returned data
* @return the return code of ccn_set_interest_filter
*/
int
setInterestFilter (const std::string &prefix, const InterestCallback &interestCallback);
/**
* @brief clear Interest filter
* @param prefix the prefix of Interest
*/
void
clearInterestFilter (const std::string &prefix);
/**
* @brief publish data and put it to local ccn content store; need to grab
* lock m_mutex first
*
* @param name the name for the data object
* @param dataBuffer the data to be published
* @param freshness the freshness time for the data object
* @return code generated by ccnx library calls, >0 if success
*/
int
publishData (const std::string &name, const std::string &dataBuffer, int freshness);
// from CcnxApp
virtual void
OnInterest (const ns3::Ptr<const ns3::CcnxInterestHeader> &interest, ns3::Ptr<ns3::Packet> packet);
virtual void
OnContentObject (const ns3::Ptr<const ns3::CcnxContentObjectHeader> &contentObject,
ns3::Ptr<ns3::Packet> payload);
private:
CcnxFilterEntryContainer<InterestCallback>::type::iterator
InterestCallbackLookup (const ns3::CcnxNameComponents &name);
CcnxFilterEntryContainer<DataCallback>::type::iterator
DataCallbackLookup (const ns3::CcnxNameComponents &name);
private:
ns3::UniformVariable m_rand; // nonce generator
CcnxFilterEntryContainer<DataCallback>::type m_dataCallbacks;
CcnxFilterEntryContainer<InterestCallback>::type m_interestCallbacks;
};
typedef boost::shared_ptr<CcnxWrapper> CcnxWrapperPtr;
} // Sync
#endif // SYNC_CCNX_WRAPPER_H