blob: ad22b6146dbce0145941e409a1ce257805cb5612 [file] [log] [blame]
Zhenkai Zhu8d935c82012-03-06 10:44:12 -08001/* -*- 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>
Chaoyi Bian3e1eb162012-04-03 16:59:32 -070019 * Chaoyi Bian <bcy@pku.edu.cn>
Zhenkai Zhu8d935c82012-03-06 10:44:12 -080020 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
21 */
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080022
Chaoyi Bian11f294f2012-03-08 14:28:06 -080023#include "sync-logic.h"
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080024#include "sync-diff-leaf.h"
25#include "sync-full-leaf.h"
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070026#include "sync-log.h"
27
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080028#include <boost/make_shared.hpp>
29#include <boost/foreach.hpp>
Alexander Afanasyev387ac952012-03-11 23:49:27 -070030#include <boost/lexical_cast.hpp>
31#include <boost/date_time/posix_time/posix_time.hpp>
Zhenkai Zhua5d06d72012-03-09 15:16:24 -080032#include <vector>
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080033
34using namespace std;
35using namespace boost;
36
Alexander Afanasyevca6f3292012-04-09 10:03:30 -070037#ifndef _STANDALONE
38INIT_LOGGER ("SyncLogic");
39#endif
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070040
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080041namespace Sync
42{
43
Alexander Afanasyev750d1872012-03-12 15:33:56 -070044SyncLogic::SyncLogic (const std::string &syncPrefix,
45 LogicUpdateCallback onUpdate,
Zhenkai Zhuce66e212012-03-12 22:27:19 -070046 LogicRemoveCallback onRemove)
Alexander Afanasyevc1030192012-03-08 22:21:28 -080047 : m_syncPrefix (syncPrefix)
Alexander Afanasyev750d1872012-03-12 15:33:56 -070048 , m_onUpdate (onUpdate)
49 , m_onRemove (onRemove)
Alexander Afanasyev3f93c2b2012-03-13 00:02:31 -070050 , m_ccnxHandle(new CcnxWrapper())
Alexander Afanasyev45fba082012-03-12 18:05:24 -070051 , m_randomGenerator (static_cast<unsigned int> (std::time (0)))
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070052 , m_rangeUniformRandom (m_randomGenerator, uniform_int<> (20,80))
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080053{
Alexander Afanasyevca6f3292012-04-09 10:03:30 -070054#ifdef _STANDALONE
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070055#ifdef _DEBUG
56#ifdef HAVE_LOG4CXX
57 // _LOG_FUNCTION (syncPrefix);
58 static int id = 0;
59 staticModuleLogger = log4cxx::Logger::getLogger ("SyncLogic." + lexical_cast<string> (id));
60 id ++;
61#endif
62#endif
Alexander Afanasyevca6f3292012-04-09 10:03:30 -070063#endif
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -070064
65 _LOG_DEBUG ("syncPrefix");
66
67#ifndef NS3_MODULE
68 // In NS3 module these functions are moved to StartApplication method
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070069
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070070 m_ccnxHandle->setInterestFilter (m_syncPrefix,
Alexander Afanasyev387ac952012-03-11 23:49:27 -070071 bind (&SyncLogic::respondSyncInterest, this, _1));
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070072
Alexander Afanasyev181d7e52012-04-09 13:54:11 -070073 m_scheduler.schedule (TIME_SECONDS (0),
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070074 bind (&SyncLogic::sendSyncInterest, this),
75 REEXPRESSING_INTEREST);
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -070076#endif
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080077}
78
Alexander Afanasyevc1030192012-03-08 22:21:28 -080079SyncLogic::~SyncLogic ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080080{
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070081 // _LOG_FUNCTION (this);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070082 // cout << "SyncLogic::~SyncLogic ()" << endl;
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080083
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070084 m_ccnxHandle.reset ();
85}
Alexander Afanasyev387ac952012-03-11 23:49:27 -070086
Alexander Afanasyevb550d6a2012-04-09 15:03:16 -070087#ifdef NS3_MODULE
88void
89SyncLogic::StartApplication ()
90{
91 m_ccnxHandle->SetNode (GetNode ());
92 m_ccnxHandle->StartApplication ();
93
94 m_ccnxHandle->setInterestFilter (m_syncPrefix,
95 bind (&SyncLogic::respondSyncInterest, this, _1));
96
97 m_scheduler.schedule (TIME_SECONDS (0),
98 bind (&SyncLogic::sendSyncInterest, this),
99 REEXPRESSING_INTEREST);
100}
101
102void
103SyncLogic::StopApplication ()
104{
105 m_ccnxHandle->StopApplication ();
106}
107#endif
108
109
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700110void
111SyncLogic::respondSyncInterest (const string &interest)
112{
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700113 _LOG_TRACE ("<< I " << interest);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700114 //cout << "Respond Sync Interest" << endl;
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700115 string hash = interest.substr(interest.find_last_of("/") + 1);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700116 // cout << "Received Sync Interest: " << hash << endl;
117
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700118 DigestPtr digest = make_shared<Digest> ();
119 try
120 {
121 istringstream is (hash);
122 is >> *digest;
123 }
124 catch (Error::DigestCalculationError &e)
125 {
126 // log error. ignoring it for now, later we should log it
127 return;
128 }
129
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700130 processSyncInterest (digest, interest, false);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700131}
132
133void
134SyncLogic::processSyncInterest (DigestConstPtr digest, const std::string &interestName, bool timedProcessing/*=false*/)
135{
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700136 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700137
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700138 // Special case when state is not empty and we have received request with zero-root digest
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700139 if (digest->isZero () && !m_state.getDigest()->isZero ())
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700140 {
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700141 _LOG_TRACE (">> D " << interestName << "/state" << " (zero)");
142
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700143 m_syncInterestTable.remove (interestName + "/state");
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700144 m_ccnxHandle->publishData (interestName + "/state",
145 lexical_cast<string> (m_state),
146 m_syncResponseFreshness);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700147 if (m_outstandingInterest == interestName)
148 {
Alexander Afanasyev181d7e52012-04-09 13:54:11 -0700149 m_scheduler.schedule (TIME_SECONDS (0),
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700150 bind (&SyncLogic::sendSyncInterest, this),
151 REEXPRESSING_INTEREST);
152 }
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700153 return;
154 }
Alexander Afanasyev763855a2012-03-13 14:17:37 -0700155
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700156 if (*m_state.getDigest() == *digest)
157 {
158 // cout << interestName << "\n";
159 if (digest->isZero ())
160 {
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700161 _LOG_TRACE ("processSyncInterest (): Digest is zero, adding /state to PIT");
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700162 m_syncInterestTable.insert (interestName + "/state");
163 }
164 else
165 {
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700166 _LOG_TRACE ("processSyncInterest (): Same state. Adding to PIT");
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700167 m_syncInterestTable.insert (interestName);
168 }
169 return;
170 }
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700171
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700172 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
173
174 if (stateInDiffLog != m_log.end ())
175 {
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700176 DiffStateConstPtr stateDiff = (*stateInDiffLog)->diff ();
177 // string state = lexical_cast<string> (*stateDiff);
178 // erase_all (state, "\n");
179 // _LOG_TRACE (">> D " << interestName << ", state: " << state);
180 // _LOG_DEBUG ("Log size: " << m_log.size ());
181
182 // BOOST_FOREACH (DiffStateConstPtr ds, m_log.get<sequenced> ())
183 // {
184 // string state = lexical_cast<string> (*ds);
185 // erase_all (state, "\n");
186 // _LOG_DEBUG (" " << state << ", " << *ds->getDigest ());
187 // }
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700188
189 m_syncInterestTable.remove (interestName);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700190 m_ccnxHandle->publishData (interestName,
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700191 lexical_cast<string> (*stateDiff),
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700192 m_syncResponseFreshness);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700193 if (m_outstandingInterest == interestName)
194 {
Alexander Afanasyev181d7e52012-04-09 13:54:11 -0700195 m_scheduler.schedule (TIME_SECONDS (0),
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700196 bind (&SyncLogic::sendSyncInterest, this),
197 REEXPRESSING_INTEREST);
198 }
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700199 return;
200 }
201
202 if (!timedProcessing)
203 {
Alexander Afanasyev181d7e52012-04-09 13:54:11 -0700204 m_scheduler.schedule (TIME_MILLISECONDS (m_rangeUniformRandom ()) /*from 20 to 100ms*/,
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700205 bind (&SyncLogic::processSyncInterest, this, digest, interestName, true),
206 DELAYED_INTEREST_PROCESSING);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700207
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700208 }
209 else
210 {
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700211 _LOG_TRACE (">> D " << interestName << "/state" << " (timed processing)");
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700212
213 m_syncInterestTable.remove (interestName + "/state");
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700214 m_ccnxHandle->publishData (interestName + "/state",
215 lexical_cast<string> (m_state),
216 m_syncResponseFreshness);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700217
218 if (m_outstandingInterest == interestName)
219 {
Alexander Afanasyev181d7e52012-04-09 13:54:11 -0700220 m_scheduler.schedule (TIME_SECONDS (0),
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700221 bind (&SyncLogic::sendSyncInterest, this),
222 REEXPRESSING_INTEREST);
223 }
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700224 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800225}
226
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800227void
228SyncLogic::processSyncData (const string &name, const string &dataBuffer)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800229{
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700230 _LOG_TRACE ("<< D " << name);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700231 DiffStatePtr diffLog = make_shared<DiffState> ();
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700232
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700233 try
234 {
235 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev02f1bff2012-04-09 15:44:42 -0700236
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700237 string last = name.substr(name.find_last_of("/") + 1);
238 istringstream ss (dataBuffer);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700239
Alexander Afanasyev02f1bff2012-04-09 15:44:42 -0700240 m_syncInterestTable.remove (name); // just in case, remove both interests from our interest table. No reason to keep them there
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700241 if (last == "state")
242 {
Alexander Afanasyev02f1bff2012-04-09 15:44:42 -0700243 m_syncInterestTable.remove (name.substr(0, name.find_last_of("/")));
244
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700245 FullState full;
246 ss >> full;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700247 BOOST_FOREACH (LeafConstPtr leaf, full.getLeaves()) // order doesn't matter
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700248 {
249 NameInfoConstPtr info = leaf->getInfo ();
250 SeqNo seq = leaf->getSeq ();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700251
252 bool inserted = false;
253 bool updated = false;
254 SeqNo oldSeq;
255 tie (inserted, updated, oldSeq) = m_state.update (info, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800256
Alexander Afanasyevd4adce52012-03-13 13:59:39 -0700257 if (inserted || updated)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700258 {
259 diffLog->update (info, seq);
Chaoyi Bian98135192012-03-27 18:34:11 -0700260 m_onUpdate (info->toString (), seq, oldSeq);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700261 }
262 }
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700263 }
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700264 else
265 {
266 DiffState diff;
267 ss >> diff;
268 BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>())
269 {
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700270 DiffLeafConstPtr diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf);
271 BOOST_ASSERT (diffLeaf != 0);
272
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700273 NameInfoConstPtr info = diffLeaf->getInfo();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700274 if (diffLeaf->getOperation() == UPDATE)
275 {
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700276 SeqNo seq = diffLeaf->getSeq();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800277
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700278 bool inserted = false;
279 bool updated = false;
280 SeqNo oldSeq;
281 tie (inserted, updated, oldSeq) = m_state.update (info, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800282
Alexander Afanasyevd4adce52012-03-13 13:59:39 -0700283 if (inserted || updated)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700284 {
285 diffLog->update (info, seq);
Chaoyi Bian98135192012-03-27 18:34:11 -0700286 m_onUpdate (info->toString (), seq, oldSeq);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700287 }
288 }
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700289 else if (diffLeaf->getOperation() == REMOVE)
290 {
291 if (m_state.remove (info))
292 {
293 diffLog->remove (info);
294 m_onRemove (info->toString ());
295 }
296 }
297 else
298 {
299 BOOST_ASSERT (false); // just in case
300 }
301 }
302 }
303
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700304 insertToDiffLog (diffLog);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700305 }
306 catch (Error::SyncXmlDecodingFailure &e)
307 {
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700308 diffLog.reset ();
309 // don't do anything
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700310 }
311
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700312 // if state has changed, then it is safe to express a new interest
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700313 if (diffLog->getLeaves ().size () > 0)
314 {
Alexander Afanasyev181d7e52012-04-09 13:54:11 -0700315 m_scheduler.schedule (TIME_SECONDS (0),
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700316 bind (&SyncLogic::sendSyncInterest, this),
317 REEXPRESSING_INTEREST);
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700318 }
319 else
320 {
321 // should not reexpress the same interest. Need at least wait for data lifetime
322 // Otherwise we will get immediate reply from the local daemon and there will be 100% utilization
323 m_scheduler.cancel (REEXPRESSING_INTEREST);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700324 // m_scheduler.schedule (posix_time::seconds (0),
Alexander Afanasyev181d7e52012-04-09 13:54:11 -0700325 m_scheduler.schedule (TIME_SECONDS (m_syncResponseFreshness) + TIME_MILLISECONDS (1),
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700326 bind (&SyncLogic::sendSyncInterest, this),
327 REEXPRESSING_INTEREST);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700328 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800329}
330
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800331void
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700332SyncLogic::satisfyPendingSyncInterests (DiffStatePtr diffLog)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800333{
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800334 vector<string> pis = m_syncInterestTable.fetchAll ();
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700335 if (pis.size () > 0)
336 {
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700337 stringstream ss;
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700338 ss << *diffLog;
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700339 bool satisfiedOwnInterest = false;
340
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700341 for (vector<string>::iterator ii = pis.begin(); ii != pis.end(); ++ii)
342 {
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700343 _LOG_TRACE (">> D " << *ii);
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700344 m_ccnxHandle->publishData (*ii, ss.str(), m_syncResponseFreshness);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700345
346 {
347 recursive_mutex::scoped_lock lock (m_stateMutex);
348 // _LOG_DEBUG (*ii << " == " << m_outstandingInterest << " = " << (*ii == m_outstandingInterest));
349 satisfiedOwnInterest = satisfiedOwnInterest || (*ii == m_outstandingInterest) || (*ii == (m_outstandingInterest + "/state"));
350 }
351 }
352
353 if (satisfiedOwnInterest)
354 {
355 _LOG_DEBUG ("Have satisfied our own interest. Scheduling interest reexpression");
356 // we need to reexpress interest only if we satisfied our own interest
Alexander Afanasyev181d7e52012-04-09 13:54:11 -0700357 m_scheduler.schedule (TIME_SECONDS (0),
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700358 bind (&SyncLogic::sendSyncInterest, this),
359 REEXPRESSING_INTEREST);
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700360 }
361 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800362}
363
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800364void
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700365SyncLogic::insertToDiffLog (DiffStatePtr diffLog)
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700366{
367 //cout << "Process Pending Interests" <<endl;
368 diffLog->setDigest (m_state.getDigest());
369 if (m_log.size () > 0)
370 {
371 m_log.get<sequenced> ().front ()->setNext (diffLog);
372 }
373 m_log.erase (m_state.getDigest()); // remove diff state with the same digest. next pointers are still valid
374 /// @todo Optimization
Alexander Afanasyev7df73332012-03-15 12:31:49 -0700375 m_log.get<sequenced> ().push_front (diffLog);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700376 // _LOG_DEBUG (*diffLog->getDigest () << " " << m_log.size ());
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700377}
378
379void
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700380SyncLogic::addLocalNames (const string &prefix, uint32_t session, uint32_t seq)
381{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700382 DiffStatePtr diff;
383 {
384 //cout << "Add local names" <<endl;
385 recursive_mutex::scoped_lock lock (m_stateMutex);
386 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700387
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700388 SeqNo seqN (session, seq);
389 m_state.update(info, seqN);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700390
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700391 _LOG_DEBUG ("addLocalNames (): new state " << *m_state.getDigest ());
392
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700393 diff = make_shared<DiffState>();
394 diff->update(info, seqN);
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700395 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700396 }
397
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700398 // _LOG_DEBUG ("PIT size: " << m_syncInterestTable.size ());
399 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700400}
401
402void
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700403SyncLogic::remove(const string &prefix)
404{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700405 DiffStatePtr diff;
406 {
407 recursive_mutex::scoped_lock lock (m_stateMutex);
408 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
409 m_state.remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700410
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700411 diff = make_shared<DiffState>();
412 diff->remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700413
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700414 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700415 }
416
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700417 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700418}
419
420void
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800421SyncLogic::sendSyncInterest ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800422{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800423 ostringstream os;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800424
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700425 {
426 // cout << "Sending Sync Interest" << endl;
427 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700428
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700429 os << m_syncPrefix << "/" << *m_state.getDigest();
430
431 _LOG_TRACE (">> I " << os.str ());
432
433 m_outstandingInterest = os.str ();
434 }
435
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800436 m_ccnxHandle->sendInterest (os.str (),
437 bind (&SyncLogic::processSyncData, this, _1, _2));
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700438
439 m_scheduler.cancel (REEXPRESSING_INTEREST);
Alexander Afanasyev181d7e52012-04-09 13:54:11 -0700440 m_scheduler.schedule (TIME_SECONDS (4),
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700441 bind (&SyncLogic::sendSyncInterest, this),
442 REEXPRESSING_INTEREST);
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800443}
444
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800445}