Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2012 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Yingdi Yu <yingdi@cs.ucla.edu> |
| 19 | */ |
| 20 | |
Alexander Afanasyev | 531803b | 2014-02-05 15:57:35 -0800 | [diff] [blame^] | 21 | #ifndef _SYNC_SOCKET_H |
| 22 | #define _SYNC_SOCKET_H |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 23 | |
Yingdi Yu | e815471 | 2014-01-21 10:20:14 -0800 | [diff] [blame] | 24 | #include <ndn-cpp-dev/face.hpp> |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 25 | #include <ndn-cpp-dev/security/validator.hpp> |
Yingdi Yu | e815471 | 2014-01-21 10:20:14 -0800 | [diff] [blame] | 26 | #include <ndn-cpp-dev/security/key-chain.hpp> |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 27 | |
Alexander Afanasyev | 531803b | 2014-02-05 15:57:35 -0800 | [diff] [blame^] | 28 | #include "sync-logic.h" |
| 29 | #include "sync-seq-no.h" |
| 30 | |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 31 | #include <utility> |
| 32 | #include <map> |
| 33 | #include <vector> |
| 34 | #include <sstream> |
| 35 | |
| 36 | namespace Sync { |
| 37 | |
| 38 | /** |
| 39 | * \ingroup sync |
| 40 | * @brief A simple interface to interact with client code |
| 41 | */ |
| 42 | class SyncSocket |
| 43 | { |
| 44 | public: |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 45 | typedef ndn::function< void (const std::vector<MissingDataInfo> &, SyncSocket * ) > NewDataCallback; |
| 46 | typedef ndn::function< void (const std::string &/*prefix*/ ) > RemoveCallback; |
| 47 | |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 48 | /** |
| 49 | * @brief the constructor for SyncAppSocket; the parameter syncPrefix |
| 50 | * should be passed to the constructor of m_syncAppWrapper; the other |
| 51 | * parameter should be passed to the constructor of m_fetcher; furthermore, |
| 52 | * the fetch function of m_fetcher should be a second paramter passed to |
| 53 | * the constructor of m_syncAppWrapper, so that m_syncAppWrapper can tell |
| 54 | * m_fetcher to fetch the actual app data after it learns the names |
| 55 | * |
| 56 | * @param syncPrefix the name prefix for Sync Interest |
| 57 | * @param dataCallback the callback to process data |
| 58 | */ |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 59 | SyncSocket (const ndn::Name &syncPrefix, |
| 60 | ndn::shared_ptr<ndn::Validator> validator, |
| 61 | ndn::shared_ptr<ndn::Face> face, |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 62 | NewDataCallback dataCallback, |
| 63 | RemoveCallback rmCallback); |
| 64 | |
| 65 | ~SyncSocket (); |
| 66 | |
| 67 | bool |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 68 | publishData(const ndn::Name &prefix, uint64_t session, const char *buf, size_t len, int freshness); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 69 | |
| 70 | void |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 71 | remove (const ndn::Name &prefix) |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 72 | { m_syncLogic.remove(prefix); } |
| 73 | |
| 74 | void |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 75 | fetchData(const ndn::Name &prefix, const SeqNo &seq, const ndn::OnDataValidated& onValidated, int retry = 0); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 76 | |
| 77 | std::string |
| 78 | getRootDigest() |
| 79 | { return m_syncLogic.getRootDigest(); } |
| 80 | |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 81 | uint64_t |
| 82 | getNextSeq (const ndn::Name &prefix, uint64_t session); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 83 | |
| 84 | SyncLogic & |
| 85 | getLogic () |
| 86 | { return m_syncLogic; } |
| 87 | |
| 88 | // make this a static function so we don't have to create socket instance without |
| 89 | // knowing the local prefix. it's a wrong place for this function anyway |
| 90 | static std::string |
| 91 | GetLocalPrefix (); |
| 92 | |
| 93 | private: |
| 94 | void |
| 95 | passCallback(const std::vector<MissingDataInfo> &v) |
| 96 | { m_newDataCallback(v, this); } |
| 97 | |
| 98 | void |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 99 | onData(const ndn::shared_ptr<const ndn::Interest>& interest, |
| 100 | const ndn::shared_ptr<ndn::Data>& data, |
| 101 | const ndn::OnDataValidated& onValidated, |
| 102 | const ndn::OnDataValidationFailed& onValidationFailed); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 103 | |
| 104 | void |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 105 | onDataTimeout(const ndn::shared_ptr<const ndn::Interest>& interest, |
| 106 | int retry, |
| 107 | const ndn::OnDataValidated& onValidated, |
| 108 | const ndn::OnDataValidationFailed& onValidationFailed); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 109 | |
| 110 | void |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 111 | onDataValidationFailed(const ndn::shared_ptr<const ndn::Data>& data); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 112 | |
| 113 | private: |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 114 | typedef std::map<ndn::Name, SeqNo> SequenceLog; |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 115 | NewDataCallback m_newDataCallback; |
| 116 | SequenceLog m_sequenceLog; |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 117 | ndn::shared_ptr<ndn::Validator> m_validator; |
| 118 | ndn::shared_ptr<ndn::KeyChain> m_keyChain; |
| 119 | ndn::shared_ptr<ndn::Face> m_face; |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 120 | SyncLogic m_syncLogic; |
| 121 | }; |
| 122 | |
| 123 | } // Sync |
| 124 | |
| 125 | #endif // SYNC_SOCKET_H |