blob: c84c6b633d38c7ce9f57d207572f84189bb299ee [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
105 if (name.size() >= 4 && name.getCompFromBackAsString (3) == m_appName)
106 {
107 string type = name.getCompFromBackAsString (2);
108 if (type == "file")
109 {
Alexander Afanasyev3cca13f2013-02-07 16:06:46 -0800110 serve_File (forwardingHint, name, interest);
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800111 }
112 else if (type == "action")
113 {
Alexander Afanasyev3cca13f2013-02-07 16:06:46 -0800114 serve_Action (forwardingHint, name, interest);
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800115 }
116 }
117}
Zhenkai Zhuc3a27872013-01-25 19:21:25 -0800118
119void
Alexander Afanasyev3cca13f2013-02-07 16:06:46 -0800120ContentServer::filterAndServe (Name forwardingHint, const Name &interest)
121{
122 if (forwardingHint.size () > 0 &&
123 m_userName.size () >= forwardingHint.size () &&
124 m_userName.getPartialName (0, forwardingHint.size ()) == forwardingHint)
125 {
126 filterAndServeImpl (Name ("/"), interest, interest); // try without forwarding hints
127 }
128
129 filterAndServeImpl (forwardingHint, interest.getPartialName (forwardingHint.size()), interest); // always try with hint... :( have to
130}
131
132void
133ContentServer::serve_Action (const Name &forwardingHint, const Name &name, const Name &interest)
Zhenkai Zhue42b4572013-01-22 15:57:54 -0800134{
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800135 _LOG_DEBUG (">> content server serving ACTION, hint: " << forwardingHint << ", interest: " << interest);
Alexander Afanasyev3cca13f2013-02-07 16:06:46 -0800136 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 -0800137 // need to unlock ccnx mutex... or at least don't lock it
138}
139
140void
Alexander Afanasyev3cca13f2013-02-07 16:06:46 -0800141ContentServer::serve_File (const Name &forwardingHint, const Name &name, const Name &interest)
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800142{
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800143 _LOG_DEBUG (">> content server serving FILE, hint: " << forwardingHint << ", interest: " << interest);
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800144
Alexander Afanasyev3cca13f2013-02-07 16:06:46 -0800145 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 -0800146 // need to unlock ccnx mutex... or at least don't lock it
147}
148
149void
Alexander Afanasyev3cca13f2013-02-07 16:06:46 -0800150ContentServer::serve_File_Execute (const Name &forwardingHint, const Name &name, const Name &interest)
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800151{
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800152 // forwardingHint: /<forwarding-hint>
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800153 // interest: /<forwarding-hint>/<device_name>/<appname>/file/<hash>/<segment>
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800154 // name: /<device_name>/<appname>/file/<hash>/<segment>
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800155
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800156 int64_t segment = name.getCompFromBackAsInt (0);
157 Name deviceName = name.getPartialName (0, name.size () - 4);
158 Hash hash (head(name.getCompFromBack (1)), name.getCompFromBack (1).size());
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800159
160 _LOG_DEBUG (" server FILE for device: " << deviceName << ", file_hash: " << hash.shortHash () << " segment: " << segment);
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800161
162 string hashStr = lexical_cast<string> (hash);
Zhenkai Zhu92bb6952013-02-06 16:43:30 -0800163
164 ObjectDbPtr db;
165
166 ScopedLock(m_dbCacheMutex);
167 {
168 DbCache::iterator it = m_dbCache.find(hash);
169 if (it != m_dbCache.end())
Zhenkai Zhue42b4572013-01-22 15:57:54 -0800170 {
Zhenkai Zhu92bb6952013-02-06 16:43:30 -0800171 db = it->second;
172 }
173 else
174 {
175 if (ObjectDb::DoesExist (m_dbFolder, deviceName, hashStr)) // this is kind of overkill, as it counts available segments
Zhenkai Zhue42b4572013-01-22 15:57:54 -0800176 {
Zhenkai Zhu92bb6952013-02-06 16:43:30 -0800177 db = boost::make_shared<ObjectDb>(m_dbFolder, hashStr);
178 m_dbCache.insert(make_pair(hash, db));
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800179 }
180 else
181 {
Zhenkai Zhu92bb6952013-02-06 16:43:30 -0800182 _LOG_ERROR ("ObjectDd doesn't exist for device: " << deviceName << ", file_hash: " << hash.shortHash ());
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800183 }
184 }
Zhenkai Zhu92bb6952013-02-06 16:43:30 -0800185 }
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800186
Zhenkai Zhu92bb6952013-02-06 16:43:30 -0800187 if (db)
188 {
189 BytesPtr co = db->fetchSegment (deviceName, segment);
190 if (co)
191 {
192 if (forwardingHint.size () == 0)
193 {
194 _LOG_DEBUG (ParsedContentObject (*co).name ());
195 m_ccnx->putToCcnd (*co);
196 }
197 else
198 {
199 if (m_freshness > 0)
200 {
201 m_ccnx->publishData(interest, *co, m_freshness);
202 }
203 else
204 {
205 m_ccnx->publishData(interest, *co);
206 }
207 }
208
209 }
210 else
211 {
212 _LOG_ERROR ("ObjectDd exists, but no segment " << segment << " for device: " << deviceName << ", file_hash: " << hash.shortHash ());
213 }
214
215 }
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800216}
217
218void
Alexander Afanasyev3cca13f2013-02-07 16:06:46 -0800219ContentServer::serve_Action_Execute (const Name &forwardingHint, const Name &name, const Name &interest)
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800220{
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800221 // forwardingHint: /<forwarding-hint>
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800222 // interest: /<forwarding-hint>/<device_name>/<appname>/action/<shared-folder>/<action-seq>
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800223 // name for actions: /<device_name>/<appname>/action/<shared-folder>/<action-seq>
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800224
Alexander Afanasyev4d086752013-02-07 13:06:04 -0800225 int64_t seqno = name.getCompFromBackAsInt (0);
226 Name deviceName = name.getPartialName (0, name.size () - 4);
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800227
228 _LOG_DEBUG (" server ACTION for device: " << deviceName << " and seqno: " << seqno);
229
230 PcoPtr pco = m_actionLog->LookupActionPco (deviceName, seqno);
231 if (pco)
232 {
233 if (forwardingHint.size () == 0)
234 {
235 m_ccnx->putToCcnd (pco->buf ());
236 }
237 else
238 {
239 const Bytes &content = pco->buf ();
240 if (m_freshness > 0)
Zhenkai Zhubc738572013-01-23 22:46:11 -0800241 {
242 m_ccnx->publishData(interest, content, m_freshness);
243 }
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800244 else
Zhenkai Zhubc738572013-01-23 22:46:11 -0800245 {
246 m_ccnx->publishData(interest, content);
247 }
Zhenkai Zhue42b4572013-01-22 15:57:54 -0800248 }
Zhenkai Zhue42b4572013-01-22 15:57:54 -0800249 }
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800250 else
251 {
252 _LOG_ERROR ("ACTION not found for device: " << deviceName << " and seqno: " << seqno);
253 }
Zhenkai Zhue42b4572013-01-22 15:57:54 -0800254}
Zhenkai Zhu92bb6952013-02-06 16:43:30 -0800255
256void
257ContentServer::flushStaleDbCache()
258{
259 ScopedLock(m_dbCacheMutex);
260 DbCache::iterator it = m_dbCache.begin();
261 while (it != m_dbCache.end())
262 {
263 ObjectDbPtr db = it->second;
264 if (db->secondsSinceLastUse() >= DB_CACHE_LIFETIME)
265 {
266 m_dbCache.erase(it++);
267 }
268 else
269 {
270 ++it;
271 }
272 }
273}