blob: ee02ae8ea5063eb8b7ba81fce6e5e0481eccf95e [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:
Yingdi Yu51c80252014-02-10 19:32:05 -080094 void
95 publishDataInternal(ndn::shared_ptr<ndn::Data> data, const ndn::Name &prefix, uint64_t session);
96
Yingdi Yu43e71612013-10-30 22:19:31 -070097 void
98 passCallback(const std::vector<MissingDataInfo> &v)
99 { m_newDataCallback(v, this); }
100
101 void
Yingdi Yu51c80252014-02-10 19:32:05 -0800102 onData(const ndn::Interest& interest, ndn::Data& data,
Yingdi Yu280bb962014-01-30 09:52:43 -0800103 const ndn::OnDataValidated& onValidated,
104 const ndn::OnDataValidationFailed& onValidationFailed);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800105
106 void
Yingdi Yu51c80252014-02-10 19:32:05 -0800107 onDataTimeout(const ndn::Interest& interest,
Yingdi Yu280bb962014-01-30 09:52:43 -0800108 int retry,
109 const ndn::OnDataValidated& onValidated,
110 const ndn::OnDataValidationFailed& onValidationFailed);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800111
112 void
Yingdi Yu280bb962014-01-30 09:52:43 -0800113 onDataValidationFailed(const ndn::shared_ptr<const ndn::Data>& data);
Yingdi Yu43e71612013-10-30 22:19:31 -0700114
115private:
Yingdi Yu6d638f02014-01-24 11:01:21 -0800116 typedef std::map<ndn::Name, SeqNo> SequenceLog;
Yingdi Yu43e71612013-10-30 22:19:31 -0700117 NewDataCallback m_newDataCallback;
118 SequenceLog m_sequenceLog;
Yingdi Yu280bb962014-01-30 09:52:43 -0800119 ndn::shared_ptr<ndn::Validator> m_validator;
120 ndn::shared_ptr<ndn::KeyChain> m_keyChain;
121 ndn::shared_ptr<ndn::Face> m_face;
Yingdi Yu51c80252014-02-10 19:32:05 -0800122 ndn::shared_ptr<boost::asio::io_service> m_ioService;
Yingdi Yu43e71612013-10-30 22:19:31 -0700123 SyncLogic m_syncLogic;
124};
125
126} // Sync
127
128#endif // SYNC_SOCKET_H