blob: 35b59c30f0a697ce46a960454f4f04210f708ae2 [file] [log] [blame]
akmhoque66e66182014-02-21 17:56:03 -06001/* -*- 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 Lehman0a7da612014-10-29 14:39:29 -050024#include "sync-common.h"
25
akmhoquec8a10f72014-04-25 18:42:55 -050026#include <ndn-cxx/face.hpp>
27#include <ndn-cxx/security/validator.hpp>
28#include <ndn-cxx/security/key-chain.hpp>
akmhoque66e66182014-02-21 17:56:03 -060029
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
38namespace Sync {
39
40/**
41 * \ingroup sync
42 * @brief A simple interface to interact with client code
43 */
44class SyncSocket
45{
46public:
akmhoque157b0a42014-05-13 00:26:37 -050047 typedef ndn::function<void(const std::vector<MissingDataInfo> &, SyncSocket *)> NewDataCallback;
48 typedef ndn::function<void(const std::string &/*prefix*/)> RemoveCallback;
akmhoque66e66182014-02-21 17:56:03 -060049
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 */
akmhoque157b0a42014-05-13 00:26:37 -050061 SyncSocket (const ndn::Name &syncPrefix,
akmhoque66e66182014-02-21 17:56:03 -060062 ndn::shared_ptr<ndn::Validator> validator,
63 ndn::shared_ptr<ndn::Face> face,
akmhoque157b0a42014-05-13 00:26:37 -050064 NewDataCallback dataCallback,
akmhoque66e66182014-02-21 17:56:03 -060065 RemoveCallback rmCallback);
66
67 ~SyncSocket ();
68
akmhoque157b0a42014-05-13 00:26:37 -050069 bool
70 publishData(const ndn::Name &prefix, uint64_t session, const char *buf, size_t len,
71 int freshness,uint64_t seq);
akmhoque66e66182014-02-21 17:56:03 -060072
akmhoque157b0a42014-05-13 00:26:37 -050073 void
74 remove (const ndn::Name &prefix)
akmhoque66e66182014-02-21 17:56:03 -060075 { m_syncLogic.remove(prefix); }
76
akmhoque157b0a42014-05-13 00:26:37 -050077 void
78 fetchData(const ndn::Name &prefix, const SeqNo &seq,
79 const ndn::OnDataValidated& onValidated, int retry = 0);
akmhoque66e66182014-02-21 17:56:03 -060080
akmhoque157b0a42014-05-13 00:26:37 -050081 std::string
82 getRootDigest()
akmhoque66e66182014-02-21 17:56:03 -060083 { return m_syncLogic.getRootDigest(); }
84
85 uint64_t
86 getNextSeq (const ndn::Name &prefix, uint64_t session);
87
88 SyncLogic &
akmhoque157b0a42014-05-13 00:26:37 -050089 getLogic ()
akmhoque66e66182014-02-21 17:56:03 -060090 { 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
akmhoque157b0a42014-05-13 00:26:37 -050095 GetLocalPrefix ();
96
akmhoque66e66182014-02-21 17:56:03 -060097private:
98 void
akmhoque157b0a42014-05-13 00:26:37 -050099 publishDataInternal(ndn::shared_ptr<ndn::Data> data,
100 const ndn::Name &prefix, uint64_t session,uint64_t seq);
akmhoque66e66182014-02-21 17:56:03 -0600101
akmhoque157b0a42014-05-13 00:26:37 -0500102 void
103 passCallback(const std::vector<MissingDataInfo> &v)
104 {
105 m_newDataCallback(v, this);
106 }
akmhoque66e66182014-02-21 17:56:03 -0600107
108 void
Alexander Afanasyev1de901f2017-03-09 12:43:57 -0800109 onData(const ndn::Interest& interest, const ndn::Data& data,
akmhoque66e66182014-02-21 17:56:03 -0600110 const ndn::OnDataValidated& onValidated,
111 const ndn::OnDataValidationFailed& onValidationFailed);
112
113 void
akmhoque157b0a42014-05-13 00:26:37 -0500114 onDataTimeout(const ndn::Interest& interest,
akmhoque66e66182014-02-21 17:56:03 -0600115 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
122private:
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;
akmhoque66e66182014-02-21 17:56:03 -0600129 SyncLogic m_syncLogic;
130};
131
132} // Sync
133
134#endif // SYNC_SOCKET_H