use the VERSION instead of master in the revno.h generated from a src tarball
[gnash.git] / libcore / LoadVariablesThread.h
blob49f2dbddfa7e262e6969bb227cdc14eb6de0317e
1 //
2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
3 // Foundation, Inc
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #ifndef GNASH_LOADVARIABLESTHREAD_H
22 #define GNASH_LOADVARIABLESTHREAD_H
24 #include "StreamProvider.h" // for inlines
25 #include "URL.h" // for inlines
28 #include <string>
29 #include <map>
30 #include <memory>
31 #include <boost/thread/thread.hpp>
32 #include <boost/thread/mutex.hpp>
33 #include <boost/bind.hpp>
35 // Forward declarations
36 namespace gnash {
37 //class URL;
40 namespace gnash {
42 // Exception thrown by LoadVariablesThread constructor if unable to connect
43 // to the stream input.
44 class NetworkException {};
46 /// A manager for loadVariable requests
48 /// Provides services for starting a "load and parse" thread, checking
49 /// its status and getting a parsed variables structure back when done.
50 ///
51 class LoadVariablesThread
53 public:
54 typedef std::map<std::string, std::string> ValuesMap;
56 /// Construct a LoadVariablesThread opening a stream for the given URL
58 /// Throws a NetworkException if unable.
59 ///
60 /// @param url
61 /// URL to post to and fetch from
62 ///
63 LoadVariablesThread(const StreamProvider& sp, const URL& url);
65 /// \brief
66 /// Construct a LoadVariablesThread opening a stream for the given URL,
67 /// posting the given url-encoded data if using HTTP.
69 /// Throws a NetworkException if unable.
70 ///
71 /// @param url
72 /// URL to post to and fetch from
73 ///
74 /// @param postdata
75 /// Url-encoded post data.
76 ///
77 LoadVariablesThread(const StreamProvider& sp, const URL& url,
78 const std::string& postdata);
80 /// Destroy the LoadVariablesThread, joining the thread if spawned
81 ~LoadVariablesThread();
83 /// Return the name,value map parsed out of the loaded stream
84 ValuesMap& getValues()
86 return _vals;
89 /// Start the load and parse thread
90 void process()
92 assert(!_thread.get());
93 assert(_stream.get());
94 _thread.reset(new boost::thread(
95 boost::bind(LoadVariablesThread::execLoadingThread, this)));
98 /// Cancel a download in progress
100 /// Locks _mutex
102 void cancel();
104 /// Return true if loading/parsing is in progress
105 bool inProgress()
107 // TODO: should we mutex-protect this ?
108 return ( _thread.get() != NULL );
111 /// Mutex-protected inspector for thread completion
113 /// Only call this method from the same thread that
114 /// also called process(), as the thread will be joined
115 /// if it completed.
117 bool completed()
119 boost::mutex::scoped_lock lock(_mutex);
120 if ( _completed && _thread.get() )
122 _thread->join();
123 _thread.reset();
125 return _completed;
128 size_t getBytesLoaded() const
130 // TODO: should we mutex-protect this ?
131 return _bytesLoaded;
134 size_t getBytesTotal() const
136 // TODO: should we mutex-protect this ?
137 return _bytesTotal;
141 private:
143 /// Prevent copy
144 LoadVariablesThread& operator==(const LoadVariablesThread&);
145 LoadVariablesThread(const LoadVariablesThread&);
147 /// Since I haven't found a way to pass boost::thread
148 /// constructor a non-static function, this is here to
149 /// workaround that limitation (in either boost or more
150 /// likely my own knowledge of it)
151 static void execLoadingThread(LoadVariablesThread* ptr)
153 //log_debug("LoadVars loading thread started");
154 ptr->completeLoad();
155 //log_debug("LoadVars loading thread completed");
159 /// Mutex-protected mutator for thread completion
160 void setCompleted()
162 boost::mutex::scoped_lock lock(_mutex);
163 _completed = true;
164 //log_debug("Completed");
168 /// Load all data from the _stream input.
170 /// This function should be run by a separate thread.
172 void completeLoad();
174 /// Parse an url-encoded query string
176 /// Variables in the string will be added as properties
177 /// of this object.
179 /// @param querystring
180 /// An url-encoded query string.
181 /// The string will be parsed using URL::parse_querystring
183 /// @return the number of variables found in the string
185 size_t parse(const std::string& str)
187 URL::parse_querystring(str, _vals);
188 return _vals.size();
191 /// Check if download cancel was requested
193 /// Locks _mutex
195 bool cancelRequested();
197 size_t _bytesLoaded;
199 size_t _bytesTotal;
201 std::auto_ptr<IOChannel> _stream;
203 std::auto_ptr<boost::thread> _thread;
205 ValuesMap _vals;
207 bool _completed;
209 bool _canceled;
211 boost::mutex _mutex;
214 } // namespace gnash
216 #endif // GNASH_LOADVARIABLESTHREAD_H