Sirikata
libcore/include/sirikata/core/transfer/TransferHandlers.hpp
Go to the documentation of this file.
00001 /*  Sirikata Transfer -- Content Transfer management system
00002  *  TransferHandlers.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: Jun 18th, 2010 */
00033 
00034 #ifndef SIRIKATA_TransferHandlers_HPP__
00035 #define SIRIKATA_TransferHandlers_HPP__
00036 
00037 #include <sirikata/core/transfer/RemoteFileMetadata.hpp>
00038 #include <sirikata/core/transfer/TransferData.hpp>
00039 #include <sirikata/core/transfer/DiskCacheLayer.hpp>
00040 #include <sirikata/core/transfer/MemoryCacheLayer.hpp>
00041 #include <sirikata/core/transfer/LRUPolicy.hpp>
00042 #include <sirikata/core/transfer/TransferRequest.hpp>
00043 
00044 namespace Sirikata {
00045 namespace Transfer {
00046 
00047 //Forward declaration
00048 class MetadataRequest;
00049 
00050 /*
00051  * Base class for an implementation that satisfies name lookups
00052  * The input is a MetadataRequest and output is RemoteFileMetadata
00053  */
00054 class NameHandler {
00055 
00056 public:
00057     typedef std::tr1::function<void(
00058                 std::tr1::shared_ptr<RemoteFileMetadata> response
00059             )> NameCallback;
00060 
00061     virtual void resolve(std::tr1::shared_ptr<MetadataRequest> request, NameCallback callback) = 0;
00062 
00063     virtual ~NameHandler() {
00064     }
00065 
00066 };
00067 
00068 /*
00069  * Base class for an implementation that satisfies chunk downloads
00070  * The input is a RemoteFileMetadata and a Chunk, output is DenseData
00071  */
00072 class ChunkHandler {
00073 
00074 public:
00075     typedef std::tr1::function<void(
00076                 std::tr1::shared_ptr<const DenseData> response
00077             )> ChunkCallback;
00078 
00079     virtual void get(std::tr1::shared_ptr<RemoteFileMetadata> file,
00080             std::tr1::shared_ptr<Chunk> chunk, ChunkCallback callback) = 0;
00081 
00082     virtual ~ChunkHandler() {
00083     }
00084 
00085 };
00086 
00087 
00088 /*
00089  * Base class for an implementation that satisfies file uploads.
00090  * Input is credentials, files, requested path, and parameters, output is the path
00091  */
00092 class UploadHandler {
00093 
00094 public:
00095     typedef std::tr1::function<void(Transfer::URI)> UploadCallback;
00096 
00097     virtual void upload(
00098         UploadRequestPtr request,
00099         UploadCallback callback) = 0;
00100 
00101     virtual ~UploadHandler() {
00102     }
00103 
00104 };
00105 
00106 /*
00107  * Mediates access to a shared cache of chunk hashes to be used
00108  * by multiple ChunkHandler implementations
00109  */
00110 class SIRIKATA_EXPORT SharedChunkCache {
00111 private:
00112     static const unsigned int DISK_LRU_CACHE_SIZE;
00113     static const unsigned int MEMORY_LRU_CACHE_SIZE;
00114 
00115     CachePolicy* mDiskCachePolicy;
00116     CachePolicy* mMemoryCachePolicy;
00117     std::vector<CacheLayer*> mCacheLayers;
00118     CacheLayer* mCache;
00119 public:
00120     SharedChunkCache();
00121     ~SharedChunkCache();
00122     CacheLayer* getCache();
00123     static SharedChunkCache& getSingleton();
00124     static void destroy();
00125 };
00126 
00127 }
00128 }
00129 
00130 #endif