1 /*
2  *  This file is part of nzbget. See <http://nzbget.net>.
3  *
4  *  Copyright (C) 2004 Sven Henkel <sidddy@users.sourceforge.net>
5  *  Copyright (C) 2007-2017 Andrey Prygunkov <hugbug@users.sourceforge.net>
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 
22 #ifndef ARTICLEDOWNLOADER_H
23 #define ARTICLEDOWNLOADER_H
24 
25 #include "NString.h"
26 #include "Observer.h"
27 #include "DownloadInfo.h"
28 #include "Thread.h"
29 #include "NntpConnection.h"
30 #include "Decoder.h"
31 #include "ArticleWriter.h"
32 #include "Util.h"
33 
34 class ArticleContentAnalyzer
35 {
36 public:
~ArticleContentAnalyzer()37 	virtual ~ArticleContentAnalyzer() {};
38 	virtual void Reset() = 0;
39 	virtual void Append(const void* buffer, int len) = 0;
40 };
41 
42 class ArticleDownloader : public Thread, public Subject
43 {
44 public:
45 	enum EStatus
46 	{
47 		adUndefined,
48 		adRunning,
49 		adWaiting,
50 		adFinished,
51 		adFailed,
52 		adRetry,
53 		adCrcError,
54 		adNotFound,
55 		adConnectError,
56 		adFatalError
57 	};
58 
59 	ArticleDownloader();
60 	virtual ~ArticleDownloader();
SetFileInfo(FileInfo * fileInfo)61 	void SetFileInfo(FileInfo* fileInfo) { m_fileInfo = fileInfo; }
GetFileInfo()62 	FileInfo* GetFileInfo() { return m_fileInfo; }
SetArticleInfo(ArticleInfo * articleInfo)63 	void SetArticleInfo(ArticleInfo* articleInfo) { m_articleInfo = articleInfo; }
GetArticleInfo()64 	ArticleInfo* GetArticleInfo() { return m_articleInfo; }
GetStatus()65 	EStatus GetStatus() { return m_status; }
GetServerStats()66 	ServerStatList* GetServerStats() { return &m_serverStats; }
67 	virtual void Run();
68 	virtual void Stop();
GetLastUpdateTime()69 	time_t GetLastUpdateTime() { return m_lastUpdateTime; }
70 	void SetLastUpdateTimeNow();
GetArticleFilename()71 	const char* GetArticleFilename() { return m_articleFilename; }
72 	void SetInfoName(const char* infoName);
GetInfoName()73 	const char* GetInfoName() { return m_infoName; }
GetConnectionName()74 	const char* GetConnectionName() { return m_connectionName; }
SetConnection(NntpConnection * connection)75 	void SetConnection(NntpConnection* connection) { m_connection = connection; }
CompleteFileParts()76 	void CompleteFileParts() { m_articleWriter.CompleteFileParts(); }
GetDownloadedSize()77 	int GetDownloadedSize() { return m_downloadedSize; }
SetContentAnalyzer(std::unique_ptr<ArticleContentAnalyzer> contentAnalyzer)78 	void SetContentAnalyzer(std::unique_ptr<ArticleContentAnalyzer> contentAnalyzer) { m_contentAnalyzer = std::move(contentAnalyzer); }
GetContentAnalyzer()79 	ArticleContentAnalyzer* GetContentAnalyzer() { return m_contentAnalyzer.get(); }
80 
81 	void LogDebugInfo();
82 
83 private:
84 	FileInfo* m_fileInfo;
85 	ArticleInfo* m_articleInfo;
86 	NntpConnection* m_connection = nullptr;
87 	EStatus m_status = adUndefined;
88 	Mutex m_connectionMutex;
89 	CString m_infoName;
90 	CString m_connectionName;
91 	CString m_articleFilename;
92 	time_t m_lastUpdateTime;
93 	Decoder m_decoder;
94 	ArticleWriter m_articleWriter;
95 	ServerStatList m_serverStats;
96 	bool m_writingStarted;
97 	int m_downloadedSize = 0;
98 	std::unique_ptr<ArticleContentAnalyzer> m_contentAnalyzer;
99 
100 	EStatus Download();
101 	EStatus DecodeCheck();
102 	void FreeConnection(bool keepConnected);
103 	EStatus CheckResponse(const char* response, const char* comment);
SetStatus(EStatus status)104 	void SetStatus(EStatus status) { m_status = status; }
105 	bool Write(char* buffer, int len);
106 	void AddServerData();
107 };
108 
109 #endif
110