blob: 542f453107c5d472781a715a12d1030a8dfa7c74 [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"
23
24using namespace Ccnx;
25using namespace std;
26
Zhenkai Zhu13d224d2013-01-23 19:40:25 -080027ContentServer::ContentServer(CcnxWrapperPtr ccnx, ActionLogPtr actionLog, const boost::filesystem::path &rootDir, int freshness)
Zhenkai Zhue42b4572013-01-22 15:57:54 -080028 : m_ccnx(ccnx)
29 , m_actionLog(actionLog)
30 , m_dbFolder(rootDir / ".chronoshare")
Zhenkai Zhu13d224d2013-01-23 19:40:25 -080031 , m_freshness(freshness)
Zhenkai Zhue42b4572013-01-22 15:57:54 -080032{
33}
34
35ContentServer::~ContentServer()
36{
37 WriteLock lock(m_mutex);
38 int size = m_prefixes.size();
39 for (PrefixIt it = m_prefixes.begin(); it != m_prefixes.end(); ++it)
40 {
41 m_ccnx->clearInterestFilter(*it);
42 }
43}
44
45void
46ContentServer::registerPrefix(const Name &prefix)
47{
48 m_ccnx->setInterestFilter(prefix, bind(&ContentServer::serve, this, _1));
49 WriteLock lock(m_mutex);
50 m_prefixes.insert(prefix);
51}
52
53void
54ContentServer::deregisterPrefix(const Name &prefix)
55{
56 WriteLock lock(m_mutex);
57 PrefixIt it = m_prefixes.find(prefix);
58 if (it != m_prefixes.end())
59 {
60 m_ccnx->clearInterestFilter(*it);
61 m_prefixes.erase(it);
62 }
63}
64
65void
66ContentServer::serve(const Name &interest)
67{
Zhenkai Zhue42b4572013-01-22 15:57:54 -080068 ReadLock lock(m_mutex);
69 for (PrefixIt it = m_prefixes.begin(); it != m_prefixes.end(); ++it)
70 {
71 Name prefix = *it;
72 int prefixSize = prefix.size();
73 int interestSize = interest.size();
74 // this is the prefix of the interest
75 if (prefixSize <= interestSize && interest.getPartialName(0, prefixSize) == prefix)
76 {
77 // originalName should be either
78 // /device-name/file/file-hash/segment, or
79 // /device-name/action/shared-folder/seq
80 Name originalName = interest.getPartialName(prefixSize);
81 int nameSize = originalName.size();
82 if (nameSize > 3)
83 {
84 Name deviceName = originalName.getPartialName(0, nameSize - 3);
85 string type = originalName.getCompAsString(nameSize - 3);
86 BytesPtr co;
87 if (type == "action")
88 {
89 // TODO:
90 // get co from m_actionLog
91 }
92 else if (type == "file")
93 {
94 Bytes hashBytes = originalName.getComp(nameSize - 2);
95 Hash hash(head(hashBytes), hashBytes.size());
96 ostringstream oss;
97 oss << hash;
98 int64_t segment = originalName.getCompAsInt(nameSize - 1);
99 ObjectDb db(m_dbFolder, oss.str());
100 co = db.fetchSegment(deviceName, segment);
101 }
102 else
103 {
104 cerr << "Discard interest that ContentServer does not know how to handle: " << interest << ", prefix: " << prefix << endl;
105 }
106
107 if (co)
108 {
Zhenkai Zhu13d224d2013-01-23 19:40:25 -0800109 if (m_freshness > 0)
110 {
111 m_ccnx->publishData(interest, *co, m_freshness);
112 }
113 else
114 {
115 m_ccnx->publishData(interest, *co);
116 }
Zhenkai Zhue42b4572013-01-22 15:57:54 -0800117 }
118 }
119 }
120 }
121}