00001 // ***************************************************************************** 00002 // 00003 // Copyright (c) 2014, Southwest Research Institute® (SwRI®) 00004 // All rights reserved. 00005 // 00006 // Redistribution and use in source and binary forms, with or without 00007 // modification, are permitted provided that the following conditions are met: 00008 // * Redistributions of source code must retain the above copyright 00009 // notice, this list of conditions and the following disclaimer. 00010 // * Redistributions in binary form must reproduce the above copyright 00011 // notice, this list of conditions and the following disclaimer in the 00012 // documentation and/or other materials provided with the distribution. 00013 // * Neither the name of Southwest Research Institute® (SwRI®) nor the 00014 // names of its contributors may be used to endorse or promote products 00015 // derived from this software without specific prior written permission. 00016 // 00017 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00018 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00019 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00020 // ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY 00021 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00022 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00023 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00024 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00025 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00026 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00027 // 00028 // ***************************************************************************** 00029 00030 #ifndef TILE_MAP_IMAGE_CACHE_H_ 00031 #define TILE_MAP_IMAGE_CACHE_H_ 00032 00033 #include <string> 00034 #include <limits> 00035 00036 #include <boost/cstdint.hpp> 00037 #include <boost/shared_ptr.hpp> 00038 00039 #include <QCache> 00040 #include <QImage> 00041 #include <QMap> 00042 #include <QMutex> 00043 #include <QNetworkReply> 00044 #include <QObject> 00045 #include <QSemaphore> 00046 #include <QSet> 00047 #include <QThread> 00048 #include <set> 00049 00050 namespace tile_map 00051 { 00052 class CacheThread; 00053 00054 class Image 00055 { 00056 public: 00057 Image(const QString& uri, size_t uri_hash, uint64_t priority = 0); 00058 ~Image(); 00059 00060 QString Uri() const { return uri_; } 00061 size_t UriHash() const { return uri_hash_; } 00062 00063 boost::shared_ptr<QImage> GetImage() { return image_; } 00064 00065 void InitializeImage(); 00066 void ClearImage(); 00067 00068 void AddFailure(); 00069 bool Failed() const { return failed_; } 00070 00071 void IncreasePriority() 00072 { 00073 if (priority_ < std::numeric_limits<uint64_t>::max()) 00074 { 00075 priority_++; 00076 } 00077 } 00078 void SetPriority(uint64_t priority) { priority_ = priority; } 00079 uint64_t Priority() const { return priority_; } 00080 00081 bool Loading() const { return loading_; } 00082 void SetLoading(bool loading) { loading_ = loading; } 00083 00084 private: 00085 QString uri_; 00086 00087 size_t uri_hash_; 00088 00089 bool loading_; 00090 int32_t failures_; 00091 bool failed_; 00092 uint64_t priority_; 00093 00094 mutable boost::shared_ptr<QImage> image_; 00095 00096 static const int MAXIMUM_FAILURES; 00097 }; 00098 typedef boost::shared_ptr<Image> ImagePtr; 00099 00100 class ImageCache : public QObject 00101 { 00102 Q_OBJECT 00103 00104 public: 00105 explicit ImageCache(const QString& cache_dir, size_t size = 4096); 00106 ~ImageCache(); 00107 00108 ImagePtr GetImage(size_t uri_hash, const QString& uri, int32_t priority = 0); 00109 00110 public Q_SLOTS: 00111 void ProcessRequest(QString uri); 00112 void ProcessReply(QNetworkReply* reply); 00113 void NetworkError(QNetworkReply::NetworkError error); 00114 void Clear(); 00115 00116 private: 00117 QNetworkAccessManager network_manager_; 00118 00119 QString cache_dir_; 00120 00121 QCache<size_t, ImagePtr> cache_; 00122 QMap<size_t, ImagePtr> unprocessed_; 00123 QSet<size_t> failed_; 00124 QMap<QString, size_t> uri_to_hash_map_; 00125 00126 QMutex cache_mutex_; 00127 QMutex unprocessed_mutex_; 00128 bool exit_; 00129 00130 uint64_t tick_; 00131 00132 CacheThread* cache_thread_; 00133 00134 QSemaphore network_request_semaphore_; 00135 00136 friend class CacheThread; 00137 00138 static const int MAXIMUM_NETWORK_REQUESTS; 00139 }; 00140 00141 class CacheThread : public QThread 00142 { 00143 Q_OBJECT 00144 public: 00145 explicit CacheThread(ImageCache* parent); 00146 00147 virtual void run(); 00148 00149 void notify(); 00150 00151 Q_SIGNALS: 00152 void RequestImage(QString); 00153 00154 private: 00155 ImageCache* p; 00156 QMutex waiting_mutex_; 00157 00158 static const int MAXIMUM_SEQUENTIAL_REQUESTS; 00159 }; 00160 00161 00162 typedef boost::shared_ptr<ImageCache> ImageCachePtr; 00163 } 00164 00165 #endif // TILE_MAP_IMAGE_CACHE_H_