Gnash  0.8.10
LoadVariablesThread.h
Go to the documentation of this file.
00001 // 
00002 //   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
00003 //   Free Software Foundation, Inc
00004 //
00005 // This program is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation; either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with this program; if not, write to the Free Software
00017 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 //
00019 
00020 
00021 #ifndef GNASH_LOADVARIABLESTHREAD_H
00022 #define GNASH_LOADVARIABLESTHREAD_H
00023 
00024 #include <string>
00025 #include <map>
00026 #include <boost/scoped_ptr.hpp>
00027 #include <boost/thread/thread.hpp>
00028 #include <boost/thread/mutex.hpp>
00029 #include <boost/bind.hpp> 
00030 
00031 #include "StreamProvider.h" // for inlines
00032 #include "URL.h" // for inlines
00033 
00034 namespace gnash {
00035 
00036 // Exception thrown by LoadVariablesThread constructor if unable to connect
00037 // to the stream input.
00038 class NetworkException {};
00039 
00041 //
00045 class LoadVariablesThread
00046 {
00047 public:
00048         typedef std::map<std::string, std::string> ValuesMap;
00049 
00051         //
00057         LoadVariablesThread(const StreamProvider& sp, const URL& url);
00058 
00062         //
00071         LoadVariablesThread(const StreamProvider& sp, const URL& url,
00072             const std::string& postdata);
00073 
00075         ~LoadVariablesThread();
00076 
00078         ValuesMap& getValues()
00079         {
00080                 return _vals;
00081         }
00082 
00084         void process()
00085         {
00086                 assert(!_thread.get());
00087                 assert(_stream.get());
00088                 _thread.reset(new boost::thread(
00089                 boost::bind(LoadVariablesThread::execLoadingThread, this)));
00090         }
00091 
00093         //
00096         void cancel();
00097 
00099         bool inProgress()
00100         {
00101                 // TODO: should we mutex-protect this ?
00102                 return ( _thread.get() != NULL );
00103         }
00104 
00106         //
00111         bool completed()
00112         {
00113                 boost::mutex::scoped_lock lock(_mutex);
00114                 if (  _completed && _thread.get() )
00115                 {
00116                         _thread->join();
00117                         _thread.reset();
00118                 }
00119                 return _completed;
00120         }
00121 
00122         size_t getBytesLoaded() const
00123         {
00124                 // TODO: should we mutex-protect this ?
00125                 return _bytesLoaded;
00126         }
00127 
00128         size_t getBytesTotal() const
00129         {
00130                 // TODO: should we mutex-protect this ?
00131                 return _bytesTotal;
00132         }
00133 
00134 
00135 private:
00136 
00138         LoadVariablesThread& operator==(const LoadVariablesThread&); 
00139         LoadVariablesThread(const LoadVariablesThread&); 
00140 
00145         static void execLoadingThread(LoadVariablesThread* ptr)
00146         {
00147                 //log_debug("LoadVars loading thread started");
00148                 ptr->completeLoad();
00149                 //log_debug("LoadVars loading thread completed");
00150         }
00151 
00152 
00154         void setCompleted()
00155         {
00156                 boost::mutex::scoped_lock lock(_mutex);
00157                 _completed = true;
00158                 //log_debug("Completed");
00159         }
00160 
00161 
00163         //
00166         void completeLoad();
00167 
00169         //
00179         size_t parse(const std::string& str)
00180         {
00181                 URL::parse_querystring(str, _vals);
00182                 return _vals.size();
00183         }
00184 
00186         //
00189         bool cancelRequested();
00190 
00191         size_t _bytesLoaded;
00192 
00193         size_t _bytesTotal;
00194 
00195     boost::scoped_ptr<IOChannel> _stream;
00196 
00197     boost::scoped_ptr<boost::thread> _thread;
00198 
00199         ValuesMap _vals;
00200 
00201         bool _completed;
00202 
00203         bool _canceled;
00204 
00205         boost::mutex _mutex;
00206 };
00207 
00208 } // namespace gnash
00209 
00210 #endif // GNASH_LOADVARIABLESTHREAD_H