blob: 170fde5617413ccb1971091c9425de3259c8d202 [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: Yingdi Yu <yingdi@cs.ucla.edu>
*/
#ifndef SYNC_SOCKET_H
#define SYNC_SOCKET_H
#include "sync-logic.h"
#include <boost/function.hpp>
#include <boost/unordered_map.hpp>
#include "sync-seq-no.h"
#include <ndn-cpp/face.hpp>
#include <ndn-cpp/security/identity/identity-manager.hpp>
#include <utility>
#include <map>
#include <vector>
#include <sstream>
namespace Sync {
/**
* \ingroup sync
* @brief A simple interface to interact with client code
*/
class SyncSocket
{
public:
typedef boost::function< void (const std::vector<MissingDataInfo> &, SyncSocket * ) > NewDataCallback;
typedef boost::function< void ( const std::string &/*prefix*/ ) > RemoveCallback;
/**
* @brief the constructor for SyncAppSocket; the parameter syncPrefix
* should be passed to the constructor of m_syncAppWrapper; the other
* parameter should be passed to the constructor of m_fetcher; furthermore,
* the fetch function of m_fetcher should be a second paramter passed to
* the constructor of m_syncAppWrapper, so that m_syncAppWrapper can tell
* m_fetcher to fetch the actual app data after it learns the names
*
* @param syncPrefix the name prefix for Sync Interest
* @param dataCallback the callback to process data
*/
SyncSocket (const std::string &syncPrefix,
ndn::ptr_lib::shared_ptr<SyncPolicyManager> syncPolicyManager,
NewDataCallback dataCallback,
RemoveCallback rmCallback);
~SyncSocket ();
bool
publishData(const std::string &prefix, uint32_t session, const char *buf, size_t len, int freshness);
void
remove (const std::string &prefix)
{ m_syncLogic.remove(prefix); }
void
fetchData(const std::string &prefix, const SeqNo &seq, const ndn::OnVerified& onVerified, int retry = 0);
std::string
getRootDigest()
{ return m_syncLogic.getRootDigest(); }
uint32_t
getNextSeq (const std::string &prefix, uint32_t session);
SyncLogic &
getLogic ()
{ return m_syncLogic; }
// make this a static function so we don't have to create socket instance without
// knowing the local prefix. it's a wrong place for this function anyway
static std::string
GetLocalPrefix ();
private:
void
connectToDaemon();
void
onConnectionData(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
const ndn::ptr_lib::shared_ptr<ndn::Data>& data);
void
onConnectionDataTimeout(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest);
void
passCallback(const std::vector<MissingDataInfo> &v)
{ m_newDataCallback(v, this); }
void
onChatCert(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
const ndn::ptr_lib::shared_ptr<ndn::Data>& cert,
ndn::ptr_lib::shared_ptr<ndn::ValidationRequest> previousStep);
void
onChatCertTimeout(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
const ndn::OnVerifyFailed& onVerifyFailed,
const ndn::ptr_lib::shared_ptr<ndn::Data>& data,
ndn::ptr_lib::shared_ptr<ndn::ValidationRequest> nextStep);
void
onChatData(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
const ndn::ptr_lib::shared_ptr<ndn::Data>& data,
const ndn::OnVerified& onVerified,
const ndn::OnVerifyFailed& onVerifyFailed);
void
onChatDataTimeout(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
int retry,
const ndn::OnVerified& onVerified,
const ndn::OnVerifyFailed& onVerifyFailed);
void
onChatDataVerifyFailed(const ndn::ptr_lib::shared_ptr<ndn::Data>& data);
private:
typedef boost::unordered_map<std::string, SeqNo> SequenceLog;
NewDataCallback m_newDataCallback;
SequenceLog m_sequenceLog;
ndn::ptr_lib::shared_ptr<SyncPolicyManager> m_syncPolicyManager;
ndn::ptr_lib::shared_ptr<ndn::IdentityManager> m_identityManager;
ndn::ptr_lib::shared_ptr<ndn::Face> m_face;
ndn::ptr_lib::shared_ptr<ndn::Transport> m_transport;
SyncLogic m_syncLogic;
};
} // Sync
#endif // SYNC_SOCKET_H