Sirikata
|
00001 /* Sirikata Transfer -- Content Transfer management system 00002 * RemoteFileMetadata.hpp 00003 * 00004 * Copyright (c) 2010, Jeff Terrace 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions are 00009 * met: 00010 * * Redistributions of source code must retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * * Redistributions in binary form must reproduce the above copyright 00013 * notice, this list of conditions and the following disclaimer in 00014 * the documentation and/or other materials provided with the 00015 * distribution. 00016 * * Neither the name of Sirikata nor the names of its contributors may 00017 * be used to endorse or promote products derived from this software 00018 * without specific prior written permission. 00019 * 00020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 00021 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 00022 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 00023 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 00024 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00025 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00026 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00027 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00028 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00029 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00030 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00031 */ 00032 /* Created on: Jan 18th, 2010 */ 00033 00034 #ifndef SIRIKATA_RemoteFileMetadata_HPP__ 00035 #define SIRIKATA_RemoteFileMetadata_HPP__ 00036 00037 #include <boost/lexical_cast.hpp> 00038 #include <sirikata/core/util/Sha256.hpp> 00039 #include <sirikata/core/transfer/Range.hpp> 00040 #include <sirikata/core/transfer/URI.hpp> 00041 00042 namespace Sirikata { 00043 namespace Transfer { 00044 00045 typedef uint64 size_type; 00046 00047 typedef SHA256 Fingerprint; 00048 00049 /* 00050 * Storage for a single chunk of a file - has start, end, length, hash 00051 * Uses Range and Fingerprint 00052 */ 00053 class Chunk { 00054 private: 00055 Fingerprint mHash; 00056 Range mRange; 00057 public: 00058 Chunk() : 00059 mHash(), mRange(true) {} 00060 00061 Chunk(const Fingerprint &fingerprint, const Range &range) : 00062 mHash(fingerprint), mRange(range) { 00063 00064 } 00065 00066 inline const Fingerprint getHash() const { 00067 return mHash; 00068 } 00069 00070 inline const Range getRange() const { 00071 return mRange; 00072 } 00073 00074 inline bool operator==(const Chunk &other) const { 00075 return (mHash == other.mHash && mRange == other.mRange); 00076 } 00077 00078 inline String toString() const { 00079 String toret = mHash.toString() + "_"; 00080 toret += boost::lexical_cast<String>(mRange.startbyte()); 00081 toret += "_"; 00082 if (mRange.goesToEndOfFile()) { 00083 toret += "EOF"; 00084 } else { 00085 toret += boost::lexical_cast<String>(mRange.endbyte()); 00086 } 00087 00088 return toret; 00089 } 00090 00091 }; 00092 00093 typedef std::list<Chunk> ChunkList; 00094 typedef std::map<std::string, std::string> FileHeaders; 00095 00099 class RemoteFileMetadata { 00100 Fingerprint mHash; 00101 URI mURI; 00102 size_type mSize; 00103 ChunkList mChunkList; 00104 FileHeaders mHeaders; 00105 00106 public: 00107 00108 RemoteFileMetadata(const Fingerprint &fingerprint, const URI &uri, size_type size, const ChunkList &chunklist, const FileHeaders &headers) 00109 : mHash(fingerprint), mURI(uri), mSize(size), mChunkList(chunklist), mHeaders(headers) { 00110 00111 } 00112 00113 inline size_type getSize() const { 00114 return mSize; 00115 } 00116 00117 inline std::string toString() const { 00118 return mURI.toString(); 00119 } 00120 00121 inline const Fingerprint &getFingerprint() const { 00122 return mHash; 00123 } 00124 00125 inline const URI &getURI() const { 00126 return mURI; 00127 } 00128 00129 inline const ChunkList &getChunkList() const { 00130 return mChunkList; 00131 } 00132 00133 inline const FileHeaders &getHeaders() const { 00134 return mHeaders; 00135 } 00136 00137 inline bool operator==(const RemoteFileMetadata &other) const { 00138 return getURI() == other.getURI(); 00139 } 00140 inline bool operator!=(const RemoteFileMetadata &other) const { 00141 return getURI() != other.getURI(); 00142 } 00143 inline bool operator<(const RemoteFileMetadata &other) const { 00144 return getURI() < other.getURI(); 00145 } 00146 00147 }; 00148 typedef std::tr1::shared_ptr<RemoteFileMetadata> RemoteFileMetadataPtr; 00149 00150 } 00151 } 00152 00153 #endif