2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
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.
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
31 #include <boost/thread/thread.hpp>
32 #include <boost/thread/mutex.hpp>
33 #include <boost/bind.hpp>
35 // Forward declarations
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.
51 class LoadVariablesThread
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.
61 /// URL to post to and fetch from
63 LoadVariablesThread(const StreamProvider
& sp
, const URL
& url
);
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.
72 /// URL to post to and fetch from
75 /// Url-encoded post data.
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()
89 /// Start the load and parse thread
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
104 /// Return true if loading/parsing is in progress
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
119 boost::mutex::scoped_lock
lock(_mutex
);
120 if ( _completed
&& _thread
.get() )
128 size_t getBytesLoaded() const
130 // TODO: should we mutex-protect this ?
134 size_t getBytesTotal() const
136 // TODO: should we mutex-protect this ?
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");
155 //log_debug("LoadVars loading thread completed");
159 /// Mutex-protected mutator for thread completion
162 boost::mutex::scoped_lock
lock(_mutex
);
164 //log_debug("Completed");
168 /// Load all data from the _stream input.
170 /// This function should be run by a separate thread.
174 /// Parse an url-encoded query string
176 /// Variables in the string will be added as properties
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
);
191 /// Check if download cancel was requested
195 bool cancelRequested();
201 std::auto_ptr
<IOChannel
> _stream
;
203 std::auto_ptr
<boost::thread
> _thread
;
216 #endif // GNASH_LOADVARIABLESTHREAD_H