blob: 35fd8ed0c3530d6609064cf0ed23948e2df8042f [file] [log] [blame]
Yingdi Yu43e71612013-10-30 22:19:31 -07001/* -*- 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 Afanasyev531803b2014-02-05 15:57:35 -080021#ifndef _SYNC_SOCKET_H
22#define _SYNC_SOCKET_H
Yingdi Yu280bb962014-01-30 09:52:43 -080023
Yingdi Yue8154712014-01-21 10:20:14 -080024#include <ndn-cpp-dev/face.hpp>
Yingdi Yu280bb962014-01-30 09:52:43 -080025#include <ndn-cpp-dev/security/validator.hpp>
Yingdi Yue8154712014-01-21 10:20:14 -080026#include <ndn-cpp-dev/security/key-chain.hpp>
Yingdi Yu280bb962014-01-30 09:52:43 -080027
Alexander Afanasyev531803b2014-02-05 15:57:35 -080028#include "sync-logic.h"
29#include "sync-seq-no.h"
30
Yingdi Yu43e71612013-10-30 22:19:31 -070031#include <utility>
32#include <map>
33#include <vector>
34#include <sstream>
35
36namespace Sync {
37
38/**
39 * \ingroup sync
40 * @brief A simple interface to interact with client code
41 */
42class SyncSocket
43{
44public:
Yingdi Yu280bb962014-01-30 09:52:43 -080045 typedef ndn::function< void (const std::vector<MissingDataInfo> &, SyncSocket * ) > NewDataCallback;
46 typedef ndn::function< void (const std::string &/*prefix*/ ) > RemoveCallback;
47
Yingdi Yu43e71612013-10-30 22:19:31 -070048 /**
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 Yu280bb962014-01-30 09:52:43 -080059 SyncSocket (const ndn::Name &syncPrefix,
60 ndn::shared_ptr<ndn::Validator> validator,
61 ndn::shared_ptr<ndn::Face> face,
Yingdi Yu43e71612013-10-30 22:19:31 -070062 NewDataCallback dataCallback,
63 RemoveCallback rmCallback);
64
65 ~SyncSocket ();
66
67 bool
Yingdi Yu280bb962014-01-30 09:52:43 -080068 publishData(const ndn::Name &prefix, uint64_t session, const char *buf, size_t len, int freshness);
Yingdi Yu43e71612013-10-30 22:19:31 -070069
70 void
Yingdi Yu6d638f02014-01-24 11:01:21 -080071 remove (const ndn::Name &prefix)
Yingdi Yu43e71612013-10-30 22:19:31 -070072 { m_syncLogic.remove(prefix); }
73
74 void
Yingdi Yu280bb962014-01-30 09:52:43 -080075 fetchData(const ndn::Name &prefix, const SeqNo &seq, const ndn::OnDataValidated& onValidated, int retry = 0);
Yingdi Yu43e71612013-10-30 22:19:31 -070076
77 std::string
78 getRootDigest()
79 { return m_syncLogic.getRootDigest(); }
80
Yingdi Yu280bb962014-01-30 09:52:43 -080081 uint64_t
82 getNextSeq (const ndn::Name &prefix, uint64_t session);
Yingdi Yu43e71612013-10-30 22:19:31 -070083
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
93private:
94 void
95 passCallback(const std::vector<MissingDataInfo> &v)
96 { m_newDataCallback(v, this); }
97
98 void
Yingdi Yu280bb962014-01-30 09:52:43 -080099 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 Yu46c9f1a2013-12-18 15:15:46 +0800103
104 void
Yingdi Yu280bb962014-01-30 09:52:43 -0800105 onDataTimeout(const ndn::shared_ptr<const ndn::Interest>& interest,
106 int retry,
107 const ndn::OnDataValidated& onValidated,
108 const ndn::OnDataValidationFailed& onValidationFailed);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800109
110 void
Yingdi Yu280bb962014-01-30 09:52:43 -0800111 onDataValidationFailed(const ndn::shared_ptr<const ndn::Data>& data);
Yingdi Yu43e71612013-10-30 22:19:31 -0700112
113private:
Yingdi Yu6d638f02014-01-24 11:01:21 -0800114 typedef std::map<ndn::Name, SeqNo> SequenceLog;
Yingdi Yu43e71612013-10-30 22:19:31 -0700115 NewDataCallback m_newDataCallback;
116 SequenceLog m_sequenceLog;
Yingdi Yu280bb962014-01-30 09:52:43 -0800117 ndn::shared_ptr<ndn::Validator> m_validator;
118 ndn::shared_ptr<ndn::KeyChain> m_keyChain;
119 ndn::shared_ptr<ndn::Face> m_face;
Yingdi Yu43e71612013-10-30 22:19:31 -0700120 SyncLogic m_syncLogic;
121};
122
123} // Sync
124
125#endif // SYNC_SOCKET_H