blob: 72df7abe63b3cbac29979abad5d06bf9cc8e819f [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"
Yingdi Yu3da10fe2014-02-27 16:37:34 -080030#include "sync-validator.h"
Alexander Afanasyev531803b2014-02-05 15:57:35 -080031
Yingdi Yu43e71612013-10-30 22:19:31 -070032#include <utility>
33#include <map>
34#include <vector>
35#include <sstream>
36
37namespace Sync {
38
39/**
40 * \ingroup sync
41 * @brief A simple interface to interact with client code
42 */
43class SyncSocket
44{
45public:
Yingdi Yu280bb962014-01-30 09:52:43 -080046 typedef ndn::function< void (const std::vector<MissingDataInfo> &, SyncSocket * ) > NewDataCallback;
47 typedef ndn::function< void (const std::string &/*prefix*/ ) > RemoveCallback;
48
Yingdi Yu0eee6002014-02-11 15:54:17 -080049 SyncSocket (const ndn::Name& syncPrefix,
Yingdi Yu3da10fe2014-02-27 16:37:34 -080050 const ndn::Name& dataPrefix,
51 uint64_t dataSession,
52 const ndn::IdentityCertificate& myCertificate,
53 ndn::shared_ptr<ndn::SecRuleRelative> dataRule,
Yingdi Yu280bb962014-01-30 09:52:43 -080054 ndn::shared_ptr<ndn::Face> face,
Yingdi Yu43e71612013-10-30 22:19:31 -070055 NewDataCallback dataCallback,
56 RemoveCallback rmCallback);
57
58 ~SyncSocket ();
59
Yingdi Yu3da10fe2014-02-27 16:37:34 -080060 void
61 publishData(const uint8_t* buf, size_t len, int freshness, bool isCert = false);
Yingdi Yu43e71612013-10-30 22:19:31 -070062
63 void
Yingdi Yu6d638f02014-01-24 11:01:21 -080064 remove (const ndn::Name &prefix)
Yingdi Yu3da10fe2014-02-27 16:37:34 -080065 {
66 m_syncLogic.remove(prefix);
67 }
Yingdi Yu43e71612013-10-30 22:19:31 -070068
69 void
Yingdi Yu280bb962014-01-30 09:52:43 -080070 fetchData(const ndn::Name &prefix, const SeqNo &seq, const ndn::OnDataValidated& onValidated, int retry = 0);
Yingdi Yu43e71612013-10-30 22:19:31 -070071
72 std::string
73 getRootDigest()
Yingdi Yu3da10fe2014-02-27 16:37:34 -080074 {
75 return m_syncLogic.getRootDigest();
76 }
Yingdi Yu43e71612013-10-30 22:19:31 -070077
Yingdi Yu280bb962014-01-30 09:52:43 -080078 uint64_t
Yingdi Yu3da10fe2014-02-27 16:37:34 -080079 getNextSeq (const ndn::Name &prefix, uint64_t session)
80 {
81 SequenceLog::iterator i = m_sequenceLog.find (prefix);
82
83 if (i != m_sequenceLog.end ())
84 {
85 SeqNo s = i->second;
86 if (s.getSession() == session)
87 return s.getSeq();
88 }
89 return 0;
90 }
Yingdi Yu43e71612013-10-30 22:19:31 -070091
92 SyncLogic &
93 getLogic ()
Yingdi Yu3da10fe2014-02-27 16:37:34 -080094 {
95 return m_syncLogic;
96 }
Yingdi Yu43e71612013-10-30 22:19:31 -070097
Yingdi Yu3da10fe2014-02-27 16:37:34 -080098 void
99 addParticipant(const ndn::IdentityCertificate& introducee)
100 {
101 ndn::shared_ptr<const IntroCertificate> introCert = m_syncValidator->addParticipant(introducee);
102 }
103
104 // // make this a static function so we don't have to create socket instance without
105 // // knowing the local prefix. it's a wrong place for this function anyway
106 // static std::string
107 // GetLocalPrefix ();
Yingdi Yu43e71612013-10-30 22:19:31 -0700108
109private:
Yingdi Yu51c80252014-02-10 19:32:05 -0800110 void
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800111 publishDataInternal(ndn::shared_ptr<ndn::Data> data,
112 const ndn::Name &prefix,
113 uint64_t session,
114 bool isCert);
Yingdi Yu51c80252014-02-10 19:32:05 -0800115
Yingdi Yu43e71612013-10-30 22:19:31 -0700116 void
117 passCallback(const std::vector<MissingDataInfo> &v)
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800118 {
119 m_newDataCallback(v, this);
120 }
Yingdi Yu43e71612013-10-30 22:19:31 -0700121
122 void
Yingdi Yu51c80252014-02-10 19:32:05 -0800123 onData(const ndn::Interest& interest, ndn::Data& data,
Yingdi Yu280bb962014-01-30 09:52:43 -0800124 const ndn::OnDataValidated& onValidated,
125 const ndn::OnDataValidationFailed& onValidationFailed);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800126
127 void
Yingdi Yu51c80252014-02-10 19:32:05 -0800128 onDataTimeout(const ndn::Interest& interest,
Yingdi Yu280bb962014-01-30 09:52:43 -0800129 int retry,
130 const ndn::OnDataValidated& onValidated,
131 const ndn::OnDataValidationFailed& onValidationFailed);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800132
133 void
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800134 onDataValidated(const ndn::shared_ptr<const ndn::Data>& data,
135 size_t interestNameSize,
136 const ndn::OnDataValidated& onValidated);
137
138 void
139 onDataValidationFailed(const ndn::shared_ptr<const ndn::Data>& data,
140 const std::string& failureInfo);
Yingdi Yu43e71612013-10-30 22:19:31 -0700141
142private:
Yingdi Yu6d638f02014-01-24 11:01:21 -0800143 typedef std::map<ndn::Name, SeqNo> SequenceLog;
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800144
145 ndn::Name m_dataPrefix;
146 uint64_t m_dataSession;
Yingdi Yu43e71612013-10-30 22:19:31 -0700147 NewDataCallback m_newDataCallback;
148 SequenceLog m_sequenceLog;
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800149 ndn::IdentityCertificate m_myCertificate;
150 ndn::KeyChain m_keyChain;
Yingdi Yu280bb962014-01-30 09:52:43 -0800151 ndn::shared_ptr<ndn::Face> m_face;
Yingdi Yu51c80252014-02-10 19:32:05 -0800152 ndn::shared_ptr<boost::asio::io_service> m_ioService;
Yingdi Yu3da10fe2014-02-27 16:37:34 -0800153 ndn::shared_ptr<SyncValidator> m_syncValidator;
Yingdi Yu43e71612013-10-30 22:19:31 -0700154 SyncLogic m_syncLogic;
155};
156
157} // Sync
158
159#endif // SYNC_SOCKET_H