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