blob: ceb971d28361da6a4916c5a7a1057420c31f5a88 [file] [log] [blame]
Zhenkai Zhue42b4572013-01-22 15:57:54 -08001/* -*- 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 Afanasyev28ca3ed2013-01-24 23:17:15 -080023#include "logging.h"
Zhenkai Zhu92bb6952013-02-06 16:43:30 -080024#include <boost/make_shared.hpp>
25#include <utility>
26#include "task.h"
27#include "periodic-task.h"
28#include "simple-interval-generator.h"
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -080029#include <boost/lexical_cast.hpp>
30
31INIT_LOGGER ("ContentServer");
Zhenkai Zhue42b4572013-01-22 15:57:54 -080032
33using namespace Ccnx;
34using namespace std;
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -080035using namespace boost;
Zhenkai Zhue42b4572013-01-22 15:57:54 -080036
Zhenkai Zhu92bb6952013-02-06 16:43:30 -080037static const int DB_CACHE_LIFETIME = 60;
38
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -080039ContentServer::ContentServer(CcnxWrapperPtr ccnx, ActionLogPtr actionLog,
40 const boost::filesystem::path &rootDir,
Alexander Afanasyev3cca13f2013-02-07 16:06:46 -080041 const Ccnx::Name &userName, const std::string &sharedFolderName,
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080042 const std::string &appName,
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -080043 int freshness)
44 : m_ccnx(ccnx)
45 , m_actionLog(actionLog)
46 , m_dbFolder(rootDir / ".chronoshare")
47 , m_freshness(freshness)
Zhenkai Zhu92bb6952013-02-06 16:43:30 -080048 , m_scheduler (new Scheduler())
Alexander Afanasyev3cca13f2013-02-07 16:06:46 -080049 , m_userName (userName)
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -080050 , m_sharedFolderName (sharedFolderName)
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080051 , m_appName (appName)
Zhenkai Zhue42b4572013-01-22 15:57:54 -080052{
Zhenkai Zhu92bb6952013-02-06 16:43:30 -080053 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 Zhue42b4572013-01-22 15:57:54 -080056}
57
58ContentServer::~ContentServer()
59{
Zhenkai Zhu92bb6952013-02-06 16:43:30 -080060 m_scheduler->shutdown ();
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -080061
62 ScopedLock lock (m_mutex);
Alexander Afanasyev3cca13f2013-02-07 16:06:46 -080063 for (PrefixIt forwardingHint = m_prefixes.begin(); forwardingHint != m_prefixes.end(); ++forwardingHint)
Zhenkai Zhue42b4572013-01-22 15:57:54 -080064 {
Alexander Afanasyev3cca13f2013-02-07 16:06:46 -080065 m_ccnx->clearInterestFilter (*forwardingHint);
Zhenkai Zhue42b4572013-01-22 15:57:54 -080066 }
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080067
68 m_prefixes.clear ();
Zhenkai Zhue42b4572013-01-22 15:57:54 -080069}
70
71void
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080072ContentServer::registerPrefix(const Name &forwardingHint)
Zhenkai Zhue42b4572013-01-22 15:57:54 -080073{
Alexander Afanasyev4d086752013-02-07 13:06:04 -080074 // 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 Zhub74e1e92013-01-25 14:36:18 -080076
Alexander Afanasyev4d086752013-02-07 13:06:04 -080077 _LOG_DEBUG (">> content server: register " << forwardingHint);
Alexander Afanasyev3cca13f2013-02-07 16:06:46 -080078
Alexander Afanasyev4d086752013-02-07 13:06:04 -080079 m_ccnx->setInterestFilter (forwardingHint, bind(&ContentServer::filterAndServe, this, forwardingHint, _1));
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -080080
81 ScopedLock lock (m_mutex);
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080082 m_prefixes.insert(forwardingHint);
Zhenkai Zhue42b4572013-01-22 15:57:54 -080083}
84
85void
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080086ContentServer::deregisterPrefix (const Name &forwardingHint)
Zhenkai Zhue42b4572013-01-22 15:57:54 -080087{
Alexander Afanasyev4d086752013-02-07 13:06:04 -080088 _LOG_DEBUG ("<< content server: deregister " << forwardingHint);
89 m_ccnx->clearInterestFilter(forwardingHint);
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080090
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -080091 ScopedLock lock (m_mutex);
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080092 m_prefixes.erase (forwardingHint);
Zhenkai Zhue42b4572013-01-22 15:57:54 -080093}
94
Zhenkai Zhuc3a27872013-01-25 19:21:25 -080095
Alexander Afanasyev3cca13f2013-02-07 16:06:46 -080096void
97ContentServer::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 Afanasyev4d086752013-02-07 13:06:04 -0800102 // name for files: /<device_name>/<appname>/file/<hash>/<segment>
103 // name for actions: /<device_name>/<appname>/action/<shared-folder>/<action-seq>
104
Alexander Afanasyevc87e8012013-02-12 14:24:24 -0800105 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 {
116 serve_Action (forwardingHint, name, interest);
117 }
118 }
119 }
120 catch (...)
121 {
122 // ignore any unexpected interests and errors
123 }
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800124}
Zhenkai Zhuc3a27872013-01-25 19:21:25 -0800125
126void
Alexander Afanasyev3cca13f2013-02-07 16:06:46 -0800127ContentServer::filterAndServe (Name forwardingHint, const Name &interest)
128{
Alexander Afanasyevc87e8012013-02-12 14:24:24 -0800129 try
Alexander Afanasyev3cca13f2013-02-07 16:06:46 -0800130 {
Alexander Afanasyevc87e8012013-02-12 14:24:24 -0800131 if (forwardingHint.size () > 0 &&
132 m_userName.size () >= forwardingHint.size () &&
133 m_userName.getPartialName (0, forwardingHint.size ()) == forwardingHint)
134 {
135 filterAndServeImpl (Name ("/"), interest, interest); // try without forwarding hints
136 }
Alexander Afanasyev3cca13f2013-02-07 16:06:46 -0800137
Alexander Afanasyevc87e8012013-02-12 14:24:24 -0800138 filterAndServeImpl (forwardingHint, interest.getPartialName (forwardingHint.size()), interest); // always try with hint... :( have to
139 }
140 catch (...)
141 {
142 // ignore any unexpected interests and errors
143 }
Alexander Afanasyev3cca13f2013-02-07 16:06:46 -0800144}
145
146void
147ContentServer::serve_Action (const Name &forwardingHint, const Name &name, const Name &interest)
Zhenkai Zhue42b4572013-01-22 15:57:54 -0800148{
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800149 _LOG_DEBUG (">> content server serving ACTION, hint: " << forwardingHint << ", interest: " << interest);
Alexander Afanasyev3cca13f2013-02-07 16:06:46 -0800150 m_scheduler->scheduleOneTimeTask (m_scheduler, 0, bind (&ContentServer::serve_Action_Execute, this, forwardingHint, name, interest), boost::lexical_cast<string>(name));
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800151 // need to unlock ccnx mutex... or at least don't lock it
152}
153
154void
Alexander Afanasyev3cca13f2013-02-07 16:06:46 -0800155ContentServer::serve_File (const Name &forwardingHint, const Name &name, const Name &interest)
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800156{
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800157 _LOG_DEBUG (">> content server serving FILE, hint: " << forwardingHint << ", interest: " << interest);
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800158
Alexander Afanasyev3cca13f2013-02-07 16:06:46 -0800159 m_scheduler->scheduleOneTimeTask (m_scheduler, 0, bind (&ContentServer::serve_File_Execute, this, forwardingHint, name, interest), boost::lexical_cast<string>(name));
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800160 // need to unlock ccnx mutex... or at least don't lock it
161}
162
163void
Alexander Afanasyev3cca13f2013-02-07 16:06:46 -0800164ContentServer::serve_File_Execute (const Name &forwardingHint, const Name &name, const Name &interest)
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800165{
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800166 // forwardingHint: /<forwarding-hint>
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800167 // interest: /<forwarding-hint>/<device_name>/<appname>/file/<hash>/<segment>
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800168 // name: /<device_name>/<appname>/file/<hash>/<segment>
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800169
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800170 int64_t segment = name.getCompFromBackAsInt (0);
171 Name deviceName = name.getPartialName (0, name.size () - 4);
172 Hash hash (head(name.getCompFromBack (1)), name.getCompFromBack (1).size());
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800173
174 _LOG_DEBUG (" server FILE for device: " << deviceName << ", file_hash: " << hash.shortHash () << " segment: " << segment);
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800175
176 string hashStr = lexical_cast<string> (hash);
Zhenkai Zhu92bb6952013-02-06 16:43:30 -0800177
178 ObjectDbPtr db;
179
180 ScopedLock(m_dbCacheMutex);
181 {
182 DbCache::iterator it = m_dbCache.find(hash);
183 if (it != m_dbCache.end())
Zhenkai Zhue42b4572013-01-22 15:57:54 -0800184 {
Zhenkai Zhu92bb6952013-02-06 16:43:30 -0800185 db = it->second;
186 }
187 else
188 {
189 if (ObjectDb::DoesExist (m_dbFolder, deviceName, hashStr)) // this is kind of overkill, as it counts available segments
Zhenkai Zhue42b4572013-01-22 15:57:54 -0800190 {
Zhenkai Zhu92bb6952013-02-06 16:43:30 -0800191 db = boost::make_shared<ObjectDb>(m_dbFolder, hashStr);
192 m_dbCache.insert(make_pair(hash, db));
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800193 }
194 else
195 {
Zhenkai Zhu92bb6952013-02-06 16:43:30 -0800196 _LOG_ERROR ("ObjectDd doesn't exist for device: " << deviceName << ", file_hash: " << hash.shortHash ());
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800197 }
198 }
Zhenkai Zhu92bb6952013-02-06 16:43:30 -0800199 }
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800200
Zhenkai Zhu92bb6952013-02-06 16:43:30 -0800201 if (db)
202 {
203 BytesPtr co = db->fetchSegment (deviceName, segment);
204 if (co)
205 {
206 if (forwardingHint.size () == 0)
207 {
208 _LOG_DEBUG (ParsedContentObject (*co).name ());
209 m_ccnx->putToCcnd (*co);
210 }
211 else
212 {
213 if (m_freshness > 0)
214 {
215 m_ccnx->publishData(interest, *co, m_freshness);
216 }
217 else
218 {
219 m_ccnx->publishData(interest, *co);
220 }
221 }
222
223 }
224 else
225 {
226 _LOG_ERROR ("ObjectDd exists, but no segment " << segment << " for device: " << deviceName << ", file_hash: " << hash.shortHash ());
227 }
228
229 }
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800230}
231
232void
Alexander Afanasyev3cca13f2013-02-07 16:06:46 -0800233ContentServer::serve_Action_Execute (const Name &forwardingHint, const Name &name, const Name &interest)
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800234{
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800235 // forwardingHint: /<forwarding-hint>
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800236 // interest: /<forwarding-hint>/<device_name>/<appname>/action/<shared-folder>/<action-seq>
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800237 // name for actions: /<device_name>/<appname>/action/<shared-folder>/<action-seq>
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800238
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800239 int64_t seqno = name.getCompFromBackAsInt (0);
240 Name deviceName = name.getPartialName (0, name.size () - 4);
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800241
242 _LOG_DEBUG (" server ACTION for device: " << deviceName << " and seqno: " << seqno);
243
244 PcoPtr pco = m_actionLog->LookupActionPco (deviceName, seqno);
245 if (pco)
246 {
247 if (forwardingHint.size () == 0)
248 {
249 m_ccnx->putToCcnd (pco->buf ());
250 }
251 else
252 {
253 const Bytes &content = pco->buf ();
254 if (m_freshness > 0)
Zhenkai Zhubc738572013-01-23 22:46:11 -0800255 {
256 m_ccnx->publishData(interest, content, m_freshness);
257 }
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800258 else
Zhenkai Zhubc738572013-01-23 22:46:11 -0800259 {
260 m_ccnx->publishData(interest, content);
261 }
Zhenkai Zhue42b4572013-01-22 15:57:54 -0800262 }
Zhenkai Zhue42b4572013-01-22 15:57:54 -0800263 }
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800264 else
265 {
266 _LOG_ERROR ("ACTION not found for device: " << deviceName << " and seqno: " << seqno);
267 }
Zhenkai Zhue42b4572013-01-22 15:57:54 -0800268}
Zhenkai Zhu92bb6952013-02-06 16:43:30 -0800269
270void
271ContentServer::flushStaleDbCache()
272{
273 ScopedLock(m_dbCacheMutex);
274 DbCache::iterator it = m_dbCache.begin();
275 while (it != m_dbCache.end())
276 {
277 ObjectDbPtr db = it->second;
278 if (db->secondsSinceLastUse() >= DB_CACHE_LIFETIME)
279 {
280 m_dbCache.erase(it++);
281 }
282 else
283 {
284 ++it;
285 }
286 }
287}