TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
UpdateFetcher.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef UpdateFetcher_h__
19 #define UpdateFetcher_h__
20 
21 #include <DBUpdater.h>
22 
23 #include <functional>
24 #include <string>
25 #include <memory>
26 #include <vector>
27 
29 {
30  typedef boost::filesystem::path Path;
31 
32 public:
33  UpdateFetcher(Path const& updateDirectory,
34  std::function<void(std::string const&)> const& apply,
35  std::function<void(Path const& path)> const& applyFile,
36  std::function<QueryResult(std::string const&)> const& retrieve);
37 
38  UpdateResult Update(bool const redundancyChecks, bool const allowRehash,
39  bool const archivedRedundancy, int32 const cleanDeadReferencesMaxCount) const;
40 
41 private:
43  {
45  MODE_REHASH
46  };
47 
48  enum State
49  {
51  ARCHIVED
52  };
53 
55  {
56  AppliedFileEntry(std::string const& name_, std::string const& hash_, State state_, uint64 timestamp_)
57  : name(name_), hash(hash_), state(state_), timestamp(timestamp_) { }
58 
59  std::string const name;
60 
61  std::string const hash;
62 
63  State const state;
64 
66 
67  static inline State StateConvert(std::string const& state)
68  {
69  return (state == "RELEASED") ? RELEASED : ARCHIVED;
70  }
71 
72  static inline std::string StateConvert(State const state)
73  {
74  return (state == RELEASED) ? "RELEASED" : "ARCHIVED";
75  }
76 
77  std::string GetStateAsString() const
78  {
79  return StateConvert(state);
80  }
81  };
82 
84  {
85  DirectoryEntry(Path const& path_, State state_) : path(path_), state(state_) { }
86 
87  Path const path;
88 
89  State const state;
90  };
91 
92  typedef std::pair<Path, State> LocaleFileEntry;
93 
94  struct PathCompare
95  {
96  inline bool operator() (LocaleFileEntry const& left, LocaleFileEntry const& right) const
97  {
98  return left.first.filename().string() < right.first.filename().string();
99  }
100  };
101 
102  typedef std::set<LocaleFileEntry, PathCompare> LocaleFileStorage;
103  typedef std::unordered_map<std::string, std::string> HashToFileNameStorage;
104  typedef std::unordered_map<std::string, AppliedFileEntry> AppliedFileStorage;
105  typedef std::vector<UpdateFetcher::DirectoryEntry> DirectoryStorage;
106 
107  LocaleFileStorage GetFileList() const;
108  void FillFileListRecursively(Path const& path, LocaleFileStorage& storage,
109  State const state, uint32 const depth) const;
110 
111  DirectoryStorage ReceiveIncludedDirectories() const;
112  AppliedFileStorage ReceiveAppliedFiles() const;
113 
114  std::string ReadSQLUpdate(Path const& file) const;
115  std::string CalculateHash(std::string const& query) const;
116 
117  uint32 Apply(Path const& path) const;
118 
119  void UpdateEntry(AppliedFileEntry const& entry, uint32 const speed = 0) const;
120  void RenameEntry(std::string const& from, std::string const& to) const;
121  void CleanUp(AppliedFileStorage const& storage) const;
122 
123  void UpdateState(std::string const& name, State const state) const;
124 
125  Path const _sourceDirectory;
126 
127  std::function<void(std::string const&)> const _apply;
128  std::function<void(Path const& path)> const _applyFile;
129  std::function<QueryResult(std::string const&)> const _retrieve;
130 };
131 
132 #endif // UpdateFetcher_h__
#define TC_DATABASE_API
Definition: Define.h:122
AppliedFileEntry(std::string const &name_, std::string const &hash_, State state_, uint64 timestamp_)
Definition: UpdateFetcher.h:56
Definition: UpdateFetcher.h:94
#define hash
Definition: private_namespace.h:186
uint64 const timestamp
Definition: UpdateFetcher.h:65
std::function< void(std::string const &)> const _apply
Definition: UpdateFetcher.h:127
Path const _sourceDirectory
Definition: UpdateFetcher.h:125
std::string GetStateAsString() const
Definition: UpdateFetcher.h:77
static State StateConvert(std::string const &state)
Definition: UpdateFetcher.h:67
std::function< void(Path const &path)> const _applyFile
Definition: UpdateFetcher.h:128
Definition: UpdateFetcher.h:83
static std::string StateConvert(State const state)
Definition: UpdateFetcher.h:72
Definition: UpdateFetcher.h:50
char * query(struct soap *soap)
Definition: httpget.cpp:244
void apply(T *val)
Definition: ByteConverter.h:41
Definition: UpdateFetcher.h:54
std::set< LocaleFileEntry, PathCompare > LocaleFileStorage
Definition: UpdateFetcher.h:102
Definition: DBUpdater.h:44
std::unordered_map< std::string, std::string > HashToFileNameStorage
Definition: UpdateFetcher.h:103
boost::filesystem::path Path
Definition: UpdateFetcher.h:30
std::string const hash
Definition: UpdateFetcher.h:61
int32_t int32
Definition: Define.h:146
std::vector< UpdateFetcher::DirectoryEntry > DirectoryStorage
Definition: UpdateFetcher.h:105
uint32_t uint32
Definition: Define.h:150
std::unordered_map< std::string, AppliedFileEntry > AppliedFileStorage
Definition: UpdateFetcher.h:104
uint64_t uint64
Definition: Define.h:149
std::shared_ptr< ResultSet > QueryResult
Definition: QueryResult.h:61
bool left(const int *a, const int *b, const int *c)
Definition: RecastContour.cpp:487
State const state
Definition: UpdateFetcher.h:89
std::pair< Path, State > LocaleFileEntry
Definition: UpdateFetcher.h:92
Definition: UpdateFetcher.h:44
std::function< QueryResult(std::string const &)> const _retrieve
Definition: UpdateFetcher.h:129
State const state
Definition: UpdateFetcher.h:63
std::string const name
Definition: UpdateFetcher.h:59
State
Definition: UpdateFetcher.h:48
Definition: UpdateFetcher.h:28
Path const path
Definition: UpdateFetcher.h:87
UpdateMode
Definition: UpdateFetcher.h:42
DirectoryEntry(Path const &path_, State state_)
Definition: UpdateFetcher.h:85
void Update(uint32 diff)
Definition: WeatherMgr.cpp:150