blob: 17a093564255e6db7d695d636509a64b25d7a71c [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 Afanasyev4f9ea482012-03-15 11:57:29 -070064
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070065 m_ccnxHandle->setInterestFilter (m_syncPrefix,
Alexander Afanasyev387ac952012-03-11 23:49:27 -070066 bind (&SyncLogic::respondSyncInterest, this, _1));
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070067
Alexander Afanasyev181d7e52012-04-09 13:54:11 -070068 m_scheduler.schedule (TIME_SECONDS (0),
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070069 bind (&SyncLogic::sendSyncInterest, this),
70 REEXPRESSING_INTEREST);
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080071}
72
Alexander Afanasyevc1030192012-03-08 22:21:28 -080073SyncLogic::~SyncLogic ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080074{
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -070075 // _LOG_FUNCTION (this);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070076 // cout << "SyncLogic::~SyncLogic ()" << endl;
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080077
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070078 m_ccnxHandle.reset ();
79}
Alexander Afanasyev387ac952012-03-11 23:49:27 -070080
81void
82SyncLogic::respondSyncInterest (const string &interest)
83{
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070084 _LOG_TRACE ("<< I " << interest);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070085 //cout << "Respond Sync Interest" << endl;
Alexander Afanasyev387ac952012-03-11 23:49:27 -070086 string hash = interest.substr(interest.find_last_of("/") + 1);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070087 // cout << "Received Sync Interest: " << hash << endl;
88
Alexander Afanasyev387ac952012-03-11 23:49:27 -070089 DigestPtr digest = make_shared<Digest> ();
90 try
91 {
92 istringstream is (hash);
93 is >> *digest;
94 }
95 catch (Error::DigestCalculationError &e)
96 {
97 // log error. ignoring it for now, later we should log it
98 return;
99 }
100
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700101 processSyncInterest (digest, interest, false);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700102}
103
104void
105SyncLogic::processSyncInterest (DigestConstPtr digest, const std::string &interestName, bool timedProcessing/*=false*/)
106{
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700107 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700108
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700109 // Special case when state is not empty and we have received request with zero-root digest
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700110 if (digest->isZero () && !m_state.getDigest()->isZero ())
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700111 {
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700112 _LOG_TRACE (">> D " << interestName << "/state" << " (zero)");
113
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700114 m_syncInterestTable.remove (interestName + "/state");
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700115 m_ccnxHandle->publishData (interestName + "/state",
116 lexical_cast<string> (m_state),
117 m_syncResponseFreshness);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700118 if (m_outstandingInterest == interestName)
119 {
Alexander Afanasyev181d7e52012-04-09 13:54:11 -0700120 m_scheduler.schedule (TIME_SECONDS (0),
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700121 bind (&SyncLogic::sendSyncInterest, this),
122 REEXPRESSING_INTEREST);
123 }
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700124 return;
125 }
Alexander Afanasyev763855a2012-03-13 14:17:37 -0700126
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700127 if (*m_state.getDigest() == *digest)
128 {
129 // cout << interestName << "\n";
130 if (digest->isZero ())
131 {
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700132 _LOG_TRACE ("processSyncInterest (): Digest is zero, adding /state to PIT");
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700133 m_syncInterestTable.insert (interestName + "/state");
134 }
135 else
136 {
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700137 _LOG_TRACE ("processSyncInterest (): Same state. Adding to PIT");
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700138 m_syncInterestTable.insert (interestName);
139 }
140 return;
141 }
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700142
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700143 DiffStateContainer::iterator stateInDiffLog = m_log.find (digest);
144
145 if (stateInDiffLog != m_log.end ())
146 {
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700147 DiffStateConstPtr stateDiff = (*stateInDiffLog)->diff ();
148 // string state = lexical_cast<string> (*stateDiff);
149 // erase_all (state, "\n");
150 // _LOG_TRACE (">> D " << interestName << ", state: " << state);
151 // _LOG_DEBUG ("Log size: " << m_log.size ());
152
153 // BOOST_FOREACH (DiffStateConstPtr ds, m_log.get<sequenced> ())
154 // {
155 // string state = lexical_cast<string> (*ds);
156 // erase_all (state, "\n");
157 // _LOG_DEBUG (" " << state << ", " << *ds->getDigest ());
158 // }
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700159
160 m_syncInterestTable.remove (interestName);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700161 m_ccnxHandle->publishData (interestName,
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700162 lexical_cast<string> (*stateDiff),
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700163 m_syncResponseFreshness);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700164 if (m_outstandingInterest == interestName)
165 {
Alexander Afanasyev181d7e52012-04-09 13:54:11 -0700166 m_scheduler.schedule (TIME_SECONDS (0),
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700167 bind (&SyncLogic::sendSyncInterest, this),
168 REEXPRESSING_INTEREST);
169 }
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700170 return;
171 }
172
173 if (!timedProcessing)
174 {
Alexander Afanasyev181d7e52012-04-09 13:54:11 -0700175 m_scheduler.schedule (TIME_MILLISECONDS (m_rangeUniformRandom ()) /*from 20 to 100ms*/,
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700176 bind (&SyncLogic::processSyncInterest, this, digest, interestName, true),
177 DELAYED_INTEREST_PROCESSING);
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700178
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700179 }
180 else
181 {
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700182 _LOG_TRACE (">> D " << interestName << "/state" << " (timed processing)");
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700183
184 m_syncInterestTable.remove (interestName + "/state");
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700185 m_ccnxHandle->publishData (interestName + "/state",
186 lexical_cast<string> (m_state),
187 m_syncResponseFreshness);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700188
189 if (m_outstandingInterest == interestName)
190 {
Alexander Afanasyev181d7e52012-04-09 13:54:11 -0700191 m_scheduler.schedule (TIME_SECONDS (0),
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700192 bind (&SyncLogic::sendSyncInterest, this),
193 REEXPRESSING_INTEREST);
194 }
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700195 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800196}
197
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800198void
199SyncLogic::processSyncData (const string &name, const string &dataBuffer)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800200{
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700201 _LOG_TRACE ("<< D " << name);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700202 DiffStatePtr diffLog = make_shared<DiffState> ();
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -0700203
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700204 try
205 {
206 recursive_mutex::scoped_lock lock (m_stateMutex);
207
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700208 string last = name.substr(name.find_last_of("/") + 1);
209 istringstream ss (dataBuffer);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700210
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700211 if (last == "state")
212 {
213 FullState full;
214 ss >> full;
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700215 BOOST_FOREACH (LeafConstPtr leaf, full.getLeaves()) // order doesn't matter
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700216 {
217 NameInfoConstPtr info = leaf->getInfo ();
218 SeqNo seq = leaf->getSeq ();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700219
220 bool inserted = false;
221 bool updated = false;
222 SeqNo oldSeq;
223 tie (inserted, updated, oldSeq) = m_state.update (info, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800224
Alexander Afanasyevd4adce52012-03-13 13:59:39 -0700225 if (inserted || updated)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700226 {
227 diffLog->update (info, seq);
Chaoyi Bian98135192012-03-27 18:34:11 -0700228 m_onUpdate (info->toString (), seq, oldSeq);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700229 }
230 }
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700231 }
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700232 else
233 {
234 DiffState diff;
235 ss >> diff;
236 BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>())
237 {
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700238 DiffLeafConstPtr diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf);
239 BOOST_ASSERT (diffLeaf != 0);
240
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700241 NameInfoConstPtr info = diffLeaf->getInfo();
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700242 if (diffLeaf->getOperation() == UPDATE)
243 {
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700244 SeqNo seq = diffLeaf->getSeq();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800245
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700246 bool inserted = false;
247 bool updated = false;
248 SeqNo oldSeq;
249 tie (inserted, updated, oldSeq) = m_state.update (info, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800250
Alexander Afanasyevd4adce52012-03-13 13:59:39 -0700251 if (inserted || updated)
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700252 {
253 diffLog->update (info, seq);
Chaoyi Bian98135192012-03-27 18:34:11 -0700254 m_onUpdate (info->toString (), seq, oldSeq);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700255 }
256 }
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700257 else if (diffLeaf->getOperation() == REMOVE)
258 {
259 if (m_state.remove (info))
260 {
261 diffLog->remove (info);
262 m_onRemove (info->toString ());
263 }
264 }
265 else
266 {
267 BOOST_ASSERT (false); // just in case
268 }
269 }
270 }
271
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700272 insertToDiffLog (diffLog);
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700273 }
274 catch (Error::SyncXmlDecodingFailure &e)
275 {
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700276 diffLog.reset ();
277 // don't do anything
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700278 }
279
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700280 // if state has changed, then it is safe to express a new interest
Alexander Afanasyev750d1872012-03-12 15:33:56 -0700281 if (diffLog->getLeaves ().size () > 0)
282 {
Alexander Afanasyev181d7e52012-04-09 13:54:11 -0700283 m_scheduler.schedule (TIME_SECONDS (0),
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700284 bind (&SyncLogic::sendSyncInterest, this),
285 REEXPRESSING_INTEREST);
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700286 }
287 else
288 {
289 // should not reexpress the same interest. Need at least wait for data lifetime
290 // Otherwise we will get immediate reply from the local daemon and there will be 100% utilization
291 m_scheduler.cancel (REEXPRESSING_INTEREST);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700292 // m_scheduler.schedule (posix_time::seconds (0),
Alexander Afanasyev181d7e52012-04-09 13:54:11 -0700293 m_scheduler.schedule (TIME_SECONDS (m_syncResponseFreshness) + TIME_MILLISECONDS (1),
Alexander Afanasyevd4cca472012-03-13 14:25:46 -0700294 bind (&SyncLogic::sendSyncInterest, this),
295 REEXPRESSING_INTEREST);
Alexander Afanasyev04fd8a82012-03-12 15:40:48 -0700296 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800297}
298
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800299void
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700300SyncLogic::satisfyPendingSyncInterests (DiffStatePtr diffLog)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800301{
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800302 vector<string> pis = m_syncInterestTable.fetchAll ();
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700303 if (pis.size () > 0)
304 {
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700305 stringstream ss;
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700306 ss << *diffLog;
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700307 bool satisfiedOwnInterest = false;
308
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700309 for (vector<string>::iterator ii = pis.begin(); ii != pis.end(); ++ii)
310 {
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700311 _LOG_TRACE (">> D " << *ii);
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700312 m_ccnxHandle->publishData (*ii, ss.str(), m_syncResponseFreshness);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700313
314 {
315 recursive_mutex::scoped_lock lock (m_stateMutex);
316 // _LOG_DEBUG (*ii << " == " << m_outstandingInterest << " = " << (*ii == m_outstandingInterest));
317 satisfiedOwnInterest = satisfiedOwnInterest || (*ii == m_outstandingInterest) || (*ii == (m_outstandingInterest + "/state"));
318 }
319 }
320
321 if (satisfiedOwnInterest)
322 {
323 _LOG_DEBUG ("Have satisfied our own interest. Scheduling interest reexpression");
324 // we need to reexpress interest only if we satisfied our own interest
Alexander Afanasyev181d7e52012-04-09 13:54:11 -0700325 m_scheduler.schedule (TIME_SECONDS (0),
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700326 bind (&SyncLogic::sendSyncInterest, this),
327 REEXPRESSING_INTEREST);
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700328 }
329 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800330}
331
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800332void
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700333SyncLogic::insertToDiffLog (DiffStatePtr diffLog)
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700334{
335 //cout << "Process Pending Interests" <<endl;
336 diffLog->setDigest (m_state.getDigest());
337 if (m_log.size () > 0)
338 {
339 m_log.get<sequenced> ().front ()->setNext (diffLog);
340 }
341 m_log.erase (m_state.getDigest()); // remove diff state with the same digest. next pointers are still valid
342 /// @todo Optimization
Alexander Afanasyev7df73332012-03-15 12:31:49 -0700343 m_log.get<sequenced> ().push_front (diffLog);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700344 // _LOG_DEBUG (*diffLog->getDigest () << " " << m_log.size ());
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700345}
346
347void
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700348SyncLogic::addLocalNames (const string &prefix, uint32_t session, uint32_t seq)
349{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700350 DiffStatePtr diff;
351 {
352 //cout << "Add local names" <<endl;
353 recursive_mutex::scoped_lock lock (m_stateMutex);
354 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700355
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700356 SeqNo seqN (session, seq);
357 m_state.update(info, seqN);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700358
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700359 _LOG_DEBUG ("addLocalNames (): new state " << *m_state.getDigest ());
360
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700361 diff = make_shared<DiffState>();
362 diff->update(info, seqN);
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700363 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700364 }
365
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700366 // _LOG_DEBUG ("PIT size: " << m_syncInterestTable.size ());
367 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700368}
369
370void
Alexander Afanasyevd91497c2012-03-12 15:39:30 -0700371SyncLogic::remove(const string &prefix)
372{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700373 DiffStatePtr diff;
374 {
375 recursive_mutex::scoped_lock lock (m_stateMutex);
376 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
377 m_state.remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700378
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700379 diff = make_shared<DiffState>();
380 diff->remove(info);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700381
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700382 insertToDiffLog (diff);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700383 }
384
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700385 satisfyPendingSyncInterests (diff);
Zhenkai Zhu0efa37b2012-03-12 13:54:12 -0700386}
387
388void
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800389SyncLogic::sendSyncInterest ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800390{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800391 ostringstream os;
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800392
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700393 {
394 // cout << "Sending Sync Interest" << endl;
395 recursive_mutex::scoped_lock lock (m_stateMutex);
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700396
Alexander Afanasyev860e6fe2012-03-15 17:30:31 -0700397 os << m_syncPrefix << "/" << *m_state.getDigest();
398
399 _LOG_TRACE (">> I " << os.str ());
400
401 m_outstandingInterest = os.str ();
402 }
403
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800404 m_ccnxHandle->sendInterest (os.str (),
405 bind (&SyncLogic::processSyncData, this, _1, _2));
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700406
407 m_scheduler.cancel (REEXPRESSING_INTEREST);
Alexander Afanasyev181d7e52012-04-09 13:54:11 -0700408 m_scheduler.schedule (TIME_SECONDS (4),
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700409 bind (&SyncLogic::sendSyncInterest, this),
410 REEXPRESSING_INTEREST);
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800411}
412
Alexander Afanasyev181d7e52012-04-09 13:54:11 -0700413#ifdef NS3_MODULE
414void
415SyncLogic::StartApplication ()
416{
417 m_ccnxHandle->SetNode (GetNode ());
418 m_ccnxHandle->StartApplication ();
419}
420
421void
422SyncLogic::StopApplication ()
423{
424 m_ccnxHandle->StopApplication ();
425}
426#endif
427
428
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800429}