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 | |
Vince Lehman | 0a7da61 | 2014-10-29 14:39:29 -0500 | [diff] [blame^] | 24 | #include "sync-common.h" |
| 25 | |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 26 | #include <ndn-cxx/face.hpp> |
| 27 | #include <ndn-cxx/security/validator.hpp> |
| 28 | #include <ndn-cxx/security/key-chain.hpp> |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 29 | |
| 30 | #include "sync-logic.h" |
| 31 | #include "sync-seq-no.h" |
| 32 | |
| 33 | #include <utility> |
| 34 | #include <map> |
| 35 | #include <vector> |
| 36 | #include <sstream> |
| 37 | |
| 38 | namespace Sync { |
| 39 | |
| 40 | /** |
| 41 | * \ingroup sync |
| 42 | * @brief A simple interface to interact with client code |
| 43 | */ |
| 44 | class SyncSocket |
| 45 | { |
| 46 | public: |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 47 | typedef ndn::function<void(const std::vector<MissingDataInfo> &, SyncSocket *)> NewDataCallback; |
| 48 | typedef ndn::function<void(const std::string &/*prefix*/)> RemoveCallback; |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * @brief the constructor for SyncAppSocket; the parameter syncPrefix |
| 52 | * should be passed to the constructor of m_syncAppWrapper; the other |
| 53 | * parameter should be passed to the constructor of m_fetcher; furthermore, |
| 54 | * the fetch function of m_fetcher should be a second paramter passed to |
| 55 | * the constructor of m_syncAppWrapper, so that m_syncAppWrapper can tell |
| 56 | * m_fetcher to fetch the actual app data after it learns the names |
| 57 | * |
| 58 | * @param syncPrefix the name prefix for Sync Interest |
| 59 | * @param dataCallback the callback to process data |
| 60 | */ |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 61 | SyncSocket (const ndn::Name &syncPrefix, |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 62 | ndn::shared_ptr<ndn::Validator> validator, |
| 63 | ndn::shared_ptr<ndn::Face> face, |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 64 | NewDataCallback dataCallback, |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 65 | RemoveCallback rmCallback); |
| 66 | |
| 67 | ~SyncSocket (); |
| 68 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 69 | bool |
| 70 | publishData(const ndn::Name &prefix, uint64_t session, const char *buf, size_t len, |
| 71 | int freshness,uint64_t seq); |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 72 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 73 | void |
| 74 | remove (const ndn::Name &prefix) |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 75 | { m_syncLogic.remove(prefix); } |
| 76 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 77 | void |
| 78 | fetchData(const ndn::Name &prefix, const SeqNo &seq, |
| 79 | const ndn::OnDataValidated& onValidated, int retry = 0); |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 80 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 81 | std::string |
| 82 | getRootDigest() |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 83 | { return m_syncLogic.getRootDigest(); } |
| 84 | |
| 85 | uint64_t |
| 86 | getNextSeq (const ndn::Name &prefix, uint64_t session); |
| 87 | |
| 88 | SyncLogic & |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 89 | getLogic () |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 90 | { return m_syncLogic; } |
| 91 | |
| 92 | // make this a static function so we don't have to create socket instance without |
| 93 | // knowing the local prefix. it's a wrong place for this function anyway |
| 94 | static std::string |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 95 | GetLocalPrefix (); |
| 96 | |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 97 | private: |
| 98 | void |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 99 | publishDataInternal(ndn::shared_ptr<ndn::Data> data, |
| 100 | const ndn::Name &prefix, uint64_t session,uint64_t seq); |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 101 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 102 | void |
| 103 | passCallback(const std::vector<MissingDataInfo> &v) |
| 104 | { |
| 105 | m_newDataCallback(v, this); |
| 106 | } |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 107 | |
| 108 | void |
| 109 | onData(const ndn::Interest& interest, ndn::Data& data, |
| 110 | const ndn::OnDataValidated& onValidated, |
| 111 | const ndn::OnDataValidationFailed& onValidationFailed); |
| 112 | |
| 113 | void |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 114 | onDataTimeout(const ndn::Interest& interest, |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 115 | int retry, |
| 116 | const ndn::OnDataValidated& onValidated, |
| 117 | const ndn::OnDataValidationFailed& onValidationFailed); |
| 118 | |
| 119 | void |
| 120 | onDataValidationFailed(const ndn::shared_ptr<const ndn::Data>& data); |
| 121 | |
| 122 | private: |
| 123 | typedef std::map<ndn::Name, SeqNo> SequenceLog; |
| 124 | NewDataCallback m_newDataCallback; |
| 125 | SequenceLog m_sequenceLog; |
| 126 | ndn::shared_ptr<ndn::Validator> m_validator; |
| 127 | ndn::shared_ptr<ndn::KeyChain> m_keyChain; |
| 128 | ndn::shared_ptr<ndn::Face> m_face; |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 129 | SyncLogic m_syncLogic; |
| 130 | }; |
| 131 | |
| 132 | } // Sync |
| 133 | |
| 134 | #endif // SYNC_SOCKET_H |