Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vsnet_notify.cpp
Go to the documentation of this file.
1 #include <config.h>
2 #include <fstream>
3 
4 #include "vsfilesystem.h"
5 #include "vsnet_dloadmgr.h"
6 #include "vsnet_notify.h"
7 #include "vsnet_debug.h"
8 
9 
10 namespace VsnetDownload
11 {
12 namespace Client
13 {
14 /*------------------------------------------------------------*
15 * definition VsnetDownload::Client::NotifyMe
16 *------------------------------------------------------------*/
17 
19  _state( Idle )
20  , _error( Ok )
21  , _total( 0 )
22  , _offset( 0 )
23 {}
24 
26 {
27  _state = s;
28  _error = e;
29 }
30 
32 {
33  _total = sz;
34 }
35 
36 void NotifyMe::addBytes( int sz )
37 {
38  _offset += sz;
39 }
40 
41 /*------------------------------------------------------------*
42 * definition VsnetDownload::Client::Item
43 *------------------------------------------------------------*/
44 
45 Item::Item( SOCKETALT sock, const string &filename, VSFileSystem::VSFileType ft, NotifyPtr notify ) :
46  _filetype( ft )
47  , _sock( sock )
48  , _filename( filename )
49  , _state( Idle )
50  , _error( Ok )
51  , _notify( notify )
52 {}
53 
55 {}
56 
58 {
59  _mx.lock();
60  State ret = _state;
61  _mx.unlock();
62  return ret;
63 }
64 
66 {
67  _mx.lock();
68  VSError ret = _error;
69  _mx.unlock();
70  return ret;
71 }
73 {
74  _filetype = ft;
75 }
76 
78 {
79  return _filetype;
80 }
81 
82 void Item::setSize( int len )
83 {
84  childSetSize( len );
85  if (_notify) _notify->setTotalBytes( len );
86 }
87 
88 void Item::append( unsigned char *buffer, int bufsize )
89 {
90  childAppend( buffer, bufsize );
91  if (_notify) _notify->addBytes( bufsize );
92 }
93 
95 {
96  COUT<<__FUNCTION__<<" "<<s<<" "<<(int) _error<<endl;
97 
98  _mx.lock();
99  _state = s;
100  VSError e = _error;
101  _mx.unlock();
102  if (_notify) _notify->notify( s, e );
103 }
104 
106 {
107  COUT<<__FUNCTION__<<" "<<s<<" "<<(int) e<<endl;
108 
109  _mx.lock();
110  _state = s;
111  _error = e;
112  _mx.unlock();
113  if (_notify) _notify->notify( s, e );
114 }
115 
116 const string& Item::getFilename() const
117 {
118  return _filename;
119 }
120 
122 {
123  return _sock;
124 }
125 
126 int Item::get_fd() const
127 {
128  return _sock.get_fd();
129 }
130 
132 {
133  _notify = ptr;
134 }
135 
136 /*------------------------------------------------------------*
137 * definition VsnetDownload::Client::File
138 *------------------------------------------------------------*/
139 
140 File::File( SOCKETALT sock, const string &filename, string localbasepath, VSFileSystem::VSFileType ft, NotifyPtr notify ) :
141  Item( sock, filename, ft, notify )
142  , _destfile( filename )
143  , _localbasepath( localbasepath )
144  , _of( NULL )
145  , _len( 0 )
146  , _offset( 0 )
147 
148 {}
149 
150 File::File( const string &destfile,
151  SOCKETALT sock,
152  const string &filename,
153  string localbasepath,
155  NotifyPtr notify ) :
156  Item( sock, filename, ft, notify )
157  , _destfile( destfile )
158  , _localbasepath( localbasepath )
159  , _of( NULL )
160  , _len( 0 )
161  , _offset( 0 )
162 
163 {}
164 
166 {
167  if (_of) {
168  _of->Close();
169  delete _of;
170  }
171 }
172 
173 void File::childSetSize( int len )
174 {
175  //string filename = _localbasepath + "/" + getFilename();
176  //string filename = getFilename();
177 
178  _of = new VSFileSystem::VSFile;
179  VSFileSystem::VSError err = _of->OpenCreateWrite( _destfile.c_str(), this->_filetype );
180  if (err > Ok) {
181  delete _of;
182  _of = NULL;
183  } else {
184  _len = len;
185  }
186 }
187 
188 void File::childAppend( unsigned char *buffer, int bufsize )
189 {
190  if (_of) {
191  _of->Write( (const char*) buffer, bufsize );
192  _offset += bufsize;
193  if (_offset >= _len) {
194  _of->Close();
195  delete _of;
196  _of = NULL;
197  }
198  }
199 }
200 
201 /*------------------------------------------------------------*
202 * definition VsnetDownload::Client::NoteFile
203 *------------------------------------------------------------*/
204 
205 NoteFile::NoteFile( SOCKETALT sock, const std::string &filename, VSFileSystem::VSFileType ft, std::string localbasepath ) :
206  File( sock, filename, localbasepath, ft, NotifyPtr() )
207  , _me( new NotifyMe )
208 {
210 }
211 
212 NoteFile::NoteFile( SOCKETALT sock, const std::string &filename, VSFileSystem::VSFileType ft ) :
213  File( sock, filename, VSFileSystem::homedir, ft, NotifyPtr() )
214  , _me( new NotifyMe )
215 {
217 }
218 
219 NoteFile::NoteFile( const std::string &destfile, SOCKETALT sock, const std::string &filename, VSFileSystem::VSFileType ft ) :
220  File( destfile, sock, filename, VSFileSystem::homedir, ft, NotifyPtr() )
221  , _me( new NotifyMe )
222 {
224 }
225 
227 {}
228 
229 /*------------------------------------------------------------*
230 * definition VsnetDownload::Client::Buffer
231 *------------------------------------------------------------*/
232 
233 Buffer::Buffer( SOCKETALT sock, const string &filename, VSFileSystem::VSFileType ft ) :
234  Item( sock, filename, ft, NotifyPtr() )
235  , _me( new NotifyMe )
236  , _len( 0 )
237  , _offset( 0 )
238 {
240 }
241 
243 {}
244 
245 boost::shared_array< Buffer::uchar >Buffer::getBuffer() const
246 {
247  return _buf;
248 }
249 
250 void Buffer::childSetSize( int len )
251 {
252  _len = len;
253  _offset = 0;
254  _buf.reset( new uchar[len] );
255 }
256 
257 void Buffer::childAppend( unsigned char *buffer, int bufsize )
258 {
259  if (_offset+bufsize <= _len) {
260  memcpy( _buf.get()+_offset, buffer, bufsize );
261  _offset += bufsize;
262  }
263 }
264 
265 /*------------------------------------------------------------*
266 * definition VsnetDownload::Client::TestItem
267 *------------------------------------------------------------*/
268 
269 TestItem::TestItem( SOCKETALT sock, const string &filename ) :
270  Item( sock, filename, VSFileSystem::UnknownFile, NotifyPtr( this ) )
271 {
272  COUT<<"Created TestItem for downloading "<<filename<<endl;
273 }
274 
275 /*------------------------------------------------------------*
276 * definition VsnetDownload::Client::FileSet::NotifyConclusion
277 *------------------------------------------------------------*/
278 
280 {
281 public:
282  NotifyConclusion( FileSet*f, std::string s );
283  virtual ~NotifyConclusion();
284 
285  virtual void notify( State s, VSError e );
286 
287 private:
288  FileSet *_fileset;
289  std::string _file;
290 };
291 
293  _fileset( f )
294  , _file( s )
295 {}
296 
298 {}
299 
301 {
302  if (s == Completed) _fileset->update( _file, (e == Ok) );
303 }
304 
305 /*------------------------------------------------------------*
306 * definition VsnetDownload::Client::FileSet
307 *------------------------------------------------------------*/
308 
309 FileSet::FileSet( boost::shared_ptr< Manager >mgr, SOCKETALT sock, std::list< std::string >filenames, std::string path ) :
310  _to_go( 0 )
311 {
312  std::list< std::string >::const_iterator cit;
313  std::list< Item* >items;
314  for (cit = filenames.begin(); cit != filenames.end(); cit++) {
315  _files.insert( std::pair< std::string, int > ( *cit, -1 ) );
316  NotifyPtr ptr( new NotifyConclusion( this, *cit ) );
317  items.push_back( new File( sock, *cit, path, VSFileSystem::UnknownFile, ptr ) );
318  _to_go++;
319  }
320  mgr->addItems( items );
321 }
322 
323 bool FileSet::isDone() const
324 {
325  return _to_go == 0;
326 }
327 
328 void FileSet::update( std::string s, bool v )
329 {
330  std::map< std::string, int >::iterator it;
331  it = _files.find( s );
332  if (it != _files.end() && it->second == -1) {
333  it->second = (v ? 1 : 0);
334  _to_go--;
335  }
336 }
337 
338 /*------------------------------------------------------------*
339 * definition VsnetDownload::Client::Notify_f
340 *------------------------------------------------------------*/
341 
342 Notify_f::Notify_f( std::string filename, NotifyFunction fun ) :
343  _filename( filename )
344  , _fun( fun )
345  , _total( 0 )
346  , _offset( 0 )
347 {}
348 
350 {}
351 
353 {
354  (*_fun)(_filename, s, e, _total, _offset);
355 }
356 
358 {
359  _total = sz;
360 }
361 
362 void Notify_f::addBytes( int sz )
363 {
364  _offset += sz;
365 }
366 
367 /*------------------------------------------------------------*
368 * definition VsnetDownload::Client::VSNotify
369 *------------------------------------------------------------*/
370 
372 {
373  cerr<<"!!! DOWNLOAD ERROR : State="<<s<<" - Error="<<(int) e<<endl;
374 }
375 
377 {}
378 
380 {}
381 }; //namespace Client
382 }; //namespace VsnetDownload
383