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.h
Go to the documentation of this file.
1 #ifndef VSNET_DLOADITEM_H
2 #define VSNET_DLOADITEM_H
3 
4 #include <config.h>
5 
6 #include <string>
7 #include <vector>
8 #include <list>
9 #include <map>
10 #include <queue>
11 #include <iostream>
12 
13 #include "vsnet_dloadenum.h"
14 
15 #include "boost/shared_ptr.hpp"
16 #include "boost/shared_array.hpp"
17 
18 #include "vsnet_socket.h"
19 
20 #include "vsfilesystem.h"
21 
22 namespace VsnetDownload
23 {
24 namespace Client
25 {
26 //forward declaration
27 class Manager;
28 
36 class Notify;
37 
47 class Item;
48 
59 class NotifyMe;
60 
66 class Notify_f;
67 
75 class Notify_fp;
76 
81 class File;
82 
90 class Buffer;
91 
95 class TestItem;
96 
106 class NoteFile;
107 
112 class FileSet;
113 
114 /*------------------------------------------------------------*
115 * declaration VsnetDownload::Client::Notify
116 *------------------------------------------------------------*/
117 
118 class Notify
119 {
120 public:
121  virtual void notify( State s, VSError e ) = 0;
122  virtual void setTotalBytes( int sz ) {}
123  virtual void addBytes( int sz ) {}
124 };
125 
126 typedef boost::shared_ptr< Notify >NotifyPtr;
127 
128 /*------------------------------------------------------------*
129 * declaration VsnetDownload::Client::NotifyMe
130 *------------------------------------------------------------*/
131 
132 class NotifyMe : public Notify
133 {
134 public: NotifyMe();
135  virtual ~NotifyMe() {}
136 
137  virtual void notify( State s, VSError e );
138  virtual void setTotalBytes( int sz );
139  virtual void addBytes( int sz );
140 
141  inline bool done() const
142  {
143  return _state == Completed;
144  }
145 
146  inline bool ok() const
147  {
148  return _error == Ok;
149  }
150 
151  inline int total() const
152  {
153  return _total;
154  }
155 
156  inline int offset() const
157  {
158  return _offset;
159  }
160 
161 private:
162  State _state;
163  VSError _error;
164  int _total;
165  int _offset;
166 };
167 
168 /*------------------------------------------------------------*
169 * declaration VsnetDownload::Client::Item
170 *------------------------------------------------------------*/
171 
172 class Item
173 {
174 public: Item( SOCKETALT sock, const std::string &filename, VSFileSystem::VSFileType = VSFileSystem::UnknownFile,
175  NotifyPtr notify = NotifyPtr() );
176  virtual ~Item();
177 
178  State state() const;
179  VSError error() const;
180 
181  void changeState( State s );
182  void changeState( State s, VSError e );
183 
186 
187  void setSize( int len );
188  void append( unsigned char *buffer, int bufsize );
189 
190  const std::string& getFilename() const;
191  SOCKETALT getSock() const;
192  int get_fd() const;
193 
194 protected:
195  virtual void childSetSize( int len ) = 0;
196  virtual void childAppend( unsigned char *buffer, int bufsize ) = 0;
197 
200 
201 private:
202  SOCKETALT _sock;
203  const std::string _filename;
204 
205  mutable VSMutex _mx;
206  State _state;
207  VSError _error;
208  NotifyPtr _notify;
209 };
210 
211 /*------------------------------------------------------------*
212 * declaration VsnetDownload::Client::File
213 *------------------------------------------------------------*/
214 
215 class File : public Item
216 {
217 public: File( SOCKETALT sock, const std::string &filename, std::string localbasepath, VSFileSystem::VSFileType ft,
218  NotifyPtr notify = NotifyPtr() );
219 
220  File( const std::string &destfile, SOCKETALT sock, const std::string &filename, std::string localbasepath,
222  NotifyPtr notify = NotifyPtr() );
223 
224  virtual ~File();
225 
226 protected:
227  virtual void childSetSize( int len );
228  virtual void childAppend( unsigned char *buffer, int bufsize );
229 
230 private:
231  std::string _destfile;
232  std::string _localbasepath;
234  int _len;
235  int _offset;
236 };
237 
238 /*------------------------------------------------------------*
239 * declaration VsnetDownload::Client::NoteFile
240 *------------------------------------------------------------*/
241 
242 class NoteFile : public File
243 {
244 private:
245  NotifyPtr _me;
246 
247  inline const NotifyMe * me() const
248  {
249  return (NotifyMe*) _me.get();
250  }
251 
252 public: NoteFile( SOCKETALT sock, const std::string &filename, VSFileSystem::VSFileType ft, std::string localbasepath );
253  NoteFile( SOCKETALT sock, const std::string &filename, VSFileSystem::VSFileType ft );
254  NoteFile( const std::string &destfile, SOCKETALT sock, const std::string &filename, VSFileSystem::VSFileType ft );
255 
256  virtual ~NoteFile();
257 
258  inline bool done() const
259  {
260  return me()->done();
261  }
262 
263  inline bool ok() const
264  {
265  return me()->ok();
266  }
267 
268  inline int total() const
269  {
270  return me()->total();
271  }
272 
273  inline int offset() const
274  {
275  return me()->offset();
276  }
277 };
278 
279 /*------------------------------------------------------------*
280 * declaration VsnetDownload::Client::Buffer
281 *------------------------------------------------------------*/
282 
283 class Buffer : public Item
284 {
285  typedef unsigned char uchar;
286  NotifyPtr _me;
287 
288  inline const NotifyMe * me() const
289  {
290  return (NotifyMe*) _me.get();
291  }
292 
293 public: Buffer( SOCKETALT sock, const std::string &filename, VSFileSystem::VSFileType ft );
294 
295  virtual ~Buffer();
296 
297  boost::shared_array< uchar >getBuffer() const;
298  int getSize()
299  {
300  return this->_len;
301  }
302 
303  inline bool done() const
304  {
305  return me()->done();
306  }
307 
308  inline bool ok() const
309  {
310  return me()->ok();
311  }
312 
313  inline int total() const
314  {
315  return me()->total();
316  }
317 
318  inline int offset() const
319  {
320  return me()->offset();
321  }
322 
323 protected:
324  virtual void childSetSize( int len );
325  virtual void childAppend( unsigned char *buffer, int bufsize );
326 
327 private:
328  boost::shared_array< uchar >_buf;
329  int _len;
330  int _offset;
331 };
332 
333 /*------------------------------------------------------------*
334 * declaration VsnetDownload::Client::TestItem
335 *------------------------------------------------------------*/
336 
337 class TestItem : public Item
338  , public NotifyMe
339 {
340 public: TestItem( SOCKETALT sock, const std::string &filename );
341 
342  virtual ~TestItem() {}
343 
344 protected:
345  virtual void childSetSize( int len ) {}
346  virtual void childAppend( unsigned char *buffer, int bufsize ) {}
347 };
348 
349 /*------------------------------------------------------------*
350 * declaration VsnetDownload::Client::FileSet
351 *------------------------------------------------------------*/
352 
369 class FileSet
370 {
371 public: FileSet( boost::shared_ptr< Manager >mgr, SOCKETALT sock, std::list< std::string >filesnames, std::string localbasepath );
372 
373  bool isDone() const;
374  void update( std::string s, bool v );
375 
376 private:
377  std::map< std::string, int >_files;
378  int _to_go;
379 
380 private:
381  class NotifyConclusion;
382 };
383 
384 /*------------------------------------------------------------*
385 * declaration VsnetDownload::Client::Notify_f
386 *------------------------------------------------------------*/
387 
388 class Notify_f : public Notify
389 {
390 public:
391  typedef void (*NotifyFunction)( std::string str, State s, VSError e, int total, int offset );
392 
393 public: Notify_f( std::string filename, NotifyFunction fun );
394  virtual ~Notify_f();
395 
396  virtual void notify( State s, VSError e );
397  virtual void setTotalBytes( int sz );
398  virtual void addBytes( int sz );
399 
400 private:
401  std::string _filename;
402  NotifyFunction _fun;
403  int _total;
404  int _offset;
405 };
406 
407 /*------------------------------------------------------------*
408 * declaration VsnetDownload::Client::Notify_fp
409 *------------------------------------------------------------*/
410 
411 struct Notify_fp : public NotifyPtr
412 {
413  Notify_fp( std::string filename, Notify_f::NotifyFunction fun ) :
414  NotifyPtr( new Notify_f( filename, fun ) )
415  {}
416 };
417 
418 /*------------------------------------------------------------*
419 * declaration VsnetDownload::Client::VSNotify
420 *------------------------------------------------------------*/
421 
422 class VSNotify : public Notify
423 {
424 public:
425  void notify( State s, VSError e );
426  void setTotalBytes( int sz );
427  void addBytes( int sz );
428 };
429 }; //namespace Client
430 }; //namespace VsnetDownload
431 
432 #endif /* VSNET_DLOADITEM_H */
433