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" |
Lijing Wang | 6977ec4 | 2016-12-25 14:45:09 -0800 | [diff] [blame] | 22 | #include "core/logging.hpp" |
| 23 | |
| 24 | #include <ndn-cxx/util/string-helper.hpp> |
| 25 | #include <ndn-cxx/security/signing-helpers.hpp> |
| 26 | |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 27 | #include <boost/lexical_cast.hpp> |
Lijing Wang | 6977ec4 | 2016-12-25 14:45:09 -0800 | [diff] [blame] | 28 | |
| 29 | namespace ndn { |
| 30 | namespace chronoshare { |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 31 | |
Alexander Afanasyev | 1cf5c43 | 2017-01-13 23:22:15 -0800 | [diff] [blame] | 32 | _LOG_INIT(ContentServer); |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 33 | |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame] | 34 | static const int DB_CACHE_LIFETIME = 60; |
| 35 | |
Lijing Wang | 6977ec4 | 2016-12-25 14:45:09 -0800 | [diff] [blame] | 36 | ContentServer::ContentServer(Face& face, ActionLogPtr actionLog, |
| 37 | const boost::filesystem::path& rootDir, const Name& userName, |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 38 | const std::string& sharedFolderName, const name::Component& appName, |
| 39 | KeyChain& keyChain, time::milliseconds freshness) |
Lijing Wang | 6977ec4 | 2016-12-25 14:45:09 -0800 | [diff] [blame] | 40 | : m_face(face) |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 41 | , m_actionLog(actionLog) |
| 42 | , m_dbFolder(rootDir / ".chronoshare") |
| 43 | , m_freshness(freshness) |
Lijing Wang | 6977ec4 | 2016-12-25 14:45:09 -0800 | [diff] [blame] | 44 | , m_scheduler(face.getIoService()) |
| 45 | , m_flushStateDbCacheEvent(m_scheduler) |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 46 | , m_userName(userName) |
| 47 | , m_sharedFolderName(sharedFolderName) |
| 48 | , m_appName(appName) |
Lijing Wang | 6977ec4 | 2016-12-25 14:45:09 -0800 | [diff] [blame] | 49 | , m_keyChain(keyChain) |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 50 | { |
Lijing Wang | 6977ec4 | 2016-12-25 14:45:09 -0800 | [diff] [blame] | 51 | m_flushStateDbCacheEvent = m_scheduler.scheduleEvent(time::seconds(DB_CACHE_LIFETIME), |
| 52 | bind(&ContentServer::flushStaleDbCache, this)); |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | ContentServer::~ContentServer() |
| 56 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 57 | ScopedLock lock(m_mutex); |
Lijing Wang | 6977ec4 | 2016-12-25 14:45:09 -0800 | [diff] [blame] | 58 | for (FilterIdIt it = m_interestFilterIds.begin(); it != m_interestFilterIds.end(); ++it) { |
| 59 | m_face.unsetInterestFilter(it->second); |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 60 | } |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 61 | |
Lijing Wang | 6977ec4 | 2016-12-25 14:45:09 -0800 | [diff] [blame] | 62 | m_interestFilterIds.clear(); |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 66 | ContentServer::registerPrefix(const Name& forwardingHint) |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 67 | { |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 68 | // Format for files: /<forwarding-hint>/<device_name>/<appname>/file/<hash>/<segment> |
Lijing Wang | 6977ec4 | 2016-12-25 14:45:09 -0800 | [diff] [blame] | 69 | // Format for actions: |
| 70 | // /<forwarding-hint>/<device_name>/<appname>/action/<shared-folder>/<action-seq> |
Zhenkai Zhu | b74e1e9 | 2013-01-25 14:36:18 -0800 | [diff] [blame] | 71 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 72 | _LOG_DEBUG(">> content server: register " << forwardingHint); |
Alexander Afanasyev | 3cca13f | 2013-02-07 16:06:46 -0800 | [diff] [blame] | 73 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 74 | ScopedLock lock(m_mutex); |
Lijing Wang | 6977ec4 | 2016-12-25 14:45:09 -0800 | [diff] [blame] | 75 | m_interestFilterIds[forwardingHint] = |
| 76 | m_face.setInterestFilter(InterestFilter(forwardingHint), |
| 77 | bind(&ContentServer::filterAndServe, this, _1, _2), |
| 78 | RegisterPrefixSuccessCallback(), RegisterPrefixFailureCallback()); |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 82 | ContentServer::deregisterPrefix(const Name& forwardingHint) |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 83 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 84 | _LOG_DEBUG("<< content server: deregister " << forwardingHint); |
Lijing Wang | 6977ec4 | 2016-12-25 14:45:09 -0800 | [diff] [blame] | 85 | m_face.unsetInterestFilter(m_interestFilterIds[forwardingHint]); |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 86 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 87 | ScopedLock lock(m_mutex); |
Lijing Wang | 6977ec4 | 2016-12-25 14:45:09 -0800 | [diff] [blame] | 88 | m_interestFilterIds.erase(forwardingHint); |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Alexander Afanasyev | 3cca13f | 2013-02-07 16:06:46 -0800 | [diff] [blame] | 91 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 92 | ContentServer::filterAndServeImpl(const Name& forwardingHint, const Name& name, const Name& interest) |
Alexander Afanasyev | 3cca13f | 2013-02-07 16:06:46 -0800 | [diff] [blame] | 93 | { |
| 94 | // interest for files: /<forwarding-hint>/<device_name>/<appname>/file/<hash>/<segment> |
Lijing Wang | 6977ec4 | 2016-12-25 14:45:09 -0800 | [diff] [blame] | 95 | // interest for actions: |
| 96 | // /<forwarding-hint>/<device_name>/<appname>/action/<shared-folder>/<action-seq> |
Alexander Afanasyev | 3cca13f | 2013-02-07 16:06:46 -0800 | [diff] [blame] | 97 | |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 98 | // name for files: /<device_name>/<appname>/file/<hash>/<segment> |
| 99 | // name for actions: /<device_name>/<appname>/action/<shared-folder>/<action-seq> |
| 100 | |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 101 | if (name.size() >= 4 && name.get(-4) == m_appName) { |
Lijing Wang | 6977ec4 | 2016-12-25 14:45:09 -0800 | [diff] [blame] | 102 | std::string type = name.get(-3).toUri(); |
| 103 | if (type == "file") { |
| 104 | serve_File(forwardingHint, name, interest); |
| 105 | } |
| 106 | else if (type == "action") { |
| 107 | std::string folder = name.get(-2).toUri(); |
| 108 | if (folder == m_sharedFolderName) { |
| 109 | serve_Action(forwardingHint, name, interest); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 110 | } |
Alexander Afanasyev | c87e801 | 2013-02-12 14:24:24 -0800 | [diff] [blame] | 111 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 112 | } |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 113 | } |
Zhenkai Zhu | c3a2787 | 2013-01-25 19:21:25 -0800 | [diff] [blame] | 114 | |
| 115 | void |
Lijing Wang | 6977ec4 | 2016-12-25 14:45:09 -0800 | [diff] [blame] | 116 | ContentServer::filterAndServe(const InterestFilter& interestFilter, const Interest& interestTrue) |
Alexander Afanasyev | 3cca13f | 2013-02-07 16:06:46 -0800 | [diff] [blame] | 117 | { |
Lijing Wang | 6977ec4 | 2016-12-25 14:45:09 -0800 | [diff] [blame] | 118 | Name forwardingHint = Name(interestFilter); |
| 119 | Name interest = interestTrue.getName(); |
| 120 | _LOG_DEBUG("I'm serving ForwardingHint: " << forwardingHint << " Interest: " << interest); |
| 121 | if (forwardingHint.size() > 0 && m_userName.size() >= forwardingHint.size() && |
| 122 | m_userName.getSubName(0, forwardingHint.size()) == forwardingHint) { |
| 123 | _LOG_DEBUG("Triggered without Forwardinghint!"); |
| 124 | filterAndServeImpl(Name("/"), interest, interest); // try without forwarding hints |
| 125 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 126 | |
Lijing Wang | 6977ec4 | 2016-12-25 14:45:09 -0800 | [diff] [blame] | 127 | _LOG_DEBUG("Triggered with Forwardinghint~!"); |
| 128 | filterAndServeImpl(forwardingHint, interest.getSubName(forwardingHint.size()), |
| 129 | interest); // always try with hint... :( have to |
Alexander Afanasyev | 3cca13f | 2013-02-07 16:06:46 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 133 | ContentServer::serve_Action(const Name& forwardingHint, const Name& name, const Name& interest) |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 134 | { |
Lijing Wang | 6977ec4 | 2016-12-25 14:45:09 -0800 | [diff] [blame] | 135 | _LOG_DEBUG( |
| 136 | ">> content server serving ACTION, hint: " << forwardingHint << ", interest: " << interest); |
| 137 | m_scheduler.scheduleEvent(time::seconds(0), |
| 138 | bind(&ContentServer::serve_Action_Execute, this, forwardingHint, name, |
| 139 | interest)); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 140 | // need to unlock ccnx mutex... or at least don't lock it |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 144 | ContentServer::serve_File(const Name& forwardingHint, const Name& name, const Name& interest) |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 145 | { |
Lijing Wang | 6977ec4 | 2016-12-25 14:45:09 -0800 | [diff] [blame] | 146 | _LOG_DEBUG(">> content server serving FILE, hint: " << forwardingHint << ", interest: " << interest); |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 147 | |
Lijing Wang | 6977ec4 | 2016-12-25 14:45:09 -0800 | [diff] [blame] | 148 | m_scheduler.scheduleEvent(time::seconds(0), |
| 149 | bind(&ContentServer::serve_File_Execute, this, forwardingHint, name, |
| 150 | interest)); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 151 | // need to unlock ccnx mutex... or at least don't lock it |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 155 | 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] | 156 | { |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 157 | // forwardingHint: /<forwarding-hint> |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 158 | // interest: /<forwarding-hint>/<device_name>/<appname>/file/<hash>/<segment> |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 159 | // name: /<device_name>/<appname>/file/<hash>/<segment> |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 160 | |
Lijing Wang | 6977ec4 | 2016-12-25 14:45:09 -0800 | [diff] [blame] | 161 | int64_t segment = name.get(-1).toNumber(); |
| 162 | Name deviceName = name.getSubName(0, name.size() - 4); |
| 163 | Buffer hash(name.get(-2).value(), name.get(-2).value_size()); |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 164 | |
Lijing Wang | 6977ec4 | 2016-12-25 14:45:09 -0800 | [diff] [blame] | 165 | _LOG_DEBUG(" server FILE for device: " << deviceName << ", file_hash: " << toHex(hash) << " segment: " |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 166 | << segment); |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 167 | |
Lijing Wang | 6977ec4 | 2016-12-25 14:45:09 -0800 | [diff] [blame] | 168 | std::string hashStr = toHex(hash); |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame] | 169 | |
Lijing Wang | 6977ec4 | 2016-12-25 14:45:09 -0800 | [diff] [blame] | 170 | shared_ptr<ObjectDb> db; |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame] | 171 | |
| 172 | ScopedLock(m_dbCacheMutex); |
| 173 | { |
| 174 | DbCache::iterator it = m_dbCache.find(hash); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 175 | if (it != m_dbCache.end()) { |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame] | 176 | db = it->second; |
| 177 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 178 | else { |
Lijing Wang | 6977ec4 | 2016-12-25 14:45:09 -0800 | [diff] [blame] | 179 | if (ObjectDb::doesExist(m_dbFolder, deviceName, |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 180 | hashStr)) // this is kind of overkill, as it counts available segments |
| 181 | { |
Lijing Wang | 6977ec4 | 2016-12-25 14:45:09 -0800 | [diff] [blame] | 182 | db = make_shared<ObjectDb>(m_dbFolder, hashStr); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 183 | m_dbCache.insert(make_pair(hash, db)); |
| 184 | } |
| 185 | else { |
Lijing Wang | 6977ec4 | 2016-12-25 14:45:09 -0800 | [diff] [blame] | 186 | _LOG_ERROR( |
| 187 | "ObjectDd doesn't exist for device: " << deviceName << ", file_hash: " << toHex(hash)); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 188 | } |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 189 | } |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame] | 190 | } |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 191 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 192 | if (db) { |
Lijing Wang | 6977ec4 | 2016-12-25 14:45:09 -0800 | [diff] [blame] | 193 | shared_ptr<Data> data = db->fetchSegment(deviceName, segment); |
| 194 | if (data) { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 195 | if (forwardingHint.size() == 0) { |
Lijing Wang | 6977ec4 | 2016-12-25 14:45:09 -0800 | [diff] [blame] | 196 | m_face.put(*data); |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame] | 197 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 198 | else { |
Lijing Wang | 6977ec4 | 2016-12-25 14:45:09 -0800 | [diff] [blame] | 199 | shared_ptr<Data> outerData = make_shared<Data>(); |
| 200 | |
| 201 | outerData->setContent(data->wireEncode()); |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 202 | outerData->setFreshnessPeriod(m_freshness); |
Lijing Wang | 6977ec4 | 2016-12-25 14:45:09 -0800 | [diff] [blame] | 203 | outerData->setName(interest); |
| 204 | |
| 205 | m_keyChain.sign(*outerData, signingWithSha256());; |
| 206 | m_face.put(*outerData); |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame] | 207 | } |
Lijing Wang | 6977ec4 | 2016-12-25 14:45:09 -0800 | [diff] [blame] | 208 | _LOG_DEBUG("Send File Data Done!"); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 209 | } |
| 210 | else { |
| 211 | _LOG_ERROR("ObjectDd exists, but no segment " << segment << " for device: " << deviceName |
| 212 | << ", file_hash: " |
Lijing Wang | 6977ec4 | 2016-12-25 14:45:09 -0800 | [diff] [blame] | 213 | << toHex(hash)); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 214 | } |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame] | 215 | } |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 219 | 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] | 220 | { |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 221 | // forwardingHint: /<forwarding-hint> |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 222 | // interest: /<forwarding-hint>/<device_name>/<appname>/action/<shared-folder>/<action-seq> |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 223 | // name for actions: /<device_name>/<appname>/action/<shared-folder>/<action-seq> |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 224 | |
Lijing Wang | 6977ec4 | 2016-12-25 14:45:09 -0800 | [diff] [blame] | 225 | int64_t seqno = name.get(-1).toNumber(); |
| 226 | Name deviceName = name.getSubName(0, name.size() - 4); |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 227 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 228 | _LOG_DEBUG(" server ACTION for device: " << deviceName << " and seqno: " << seqno); |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 229 | |
Lijing Wang | 6977ec4 | 2016-12-25 14:45:09 -0800 | [diff] [blame] | 230 | shared_ptr<Data> data = m_actionLog->LookupActionData(deviceName, seqno); |
| 231 | if (data) { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 232 | if (forwardingHint.size() == 0) { |
Lijing Wang | 6977ec4 | 2016-12-25 14:45:09 -0800 | [diff] [blame] | 233 | m_keyChain.sign(*data); |
| 234 | m_face.put(*data); |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 235 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 236 | else { |
Lijing Wang | 6977ec4 | 2016-12-25 14:45:09 -0800 | [diff] [blame] | 237 | data->setName(interest); |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 238 | data->setFreshnessPeriod(m_freshness); |
Lijing Wang | 6977ec4 | 2016-12-25 14:45:09 -0800 | [diff] [blame] | 239 | m_keyChain.sign(*data); |
| 240 | m_face.put(*data); |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 241 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 242 | } |
| 243 | else { |
| 244 | _LOG_ERROR("ACTION not found for device: " << deviceName << " and seqno: " << seqno); |
| 245 | } |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 246 | } |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame] | 247 | |
| 248 | void |
| 249 | ContentServer::flushStaleDbCache() |
| 250 | { |
| 251 | ScopedLock(m_dbCacheMutex); |
| 252 | DbCache::iterator it = m_dbCache.begin(); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 253 | while (it != m_dbCache.end()) { |
Lijing Wang | 6977ec4 | 2016-12-25 14:45:09 -0800 | [diff] [blame] | 254 | shared_ptr<ObjectDb> db = it->second; |
| 255 | if (time::steady_clock::now() >= time::seconds(DB_CACHE_LIFETIME) + db->getLastUsed()) { |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame] | 256 | m_dbCache.erase(it++); |
| 257 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 258 | else { |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame] | 259 | ++it; |
| 260 | } |
| 261 | } |
Lijing Wang | 6977ec4 | 2016-12-25 14:45:09 -0800 | [diff] [blame] | 262 | |
| 263 | m_flushStateDbCacheEvent = m_scheduler.scheduleEvent(time::seconds(DB_CACHE_LIFETIME), |
| 264 | bind(&ContentServer::flushStaleDbCache, this)); |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame] | 265 | } |
Lijing Wang | 6977ec4 | 2016-12-25 14:45:09 -0800 | [diff] [blame] | 266 | |
| 267 | } // namespace chronoshare |
| 268 | } // namespace ndn |