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 | |
| 22 | #include "content-server.h" |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 23 | #include "logging.h" |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame^] | 24 | #include <boost/make_shared.hpp> |
| 25 | #include <utility> |
| 26 | #include "task.h" |
| 27 | #include "periodic-task.h" |
| 28 | #include "simple-interval-generator.h" |
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 | |
| 33 | using namespace Ccnx; |
| 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 | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 39 | ContentServer::ContentServer(CcnxWrapperPtr ccnx, ActionLogPtr actionLog, |
| 40 | const boost::filesystem::path &rootDir, |
| 41 | const Ccnx::Name &deviceName, 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) |
| 44 | : m_ccnx(ccnx) |
| 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 | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 49 | , m_deviceName (deviceName) |
| 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); |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 63 | for (PrefixIt it = m_prefixes.begin(); it != m_prefixes.end(); ++it) |
| 64 | { |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 65 | Name filePrefix = Name (*it)(m_appName)("file"); |
| 66 | Name actionPrefix = Name (*it)(m_appName)(m_sharedFolderName)("action"); |
| 67 | |
| 68 | m_ccnx->clearInterestFilter(filePrefix); |
| 69 | m_ccnx->clearInterestFilter(actionPrefix); |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 70 | } |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 71 | |
| 72 | m_prefixes.clear (); |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 76 | ContentServer::registerPrefix(const Name &forwardingHint) |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 77 | { |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 78 | // Format for files: /<forwarding-hint>/<appname>/file/<hash>/<device_name>/<segment> |
| 79 | // Format for actions: /<forwarding-hint>/<appname>/<shared-folder>/action/<device_name>/<action-seq> |
Zhenkai Zhu | b74e1e9 | 2013-01-25 14:36:18 -0800 | [diff] [blame] | 80 | |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 81 | Name filePrefix = Name (forwardingHint)(m_appName)("file"); |
| 82 | Name actionPrefix = Name (forwardingHint)(m_appName)(m_sharedFolderName)("action"); |
| 83 | |
| 84 | m_ccnx->setInterestFilter (filePrefix, bind(&ContentServer::serve_File, this, forwardingHint, filePrefix, _1)); |
| 85 | m_ccnx->setInterestFilter (actionPrefix, bind(&ContentServer::serve_Action, this, forwardingHint, actionPrefix, _1)); |
| 86 | |
| 87 | _LOG_DEBUG (">> content server: register FILE " << filePrefix); |
| 88 | _LOG_DEBUG (">> content server: register ACTION " << actionPrefix); |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 89 | |
| 90 | ScopedLock lock (m_mutex); |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 91 | m_prefixes.insert(forwardingHint); |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | void |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 95 | ContentServer::deregisterPrefix (const Name &forwardingHint) |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 96 | { |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 97 | Name filePrefix = Name (forwardingHint)(m_appName)("file"); |
| 98 | Name actionPrefix = Name (forwardingHint)(m_appName)(m_sharedFolderName)("action"); |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 99 | |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 100 | m_ccnx->clearInterestFilter(filePrefix); |
| 101 | m_ccnx->clearInterestFilter(actionPrefix); |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 102 | |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 103 | _LOG_DEBUG ("<< content server: deregister FILE " << filePrefix); |
| 104 | _LOG_DEBUG ("<< content server: deregister ACTION " << actionPrefix); |
| 105 | |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 106 | ScopedLock lock (m_mutex); |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 107 | m_prefixes.erase (forwardingHint); |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 110 | // void |
| 111 | // ContentServer::serve(Name forwardingHint, const Name &interest) |
| 112 | // { |
| 113 | // // /forwardingHint/app-name/device-name/action/shared-folder/action-seq |
| 114 | // // /forwardingHint/app-name/device-name/file/file-hash/segment |
Zhenkai Zhu | c3a2787 | 2013-01-25 19:21:25 -0800 | [diff] [blame] | 115 | |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 116 | // Name name = interest.getPartialName(forwardingHint.size()); |
| 117 | // if (name.size() > 3) |
| 118 | // { |
| 119 | // string type = name.getCompAsString(name.size() - 3); |
| 120 | // if (type == "action") |
| 121 | // { |
| 122 | // serve_Action (forwardingHint, interest); |
| 123 | // } |
| 124 | // else if (type == "file") |
| 125 | // { |
| 126 | // serve_File (forwardingHint, interest); |
| 127 | // } |
| 128 | // } |
| 129 | // } |
Zhenkai Zhu | c3a2787 | 2013-01-25 19:21:25 -0800 | [diff] [blame] | 130 | |
| 131 | void |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 132 | ContentServer::serve_Action (Name forwardingHint, Name locatorPrefix, Name interest) |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 133 | { |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 134 | _LOG_DEBUG (">> content server serving ACTION, hint: " << forwardingHint << ", locatorPrefix: " << locatorPrefix << ", interest: " << interest); |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame^] | 135 | m_scheduler->scheduleOneTimeTask (m_scheduler, 0, bind (&ContentServer::serve_Action_Execute, this, forwardingHint, locatorPrefix, interest), boost::lexical_cast<string>(interest)); |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 136 | // need to unlock ccnx mutex... or at least don't lock it |
| 137 | } |
| 138 | |
| 139 | void |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 140 | ContentServer::serve_File (Name forwardingHint, Name locatorPrefix, Name interest) |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 141 | { |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 142 | _LOG_DEBUG (">> content server serving FILE, hint: " << forwardingHint << ", locatorPrefix: " << locatorPrefix << ", interest: " << interest); |
| 143 | |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame^] | 144 | m_scheduler->scheduleOneTimeTask (m_scheduler, 0, bind (&ContentServer::serve_File_Execute, this, forwardingHint, locatorPrefix, interest), boost::lexical_cast<string>(interest)); |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 145 | // need to unlock ccnx mutex... or at least don't lock it |
| 146 | } |
| 147 | |
| 148 | void |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 149 | ContentServer::serve_File_Execute (Name forwardingHint, Name locatorPrefix, Name interest) |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 150 | { |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 151 | // forwardingHint: /<forwarding-hint> |
| 152 | // locatorPrefix: /<forwarding-hint>/<appname>/file |
| 153 | // interest: /<forwarding-hint>/<appname>/file/<hash>/<device_name>/<segment> |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 154 | |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 155 | Name pureInterest = interest.getPartialName (locatorPrefix.size ()); |
| 156 | // pureInterest: /<hash>/<device_name>/<segment> |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 157 | |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 158 | int64_t segment = pureInterest.getCompFromBackAsInt (0); |
| 159 | Name deviceName = pureInterest.getPartialName (1, pureInterest.size () - 2); |
| 160 | Hash hash (head(pureInterest.getComp (0)), pureInterest.getComp (0).size()); |
| 161 | |
| 162 | _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] | 163 | |
| 164 | string hashStr = lexical_cast<string> (hash); |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame^] | 165 | |
| 166 | ObjectDbPtr db; |
| 167 | |
| 168 | ScopedLock(m_dbCacheMutex); |
| 169 | { |
| 170 | DbCache::iterator it = m_dbCache.find(hash); |
| 171 | if (it != m_dbCache.end()) |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 172 | { |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame^] | 173 | db = it->second; |
| 174 | } |
| 175 | else |
| 176 | { |
| 177 | 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] | 178 | { |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame^] | 179 | db = boost::make_shared<ObjectDb>(m_dbFolder, hashStr); |
| 180 | m_dbCache.insert(make_pair(hash, db)); |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 181 | } |
| 182 | else |
| 183 | { |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame^] | 184 | _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] | 185 | } |
| 186 | } |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame^] | 187 | } |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 188 | |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame^] | 189 | if (db) |
| 190 | { |
| 191 | BytesPtr co = db->fetchSegment (deviceName, segment); |
| 192 | if (co) |
| 193 | { |
| 194 | if (forwardingHint.size () == 0) |
| 195 | { |
| 196 | _LOG_DEBUG (ParsedContentObject (*co).name ()); |
| 197 | m_ccnx->putToCcnd (*co); |
| 198 | } |
| 199 | else |
| 200 | { |
| 201 | if (m_freshness > 0) |
| 202 | { |
| 203 | m_ccnx->publishData(interest, *co, m_freshness); |
| 204 | } |
| 205 | else |
| 206 | { |
| 207 | m_ccnx->publishData(interest, *co); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | } |
| 212 | else |
| 213 | { |
| 214 | _LOG_ERROR ("ObjectDd exists, but no segment " << segment << " for device: " << deviceName << ", file_hash: " << hash.shortHash ()); |
| 215 | } |
| 216 | |
| 217 | } |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | void |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 221 | ContentServer::serve_Action_Execute (Name forwardingHint, Name locatorPrefix, Name interest) |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 222 | { |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 223 | // forwardingHint: /<forwarding-hint> |
| 224 | // locatorPrefix: /<forwarding-hint>/<appname>/<shared-folder>/action |
| 225 | // interest: /<forwarding-hint>/<appname>/<shared-folder>/action/<device_name>/<action-seq> |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 226 | |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 227 | Name pureInterest = interest.getPartialName (locatorPrefix.size ()); |
| 228 | // pureInterest: /<device_name>/<action-seq> |
| 229 | |
| 230 | int64_t seqno = pureInterest.getCompFromBackAsInt (0); |
| 231 | Name deviceName = pureInterest.getPartialName (0, pureInterest.size () - 1); |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 232 | |
| 233 | _LOG_DEBUG (" server ACTION for device: " << deviceName << " and seqno: " << seqno); |
| 234 | |
| 235 | PcoPtr pco = m_actionLog->LookupActionPco (deviceName, seqno); |
| 236 | if (pco) |
| 237 | { |
| 238 | if (forwardingHint.size () == 0) |
| 239 | { |
| 240 | m_ccnx->putToCcnd (pco->buf ()); |
| 241 | } |
| 242 | else |
| 243 | { |
| 244 | const Bytes &content = pco->buf (); |
| 245 | if (m_freshness > 0) |
Zhenkai Zhu | bc73857 | 2013-01-23 22:46:11 -0800 | [diff] [blame] | 246 | { |
| 247 | m_ccnx->publishData(interest, content, m_freshness); |
| 248 | } |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 249 | else |
Zhenkai Zhu | bc73857 | 2013-01-23 22:46:11 -0800 | [diff] [blame] | 250 | { |
| 251 | m_ccnx->publishData(interest, content); |
| 252 | } |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 253 | } |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 254 | } |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 255 | else |
| 256 | { |
| 257 | _LOG_ERROR ("ACTION not found for device: " << deviceName << " and seqno: " << seqno); |
| 258 | } |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 259 | } |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame^] | 260 | |
| 261 | void |
| 262 | ContentServer::flushStaleDbCache() |
| 263 | { |
| 264 | ScopedLock(m_dbCacheMutex); |
| 265 | DbCache::iterator it = m_dbCache.begin(); |
| 266 | while (it != m_dbCache.end()) |
| 267 | { |
| 268 | ObjectDbPtr db = it->second; |
| 269 | if (db->secondsSinceLastUse() >= DB_CACHE_LIFETIME) |
| 270 | { |
| 271 | m_dbCache.erase(it++); |
| 272 | } |
| 273 | else |
| 274 | { |
| 275 | ++it; |
| 276 | } |
| 277 | } |
| 278 | } |