blob: 91b656183bdb1f7cb690bb5f3787d72e0cf197a5 [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{
Zhenkai Zhu81907f22013-01-23 17:32:34 -080067 int size = interest.size();
68 cout << ">>> Serving: " << interest.getPartialName(0, size - 2) << ", seq = " << interest.getCompAsInt(size -1) << endl;
69
70
71
Zhenkai Zhue42b4572013-01-22 15:57:54 -080072 ReadLock lock(m_mutex);
73 for (PrefixIt it = m_prefixes.begin(); it != m_prefixes.end(); ++it)
74 {
75 Name prefix = *it;
76 int prefixSize = prefix.size();
77 int interestSize = interest.size();
78 // this is the prefix of the interest
79 if (prefixSize <= interestSize && interest.getPartialName(0, prefixSize) == prefix)
80 {
81 // originalName should be either
82 // /device-name/file/file-hash/segment, or
83 // /device-name/action/shared-folder/seq
84 Name originalName = interest.getPartialName(prefixSize);
85 int nameSize = originalName.size();
86 if (nameSize > 3)
87 {
88 Name deviceName = originalName.getPartialName(0, nameSize - 3);
89 string type = originalName.getCompAsString(nameSize - 3);
90 BytesPtr co;
91 if (type == "action")
92 {
93 // TODO:
94 // get co from m_actionLog
95 }
96 else if (type == "file")
97 {
98 Bytes hashBytes = originalName.getComp(nameSize - 2);
99 Hash hash(head(hashBytes), hashBytes.size());
100 ostringstream oss;
101 oss << hash;
102 int64_t segment = originalName.getCompAsInt(nameSize - 1);
103 ObjectDb db(m_dbFolder, oss.str());
104 co = db.fetchSegment(deviceName, segment);
105 }
106 else
107 {
108 cerr << "Discard interest that ContentServer does not know how to handle: " << interest << ", prefix: " << prefix << endl;
109 }
110
111 if (co)
112 {
113 m_ccnx->publishData(interest, *co);
114 }
115 }
116 }
117 }
118}