blob: 25c3aae872cc279defe0f075f3d7e973cc994af2 [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: Zhenkai Zhu <zhenkai@cs.ucla.edu>
19 * Chaoyi Bian <bcy@pku.edu.cn>
20 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
21 * Yingdi Yu <yingdi@cs.ucla.edu>
22 */
23
24#ifndef SYNC_LOGIC_H
25#define SYNC_LOGIC_H
26
27#include <boost/random.hpp>
28#include <memory>
29#include <map>
30
akmhoquec8a10f72014-04-25 18:42:55 -050031#include <ndn-cxx/face.hpp>
32#include <ndn-cxx/security/validator.hpp>
33#include <ndn-cxx/security/key-chain.hpp>
34#include <ndn-cxx/util/scheduler.hpp>
akmhoque66e66182014-02-21 17:56:03 -060035
36#include "sync-interest-table.h"
37#include "sync-diff-state.h"
38#include "sync-full-state.h"
39#include "sync-std-name-info.h"
40
41#include "sync-diff-state-container.h"
42
43#ifdef _DEBUG
44#ifdef HAVE_LOG4CXX
45#include <log4cxx/logger.h>
46#endif
47#endif
48
49namespace Sync {
50
51struct MissingDataInfo {
52 std::string prefix;
53 SeqNo low;
54 SeqNo high;
55};
56
57/**
58 * \ingroup sync
59 * @brief A wrapper for SyncApp, which handles ccnx related things (process
60 * interests and data)
61 */
62
63class SyncLogic
64{
65public:
akmhoque66e66182014-02-21 17:56:03 -060066 typedef boost::function< void (const std::vector<MissingDataInfo> & ) > LogicUpdateCallback;
67 typedef boost::function< void (const std::string &/*prefix*/ ) > LogicRemoveCallback;
68 typedef boost::function< void (const std::string &)> LogicPerBranchCallback;
69
70 /**
71 * @brief Constructor
72 * @param syncPrefix the name prefix to use for the Sync Interest
73 * @param onUpdate function that will be called when new state is detected
74 * @param onRemove function that will be called when state is removed
75 * @param ccnxHandle ccnx handle
76 * the app data when new remote names are learned
77 */
78 SyncLogic (const ndn::Name& syncPrefix,
79 ndn::shared_ptr<ndn::Validator> validator,
80 ndn::shared_ptr<ndn::Face> face,
81 LogicUpdateCallback onUpdate,
82 LogicRemoveCallback onRemove);
83
84 SyncLogic (const ndn::Name& syncPrefix,
85 ndn::shared_ptr<ndn::Validator> validator,
86 ndn::shared_ptr<ndn::Face> face,
87 LogicPerBranchCallback onUpdateBranch);
88
89 ~SyncLogic ();
90
91 /**
92 * a wrapper for the same func in SyncApp
93 */
94 void addLocalNames (const ndn::Name &prefix, uint64_t session, uint64_t seq);
95
96 /**
97 * @brief remove a participant's subtree from the sync tree
98 * @param prefix the name prefix for the participant
99 */
100 void remove (const ndn::Name &prefix);
101
102 std::string
103 getRootDigest();
104
105#ifdef _DEBUG
106 ndn::Scheduler &
107 getScheduler () { return m_scheduler; }
108#endif
109
110 void
111 printState () const;
112
113 std::map<std::string, bool>
114 getBranchPrefixes() const;
115
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700116private:
akmhoque66e66182014-02-21 17:56:03 -0600117 void
118 delayedChecksLoop ();
119
120 void
121 onSyncInterest (const ndn::Name& prefix, const ndn::Interest& interest);
122
123 void
akmhoque157b0a42014-05-13 00:26:37 -0500124 onSyncRegisterSucceed(const ndn::Name& prefix);
125
126 void
akmhoque66e66182014-02-21 17:56:03 -0600127 onSyncRegisterFailed(const ndn::Name& prefix, const std::string& msg);
128
129 void
130 onSyncData(const ndn::Interest& interest, ndn::Data& data);
131
132 void
133 onSyncTimeout(const ndn::Interest& interest);
134
135 void
136 onSyncDataValidationFailed(const ndn::shared_ptr<const ndn::Data>& data);
137
138 void
139 onSyncDataValidated(const ndn::shared_ptr<const ndn::Data>& data);
140
141 void
142 processSyncInterest (const ndn::Name &name,
143 DigestConstPtr digest, bool timedProcessing=false);
144
145 void
146 processSyncData (const ndn::Name &name,
147 DigestConstPtr digest, const char *wireData, size_t len);
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700148
akmhoque66e66182014-02-21 17:56:03 -0600149 void
150 processSyncRecoveryInterest (const ndn::Name &name,
151 DigestConstPtr digest);
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700152
153 void
akmhoque66e66182014-02-21 17:56:03 -0600154 insertToDiffLog (DiffStatePtr diff);
155
156 void
157 satisfyPendingSyncInterests (DiffStateConstPtr diff);
158
159 boost::tuple<DigestConstPtr, std::string>
160 convertNameToDigestAndType (const ndn::Name &name);
161
162 void
163 sendSyncInterest ();
164
165 void
166 sendSyncRecoveryInterests (DigestConstPtr digest);
167
168 void
169 sendSyncData (const ndn::Name &name,
170 DigestConstPtr digest, StateConstPtr state);
171
172 void
173 sendSyncData (const ndn::Name &name,
174 DigestConstPtr digest, SyncStateMsg &msg);
175
176 size_t
177 getNumberOfBranches () const;
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700178
akmhoque66e66182014-02-21 17:56:03 -0600179private:
180 FullStatePtr m_state;
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700181
akmhoque66e66182014-02-21 17:56:03 -0600182 DiffStateContainer m_log;
183
184 ndn::Name m_outstandingInterestName;
185 SyncInterestTable m_syncInterestTable;
186
187 ndn::Name m_syncPrefix;
188 LogicUpdateCallback m_onUpdate;
189 LogicRemoveCallback m_onRemove;
190 LogicPerBranchCallback m_onUpdateBranch;
191 bool m_perBranch;
192 ndn::ptr_lib::shared_ptr<ndn::Validator> m_validator;
193 ndn::ptr_lib::shared_ptr<ndn::KeyChain> m_keyChain;
194 ndn::ptr_lib::shared_ptr<ndn::Face> m_face;
195 const ndn::RegisteredPrefixId* m_syncRegisteredPrefixId;
196
197 ndn::Scheduler m_scheduler;
198
199 boost::mt19937 m_randomGenerator;
200 boost::variate_generator<boost::mt19937&, boost::uniform_int<> > m_rangeUniformRandom;
201 boost::variate_generator<boost::mt19937&, boost::uniform_int<> > m_reexpressionJitter;
202
akmhoque66e66182014-02-21 17:56:03 -0600203
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700204 static const int m_unknownDigestStoreTime = 10; // seconds
205 static const int m_syncResponseFreshness; // MUST BE dividable by 1000!!!
206 static const int m_syncInterestReexpress; // seconds
akmhoque66e66182014-02-21 17:56:03 -0600207 static const int m_defaultRecoveryRetransmitInterval = 200; // milliseconds
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700208
209
akmhoque66e66182014-02-21 17:56:03 -0600210 uint32_t m_recoveryRetransmissionInterval; // milliseconds
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700211
akmhoque66e66182014-02-21 17:56:03 -0600212 ndn::EventId m_delayedInterestProcessingId;
213 ndn::EventId m_reexpressingInterestId;
214 ndn::EventId m_reexpressingRecoveryInterestId;
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700215
akmhoque66e66182014-02-21 17:56:03 -0600216 std::string m_instanceId;
217 static int m_instanceCounter;
218};
219
220
221} // Sync
222
223#endif // SYNC_APP_WRAPPER_H