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 | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 65 | m_ccnx->clearInterestFilter (*it); |
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); |
| 78 | m_ccnx->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); |
| 88 | m_ccnx->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 | |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 94 | void |
| 95 | ContentServer::filterAndServe (Name forwardingHint, const Name &interest) |
| 96 | { |
| 97 | // Format for files: /<forwarding-hint>/<device_name>/<appname>/file/<hash>/<segment> |
| 98 | // Format for actions: /<forwarding-hint>/<device_name>/<appname>/action/<shared-folder>/<action-seq> |
Zhenkai Zhu | c3a2787 | 2013-01-25 19:21:25 -0800 | [diff] [blame] | 99 | |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 100 | Name name = interest.getPartialName (forwardingHint.size()); |
| 101 | // name for files: /<device_name>/<appname>/file/<hash>/<segment> |
| 102 | // name for actions: /<device_name>/<appname>/action/<shared-folder>/<action-seq> |
| 103 | |
| 104 | if (name.size() >= 4 && name.getCompFromBackAsString (3) == m_appName) |
| 105 | { |
| 106 | string type = name.getCompFromBackAsString (2); |
| 107 | if (type == "file") |
| 108 | { |
| 109 | serve_File (forwardingHint, interest); |
| 110 | } |
| 111 | else if (type == "action") |
| 112 | { |
| 113 | serve_Action (forwardingHint, interest); |
| 114 | } |
| 115 | } |
| 116 | } |
Zhenkai Zhu | c3a2787 | 2013-01-25 19:21:25 -0800 | [diff] [blame] | 117 | |
| 118 | void |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 119 | ContentServer::serve_Action (Name forwardingHint, Name interest) |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 120 | { |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 121 | _LOG_DEBUG (">> content server serving ACTION, hint: " << forwardingHint << ", interest: " << interest); |
| 122 | m_scheduler->scheduleOneTimeTask (m_scheduler, 0, bind (&ContentServer::serve_Action_Execute, this, forwardingHint, interest), boost::lexical_cast<string>(interest)); |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 123 | // need to unlock ccnx mutex... or at least don't lock it |
| 124 | } |
| 125 | |
| 126 | void |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 127 | ContentServer::serve_File (Name forwardingHint, Name interest) |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 128 | { |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 129 | _LOG_DEBUG (">> content server serving FILE, hint: " << forwardingHint << ", interest: " << interest); |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 130 | |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 131 | m_scheduler->scheduleOneTimeTask (m_scheduler, 0, bind (&ContentServer::serve_File_Execute, this, forwardingHint, interest), boost::lexical_cast<string>(interest)); |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 132 | // need to unlock ccnx mutex... or at least don't lock it |
| 133 | } |
| 134 | |
| 135 | void |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 136 | ContentServer::serve_File_Execute (Name forwardingHint, Name interest) |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 137 | { |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 138 | // forwardingHint: /<forwarding-hint> |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 139 | // interest: /<forwarding-hint>/<device_name>/<appname>/file/<hash>/<segment> |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 140 | |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 141 | Name name = interest.getPartialName (forwardingHint.size()); |
| 142 | // name: /<device_name>/<appname>/file/<hash>/<segment> |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 143 | |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 144 | int64_t segment = name.getCompFromBackAsInt (0); |
| 145 | Name deviceName = name.getPartialName (0, name.size () - 4); |
| 146 | Hash hash (head(name.getCompFromBack (1)), name.getCompFromBack (1).size()); |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 147 | |
| 148 | _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] | 149 | |
| 150 | string hashStr = lexical_cast<string> (hash); |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame] | 151 | |
| 152 | ObjectDbPtr db; |
| 153 | |
| 154 | ScopedLock(m_dbCacheMutex); |
| 155 | { |
| 156 | DbCache::iterator it = m_dbCache.find(hash); |
| 157 | if (it != m_dbCache.end()) |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 158 | { |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame] | 159 | db = it->second; |
| 160 | } |
| 161 | else |
| 162 | { |
| 163 | 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] | 164 | { |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame] | 165 | db = boost::make_shared<ObjectDb>(m_dbFolder, hashStr); |
| 166 | m_dbCache.insert(make_pair(hash, db)); |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 167 | } |
| 168 | else |
| 169 | { |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame] | 170 | _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] | 171 | } |
| 172 | } |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame] | 173 | } |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 174 | |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame] | 175 | if (db) |
| 176 | { |
| 177 | BytesPtr co = db->fetchSegment (deviceName, segment); |
| 178 | if (co) |
| 179 | { |
| 180 | if (forwardingHint.size () == 0) |
| 181 | { |
| 182 | _LOG_DEBUG (ParsedContentObject (*co).name ()); |
| 183 | m_ccnx->putToCcnd (*co); |
| 184 | } |
| 185 | else |
| 186 | { |
| 187 | if (m_freshness > 0) |
| 188 | { |
| 189 | m_ccnx->publishData(interest, *co, m_freshness); |
| 190 | } |
| 191 | else |
| 192 | { |
| 193 | m_ccnx->publishData(interest, *co); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | } |
| 198 | else |
| 199 | { |
| 200 | _LOG_ERROR ("ObjectDd exists, but no segment " << segment << " for device: " << deviceName << ", file_hash: " << hash.shortHash ()); |
| 201 | } |
| 202 | |
| 203 | } |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | void |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 207 | ContentServer::serve_Action_Execute (Name forwardingHint, Name interest) |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 208 | { |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 209 | // forwardingHint: /<forwarding-hint> |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 210 | // interest: /<forwarding-hint>/<device_name>/<appname>/action/<shared-folder>/<action-seq> |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 211 | |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 212 | Name name = interest.getPartialName (forwardingHint.size()); |
| 213 | // name for actions: /<device_name>/<appname>/action/<shared-folder>/<action-seq> |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 214 | |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 215 | int64_t seqno = name.getCompFromBackAsInt (0); |
| 216 | Name deviceName = name.getPartialName (0, name.size () - 4); |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 217 | |
| 218 | _LOG_DEBUG (" server ACTION for device: " << deviceName << " and seqno: " << seqno); |
| 219 | |
| 220 | PcoPtr pco = m_actionLog->LookupActionPco (deviceName, seqno); |
| 221 | if (pco) |
| 222 | { |
| 223 | if (forwardingHint.size () == 0) |
| 224 | { |
| 225 | m_ccnx->putToCcnd (pco->buf ()); |
| 226 | } |
| 227 | else |
| 228 | { |
| 229 | const Bytes &content = pco->buf (); |
| 230 | if (m_freshness > 0) |
Zhenkai Zhu | bc73857 | 2013-01-23 22:46:11 -0800 | [diff] [blame] | 231 | { |
| 232 | m_ccnx->publishData(interest, content, m_freshness); |
| 233 | } |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 234 | else |
Zhenkai Zhu | bc73857 | 2013-01-23 22:46:11 -0800 | [diff] [blame] | 235 | { |
| 236 | m_ccnx->publishData(interest, content); |
| 237 | } |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 238 | } |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 239 | } |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 240 | else |
| 241 | { |
| 242 | _LOG_ERROR ("ACTION not found for device: " << deviceName << " and seqno: " << seqno); |
| 243 | } |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 244 | } |
Zhenkai Zhu | 92bb695 | 2013-02-06 16:43:30 -0800 | [diff] [blame] | 245 | |
| 246 | void |
| 247 | ContentServer::flushStaleDbCache() |
| 248 | { |
| 249 | ScopedLock(m_dbCacheMutex); |
| 250 | DbCache::iterator it = m_dbCache.begin(); |
| 251 | while (it != m_dbCache.end()) |
| 252 | { |
| 253 | ObjectDbPtr db = it->second; |
| 254 | if (db->secondsSinceLastUse() >= DB_CACHE_LIFETIME) |
| 255 | { |
| 256 | m_dbCache.erase(it++); |
| 257 | } |
| 258 | else |
| 259 | { |
| 260 | ++it; |
| 261 | } |
| 262 | } |
| 263 | } |