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 "StreamProvider.h" // for inlines
32 #include "URL.h" // for inlines
33 
34 namespace gnash {
35 
36 // Exception thrown by LoadVariablesThread constructor if unable to connect
37 // to the stream input.
39 
41 //
46 {
47 public:
48  typedef std::map<std::string, std::string> ValuesMap;
49 
51  //
57  LoadVariablesThread(const StreamProvider& sp, const URL& url);
58 
62  //
71  LoadVariablesThread(const StreamProvider& sp, const URL& url,
72  const std::string& postdata);
73 
76 
79  {
80  return _vals;
81  }
82 
84  void process()
85  {
86  assert(!_thread.get());
87  assert(_stream.get());
88  _thread.reset(new boost::thread(
89  boost::bind(LoadVariablesThread::execLoadingThread, this)));
90  }
91 
93  //
96  void cancel();
97 
99  bool inProgress()
100  {
101  // TODO: should we mutex-protect this ?
102  return ( _thread.get() != NULL );
103  }
104 
106  //
111  bool completed()
112  {
113  boost::mutex::scoped_lock lock(_mutex);
114  if ( _completed && _thread.get() )
115  {
116  _thread->join();
117  _thread.reset();
118  }
119  return _completed;
120  }
121 
122  size_t getBytesLoaded() const
123  {
124  // TODO: should we mutex-protect this ?
125  return _bytesLoaded;
126  }
127 
128  size_t getBytesTotal() const
129  {
130  // TODO: should we mutex-protect this ?
131  return _bytesTotal;
132  }
133 
134 
135 private:
136 
138  LoadVariablesThread& operator==(const LoadVariablesThread&);
140 
145  static void execLoadingThread(LoadVariablesThread* ptr)
146  {
147  //log_debug("LoadVars loading thread started");
148  ptr->completeLoad();
149  //log_debug("LoadVars loading thread completed");
150  }
151 
152 
154  void setCompleted()
155  {
156  boost::mutex::scoped_lock lock(_mutex);
157  _completed = true;
158  //log_debug("Completed");
159  }
160 
161 
163  //
166  void completeLoad();
167 
169  //
179  size_t parse(const std::string& str)
180  {
181  URL::parse_querystring(str, _vals);
182  return _vals.size();
183  }
184 
186  //
189  bool cancelRequested();
190 
191  size_t _bytesLoaded;
192 
193  size_t _bytesTotal;
194 
195  boost::scoped_ptr<IOChannel> _stream;
196 
197  boost::scoped_ptr<boost::thread> _thread;
198 
199  ValuesMap _vals;
200 
201  bool _completed;
202 
203  bool _canceled;
204 
205  boost::mutex _mutex;
206 };
207 
208 } // namespace gnash
209 
210 #endif // GNASH_LOADVARIABLESTHREAD_H