Sirikata
|
00001 /* Sirikata 00002 * AssetDownloadTask.hpp 00003 * 00004 * Copyright (c) 2011, Stanford University 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 00033 #ifndef _SIRIKATA_OGRE_ASSET_DOWNLOAD_TASK_HPP_ 00034 #define _SIRIKATA_OGRE_ASSET_DOWNLOAD_TASK_HPP_ 00035 00036 #include <sirikata/core/transfer/ResourceDownloadTask.hpp> 00037 #include <sirikata/mesh/Visual.hpp> 00038 00039 namespace Sirikata { 00040 namespace Graphics { 00041 class OgreRenderer; 00042 00043 class ParseMeshTaskInfo; 00044 typedef std::tr1::shared_ptr<ParseMeshTaskInfo> ParseMeshTaskHandle; 00045 } 00046 00056 class AssetDownloadTask : public SelfWeakPtr<AssetDownloadTask>{ 00057 public: 00058 typedef std::tr1::function<void()> FinishedCallback; 00059 00060 struct ResourceData { 00061 Transfer::TransferRequestPtr request; 00062 Transfer::DenseDataPtr response; 00063 }; 00064 typedef std::map<Transfer::URI, ResourceData> Dependencies; 00065 private: 00066 AssetDownloadTask(const Transfer::URI& uri, Graphics::OgreRenderer* const scene, double priority, bool isAgg, FinishedCallback cb); 00067 public: 00068 static std::tr1::shared_ptr<AssetDownloadTask> construct(const Transfer::URI& uri, Graphics::OgreRenderer* const scene, double priority, FinishedCallback cb); 00069 static std::tr1::shared_ptr<AssetDownloadTask> construct(const Transfer::URI& uri, Graphics::OgreRenderer* const scene, double priority, bool isAgg, FinishedCallback cb); 00070 00071 ~AssetDownloadTask(); 00072 00073 Mesh::VisualPtr asset() const { return mAsset; } 00074 const Dependencies& dependencies() const { return mDependencies; } 00075 float64 priority() const { return mPriority; } 00076 00077 void getDownloadTasks( 00078 std::vector<String>& finishedDownloads, std::vector<String>& activeDownloads); 00079 00080 00081 void updatePriority(float64 priority); 00082 void cancel(); 00083 00084 typedef std::map<const String, Transfer::ResourceDownloadTaskPtr> ActiveDownloadMap; 00085 00086 private: 00087 00088 void downloadAssetFile(); 00089 static void weakAssetFileDownloaded(std::tr1::weak_ptr<AssetDownloadTask> thus, Transfer::ResourceDownloadTaskPtr taskptr, 00090 Transfer::TransferRequestPtr request, Transfer::DenseDataPtr response); 00091 void assetFileDownloaded(Transfer::ResourceDownloadTaskPtr taskptr, Transfer::ChunkRequestPtr request, Transfer::DenseDataPtr response); 00092 static void weakHandleAssetParsed(std::tr1::weak_ptr<AssetDownloadTask> thus, Mesh::VisualPtr md); 00093 void handleAssetParsed(Mesh::VisualPtr md); 00094 00095 void addDependentDownload(Transfer::ResourceDownloadTaskPtr resPtr); 00096 void addDependentDownload(const Transfer::URI& depUrl); 00097 void addDependentDownload(const Transfer::URI& depUrl, const Transfer::Chunk& depChunk); 00098 00099 // Start is separate from add so we can be sure mActiveDownloads isn't being 00100 // modified after we start the downloads, except by the completion handler 00101 void startDependentDownloads(); 00102 00103 static void weakTextureDownloaded(const std::tr1::weak_ptr<AssetDownloadTask>&, Transfer::URI uri, Transfer::ResourceDownloadTaskPtr taskptr, 00104 Transfer::TransferRequestPtr request, Transfer::DenseDataPtr response); 00105 00106 void textureDownloaded(Transfer::URI uri, Transfer::ResourceDownloadTaskPtr taskptr, Transfer::TransferRequestPtr request, 00107 Transfer::DenseDataPtr response); 00108 00109 00110 // Fails the entire process as a result of one dependency (or the original 00111 // asset) failing to download. 00112 void failDownload(); 00113 00114 void cancelNoLock(); 00115 00116 // Get the URL for an asset, deciding automatically whether it 00117 // needs to be relative or absolute 00118 Transfer::URI getURL(const Transfer::URI& orig, const String& given_url); 00119 00120 Graphics::OgreRenderer *const mScene; 00121 Transfer::URI mAssetURI; 00122 double mPriority; 00123 FinishedCallback mCB; 00124 00125 Graphics::ParseMeshTaskHandle mParseMeshHandle; 00126 Mesh::VisualPtr mAsset; 00127 Dependencies mDependencies; 00128 bool mIsAggregate; 00129 00130 // Active downloads, for making sure shared_ptrs stick around and for cancelling 00131 ActiveDownloadMap mActiveDownloads; 00132 std::vector<String> mFinishedDownloads; 00133 00134 boost::mutex mDependentDownloadMutex; 00135 00136 public: 00137 00138 ActiveDownloadMap::size_type getOutstandingDependentDownloads(); 00139 00140 00141 }; 00142 typedef std::tr1::shared_ptr<AssetDownloadTask> AssetDownloadTaskPtr; 00143 00144 } // namespace Sirikata 00145 00146 #endif //_SIRIKATA_OGRE_ASSET_DOWNLOAD_TASK_HPP_