Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 23 | #include "logging.h" |
| 24 | #include <boost/lexical_cast.hpp> |
| 25 | |
| 26 | INIT_LOGGER ("ContentServer"); |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 27 | |
| 28 | using namespace Ccnx; |
| 29 | using namespace std; |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 30 | using namespace boost; |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 31 | |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 32 | ContentServer::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 Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 43 | { |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 44 | m_executor.start (); |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | ContentServer::~ContentServer() |
| 48 | { |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 49 | m_executor.shutdown (); |
| 50 | |
| 51 | ScopedLock lock (m_mutex); |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 52 | for (PrefixIt it = m_prefixes.begin(); it != m_prefixes.end(); ++it) |
| 53 | { |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 54 | m_ccnx->clearInterestFilter (Name (*it)(m_deviceName)("action")(m_sharedFolderName)); |
| 55 | m_ccnx->clearInterestFilter (Name (*it)(m_deviceName)("file")); |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
| 59 | void |
| 60 | ContentServer::registerPrefix(const Name &prefix) |
| 61 | { |
Zhenkai Zhu | c3a2787 | 2013-01-25 19:21:25 -0800 | [diff] [blame] | 62 | m_ccnx->setInterestFilter (prefix, bind(&ContentServer::serve, this, prefix, _1)); |
Zhenkai Zhu | b74e1e9 | 2013-01-25 14:36:18 -0800 | [diff] [blame] | 63 | |
Zhenkai Zhu | c3a2787 | 2013-01-25 19:21:25 -0800 | [diff] [blame] | 64 | _LOG_DEBUG (">> content server: register " << prefix); |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 65 | |
| 66 | ScopedLock lock (m_mutex); |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 67 | m_prefixes.insert(prefix); |
| 68 | } |
| 69 | |
| 70 | void |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 71 | ContentServer::deregisterPrefix (const Name &prefix) |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 72 | { |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 73 | |
Zhenkai Zhu | c3a2787 | 2013-01-25 19:21:25 -0800 | [diff] [blame] | 74 | m_ccnx->clearInterestFilter(prefix); |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 75 | |
Zhenkai Zhu | c3a2787 | 2013-01-25 19:21:25 -0800 | [diff] [blame] | 76 | _LOG_DEBUG ("<< content server: deregister " << prefix); |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 77 | ScopedLock lock (m_mutex); |
| 78 | m_prefixes.erase (prefix); |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | void |
Zhenkai Zhu | c3a2787 | 2013-01-25 19:21:25 -0800 | [diff] [blame] | 82 | ContentServer::serve(Name forwardingHint, const Name &interest) |
| 83 | { |
| 84 | // /forwardingHint/device-name/action/shared-folder/action-seq |
| 85 | // /forwardingHint/device-name/file/file-hash/segment |
| 86 | |
| 87 | Name name = interest.getPartialName(forwardingHint.size()); |
| 88 | if (name.size() > 3) |
| 89 | { |
| 90 | string type = name.getCompAsString(name.size() - 3); |
| 91 | if (type == "action") |
| 92 | { |
| 93 | serve_Action (forwardingHint, interest); |
| 94 | } |
| 95 | else if (type == "file") |
| 96 | { |
| 97 | serve_File (forwardingHint, interest); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | void |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 103 | ContentServer::serve_Action (Name forwardingHint, const Name &interest) |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 104 | { |
Zhenkai Zhu | b74e1e9 | 2013-01-25 14:36:18 -0800 | [diff] [blame] | 105 | _LOG_DEBUG (">> content server serving, forwardHing " << forwardingHint << ", interest " << interest); |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 106 | m_executor.execute (bind (&ContentServer::serve_Action_Execute, this, forwardingHint, interest)); |
| 107 | // need to unlock ccnx mutex... or at least don't lock it |
| 108 | } |
| 109 | |
| 110 | void |
| 111 | ContentServer::serve_File (Name forwardingHint, const Name &interest) |
| 112 | { |
Zhenkai Zhu | b74e1e9 | 2013-01-25 14:36:18 -0800 | [diff] [blame] | 113 | _LOG_DEBUG (">> content server serving, forwardHing " << forwardingHint << ", interest " << interest); |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 114 | m_executor.execute (bind (&ContentServer::serve_File_Execute, this, forwardingHint, interest)); |
| 115 | // need to unlock ccnx mutex... or at least don't lock it |
| 116 | } |
| 117 | |
| 118 | void |
| 119 | ContentServer::serve_File_Execute (Name forwardingHint, Name interest) |
| 120 | { |
| 121 | // /device-name/file/<file-hash>/segment, or |
| 122 | |
| 123 | int64_t segment = interest.getCompFromBackAsInt (0); |
| 124 | Name deviceName = interest.getPartialName (forwardingHint.size (), interest.size () - forwardingHint.size () - 3); |
| 125 | Hash hash (head(interest.getCompFromBack (1)), interest.getCompFromBack (1).size()); |
| 126 | |
| 127 | _LOG_DEBUG (" server FILE for device: " << deviceName << ", file_hash: " << hash << " segment: " << segment); |
| 128 | |
| 129 | string hashStr = lexical_cast<string> (hash); |
| 130 | if (ObjectDb::DoesExist (m_dbFolder, deviceName, hashStr)) // this is kind of overkill, as it counts available segments |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 131 | { |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 132 | ObjectDb db (m_dbFolder, hashStr); |
| 133 | // may do prefetching |
| 134 | |
| 135 | BytesPtr co = db.fetchSegment (deviceName, segment); |
| 136 | if (co) |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 137 | { |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 138 | if (forwardingHint.size () == 0) |
| 139 | { |
| 140 | m_ccnx->putToCcnd (*co); |
| 141 | } |
| 142 | else |
| 143 | { |
| 144 | if (m_freshness > 0) |
| 145 | { |
| 146 | m_ccnx->publishData(interest, *co, m_freshness); |
| 147 | } |
| 148 | else |
| 149 | { |
| 150 | m_ccnx->publishData(interest, *co); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | } |
| 155 | else |
| 156 | { |
| 157 | _LOG_ERROR ("ObjectDd exists, but no segment " << segment << " for device: " << deviceName << ", file_hash: " << hash); |
| 158 | } |
| 159 | } |
| 160 | else |
| 161 | { |
| 162 | _LOG_ERROR ("ObjectDd doesn't exist for device: " << deviceName << ", file_hash: " << hash); |
| 163 | } |
| 164 | |
| 165 | } |
| 166 | |
| 167 | void |
| 168 | ContentServer::serve_Action_Execute (Name forwardingHint, Name interest) |
| 169 | { |
| 170 | // /device-name/action/shared-folder/seq |
| 171 | |
| 172 | int64_t seqno = interest.getCompFromBackAsInt (0); |
| 173 | Name deviceName = interest.getPartialName (forwardingHint.size (), interest.size () - forwardingHint.size () - 3); |
| 174 | |
| 175 | _LOG_DEBUG (" server ACTION for device: " << deviceName << " and seqno: " << seqno); |
| 176 | |
| 177 | PcoPtr pco = m_actionLog->LookupActionPco (deviceName, seqno); |
| 178 | if (pco) |
| 179 | { |
| 180 | if (forwardingHint.size () == 0) |
| 181 | { |
| 182 | m_ccnx->putToCcnd (pco->buf ()); |
| 183 | } |
| 184 | else |
| 185 | { |
| 186 | const Bytes &content = pco->buf (); |
| 187 | if (m_freshness > 0) |
Zhenkai Zhu | bc73857 | 2013-01-23 22:46:11 -0800 | [diff] [blame] | 188 | { |
| 189 | m_ccnx->publishData(interest, content, m_freshness); |
| 190 | } |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 191 | else |
Zhenkai Zhu | bc73857 | 2013-01-23 22:46:11 -0800 | [diff] [blame] | 192 | { |
| 193 | m_ccnx->publishData(interest, content); |
| 194 | } |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 195 | } |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 196 | } |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 197 | else |
| 198 | { |
| 199 | _LOG_ERROR ("ACTION not found for device: " << deviceName << " and seqno: " << seqno); |
| 200 | } |
Zhenkai Zhu | e42b457 | 2013-01-22 15:57:54 -0800 | [diff] [blame] | 201 | } |