blob: e960e1487ac38450dc87b89a2fec0c82785186a9 [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 {
Zhenkai Zhu4c656b92013-01-26 10:53:59 -080054 m_ccnx->clearInterestFilter (*it);
Zhenkai Zhue42b4572013-01-22 15:57:54 -080055 }
56}
57
58void
59ContentServer::registerPrefix(const Name &prefix)
60{
Zhenkai Zhuc3a27872013-01-25 19:21:25 -080061 m_ccnx->setInterestFilter (prefix, bind(&ContentServer::serve, this, prefix, _1));
Zhenkai Zhub74e1e92013-01-25 14:36:18 -080062
Zhenkai Zhuc3a27872013-01-25 19:21:25 -080063 _LOG_DEBUG (">> content server: register " << prefix);
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -080064
65 ScopedLock lock (m_mutex);
Zhenkai Zhue42b4572013-01-22 15:57:54 -080066 m_prefixes.insert(prefix);
67}
68
69void
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -080070ContentServer::deregisterPrefix (const Name &prefix)
Zhenkai Zhue42b4572013-01-22 15:57:54 -080071{
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -080072
Zhenkai Zhuc3a27872013-01-25 19:21:25 -080073 m_ccnx->clearInterestFilter(prefix);
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -080074
Zhenkai Zhuc3a27872013-01-25 19:21:25 -080075 _LOG_DEBUG ("<< content server: deregister " << prefix);
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -080076 ScopedLock lock (m_mutex);
77 m_prefixes.erase (prefix);
Zhenkai Zhue42b4572013-01-22 15:57:54 -080078}
79
80void
Zhenkai Zhuc3a27872013-01-25 19:21:25 -080081ContentServer::serve(Name forwardingHint, const Name &interest)
82{
83 // /forwardingHint/device-name/action/shared-folder/action-seq
84 // /forwardingHint/device-name/file/file-hash/segment
85
86 Name name = interest.getPartialName(forwardingHint.size());
87 if (name.size() > 3)
88 {
89 string type = name.getCompAsString(name.size() - 3);
90 if (type == "action")
91 {
92 serve_Action (forwardingHint, interest);
93 }
94 else if (type == "file")
95 {
96 serve_File (forwardingHint, interest);
97 }
98 }
99}
100
101void
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800102ContentServer::serve_Action (Name forwardingHint, const Name &interest)
Zhenkai Zhue42b4572013-01-22 15:57:54 -0800103{
Zhenkai Zhub74e1e92013-01-25 14:36:18 -0800104 _LOG_DEBUG (">> content server serving, forwardHing " << forwardingHint << ", interest " << interest);
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800105 m_executor.execute (bind (&ContentServer::serve_Action_Execute, this, forwardingHint, interest));
106 // need to unlock ccnx mutex... or at least don't lock it
107}
108
109void
110ContentServer::serve_File (Name forwardingHint, const Name &interest)
111{
Zhenkai Zhub74e1e92013-01-25 14:36:18 -0800112 _LOG_DEBUG (">> content server serving, forwardHing " << forwardingHint << ", interest " << interest);
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800113 m_executor.execute (bind (&ContentServer::serve_File_Execute, this, forwardingHint, interest));
114 // need to unlock ccnx mutex... or at least don't lock it
115}
116
117void
118ContentServer::serve_File_Execute (Name forwardingHint, Name interest)
119{
120 // /device-name/file/<file-hash>/segment, or
121
122 int64_t segment = interest.getCompFromBackAsInt (0);
123 Name deviceName = interest.getPartialName (forwardingHint.size (), interest.size () - forwardingHint.size () - 3);
124 Hash hash (head(interest.getCompFromBack (1)), interest.getCompFromBack (1).size());
125
126 _LOG_DEBUG (" server FILE for device: " << deviceName << ", file_hash: " << hash << " segment: " << segment);
127
128 string hashStr = lexical_cast<string> (hash);
129 if (ObjectDb::DoesExist (m_dbFolder, deviceName, hashStr)) // this is kind of overkill, as it counts available segments
Zhenkai Zhue42b4572013-01-22 15:57:54 -0800130 {
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800131 ObjectDb db (m_dbFolder, hashStr);
132 // may do prefetching
133
134 BytesPtr co = db.fetchSegment (deviceName, segment);
135 if (co)
Zhenkai Zhue42b4572013-01-22 15:57:54 -0800136 {
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800137 if (forwardingHint.size () == 0)
138 {
139 m_ccnx->putToCcnd (*co);
140 }
141 else
142 {
143 if (m_freshness > 0)
144 {
145 m_ccnx->publishData(interest, *co, m_freshness);
146 }
147 else
148 {
149 m_ccnx->publishData(interest, *co);
150 }
151 }
152
153 }
154 else
155 {
156 _LOG_ERROR ("ObjectDd exists, but no segment " << segment << " for device: " << deviceName << ", file_hash: " << hash);
157 }
158 }
159 else
160 {
161 _LOG_ERROR ("ObjectDd doesn't exist for device: " << deviceName << ", file_hash: " << hash);
162 }
163
164}
165
166void
167ContentServer::serve_Action_Execute (Name forwardingHint, Name interest)
168{
169 // /device-name/action/shared-folder/seq
170
171 int64_t seqno = interest.getCompFromBackAsInt (0);
172 Name deviceName = interest.getPartialName (forwardingHint.size (), interest.size () - forwardingHint.size () - 3);
173
174 _LOG_DEBUG (" server ACTION for device: " << deviceName << " and seqno: " << seqno);
175
176 PcoPtr pco = m_actionLog->LookupActionPco (deviceName, seqno);
177 if (pco)
178 {
179 if (forwardingHint.size () == 0)
180 {
181 m_ccnx->putToCcnd (pco->buf ());
182 }
183 else
184 {
185 const Bytes &content = pco->buf ();
186 if (m_freshness > 0)
Zhenkai Zhubc738572013-01-23 22:46:11 -0800187 {
188 m_ccnx->publishData(interest, content, m_freshness);
189 }
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800190 else
Zhenkai Zhubc738572013-01-23 22:46:11 -0800191 {
192 m_ccnx->publishData(interest, content);
193 }
Zhenkai Zhue42b4572013-01-22 15:57:54 -0800194 }
Zhenkai Zhue42b4572013-01-22 15:57:54 -0800195 }
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800196 else
197 {
198 _LOG_ERROR ("ACTION not found for device: " << deviceName << " and seqno: " << seqno);
199 }
Zhenkai Zhue42b4572013-01-22 15:57:54 -0800200}