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 | |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 24 | #include <ndn-cxx/face.hpp> |
| 25 | #include <ndn-cxx/security/validator.hpp> |
| 26 | #include <ndn-cxx/security/key-chain.hpp> |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 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: |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 45 | typedef ndn::function<void(const std::vector<MissingDataInfo> &, SyncSocket *)> NewDataCallback; |
| 46 | typedef ndn::function<void(const std::string &/*prefix*/)> RemoveCallback; |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 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 | */ |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 59 | SyncSocket (const ndn::Name &syncPrefix, |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 60 | ndn::shared_ptr<ndn::Validator> validator, |
| 61 | ndn::shared_ptr<ndn::Face> face, |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 62 | NewDataCallback dataCallback, |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 63 | RemoveCallback rmCallback); |
| 64 | |
| 65 | ~SyncSocket (); |
| 66 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 67 | bool |
| 68 | publishData(const ndn::Name &prefix, uint64_t session, const char *buf, size_t len, |
| 69 | int freshness,uint64_t seq); |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 70 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 71 | void |
| 72 | remove (const ndn::Name &prefix) |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 73 | { m_syncLogic.remove(prefix); } |
| 74 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 75 | void |
| 76 | fetchData(const ndn::Name &prefix, const SeqNo &seq, |
| 77 | const ndn::OnDataValidated& onValidated, int retry = 0); |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 78 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 79 | std::string |
| 80 | getRootDigest() |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 81 | { return m_syncLogic.getRootDigest(); } |
| 82 | |
| 83 | uint64_t |
| 84 | getNextSeq (const ndn::Name &prefix, uint64_t session); |
| 85 | |
| 86 | SyncLogic & |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 87 | getLogic () |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 88 | { return m_syncLogic; } |
| 89 | |
| 90 | // make this a static function so we don't have to create socket instance without |
| 91 | // knowing the local prefix. it's a wrong place for this function anyway |
| 92 | static std::string |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 93 | GetLocalPrefix (); |
| 94 | |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 95 | private: |
| 96 | void |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 97 | publishDataInternal(ndn::shared_ptr<ndn::Data> data, |
| 98 | const ndn::Name &prefix, uint64_t session,uint64_t seq); |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 99 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 100 | void |
| 101 | passCallback(const std::vector<MissingDataInfo> &v) |
| 102 | { |
| 103 | m_newDataCallback(v, this); |
| 104 | } |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 105 | |
| 106 | void |
| 107 | onData(const ndn::Interest& interest, ndn::Data& data, |
| 108 | const ndn::OnDataValidated& onValidated, |
| 109 | const ndn::OnDataValidationFailed& onValidationFailed); |
| 110 | |
| 111 | void |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 112 | onDataTimeout(const ndn::Interest& interest, |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 113 | int retry, |
| 114 | const ndn::OnDataValidated& onValidated, |
| 115 | const ndn::OnDataValidationFailed& onValidationFailed); |
| 116 | |
| 117 | void |
| 118 | onDataValidationFailed(const ndn::shared_ptr<const ndn::Data>& data); |
| 119 | |
| 120 | private: |
| 121 | typedef std::map<ndn::Name, SeqNo> SequenceLog; |
| 122 | NewDataCallback m_newDataCallback; |
| 123 | SequenceLog m_sequenceLog; |
| 124 | ndn::shared_ptr<ndn::Validator> m_validator; |
| 125 | ndn::shared_ptr<ndn::KeyChain> m_keyChain; |
| 126 | ndn::shared_ptr<ndn::Face> m_face; |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 127 | SyncLogic m_syncLogic; |
| 128 | }; |
| 129 | |
| 130 | } // Sync |
| 131 | |
| 132 | #endif // SYNC_SOCKET_H |