akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [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 | |
| 21 | #ifndef _SYNC_SOCKET_H |
| 22 | #define _SYNC_SOCKET_H |
| 23 | |
| 24 | #include <ndn-cpp-dev/face.hpp> |
| 25 | #include <ndn-cpp-dev/security/validator.hpp> |
| 26 | #include <ndn-cpp-dev/security/key-chain.hpp> |
| 27 | |
| 28 | #include "sync-logic.h" |
| 29 | #include "sync-seq-no.h" |
| 30 | |
| 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: |
| 45 | typedef ndn::function< void (const std::vector<MissingDataInfo> &, SyncSocket * ) > NewDataCallback; |
| 46 | typedef ndn::function< void (const std::string &/*prefix*/ ) > RemoveCallback; |
| 47 | |
| 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 | */ |
| 59 | SyncSocket (const ndn::Name &syncPrefix, |
| 60 | ndn::shared_ptr<ndn::Validator> validator, |
| 61 | ndn::shared_ptr<ndn::Face> face, |
| 62 | NewDataCallback dataCallback, |
| 63 | RemoveCallback rmCallback); |
| 64 | |
| 65 | ~SyncSocket (); |
| 66 | |
| 67 | bool |
| 68 | publishData(const ndn::Name &prefix, uint64_t session, const char *buf, size_t len, int freshness,uint64_t seq); |
| 69 | |
| 70 | void |
| 71 | remove (const ndn::Name &prefix) |
| 72 | { m_syncLogic.remove(prefix); } |
| 73 | |
| 74 | void |
| 75 | fetchData(const ndn::Name &prefix, const SeqNo &seq, const ndn::OnDataValidated& onValidated, int retry = 0); |
| 76 | |
| 77 | std::string |
| 78 | getRootDigest() |
| 79 | { return m_syncLogic.getRootDigest(); } |
| 80 | |
| 81 | uint64_t |
| 82 | getNextSeq (const ndn::Name &prefix, uint64_t session); |
| 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 | publishDataInternal(ndn::shared_ptr<ndn::Data> data, const ndn::Name &prefix, uint64_t session,uint64_t seq); |
| 96 | |
| 97 | void |
| 98 | passCallback(const std::vector<MissingDataInfo> &v) |
| 99 | { m_newDataCallback(v, this); } |
| 100 | |
| 101 | void |
| 102 | onData(const ndn::Interest& interest, ndn::Data& data, |
| 103 | const ndn::OnDataValidated& onValidated, |
| 104 | const ndn::OnDataValidationFailed& onValidationFailed); |
| 105 | |
| 106 | void |
| 107 | onDataTimeout(const ndn::Interest& interest, |
| 108 | int retry, |
| 109 | const ndn::OnDataValidated& onValidated, |
| 110 | const ndn::OnDataValidationFailed& onValidationFailed); |
| 111 | |
| 112 | void |
| 113 | onDataValidationFailed(const ndn::shared_ptr<const ndn::Data>& data); |
| 114 | |
| 115 | private: |
| 116 | typedef std::map<ndn::Name, SeqNo> SequenceLog; |
| 117 | NewDataCallback m_newDataCallback; |
| 118 | SequenceLog m_sequenceLog; |
| 119 | ndn::shared_ptr<ndn::Validator> m_validator; |
| 120 | ndn::shared_ptr<ndn::KeyChain> m_keyChain; |
| 121 | ndn::shared_ptr<ndn::Face> m_face; |
| 122 | ndn::shared_ptr<boost::asio::io_service> m_ioService; |
| 123 | SyncLogic m_syncLogic; |
| 124 | }; |
| 125 | |
| 126 | } // Sync |
| 127 | |
| 128 | #endif // SYNC_SOCKET_H |