Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 1cf5c43 | 2017-01-13 23:22:15 -0800 | [diff] [blame^] | 3 | * Copyright (c) 2013-2017, Regents of the University of California. |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 5 | * This file is part of ChronoShare, a decentralized file sharing application over NDN. |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 6 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 7 | * ChronoShare is free software: you can redistribute it and/or modify it under the terms |
| 8 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 9 | * version 3 of the License, or (at your option) any later version. |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 10 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 11 | * ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 14 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 15 | * You should have received copies of the GNU General Public License along with |
| 16 | * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * See AUTHORS.md for complete list of ChronoShare authors and contributors. |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 19 | */ |
| 20 | |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame] | 21 | #include "content-server.hpp" |
| 22 | #include "logging.hpp" |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame] | 23 | #include "periodic-task.hpp" |
| 24 | #include "simple-interval-generator.hpp" |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 25 | #include "task.hpp" |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 26 | #include <boost/lexical_cast.hpp> |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 27 | #include <boost/make_shared.hpp> |
| 28 | #include <utility> |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 29 | |
Alexander Afanasyev | 1cf5c43 | 2017-01-13 23:22:15 -0800 | [diff] [blame^] | 30 | _LOG_INIT(ContentServer); |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 31 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 32 | using namespace Ndnx; |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 33 | using namespace std; |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 34 | using namespace boost; |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 35 | |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame] | 36 | static const int DB_CACHE_LIFETIME = 60; |
| 37 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 38 | ContentServer::ContentServer(CcnxWrapperPtr ccnx, ActionLogPtr actionLog, |
| 39 | const boost::filesystem::path& rootDir, const Ccnx::Name& userName, |
| 40 | const std::string& sharedFolderName, const std::string& appName, |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 41 | int freshness) |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 42 | : m_ndnx(ndnx) |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 43 | , m_actionLog(actionLog) |
| 44 | , m_dbFolder(rootDir / ".chronoshare") |
| 45 | , m_freshness(freshness) |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 46 | , m_scheduler(new Scheduler()) |
| 47 | , m_userName(userName) |
| 48 | , m_sharedFolderName(sharedFolderName) |
| 49 | , m_appName(appName) |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 50 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 51 | m_scheduler->start(); |
| 52 | TaskPtr flushStaleDbCacheTask = |
| 53 | boost::make_shared<PeriodicTask>(boost::bind(&ContentServer::flushStaleDbCache, this), |
| 54 | "flush-state-db-cache", m_scheduler, |
| 55 | boost::make_shared<SimpleIntervalGenerator>(DB_CACHE_LIFETIME)); |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame] | 56 | m_scheduler->addTask(flushStaleDbCacheTask); |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | ContentServer::~ContentServer() |
| 60 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 61 | m_scheduler->shutdown(); |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 62 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 63 | ScopedLock lock(m_mutex); |
| 64 | for (PrefixIt forwardingHint = m_prefixes.begin(); forwardingHint != m_prefixes.end(); |
| 65 | ++forwardingHint) { |
| 66 | m_ccnx->clearInterestFilter(*forwardingHint); |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 67 | } |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 68 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 69 | m_prefixes.clear(); |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 73 | ContentServer::registerPrefix(const Name& forwardingHint) |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 74 | { |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 75 | // Format for files: /<forwarding-hint>/<device_name>/<appname>/file/<hash>/<segment> |
| 76 | // Format for actions: /<forwarding-hint>/<device_name>/<appname>/action/<shared-folder>/<action-seq> |
Zhenkai Zhu | b74e1e9 | 2013-01-25 14:36:18 -0800 | [diff] [blame] | 77 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 78 | _LOG_DEBUG(">> content server: register " << forwardingHint); |
Alexander Afanasyev | 3cca13f | 2013-02-07 16:06:46 -0800 | [diff] [blame] | 79 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 80 | m_ccnx->setInterestFilter(forwardingHint, |
| 81 | bind(&ContentServer::filterAndServe, this, forwardingHint, _1)); |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 82 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 83 | ScopedLock lock(m_mutex); |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 84 | m_prefixes.insert(forwardingHint); |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 88 | ContentServer::deregisterPrefix(const Name& forwardingHint) |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 89 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 90 | _LOG_DEBUG("<< content server: deregister " << forwardingHint); |
| 91 | m_ccnx->clearInterestFilter(forwardingHint); |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 92 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 93 | ScopedLock lock(m_mutex); |
| 94 | m_prefixes.erase(forwardingHint); |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Zhenkai Zhu | c3a2787 | 2013-01-25 19:21:25 -0800 | [diff] [blame] | 97 | |
Alexander Afanasyev | 3cca13f | 2013-02-07 16:06:46 -0800 | [diff] [blame] | 98 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 99 | ContentServer::filterAndServeImpl(const Name& forwardingHint, const Name& name, const Name& interest) |
Alexander Afanasyev | 3cca13f | 2013-02-07 16:06:46 -0800 | [diff] [blame] | 100 | { |
| 101 | // interest for files: /<forwarding-hint>/<device_name>/<appname>/file/<hash>/<segment> |
| 102 | // interest for actions: /<forwarding-hint>/<device_name>/<appname>/action/<shared-folder>/<action-seq> |
| 103 | |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 104 | // name for files: /<device_name>/<appname>/file/<hash>/<segment> |
| 105 | // name for actions: /<device_name>/<appname>/action/<shared-folder>/<action-seq> |
| 106 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 107 | try { |
| 108 | if (name.size() >= 4 && name.getCompFromBackAsString(3) == m_appName) { |
| 109 | string type = name.getCompFromBackAsString(2); |
| 110 | if (type == "file") { |
| 111 | serve_File(forwardingHint, name, interest); |
| 112 | } |
| 113 | else if (type == "action") { |
| 114 | string folder = name.getCompFromBackAsString(1); |
| 115 | if (folder == m_sharedFolderName) { |
| 116 | serve_Action(forwardingHint, name, interest); |
Alexander Afanasyev | c87e801 | 2013-02-12 14:24:24 -0800 | [diff] [blame] | 117 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 118 | } |
Alexander Afanasyev | c87e801 | 2013-02-12 14:24:24 -0800 | [diff] [blame] | 119 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 120 | } |
| 121 | catch (Ccnx::NameException& ne) { |
| 122 | // ignore any unexpected interests and errors |
| 123 | _LOG_ERROR(boost::get_error_info<Ccnx::error_info_str>(ne)); |
| 124 | } |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 125 | } |
Zhenkai Zhu | c3a2787 | 2013-01-25 19:21:25 -0800 | [diff] [blame] | 126 | |
| 127 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 128 | ContentServer::filterAndServe(Name forwardingHint, const Name& interest) |
Alexander Afanasyev | 3cca13f | 2013-02-07 16:06:46 -0800 | [diff] [blame] | 129 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 130 | try { |
| 131 | if (forwardingHint.size() > 0 && m_userName.size() >= forwardingHint.size() && |
| 132 | m_userName.getPartialName(0, forwardingHint.size()) == forwardingHint) { |
| 133 | filterAndServeImpl(Name("/"), interest, interest); // try without forwarding hints |
Alexander Afanasyev | c87e801 | 2013-02-12 14:24:24 -0800 | [diff] [blame] | 134 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 135 | |
| 136 | filterAndServeImpl(forwardingHint, interest.getPartialName(forwardingHint.size()), |
| 137 | interest); // always try with hint... :( have to |
| 138 | } |
| 139 | catch (Ccnx::NameException& ne) { |
| 140 | // ignore any unexpected interests and errors |
| 141 | _LOG_ERROR(boost::get_error_info<Ccnx::error_info_str>(ne)); |
| 142 | } |
Alexander Afanasyev | 3cca13f | 2013-02-07 16:06:46 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 146 | ContentServer::serve_Action(const Name& forwardingHint, const Name& name, const Name& interest) |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 147 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 148 | _LOG_DEBUG(">> content server serving ACTION, hint: " << forwardingHint |
| 149 | << ", interest: " << interest); |
| 150 | m_scheduler->scheduleOneTimeTask(m_scheduler, 0, bind(&ContentServer::serve_Action_Execute, this, |
| 151 | forwardingHint, name, interest), |
| 152 | boost::lexical_cast<string>(name)); |
| 153 | // need to unlock ccnx mutex... or at least don't lock it |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 157 | ContentServer::serve_File(const Name& forwardingHint, const Name& name, const Name& interest) |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 158 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 159 | _LOG_DEBUG(">> content server serving FILE, hint: " << forwardingHint |
| 160 | << ", interest: " << interest); |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 161 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 162 | m_scheduler->scheduleOneTimeTask(m_scheduler, 0, bind(&ContentServer::serve_File_Execute, this, |
| 163 | forwardingHint, name, interest), |
| 164 | boost::lexical_cast<string>(name)); |
| 165 | // need to unlock ccnx mutex... or at least don't lock it |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 169 | ContentServer::serve_File_Execute(const Name& forwardingHint, const Name& name, const Name& interest) |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 170 | { |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 171 | // forwardingHint: /<forwarding-hint> |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 172 | // interest: /<forwarding-hint>/<device_name>/<appname>/file/<hash>/<segment> |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 173 | // name: /<device_name>/<appname>/file/<hash>/<segment> |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 174 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 175 | int64_t segment = name.getCompFromBackAsInt(0); |
| 176 | Name deviceName = name.getPartialName(0, name.size() - 4); |
| 177 | Hash hash(head(name.getCompFromBack(1)), name.getCompFromBack(1).size()); |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 178 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 179 | _LOG_DEBUG(" server FILE for device: " << deviceName << ", file_hash: " << hash.shortHash() |
| 180 | << " segment: " |
| 181 | << segment); |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 182 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 183 | string hashStr = lexical_cast<string>(hash); |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame] | 184 | |
| 185 | ObjectDbPtr db; |
| 186 | |
| 187 | ScopedLock(m_dbCacheMutex); |
| 188 | { |
| 189 | DbCache::iterator it = m_dbCache.find(hash); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 190 | if (it != m_dbCache.end()) { |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame] | 191 | db = it->second; |
| 192 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 193 | else { |
| 194 | if (ObjectDb::DoesExist(m_dbFolder, deviceName, |
| 195 | hashStr)) // this is kind of overkill, as it counts available segments |
| 196 | { |
| 197 | db = boost::make_shared<ObjectDb>(m_dbFolder, hashStr); |
| 198 | m_dbCache.insert(make_pair(hash, db)); |
| 199 | } |
| 200 | else { |
| 201 | _LOG_ERROR("ObjectDd doesn't exist for device: " << deviceName << ", file_hash: " |
| 202 | << hash.shortHash()); |
| 203 | } |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 204 | } |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame] | 205 | } |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 206 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 207 | if (db) { |
| 208 | BytesPtr co = db->fetchSegment(deviceName, segment); |
| 209 | if (co) { |
| 210 | if (forwardingHint.size() == 0) { |
| 211 | _LOG_DEBUG(ParsedContentObject(*co).name()); |
| 212 | m_ccnx->putToCcnd(*co); |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame] | 213 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 214 | else { |
| 215 | if (m_freshness > 0) { |
| 216 | m_ccnx->publishData(interest, *co, m_freshness); |
| 217 | } |
| 218 | else { |
| 219 | m_ccnx->publishData(interest, *co); |
| 220 | } |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame] | 221 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 222 | } |
| 223 | else { |
| 224 | _LOG_ERROR("ObjectDd exists, but no segment " << segment << " for device: " << deviceName |
| 225 | << ", file_hash: " |
| 226 | << hash.shortHash()); |
| 227 | } |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame] | 228 | } |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 232 | ContentServer::serve_Action_Execute(const Name& forwardingHint, const Name& name, const Name& interest) |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 233 | { |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 234 | // forwardingHint: /<forwarding-hint> |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 235 | // interest: /<forwarding-hint>/<device_name>/<appname>/action/<shared-folder>/<action-seq> |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 236 | // name for actions: /<device_name>/<appname>/action/<shared-folder>/<action-seq> |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 237 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 238 | int64_t seqno = name.getCompFromBackAsInt(0); |
| 239 | Name deviceName = name.getPartialName(0, name.size() - 4); |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 240 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 241 | _LOG_DEBUG(" server ACTION for device: " << deviceName << " and seqno: " << seqno); |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 242 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 243 | PcoPtr pco = m_actionLog->LookupActionPco(deviceName, seqno); |
| 244 | if (pco) { |
| 245 | if (forwardingHint.size() == 0) { |
| 246 | m_ccnx->putToCcnd(pco->buf()); |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 247 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 248 | else { |
| 249 | const Bytes& content = pco->buf(); |
| 250 | if (m_freshness > 0) { |
| 251 | m_ccnx->publishData(interest, content, m_freshness); |
| 252 | } |
| 253 | else { |
| 254 | m_ccnx->publishData(interest, content); |
| 255 | } |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 256 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 257 | } |
| 258 | else { |
| 259 | _LOG_ERROR("ACTION not found for device: " << deviceName << " and seqno: " << seqno); |
| 260 | } |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 261 | } |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame] | 262 | |
| 263 | void |
| 264 | ContentServer::flushStaleDbCache() |
| 265 | { |
| 266 | ScopedLock(m_dbCacheMutex); |
| 267 | DbCache::iterator it = m_dbCache.begin(); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 268 | while (it != m_dbCache.end()) { |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame] | 269 | ObjectDbPtr db = it->second; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 270 | if (db->secondsSinceLastUse() >= DB_CACHE_LIFETIME) { |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame] | 271 | m_dbCache.erase(it++); |
| 272 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 273 | else { |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame] | 274 | ++it; |
| 275 | } |
| 276 | } |
| 277 | } |