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