1 // ==============================================================
2 //	This file is part of MegaGlest Shared Library (www.glest.org)
3 //
4 //	Copyright (C) 2009-2010 Titus Tscharntke (info@titusgames.de) and
5 //                          Mark Vejvoda (mark_vejvoda@hotmail.com)
6 //
7 //	You can redistribute this code and/or modify it under
8 //	the terms of the GNU General Public License as published
9 //	by the Free Software Foundation; either version 2 of the
10 //	License, or (at your option) any later version
11 // ==============================================================
12 #ifndef _SHARED_PLATFORMCOMMON_MINIFTPCLIENTTHREAD_H_
13 #define _SHARED_PLATFORMCOMMON_MINIFTPCLIENTTHREAD_H_
14 
15 #include "base_thread.h"
16 #include <vector>
17 #include <string>
18 #include "platform_common.h"
19 #include "leak_dumper.h"
20 
21 using namespace std;
22 
23 namespace Shared { namespace PlatformCommon {
24 
25 // =====================================================
26 //	class FTPClientThread
27 // =====================================================
28 
29 enum FTP_Client_ResultType {
30     ftp_crt_SUCCESS 	= 0,
31     ftp_crt_PARTIALFAIL = 1,
32     ftp_crt_FAIL    	= 2,
33     ftp_crt_ABORTED 	= 3,
34     ftp_crt_HOST_NOT_ACCEPTING = 4
35 };
36 
37 enum FTP_Client_CallbackType {
38     ftp_cct_Map                 = 0,
39     ftp_cct_Tileset             = 1,
40     ftp_cct_Techtree            = 2,
41     ftp_cct_Scenario           	= 3,
42     ftp_cct_File           		= 4,
43     ftp_cct_TempFile       		= 5,
44     ftp_cct_DownloadProgress    = 6,
45     ftp_cct_ExtractProgress     = 7
46 };
47 
48 class FTPClientCallbackInterface {
49 public:
~FTPClientCallbackInterface()50 	virtual ~FTPClientCallbackInterface() {}
51     struct FtpProgressStats {
52       double download_total;
53       double download_now;
54       double upload_total;
55       double upload_now;
56       string currentFilename;
57       FTP_Client_CallbackType downloadType;
58     };
59 
60     virtual void FTPClient_CallbackEvent(string itemName,
61     										 FTP_Client_CallbackType type,
62     										 pair<FTP_Client_ResultType,string> result,
63     										 void *userdata) = 0;
64 };
65 
66 class FTPClientThread : public BaseThread, public ShellCommandOutputCallbackInterface
67 {
68 protected:
69     int portNumber;
70     string serverUrl;
71     FTPClientCallbackInterface *pCBObject;
72     std::pair<string,string> mapsPath;
73     std::pair<string,string> tilesetsPath;
74     std::pair<string,string> techtreesPath;
75     std::pair<string,string> scenariosPath;
76     string tempFilesPath;
77 
78     Mutex mutexMapFileList;
79     vector<pair<string,string> > mapFileList;
80 
81     Mutex mutexTilesetList;
82     vector<pair<string,string> > tilesetList;
83 
84     Mutex mutexTechtreeList;
85     vector<pair<string,string> > techtreeList;
86 
87     Mutex mutexScenarioList;
88     vector<pair<string,string> > scenarioList;
89 
90     Mutex mutexFileList;
91     vector<pair<string,string> > fileList;
92 
93     Mutex mutexTempFileList;
94     vector<pair<string,string> > tempFileList;
95 
96     void getMapFromServer(pair<string,string> mapFilename);
97     pair<FTP_Client_ResultType,string> getMapFromServer(pair<string,string> mapFileName, string ftpUser, string ftpUserPassword);
98 
99     void getTilesetFromServer(pair<string,string> tileSetName);
100     pair<FTP_Client_ResultType,string> getTilesetFromServer(pair<string,string> tileSetName, string tileSetNameSubfolder, string ftpUser, string ftpUserPassword, bool findArchive);
101 
102     void getTechtreeFromServer(pair<string,string> techtreeName);
103     pair<FTP_Client_ResultType,string> getTechtreeFromServer(pair<string,string> techtreeName, string ftpUser, string ftpUserPassword);
104 
105     void getScenarioFromServer(pair<string,string> fileName);
106     pair<FTP_Client_ResultType,string> getScenarioInternalFromServer(pair<string,string> fileName);
107 
108     void getFileFromServer(pair<string,string> fileName);
109     pair<FTP_Client_ResultType,string> getFileInternalFromServer(pair<string,string> fileName);
110 
111     void getTempFileFromServer(pair<string,string> fileName);
112     pair<FTP_Client_ResultType,string> getTempFileInternalFromServer(pair<string,string> fileName);
113 
114     Mutex mutexProgressMutex;
115 
116     string fileArchiveExtension;
117     string fileArchiveExtractCommand;
118     string fileArchiveExtractCommandParameters;
119     int fileArchiveExtractCommandSuccessResult;
120 
121     pair<FTP_Client_ResultType,string> getFileFromServer(FTP_Client_CallbackType downloadType,
122     		pair<string,string> fileNameTitle,
123     		string remotePath, string destFileSaveAs, string ftpUser,
124     		string ftpUserPassword, vector <string> *wantDirListOnly=NULL);
125 
126     string shellCommandCallbackUserData;
127     virtual void * getShellCommandOutput_UserData(string cmd);
128     virtual void ShellCommandOutput_CallbackEvent(string cmd,char *output,void *userdata);
129 
130 public:
131 
132     FTPClientThread(int portNumber,string serverUrl,
133     				std::pair<string,string> mapsPath,
134     				std::pair<string,string> tilesetsPath,
135     				std::pair<string,string> techtreesPath,
136     				std::pair<string,string> scenariosPath,
137     				FTPClientCallbackInterface *pCBObject,
138     				string fileArchiveExtension,
139     				string fileArchiveExtractCommand,
140     				string fileArchiveExtractCommandParameters,
141     				int fileArchiveExtractCommandSuccessResult,
142     				string tempFilesPath);
143     virtual void execute();
144     virtual void signalQuit();
145     virtual bool shutdownAndWait();
146 
147     void addMapToRequests(string mapFilename,string URL="");
148     void addTilesetToRequests(string tileSetName,string URL="");
149     void addTechtreeToRequests(string techtreeName,string URL="");
150     void addScenarioToRequests(string fileName,string URL="");
151     void addFileToRequests(string fileName,string URL="");
152     void addTempFileToRequests(string fileName,string URL="");
153 
154     FTPClientCallbackInterface * getCallBackObject();
155     void setCallBackObject(FTPClientCallbackInterface *value);
156 
getProgressMutex()157     Mutex * getProgressMutex() { return &mutexProgressMutex; }
158 };
159 
160 }}//end namespace
161 
162 #endif
163