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