blob: 3b3c9c282c7e4a27ac16bb0454c6f8d9cf7e0757 [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);
Zhenkai Zhue42b4572013-01-22 15:57:54 -080086 if (type == "action")
87 {
Zhenkai Zhubc738572013-01-23 22:46:11 -080088 int64_t seqno = originalName.getCompAsInt(nameSize - 1);
89 PcoPtr pco = m_actionLog->LookupActionPco(deviceName, seqno);
90 if (pco)
91 {
92 Bytes content = pco->buf();
93 if (m_freshness > 0)
94 {
95 m_ccnx->publishData(interest, content, m_freshness);
96 }
97 else
98 {
99 m_ccnx->publishData(interest, content);
100 }
101 }
Zhenkai Zhue42b4572013-01-22 15:57:54 -0800102 }
103 else if (type == "file")
104 {
105 Bytes hashBytes = originalName.getComp(nameSize - 2);
106 Hash hash(head(hashBytes), hashBytes.size());
107 ostringstream oss;
108 oss << hash;
109 int64_t segment = originalName.getCompAsInt(nameSize - 1);
110 ObjectDb db(m_dbFolder, oss.str());
Zhenkai Zhubc738572013-01-23 22:46:11 -0800111 BytesPtr co = db.fetchSegment(deviceName, segment);
112 if (co)
113 {
114 if (m_freshness > 0)
115 {
116 m_ccnx->publishData(interest, *co, m_freshness);
117 }
118 else
119 {
120 m_ccnx->publishData(interest, *co);
121 }
122 }
Zhenkai Zhue42b4572013-01-22 15:57:54 -0800123 }
124 else
125 {
126 cerr << "Discard interest that ContentServer does not know how to handle: " << interest << ", prefix: " << prefix << endl;
127 }
128
Zhenkai Zhue42b4572013-01-22 15:57:54 -0800129 }
130 }
131 }
132}