blob: 0cf5c438152581939a4dc5c4d4d55c1e34a4b07f [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"
24#include <boost/lexical_cast.hpp>
25
26INIT_LOGGER ("ContentServer");
Zhenkai Zhue42b4572013-01-22 15:57:54 -080027
28using namespace Ccnx;
29using namespace std;
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -080030using namespace boost;
Zhenkai Zhue42b4572013-01-22 15:57:54 -080031
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -080032ContentServer::ContentServer(CcnxWrapperPtr ccnx, ActionLogPtr actionLog,
33 const boost::filesystem::path &rootDir,
34 const Ccnx::Name &deviceName, const std::string &sharedFolderName,
35 int freshness)
36 : m_ccnx(ccnx)
37 , m_actionLog(actionLog)
38 , m_dbFolder(rootDir / ".chronoshare")
39 , m_freshness(freshness)
40 , m_executor (1)
41 , m_deviceName (deviceName)
42 , m_sharedFolderName (sharedFolderName)
Zhenkai Zhue42b4572013-01-22 15:57:54 -080043{
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -080044 m_executor.start ();
Zhenkai Zhue42b4572013-01-22 15:57:54 -080045}
46
47ContentServer::~ContentServer()
48{
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -080049 m_executor.shutdown ();
50
51 ScopedLock lock (m_mutex);
Zhenkai Zhue42b4572013-01-22 15:57:54 -080052 for (PrefixIt it = m_prefixes.begin(); it != m_prefixes.end(); ++it)
53 {
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -080054 m_ccnx->clearInterestFilter (Name (*it)(m_deviceName)("action")(m_sharedFolderName));
55 m_ccnx->clearInterestFilter (Name (*it)(m_deviceName)("file"));
Zhenkai Zhue42b4572013-01-22 15:57:54 -080056 }
57}
58
59void
60ContentServer::registerPrefix(const Name &prefix)
61{
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -080062 _LOG_DEBUG (">> register " << prefix);
63 m_ccnx->setInterestFilter (Name (prefix)(m_deviceName)("action")(m_sharedFolderName), bind(&ContentServer::serve_Action, this, prefix, _1));
64 m_ccnx->setInterestFilter (Name (prefix)(m_deviceName)("file"), bind(&ContentServer::serve_File, this, prefix, _1));
65
66 ScopedLock lock (m_mutex);
Zhenkai Zhue42b4572013-01-22 15:57:54 -080067 m_prefixes.insert(prefix);
68}
69
70void
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -080071ContentServer::deregisterPrefix (const Name &prefix)
Zhenkai Zhue42b4572013-01-22 15:57:54 -080072{
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -080073 _LOG_DEBUG ("<< deregister " << prefix);
74
75 m_ccnx->clearInterestFilter(Name (prefix)(m_deviceName)("action")(m_sharedFolderName));
76 m_ccnx->clearInterestFilter(Name (prefix)(m_deviceName)("file"));
77
78 ScopedLock lock (m_mutex);
79 m_prefixes.erase (prefix);
Zhenkai Zhue42b4572013-01-22 15:57:54 -080080}
81
82void
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -080083ContentServer::serve_Action (Name forwardingHint, const Name &interest)
Zhenkai Zhue42b4572013-01-22 15:57:54 -080084{
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -080085 m_executor.execute (bind (&ContentServer::serve_Action_Execute, this, forwardingHint, interest));
86 // need to unlock ccnx mutex... or at least don't lock it
87}
88
89void
90ContentServer::serve_File (Name forwardingHint, const Name &interest)
91{
92 m_executor.execute (bind (&ContentServer::serve_File_Execute, this, forwardingHint, interest));
93 // need to unlock ccnx mutex... or at least don't lock it
94}
95
96void
97ContentServer::serve_File_Execute (Name forwardingHint, Name interest)
98{
99 // /device-name/file/<file-hash>/segment, or
100
101 int64_t segment = interest.getCompFromBackAsInt (0);
102 Name deviceName = interest.getPartialName (forwardingHint.size (), interest.size () - forwardingHint.size () - 3);
103 Hash hash (head(interest.getCompFromBack (1)), interest.getCompFromBack (1).size());
104
105 _LOG_DEBUG (" server FILE for device: " << deviceName << ", file_hash: " << hash << " segment: " << segment);
106
107 string hashStr = lexical_cast<string> (hash);
108 if (ObjectDb::DoesExist (m_dbFolder, deviceName, hashStr)) // this is kind of overkill, as it counts available segments
Zhenkai Zhue42b4572013-01-22 15:57:54 -0800109 {
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800110 ObjectDb db (m_dbFolder, hashStr);
111 // may do prefetching
112
113 BytesPtr co = db.fetchSegment (deviceName, segment);
114 if (co)
Zhenkai Zhue42b4572013-01-22 15:57:54 -0800115 {
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800116 if (forwardingHint.size () == 0)
117 {
118 m_ccnx->putToCcnd (*co);
119 }
120 else
121 {
122 if (m_freshness > 0)
123 {
124 m_ccnx->publishData(interest, *co, m_freshness);
125 }
126 else
127 {
128 m_ccnx->publishData(interest, *co);
129 }
130 }
131
132 }
133 else
134 {
135 _LOG_ERROR ("ObjectDd exists, but no segment " << segment << " for device: " << deviceName << ", file_hash: " << hash);
136 }
137 }
138 else
139 {
140 _LOG_ERROR ("ObjectDd doesn't exist for device: " << deviceName << ", file_hash: " << hash);
141 }
142
143}
144
145void
146ContentServer::serve_Action_Execute (Name forwardingHint, Name interest)
147{
148 // /device-name/action/shared-folder/seq
149
150 int64_t seqno = interest.getCompFromBackAsInt (0);
151 Name deviceName = interest.getPartialName (forwardingHint.size (), interest.size () - forwardingHint.size () - 3);
152
153 _LOG_DEBUG (" server ACTION for device: " << deviceName << " and seqno: " << seqno);
154
155 PcoPtr pco = m_actionLog->LookupActionPco (deviceName, seqno);
156 if (pco)
157 {
158 if (forwardingHint.size () == 0)
159 {
160 m_ccnx->putToCcnd (pco->buf ());
161 }
162 else
163 {
164 const Bytes &content = pco->buf ();
165 if (m_freshness > 0)
Zhenkai Zhubc738572013-01-23 22:46:11 -0800166 {
167 m_ccnx->publishData(interest, content, m_freshness);
168 }
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800169 else
Zhenkai Zhubc738572013-01-23 22:46:11 -0800170 {
171 m_ccnx->publishData(interest, content);
172 }
Zhenkai Zhue42b4572013-01-22 15:57:54 -0800173 }
Zhenkai Zhue42b4572013-01-22 15:57:54 -0800174 }
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800175 else
176 {
177 _LOG_ERROR ("ACTION not found for device: " << deviceName << " and seqno: " << seqno);
178 }
Zhenkai Zhue42b4572013-01-22 15:57:54 -0800179}