Sirikata
libcore/include/sirikata/core/transfer/MeerkatTransferHandler.hpp
Go to the documentation of this file.
00001 /*  Sirikata Transfer -- Content Transfer management system
00002  *  MeerkatTransferHandler.hpp
00003  *
00004  *  Copyright (c) 2011, 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_MeerkatTransferHandler_HPP__
00035 #define SIRIKATA_MeerkatTransferHandler_HPP__
00036 
00037 #include <sirikata/core/util/Platform.hpp>
00038 #include <sirikata/core/transfer/RemoteFileMetadata.hpp>
00039 #include <sirikata/core/transfer/HttpManager.hpp>
00040 #include <sirikata/core/network/Address.hpp>
00041 #include <sirikata/core/transfer/TransferHandlers.hpp>
00042 
00043 namespace Sirikata {
00044 namespace Transfer {
00045 
00046 /*
00047  * Implements name lookups via HTTP
00048  */
00049 class SIRIKATA_EXPORT MeerkatNameHandler
00050     : public NameHandler, public AutoSingleton<MeerkatNameHandler> {
00051 
00052 private:
00053     //TODO: should get these from settings
00054     const std::string CDN_HOST_NAME;
00055     const std::string CDN_SERVICE;
00056     const std::string CDN_DNS_URI_PREFIX;
00057     const Network::Address mCdnAddr;
00058 
00059 public:
00060     MeerkatNameHandler();
00061     ~MeerkatNameHandler();
00062 
00063     /*
00064      * Resolves a metadata request via HTTP and calls callback when completed
00065      */
00066     void resolve(std::tr1::shared_ptr<MetadataRequest> request, NameCallback callback);
00067 
00068     /*
00069      * Callback from HttpManager when an http request finishes
00070      */
00071     void request_finished(std::tr1::shared_ptr<HttpManager::HttpResponse> response,
00072             HttpManager::ERR_TYPE error, const boost::system::error_code& boost_error,
00073             std::tr1::shared_ptr<MetadataRequest> request, NameCallback callback);
00074 
00075     static MeerkatNameHandler& getSingleton();
00076     static void destroy();
00077 };
00078 
00079 /*
00080  * Implements chunk downloading via HTTP
00081  */
00082 class SIRIKATA_EXPORT MeerkatChunkHandler
00083     : public ChunkHandler, public AutoSingleton<MeerkatChunkHandler> {
00084 
00085 private:
00086     //TODO: should get these from settings
00087     const std::string CDN_HOST_NAME;
00088     const std::string CDN_SERVICE;
00089     const std::string CDN_DOWNLOAD_URI_PREFIX;
00090     const Network::Address mCdnAddr;
00091 
00092     void cache_check_callback(const SparseData* data, const URI& uri,
00093             std::tr1::shared_ptr<Chunk> chunk, ChunkCallback callback);
00094 
00095 public:
00096     MeerkatChunkHandler();
00097     ~MeerkatChunkHandler();
00098 
00099     /*
00100      * Downloads the chunk referenced and calls callback when completed
00101      */
00102     void get(std::tr1::shared_ptr<RemoteFileMetadata> file,
00103             std::tr1::shared_ptr<Chunk> chunk, ChunkCallback callback);
00104 
00105     /*
00106      * Downloads the chunk referenced and calls callback when completed
00107      */
00108     void get(std::tr1::shared_ptr<Chunk> chunk, ChunkCallback callback);
00109 
00110     /*
00111      * Callback from HttpManager when an http request finishes
00112      */
00113     void request_finished(std::tr1::shared_ptr<HttpManager::HttpResponse> response,
00114             HttpManager::ERR_TYPE error, const boost::system::error_code& boost_error,
00115             const URI& uri, std::tr1::shared_ptr<Chunk> chunk,
00116             bool chunkReq, ChunkCallback callback);
00117 
00118     static MeerkatChunkHandler& getSingleton();
00119     static void destroy();
00120 };
00121 
00122 
00123 /*
00124  * Implements uploads via HTTP
00125  */
00126 class SIRIKATA_EXPORT MeerkatUploadHandler
00127     : public UploadHandler, public AutoSingleton<MeerkatUploadHandler> {
00128 
00129 private:
00130     //TODO: should get these from settings
00131     const std::string CDN_HOST_NAME;
00132     const std::string CDN_SERVICE;
00133     const std::string CDN_UPLOAD_URI_PREFIX;
00134     const std::string CDN_UPLOAD_STATUS_URI_PREFIX;
00135     const Network::Address mCdnAddr;
00136 
00137     void getServerProps(UploadRequestPtr request, Network::Address& cdn_addr, String& full_oauth_hostinfo);
00138 
00139 
00140     void request_finished(std::tr1::shared_ptr<HttpManager::HttpResponse> response,
00141             HttpManager::ERR_TYPE error, const boost::system::error_code& boost_error,
00142             UploadRequestPtr request, UploadCallback callback);
00143 
00144     void requestStatus(UploadRequestPtr request, const String& task_id, UploadCallback callback, int32 retries);
00145 
00146     void handleRequestStatusResult(
00147         std::tr1::shared_ptr<HttpManager::HttpResponse> response,
00148         HttpManager::ERR_TYPE error, const boost::system::error_code& boost_error,
00149         UploadRequestPtr request, const String& task_id, UploadCallback callback,
00150         int32 retries
00151     );
00152 
00153 public:
00154     MeerkatUploadHandler();
00155     ~MeerkatUploadHandler();
00156 
00157     virtual void upload(
00158         UploadRequestPtr request,
00159         UploadCallback callback);
00160 
00161     static MeerkatUploadHandler& getSingleton();
00162     static void destroy();
00163 };
00164 
00165 
00166 }
00167 }
00168 
00169 #endif