1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 //    electricsheep for windows - collaborative screensaver
4 //    Copyright 2003 Nicholas Long <nlong@cox.net>
5 //	  electricsheep for windows is based of software
6 //	  written by Scott Draves <source@electricsheep.org>
7 //
8 //    This program is free software; you can redistribute it and/or modify
9 //    it under the terms of the GNU General Public License as published by
10 //    the Free Software Foundation; either version 2 of the License, or
11 //    (at your option) any later version.
12 //
13 //    This program is distributed in the hope that it will be useful,
14 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 //    GNU General Public License for more details.
17 //
18 //    You should have received a copy of the GNU General Public License
19 //    along with this program; if not, write to the Free Software
20 //    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 //
22 ///////////////////////////////////////////////////////////////////////////////
23 #ifndef _SHEEPDOWNLOADER_H_
24 #define _SHEEPDOWNLOADER_H_
25 
26 #if 1//DASVO_TEST
27 #include "tinyxml.h"
28 #else
29 #include "expat.h"
30 #endif
31 #include "Sheep.h"
32 #include "Networking.h"
33 
34 namespace ContentDownloader
35 {
36 class SheepRenderer;
37 
38 /*
39 
40 	This class is responsible for downloading and queueing new sheep to the renderer..
41 */
42 class SheepDownloader
43 {
44 	// number of sheep that the downloader has downloaded
45 	static int fDownloadedSheep;
46 
47 	// sheep flocks
48 	SheepArray fServerFlock;
49 	SheepArray fClientFlock;
50 	SheepRenderer *fRenderer;
51 
52 	// boolean for message checks
53 	bool fHasMessage;
54 	static uint32 fCurrentGeneration;
55 
56 	static time_t fLastListTime;
57 
58 	static boost::mutex	s_DownloaderMutex;
59 
60 	bool m_bAborted;
61 
62 	Network::spCFileDownloader m_spSheepDownloader;
63 
64 	boost::mutex m_AbortMutex;
65 
66 	protected:
67 
68 		//	Downloads the given sheep and queues it up for rendering.
69 		bool downloadSheep( Sheep *sheep );
70 
71 		//	Function to parse the cache and find a sheep to download.
72 		void findSheepToDownload();
73 
74 		//	Ipdate the cached sheep and make sure there is enough room for a second sheep.
75 		void updateCachedSheep();
76 
77 		//	Clears the flock list being maintained
78 		void clearFlocks();
79 
80 		//	Delete enough sheep to clear enough room for the given amount of bytes.
81 		void deleteCached( const uint64 &bytes, const int getGenerationType );
82 
83 		bool isFolderAccessible( const char *folder );
84 
85 		//	This methods parses the sheep list and intializes the array of server sheep.
86 		void parseSheepList();
87 
88 #if 1//DASVO_TEST
89 		void handleListElement(TiXmlElement* listElement);
90 #else
91 		//	Functions to parse the XML structures.
92 		static void listStartElement( void *userData, const char *name, const char **atts );
93 		static void characterHandler( void *userData, const char *s, int len );
94 		static void getEndElement( void *userData, const char *name );
95 #endif
96 
97 		//	Message retrival from server.
setHasMessage(const bool & hasMessage)98 		void setHasMessage(const bool &hasMessage) { fHasMessage = hasMessage; }
hasMessage()99 		bool hasMessage() const { return fHasMessage; }
100 
setCurrentGeneration(const uint32 & generation)101 		void setCurrentGeneration(const uint32 &generation) { fCurrentGeneration = generation; }
102 
103 		//	Checks the disk space to make sure the cache is not being overflowed.
104 		int cacheOverflow(const double &bytes, const int getGenerationType) const;
105 
106 		// Clean global and static data for the downloader threads.
107 		static void closeDownloader();
108 
109 		//	Function to initialize the downloader threads
110 		static void initializeDownloader();
111 
112 		void deleteSheep(Sheep *sheep);
113 
114 		static bool fGotList;
115 
116 		static bool fListDirty;
117 
118 	public:
119 			SheepDownloader();
120 			virtual ~SheepDownloader();
121 
122 			static void shepherdCallback(void* data);
123 
124 			static int numberOfDownloadedSheep();
125 
currentGeneration()126 			static uint32 currentGeneration() { return fCurrentGeneration; }
127 
128 			static bool getSheepList();
129 
130 
131 			// add to the number of downloaded sheep (called by torrent)
addDownloadedSheep(int sheep)132 			static void addDownloadedSheep(int sheep) { fDownloadedSheep += sheep; }
133 
134 			void deleteSheepId(uint32 sheepId);
135 
136 			// Aborts the working thread
137 			void Abort( void );
138 
139 			//	Declare friend classes for protected data accesss.
140 			friend class Shepherd;
141 };
142 
143 };
144 
145 #endif
146