Gnash  0.8.11dev
LoadVariablesThread.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 // Free Software 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.
14 //
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
18 //
19 
20 
21 #ifndef GNASH_LOADVARIABLESTHREAD_H
22 #define GNASH_LOADVARIABLESTHREAD_H
23 
24 #include <string>
25 #include <map>
26 #include <boost/scoped_ptr.hpp>
27 #include <boost/thread/thread.hpp>
28 #include <boost/thread/mutex.hpp>
29 #include <boost/bind.hpp>
30 
31 #include "URL.h" // for inlines
32 
33 namespace gnash {
34  class StreamProvider;
35  class IOChannel;
36 }
37 
38 namespace gnash {
39 
40 // Exception thrown by LoadVariablesThread constructor if unable to connect
41 // to the stream input.
43 
45 //
50 {
51 public:
52  typedef std::map<std::string, std::string> ValuesMap;
53 
55  //
61  LoadVariablesThread(const StreamProvider& sp, const URL& url);
62 
66  //
75  LoadVariablesThread(const StreamProvider& sp, const URL& url,
76  const std::string& postdata);
77 
80 
82  ValuesMap& getValues()
83  {
84  return _vals;
85  }
86 
88  void process()
89  {
90  assert(!_thread.get());
91  assert(_stream.get());
92  _thread.reset(new boost::thread(
93  boost::bind(LoadVariablesThread::execLoadingThread, this)));
94  }
95 
97  //
100  void cancel();
101 
103  bool inProgress()
104  {
105  // TODO: should we mutex-protect this ?
106  return ( _thread.get() != NULL );
107  }
108 
110  //
115  bool completed()
116  {
117  boost::mutex::scoped_lock lock(_mutex);
118  if ( _completed && _thread.get() )
119  {
120  _thread->join();
121  _thread.reset();
122  }
123  return _completed;
124  }
125 
126  size_t getBytesLoaded() const
127  {
128  // TODO: should we mutex-protect this ?
129  return _bytesLoaded;
130  }
131 
132  size_t getBytesTotal() const
133  {
134  // TODO: should we mutex-protect this ?
135  return _bytesTotal;
136  }
137 
138 
139 private:
140 
142  LoadVariablesThread& operator==(const LoadVariablesThread&);
144 
149  static void execLoadingThread(LoadVariablesThread* ptr)
150  {
151  //log_debug("LoadVars loading thread started");
152  ptr->completeLoad();
153  //log_debug("LoadVars loading thread completed");
154  }
155 
156 
158  void setCompleted()
159  {
160  boost::mutex::scoped_lock lock(_mutex);
161  _completed = true;
162  //log_debug("Completed");
163  }
164 
165 
167  //
170  void completeLoad();
171 
173  //
183  size_t parse(const std::string& str)
184  {
185  URL::parse_querystring(str, _vals);
186  return _vals.size();
187  }
188 
190  //
193  bool cancelRequested();
194 
195  size_t _bytesLoaded;
196 
197  size_t _bytesTotal;
198 
199  boost::scoped_ptr<IOChannel> _stream;
200 
201  boost::scoped_ptr<boost::thread> _thread;
202 
203  ValuesMap _vals;
204 
205  bool _completed;
206 
207  bool _canceled;
208 
209  boost::mutex _mutex;
210 };
211 
212 } // namespace gnash
213 
214 #endif // GNASH_LOADVARIABLESTHREAD_H
static void parse_querystring(const std::string &query_string, std::map< std::string, std::string > &target_map)
Parse a query string filling the provided map.
Definition: URL.cpp:355
LoadVariablesThread(const StreamProvider &sp, const URL &url)
Construct a LoadVariablesThread opening a stream for the given URL.
Definition: LoadVariablesThread.cpp:156
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:40
std::map< std::string, std::string > ValuesMap
Definition: LoadVariablesThread.h:52
bool inProgress()
Return true if loading/parsing is in progress.
Definition: LoadVariablesThread.h:103
void process()
Start the load and parse thread.
Definition: LoadVariablesThread.h:88
size_t getBytesLoaded() const
Definition: LoadVariablesThread.h:126
A manager for loadVariable requests.
Definition: LoadVariablesThread.h:49
bool completed()
Mutex-protected inspector for thread completion.
Definition: LoadVariablesThread.h:115
~LoadVariablesThread()
Destroy the LoadVariablesThread, joining the thread if spawned.
Definition: LoadVariablesThread.cpp:185
std::string url
Definition: gnash.cpp:59
ValuesMap & getValues()
Return the name,value map parsed out of the loaded stream.
Definition: LoadVariablesThread.h:82
void cancel()
Cancel a download in progress.
Definition: LoadVariablesThread.cpp:172
size_t getBytesTotal() const
Definition: LoadVariablesThread.h:132
A StreamProvider makes IOChannels available to the core on request.
Definition: StreamProvider.h:49
Definition: LoadVariablesThread.h:42
Uniform Resource Locator.
Definition: URL.h:34