Zhenkai Zhu | 8d935c8 | 2012-03-06 10:44:12 -0800 | [diff] [blame] | 1 | /* -*- 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 Bian | 3e1eb16 | 2012-04-03 16:59:32 -0700 | [diff] [blame] | 19 | * Chaoyi Bian <bcy@pku.edu.cn> |
Zhenkai Zhu | 8d935c8 | 2012-03-06 10:44:12 -0800 | [diff] [blame] | 20 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 21 | */ |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 22 | |
Chaoyi Bian | 11f294f | 2012-03-08 14:28:06 -0800 | [diff] [blame] | 23 | #include "sync-logic.h" |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 24 | #include "sync-diff-leaf.h" |
| 25 | #include "sync-full-leaf.h" |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 26 | #include "sync-log.h" |
| 27 | |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 28 | #include <boost/make_shared.hpp> |
| 29 | #include <boost/foreach.hpp> |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 30 | #include <boost/lexical_cast.hpp> |
| 31 | #include <boost/date_time/posix_time/posix_time.hpp> |
Zhenkai Zhu | a5d06d7 | 2012-03-09 15:16:24 -0800 | [diff] [blame] | 32 | #include <vector> |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 33 | |
| 34 | using namespace std; |
| 35 | using namespace boost; |
| 36 | |
Alexander Afanasyev | ca6f329 | 2012-04-09 10:03:30 -0700 | [diff] [blame] | 37 | #ifndef _STANDALONE |
| 38 | INIT_LOGGER ("SyncLogic"); |
| 39 | #endif |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 40 | |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 41 | namespace Sync |
| 42 | { |
| 43 | |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 44 | SyncLogic::SyncLogic (const std::string &syncPrefix, |
| 45 | LogicUpdateCallback onUpdate, |
Zhenkai Zhu | ce66e21 | 2012-03-12 22:27:19 -0700 | [diff] [blame] | 46 | LogicRemoveCallback onRemove) |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 47 | : m_syncPrefix (syncPrefix) |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 48 | , m_onUpdate (onUpdate) |
| 49 | , m_onRemove (onRemove) |
Alexander Afanasyev | 3f93c2b | 2012-03-13 00:02:31 -0700 | [diff] [blame] | 50 | , m_ccnxHandle(new CcnxWrapper()) |
Alexander Afanasyev | 73f7f9a | 2012-04-09 15:45:47 -0700 | [diff] [blame] | 51 | #ifndef NS3_MODULE |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 52 | , m_randomGenerator (static_cast<unsigned int> (std::time (0))) |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 53 | , m_rangeUniformRandom (m_randomGenerator, uniform_int<> (20,80)) |
Alexander Afanasyev | 73f7f9a | 2012-04-09 15:45:47 -0700 | [diff] [blame] | 54 | #else |
Alexander Afanasyev | 70f3205 | 2012-04-24 14:10:38 -0700 | [diff] [blame] | 55 | , m_rangeUniformRandom (1000,2000) |
Alexander Afanasyev | 73f7f9a | 2012-04-09 15:45:47 -0700 | [diff] [blame] | 56 | #endif |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 57 | { |
Alexander Afanasyev | ca6f329 | 2012-04-09 10:03:30 -0700 | [diff] [blame] | 58 | #ifdef _STANDALONE |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 59 | #ifdef _DEBUG |
| 60 | #ifdef HAVE_LOG4CXX |
| 61 | // _LOG_FUNCTION (syncPrefix); |
| 62 | static int id = 0; |
| 63 | staticModuleLogger = log4cxx::Logger::getLogger ("SyncLogic." + lexical_cast<string> (id)); |
| 64 | id ++; |
| 65 | #endif |
| 66 | #endif |
Alexander Afanasyev | ca6f329 | 2012-04-09 10:03:30 -0700 | [diff] [blame] | 67 | #endif |
Alexander Afanasyev | b550d6a | 2012-04-09 15:03:16 -0700 | [diff] [blame] | 68 | |
| 69 | _LOG_DEBUG ("syncPrefix"); |
| 70 | |
| 71 | #ifndef NS3_MODULE |
| 72 | // In NS3 module these functions are moved to StartApplication method |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 73 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 74 | m_ccnxHandle->setInterestFilter (m_syncPrefix, |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 75 | bind (&SyncLogic::respondSyncInterest, this, _1)); |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 76 | |
Alexander Afanasyev | 181d7e5 | 2012-04-09 13:54:11 -0700 | [diff] [blame] | 77 | m_scheduler.schedule (TIME_SECONDS (0), |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 78 | bind (&SyncLogic::sendSyncInterest, this), |
| 79 | REEXPRESSING_INTEREST); |
Alexander Afanasyev | b550d6a | 2012-04-09 15:03:16 -0700 | [diff] [blame] | 80 | #endif |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 83 | SyncLogic::~SyncLogic () |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 84 | { |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 85 | // _LOG_FUNCTION (this); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 86 | // cout << "SyncLogic::~SyncLogic ()" << endl; |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 87 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 88 | m_ccnxHandle.reset (); |
| 89 | } |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 90 | |
Alexander Afanasyev | b550d6a | 2012-04-09 15:03:16 -0700 | [diff] [blame] | 91 | #ifdef NS3_MODULE |
| 92 | void |
| 93 | SyncLogic::StartApplication () |
| 94 | { |
| 95 | m_ccnxHandle->SetNode (GetNode ()); |
| 96 | m_ccnxHandle->StartApplication (); |
| 97 | |
| 98 | m_ccnxHandle->setInterestFilter (m_syncPrefix, |
| 99 | bind (&SyncLogic::respondSyncInterest, this, _1)); |
| 100 | |
| 101 | m_scheduler.schedule (TIME_SECONDS (0), |
| 102 | bind (&SyncLogic::sendSyncInterest, this), |
| 103 | REEXPRESSING_INTEREST); |
| 104 | } |
| 105 | |
| 106 | void |
| 107 | SyncLogic::StopApplication () |
| 108 | { |
Alexander Afanasyev | 434f161 | 2012-04-21 21:19:15 -0700 | [diff] [blame] | 109 | m_ccnxHandle->clearInterestFilter (m_syncPrefix); |
Alexander Afanasyev | b550d6a | 2012-04-09 15:03:16 -0700 | [diff] [blame] | 110 | m_ccnxHandle->StopApplication (); |
Alexander Afanasyev | 40942f4 | 2012-04-21 20:53:16 -0700 | [diff] [blame] | 111 | m_scheduler.cancel (REEXPRESSING_INTEREST); |
| 112 | m_scheduler.cancel (DELAYED_INTEREST_PROCESSING); |
Alexander Afanasyev | b550d6a | 2012-04-09 15:03:16 -0700 | [diff] [blame] | 113 | } |
| 114 | #endif |
| 115 | |
| 116 | |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 117 | void |
| 118 | SyncLogic::respondSyncInterest (const string &interest) |
| 119 | { |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 120 | _LOG_TRACE ("<< I " << interest); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 121 | //cout << "Respond Sync Interest" << endl; |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 122 | string hash = interest.substr(interest.find_last_of("/") + 1); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 123 | // cout << "Received Sync Interest: " << hash << endl; |
| 124 | |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 125 | DigestPtr digest = make_shared<Digest> (); |
| 126 | try |
| 127 | { |
| 128 | istringstream is (hash); |
| 129 | is >> *digest; |
| 130 | } |
| 131 | catch (Error::DigestCalculationError &e) |
| 132 | { |
| 133 | // log error. ignoring it for now, later we should log it |
| 134 | return; |
| 135 | } |
| 136 | |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 137 | processSyncInterest (digest, interest, false); |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | void |
| 141 | SyncLogic::processSyncInterest (DigestConstPtr digest, const std::string &interestName, bool timedProcessing/*=false*/) |
| 142 | { |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 143 | recursive_mutex::scoped_lock lock (m_stateMutex); |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 144 | |
Alexander Afanasyev | dd0eba7 | 2012-03-13 13:57:10 -0700 | [diff] [blame] | 145 | // Special case when state is not empty and we have received request with zero-root digest |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 146 | if (digest->isZero () && !m_state.getDigest()->isZero ()) |
Alexander Afanasyev | dd0eba7 | 2012-03-13 13:57:10 -0700 | [diff] [blame] | 147 | { |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 148 | _LOG_TRACE (">> D " << interestName << "/state" << " (zero)"); |
| 149 | |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 150 | m_syncInterestTable.remove (interestName + "/state"); |
Alexander Afanasyev | dd0eba7 | 2012-03-13 13:57:10 -0700 | [diff] [blame] | 151 | m_ccnxHandle->publishData (interestName + "/state", |
| 152 | lexical_cast<string> (m_state), |
| 153 | m_syncResponseFreshness); |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 154 | if (m_outstandingInterest == interestName) |
| 155 | { |
Alexander Afanasyev | 181d7e5 | 2012-04-09 13:54:11 -0700 | [diff] [blame] | 156 | m_scheduler.schedule (TIME_SECONDS (0), |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 157 | bind (&SyncLogic::sendSyncInterest, this), |
| 158 | REEXPRESSING_INTEREST); |
| 159 | } |
Alexander Afanasyev | dd0eba7 | 2012-03-13 13:57:10 -0700 | [diff] [blame] | 160 | return; |
| 161 | } |
Alexander Afanasyev | 763855a | 2012-03-13 14:17:37 -0700 | [diff] [blame] | 162 | |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 163 | if (*m_state.getDigest() == *digest) |
| 164 | { |
| 165 | // cout << interestName << "\n"; |
| 166 | if (digest->isZero ()) |
| 167 | { |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 168 | _LOG_TRACE ("processSyncInterest (): Digest is zero, adding /state to PIT"); |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 169 | m_syncInterestTable.insert (interestName + "/state"); |
| 170 | } |
| 171 | else |
| 172 | { |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 173 | _LOG_TRACE ("processSyncInterest (): Same state. Adding to PIT"); |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 174 | m_syncInterestTable.insert (interestName); |
| 175 | } |
| 176 | return; |
| 177 | } |
Alexander Afanasyev | dd0eba7 | 2012-03-13 13:57:10 -0700 | [diff] [blame] | 178 | |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 179 | DiffStateContainer::iterator stateInDiffLog = m_log.find (digest); |
| 180 | |
| 181 | if (stateInDiffLog != m_log.end ()) |
| 182 | { |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 183 | DiffStateConstPtr stateDiff = (*stateInDiffLog)->diff (); |
| 184 | // string state = lexical_cast<string> (*stateDiff); |
| 185 | // erase_all (state, "\n"); |
| 186 | // _LOG_TRACE (">> D " << interestName << ", state: " << state); |
| 187 | // _LOG_DEBUG ("Log size: " << m_log.size ()); |
| 188 | |
| 189 | // BOOST_FOREACH (DiffStateConstPtr ds, m_log.get<sequenced> ()) |
| 190 | // { |
| 191 | // string state = lexical_cast<string> (*ds); |
| 192 | // erase_all (state, "\n"); |
| 193 | // _LOG_DEBUG (" " << state << ", " << *ds->getDigest ()); |
| 194 | // } |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 195 | |
| 196 | m_syncInterestTable.remove (interestName); |
Alexander Afanasyev | 73f7f9a | 2012-04-09 15:45:47 -0700 | [diff] [blame] | 197 | _LOG_DEBUG (">> D" << interestName); |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 198 | m_ccnxHandle->publishData (interestName, |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 199 | lexical_cast<string> (*stateDiff), |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 200 | m_syncResponseFreshness); |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 201 | if (m_outstandingInterest == interestName) |
| 202 | { |
Alexander Afanasyev | 181d7e5 | 2012-04-09 13:54:11 -0700 | [diff] [blame] | 203 | m_scheduler.schedule (TIME_SECONDS (0), |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 204 | bind (&SyncLogic::sendSyncInterest, this), |
| 205 | REEXPRESSING_INTEREST); |
| 206 | } |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 207 | return; |
| 208 | } |
| 209 | |
| 210 | if (!timedProcessing) |
| 211 | { |
Alexander Afanasyev | 3a22913 | 2012-04-25 15:07:26 -0700 | [diff] [blame^] | 212 | UnknownDigestContainer::index<hashed>::type::iterator previousUnknownDigest = m_recentUnknownDigests.get<hashed> ().find (digest); |
| 213 | if (previousUnknownDigest != m_recentUnknownDigests.get<hashed> ().end ()) |
| 214 | { |
| 215 | _LOG_DEBUG ("Digest is not in the log, but we have already seen it. (Don't do anything)"); |
| 216 | } |
| 217 | else |
| 218 | { |
| 219 | m_recentUnknownDigests.insert (DigestTime (digest, TIME_NOW + TIME_SECONDS (m_unknownDigestStoreTime))); |
| 220 | |
| 221 | uint32_t waitDelay = |
Alexander Afanasyev | 73f7f9a | 2012-04-09 15:45:47 -0700 | [diff] [blame] | 222 | #ifndef NS3_MODULE |
Alexander Afanasyev | 3a22913 | 2012-04-25 15:07:26 -0700 | [diff] [blame^] | 223 | m_rangeUniformRandom () |
Alexander Afanasyev | 73f7f9a | 2012-04-09 15:45:47 -0700 | [diff] [blame] | 224 | #else |
Alexander Afanasyev | 3a22913 | 2012-04-25 15:07:26 -0700 | [diff] [blame^] | 225 | m_rangeUniformRandom.GetValue () |
Alexander Afanasyev | 73f7f9a | 2012-04-09 15:45:47 -0700 | [diff] [blame] | 226 | #endif |
Alexander Afanasyev | 3a22913 | 2012-04-25 15:07:26 -0700 | [diff] [blame^] | 227 | ; |
Alexander Afanasyev | 73f7f9a | 2012-04-09 15:45:47 -0700 | [diff] [blame] | 228 | |
Alexander Afanasyev | 3a22913 | 2012-04-25 15:07:26 -0700 | [diff] [blame^] | 229 | _LOG_DEBUG ("Digest is not in the log. Schedule processing after small delay: " << waitDelay << "ms"); |
| 230 | m_scheduler.schedule (TIME_MILLISECONDS (waitDelay) /*from 20 to 100ms*/, |
| 231 | bind (&SyncLogic::processSyncInterest, this, digest, interestName, true), |
| 232 | DELAYED_INTEREST_PROCESSING); |
| 233 | } |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 234 | } |
| 235 | else |
| 236 | { |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 237 | _LOG_TRACE (">> D " << interestName << "/state" << " (timed processing)"); |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 238 | |
| 239 | m_syncInterestTable.remove (interestName + "/state"); |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 240 | m_ccnxHandle->publishData (interestName + "/state", |
| 241 | lexical_cast<string> (m_state), |
| 242 | m_syncResponseFreshness); |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 243 | |
| 244 | if (m_outstandingInterest == interestName) |
| 245 | { |
Alexander Afanasyev | 181d7e5 | 2012-04-09 13:54:11 -0700 | [diff] [blame] | 246 | m_scheduler.schedule (TIME_SECONDS (0), |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 247 | bind (&SyncLogic::sendSyncInterest, this), |
| 248 | REEXPRESSING_INTEREST); |
| 249 | } |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 250 | } |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 251 | } |
| 252 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 253 | void |
| 254 | SyncLogic::processSyncData (const string &name, const string &dataBuffer) |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 255 | { |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 256 | _LOG_TRACE ("<< D " << name); |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 257 | DiffStatePtr diffLog = make_shared<DiffState> (); |
Alexander Afanasyev | e4e2bf7 | 2012-03-12 12:44:54 -0700 | [diff] [blame] | 258 | |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 259 | try |
| 260 | { |
| 261 | recursive_mutex::scoped_lock lock (m_stateMutex); |
Alexander Afanasyev | 02f1bff | 2012-04-09 15:44:42 -0700 | [diff] [blame] | 262 | |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 263 | string last = name.substr(name.find_last_of("/") + 1); |
| 264 | istringstream ss (dataBuffer); |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 265 | |
Alexander Afanasyev | 02f1bff | 2012-04-09 15:44:42 -0700 | [diff] [blame] | 266 | m_syncInterestTable.remove (name); // just in case, remove both interests from our interest table. No reason to keep them there |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 267 | if (last == "state") |
| 268 | { |
Alexander Afanasyev | 02f1bff | 2012-04-09 15:44:42 -0700 | [diff] [blame] | 269 | m_syncInterestTable.remove (name.substr(0, name.find_last_of("/"))); |
| 270 | |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 271 | FullState full; |
| 272 | ss >> full; |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 273 | BOOST_FOREACH (LeafConstPtr leaf, full.getLeaves()) // order doesn't matter |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 274 | { |
| 275 | NameInfoConstPtr info = leaf->getInfo (); |
| 276 | SeqNo seq = leaf->getSeq (); |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 277 | |
| 278 | bool inserted = false; |
| 279 | bool updated = false; |
| 280 | SeqNo oldSeq; |
| 281 | tie (inserted, updated, oldSeq) = m_state.update (info, seq); |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 282 | |
Alexander Afanasyev | d4adce5 | 2012-03-13 13:59:39 -0700 | [diff] [blame] | 283 | if (inserted || updated) |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 284 | { |
| 285 | diffLog->update (info, seq); |
Chaoyi Bian | 9813519 | 2012-03-27 18:34:11 -0700 | [diff] [blame] | 286 | m_onUpdate (info->toString (), seq, oldSeq); |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 287 | } |
| 288 | } |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 289 | } |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 290 | else |
| 291 | { |
| 292 | DiffState diff; |
| 293 | ss >> diff; |
| 294 | BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>()) |
| 295 | { |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 296 | DiffLeafConstPtr diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf); |
| 297 | BOOST_ASSERT (diffLeaf != 0); |
| 298 | |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 299 | NameInfoConstPtr info = diffLeaf->getInfo(); |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 300 | if (diffLeaf->getOperation() == UPDATE) |
| 301 | { |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 302 | SeqNo seq = diffLeaf->getSeq(); |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 303 | |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 304 | bool inserted = false; |
| 305 | bool updated = false; |
| 306 | SeqNo oldSeq; |
| 307 | tie (inserted, updated, oldSeq) = m_state.update (info, seq); |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 308 | |
Alexander Afanasyev | d4adce5 | 2012-03-13 13:59:39 -0700 | [diff] [blame] | 309 | if (inserted || updated) |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 310 | { |
| 311 | diffLog->update (info, seq); |
Chaoyi Bian | 9813519 | 2012-03-27 18:34:11 -0700 | [diff] [blame] | 312 | m_onUpdate (info->toString (), seq, oldSeq); |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 313 | } |
| 314 | } |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 315 | else if (diffLeaf->getOperation() == REMOVE) |
| 316 | { |
| 317 | if (m_state.remove (info)) |
| 318 | { |
| 319 | diffLog->remove (info); |
| 320 | m_onRemove (info->toString ()); |
| 321 | } |
| 322 | } |
| 323 | else |
| 324 | { |
| 325 | BOOST_ASSERT (false); // just in case |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 330 | insertToDiffLog (diffLog); |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 331 | } |
| 332 | catch (Error::SyncXmlDecodingFailure &e) |
| 333 | { |
Alexander Afanasyev | d4cca47 | 2012-03-13 14:25:46 -0700 | [diff] [blame] | 334 | diffLog.reset (); |
| 335 | // don't do anything |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Alexander Afanasyev | d4cca47 | 2012-03-13 14:25:46 -0700 | [diff] [blame] | 338 | // if state has changed, then it is safe to express a new interest |
Alexander Afanasyev | 750d187 | 2012-03-12 15:33:56 -0700 | [diff] [blame] | 339 | if (diffLog->getLeaves ().size () > 0) |
| 340 | { |
Alexander Afanasyev | 181d7e5 | 2012-04-09 13:54:11 -0700 | [diff] [blame] | 341 | m_scheduler.schedule (TIME_SECONDS (0), |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 342 | bind (&SyncLogic::sendSyncInterest, this), |
| 343 | REEXPRESSING_INTEREST); |
Alexander Afanasyev | d4cca47 | 2012-03-13 14:25:46 -0700 | [diff] [blame] | 344 | } |
| 345 | else |
| 346 | { |
| 347 | // should not reexpress the same interest. Need at least wait for data lifetime |
| 348 | // Otherwise we will get immediate reply from the local daemon and there will be 100% utilization |
| 349 | m_scheduler.cancel (REEXPRESSING_INTEREST); |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 350 | // m_scheduler.schedule (posix_time::seconds (0), |
Alexander Afanasyev | 70f3205 | 2012-04-24 14:10:38 -0700 | [diff] [blame] | 351 | m_scheduler.schedule (TIME_MILLISECONDS (m_syncResponseFreshness) + TIME_MILLISECONDS (1), |
Alexander Afanasyev | d4cca47 | 2012-03-13 14:25:46 -0700 | [diff] [blame] | 352 | bind (&SyncLogic::sendSyncInterest, this), |
| 353 | REEXPRESSING_INTEREST); |
Alexander Afanasyev | 04fd8a8 | 2012-03-12 15:40:48 -0700 | [diff] [blame] | 354 | } |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 355 | } |
| 356 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 357 | void |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 358 | SyncLogic::satisfyPendingSyncInterests (DiffStatePtr diffLog) |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 359 | { |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 360 | vector<string> pis = m_syncInterestTable.fetchAll (); |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 361 | if (pis.size () > 0) |
| 362 | { |
Alexander Afanasyev | f07ab35 | 2012-03-13 12:57:46 -0700 | [diff] [blame] | 363 | stringstream ss; |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 364 | ss << *diffLog; |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 365 | bool satisfiedOwnInterest = false; |
| 366 | |
Alexander Afanasyev | f07ab35 | 2012-03-13 12:57:46 -0700 | [diff] [blame] | 367 | for (vector<string>::iterator ii = pis.begin(); ii != pis.end(); ++ii) |
| 368 | { |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 369 | _LOG_TRACE (">> D " << *ii); |
Alexander Afanasyev | f07ab35 | 2012-03-13 12:57:46 -0700 | [diff] [blame] | 370 | m_ccnxHandle->publishData (*ii, ss.str(), m_syncResponseFreshness); |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 371 | |
| 372 | { |
| 373 | recursive_mutex::scoped_lock lock (m_stateMutex); |
| 374 | // _LOG_DEBUG (*ii << " == " << m_outstandingInterest << " = " << (*ii == m_outstandingInterest)); |
| 375 | satisfiedOwnInterest = satisfiedOwnInterest || (*ii == m_outstandingInterest) || (*ii == (m_outstandingInterest + "/state")); |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | if (satisfiedOwnInterest) |
| 380 | { |
| 381 | _LOG_DEBUG ("Have satisfied our own interest. Scheduling interest reexpression"); |
| 382 | // we need to reexpress interest only if we satisfied our own interest |
Alexander Afanasyev | 181d7e5 | 2012-04-09 13:54:11 -0700 | [diff] [blame] | 383 | m_scheduler.schedule (TIME_SECONDS (0), |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 384 | bind (&SyncLogic::sendSyncInterest, this), |
| 385 | REEXPRESSING_INTEREST); |
Alexander Afanasyev | f07ab35 | 2012-03-13 12:57:46 -0700 | [diff] [blame] | 386 | } |
| 387 | } |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 388 | } |
| 389 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 390 | void |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 391 | SyncLogic::insertToDiffLog (DiffStatePtr diffLog) |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 392 | { |
| 393 | //cout << "Process Pending Interests" <<endl; |
| 394 | diffLog->setDigest (m_state.getDigest()); |
| 395 | if (m_log.size () > 0) |
| 396 | { |
| 397 | m_log.get<sequenced> ().front ()->setNext (diffLog); |
| 398 | } |
| 399 | m_log.erase (m_state.getDigest()); // remove diff state with the same digest. next pointers are still valid |
| 400 | /// @todo Optimization |
Alexander Afanasyev | 7df7333 | 2012-03-15 12:31:49 -0700 | [diff] [blame] | 401 | m_log.get<sequenced> ().push_front (diffLog); |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 402 | // _LOG_DEBUG (*diffLog->getDigest () << " " << m_log.size ()); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | void |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 406 | SyncLogic::addLocalNames (const string &prefix, uint32_t session, uint32_t seq) |
| 407 | { |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 408 | DiffStatePtr diff; |
| 409 | { |
| 410 | //cout << "Add local names" <<endl; |
| 411 | recursive_mutex::scoped_lock lock (m_stateMutex); |
| 412 | NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix); |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 413 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 414 | SeqNo seqN (session, seq); |
| 415 | m_state.update(info, seqN); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 416 | |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 417 | _LOG_DEBUG ("addLocalNames (): new state " << *m_state.getDigest ()); |
| 418 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 419 | diff = make_shared<DiffState>(); |
| 420 | diff->update(info, seqN); |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 421 | insertToDiffLog (diff); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 422 | } |
| 423 | |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 424 | // _LOG_DEBUG ("PIT size: " << m_syncInterestTable.size ()); |
| 425 | satisfyPendingSyncInterests (diff); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | void |
Alexander Afanasyev | d91497c | 2012-03-12 15:39:30 -0700 | [diff] [blame] | 429 | SyncLogic::remove(const string &prefix) |
| 430 | { |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 431 | DiffStatePtr diff; |
| 432 | { |
| 433 | recursive_mutex::scoped_lock lock (m_stateMutex); |
| 434 | NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix); |
| 435 | m_state.remove(info); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 436 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 437 | diff = make_shared<DiffState>(); |
| 438 | diff->remove(info); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 439 | |
Alexander Afanasyev | 235c6d7 | 2012-03-15 22:28:43 -0700 | [diff] [blame] | 440 | insertToDiffLog (diff); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 443 | satisfyPendingSyncInterests (diff); |
Zhenkai Zhu | 0efa37b | 2012-03-12 13:54:12 -0700 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | void |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 447 | SyncLogic::sendSyncInterest () |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 448 | { |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 449 | ostringstream os; |
Chaoyi Bian | 89ee2dc | 2012-03-09 14:06:01 -0800 | [diff] [blame] | 450 | |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 451 | { |
| 452 | // cout << "Sending Sync Interest" << endl; |
| 453 | recursive_mutex::scoped_lock lock (m_stateMutex); |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 454 | |
Alexander Afanasyev | 860e6fe | 2012-03-15 17:30:31 -0700 | [diff] [blame] | 455 | os << m_syncPrefix << "/" << *m_state.getDigest(); |
| 456 | |
| 457 | _LOG_TRACE (">> I " << os.str ()); |
| 458 | |
| 459 | m_outstandingInterest = os.str (); |
| 460 | } |
| 461 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 462 | m_ccnxHandle->sendInterest (os.str (), |
| 463 | bind (&SyncLogic::processSyncData, this, _1, _2)); |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 464 | |
| 465 | m_scheduler.cancel (REEXPRESSING_INTEREST); |
Alexander Afanasyev | 89571f1 | 2012-04-23 13:44:57 -0700 | [diff] [blame] | 466 | m_scheduler.schedule (TIME_SECONDS (m_syncInterestReexpress), |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 467 | bind (&SyncLogic::sendSyncInterest, this), |
| 468 | REEXPRESSING_INTEREST); |
Chaoyi Bian | 44fff0c | 2012-03-07 21:07:22 -0800 | [diff] [blame] | 469 | } |
| 470 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 471 | } |