Panda3D
 All Classes Functions Variables Enumerations
downloadDb.h
1 // Filename: downloadDb.h
2 // Created by: shochet (06Sep00)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 #ifndef DOWNLOADDB_H
15 #define DOWNLOADDB_H
16 
17 #include "pandabase.h"
18 #include "pnotify.h"
19 #include "filename.h"
20 #include "multifile.h"
21 #include "datagram.h"
22 #include "datagramIterator.h"
23 
24 #include "pvector.h"
25 #include "pointerTo.h"
26 #include "pmap.h"
27 
28 #include "hashVal.h"
29 
30 class StreamReader;
31 class StreamWriter;
32 typedef PN_stdfloat Phase;
33 class Ramfile;
34 
35 /*
36 //////////////////////////////////////////////////
37 // Database Format
38 //////////////////////////////////////////////////
39 magic_number
40 number_of_multifiles
41 header_length multifile_name phase version size status num_files
42  header_length file_name version hash
43  header_length file_name version hash
44 header_length multifile_name phase version size status num_files
45  header_length file_name version hash
46  header_length file_name version hash
47  ...
48 ...
49 
50 
51 A Db is a Vector<MultifileRecord>
52 MultifileRecord is a Vector<FileRecord>
53 */
54 
55 
56 ////////////////////////////////////////////////////////////////////
57 // Class : DownloadDb
58 // Description : A listing of files within multifiles for management
59 // of client-side synchronization with a server-provided
60 // set of files.
61 //
62 // This class manages one copy of the database for the
63 // client, representing the files on the client system,
64 // and another copy for the server, representing the
65 // files the server has available.
66 ////////////////////////////////////////////////////////////////////
67 class EXPCL_PANDAEXPRESS DownloadDb {
68 PUBLISHED:
69  // Status of a multifile is stored in this enum
70  // Note these values are in increasing order of "doneness"
71  // So if you are decompressed, you are complete
72  // If you are extracted, you are decompressed and complete
73  enum Status {
74  Status_incomplete = 0,
75  Status_complete = 1,
76  Status_decompressed = 2,
77  Status_extracted = 3
78  };
79 
80  DownloadDb();
81  DownloadDb(Ramfile &server_file, Filename &client_file);
82  DownloadDb(Filename &server_file, Filename &client_file);
83  ~DownloadDb();
84 
85  void output(ostream &out) const;
86  void write(ostream &out) const;
87  void write_version_map(ostream &out) const;
88 
89  // Write a database file
90  bool write_client_db(Filename &file);
91  bool write_server_db(Filename &file);
92 
93  INLINE int get_client_num_multifiles() const;
94  INLINE int get_server_num_multifiles() const;
95 
96  INLINE string get_client_multifile_name(int index) const;
97  INLINE string get_server_multifile_name(int index) const;
98 
99  INLINE int get_client_multifile_size(string mfname) const;
100  INLINE void set_client_multifile_size(string mfname, int size);
101  INLINE int set_client_multifile_delta_size(string mfname, int size);
102  INLINE int get_server_multifile_size(string mfname) const;
103  INLINE void set_server_multifile_size(string mfname, int size);
104 
105  INLINE Phase get_client_multifile_phase(string mfname) const;
106  INLINE Phase get_server_multifile_phase(string mfname) const;
107 
108  INLINE void set_client_multifile_incomplete(string mfname);
109  INLINE void set_client_multifile_complete(string mfname);
110  INLINE void set_client_multifile_decompressed(string mfname);
111  INLINE void set_client_multifile_extracted(string mfname);
112 
113  INLINE int get_server_num_files(string mfname) const;
114  INLINE string get_server_file_name(string mfname, int index) const;
115 
116  // Queries from the Launcher
117  bool client_multifile_exists(string mfname) const;
118  bool client_multifile_complete(string mfname) const;
119  bool client_multifile_decompressed(string mfname) const;
120  bool client_multifile_extracted(string mfname) const;
121 
122  // Ask what version (told with the hash) this multifile is
123  HashVal get_client_multifile_hash(string mfname) const;
124  void set_client_multifile_hash(string mfname, HashVal val);
125  HashVal get_server_multifile_hash(string mfname) const;
126  void set_server_multifile_hash(string mfname, HashVal val);
127 
128  // Operations on multifiles
129  void delete_client_multifile(string mfname);
130  void add_client_multifile(string server_mfname);
131  void expand_client_multifile(string mfname);
132 
133  // Server side operations to create multifile records
134  void create_new_server_db();
135  void server_add_multifile(string mfname, Phase phase, int size, int status);
136  void server_add_file(string mfname, string fname);
137 
138 public:
139 
140  class EXPCL_PANDAEXPRESS FileRecord : public ReferenceCount {
141  public:
142  FileRecord();
143  FileRecord(string name);
144  void write(ostream &out) const;
145  string _name;
146  };
147 
149 
150  class EXPCL_PANDAEXPRESS MultifileRecord : public ReferenceCount {
151  public:
152  MultifileRecord();
153  MultifileRecord(string name, Phase phase, int size, int status);
154  void write(ostream &out) const;
155  int get_num_files() const;
156  string get_file_name(int index) const;
157  bool file_exists(string fname) const;
158  PT(FileRecord) get_file_record_named(string fname) const;
159  void add_file_record(PT(FileRecord) fr);
160  string _name;
161  Phase _phase;
162  int _size;
163  int _status;
164  HashVal _hash;
165  PN_int32 _num_files;
166  FileRecords _file_records;
167  };
168 
170 
171  class EXPCL_PANDAEXPRESS Db {
172  public:
173  Db();
174  void write(ostream &out) const;
175  int get_num_multifiles() const;
176  string get_multifile_name(int index) const;
177  bool multifile_exists(string mfname) const;
178  PT(MultifileRecord) get_multifile_record_named(string mfname) const;
179  void add_multifile_record(PT(MultifileRecord) mfr);
180  int parse_header(const string &data);
181  int parse_record_header(const string &data);
182  PT(MultifileRecord) parse_mfr(const string &data);
183  PT(FileRecord) parse_fr(const string &data);
184  bool read(StreamReader &sr, bool want_server_info);
185  bool write(StreamWriter &sw, bool want_server_info);
186  Filename _filename;
187  MultifileRecords _mfile_records;
188  bool write_header(ostream &write_stream);
189  bool write_bogus_header(StreamWriter &sw);
190  private:
191  PN_int32 _header_length;
192  };
193 
194 PUBLISHED:
195  Db read_db(Filename &file, bool want_server_info);
196  Db read_db(Ramfile &file, bool want_server_info);
197  bool write_db(Filename &file, Db db, bool want_server_info);
198 
199 public:
200  // The download db stores two databases, one that represents the
201  // client's state and one that represents the server state.
202  Db _client_db;
203  Db _server_db;
204 
205  // Magic number for knowing this is a download Db
206  static PN_uint32 _magic_number;
207  static PN_uint32 _bogus_magic_number;
210 
211 PUBLISHED:
212  void add_version(const Filename &name, const HashVal &hash, int version);
213  void insert_new_version(const Filename &name, const HashVal &hash);
214  bool has_version(const Filename &name) const;
215  int get_num_versions(const Filename &name) const;
216  void set_num_versions(const Filename &name, int num_versions);
217 
218  int get_version(const Filename &name, const HashVal &hash) const;
219  const HashVal &get_hash(const Filename &name, int version) const;
220 
221 protected:
222  void write_version_map(StreamWriter &sw);
223  bool read_version_map(StreamReader &sr);
224  VersionMap _versions;
225 };
226 
227 INLINE ostream &operator << (ostream &out, const DownloadDb &dldb) {
228  dldb.output(out);
229  return out;
230 }
231 
232 
233 #include "downloadDb.I"
234 
235 #endif
A StreamWriter object is used to write sequential binary data directly to an ostream.
Definition: streamWriter.h:33
A listing of files within multifiles for management of client-side synchronization with a server-prov...
Definition: downloadDb.h:67
Stores a 128-bit value that represents the hashed contents (typically MD5) of a file or buffer...
Definition: hashVal.h:32
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:44
An in-memory buffer specifically designed for downloading files to memory.
Definition: ramfile.h:27
A base class for all things that want to be reference-counted.
A class to read sequential binary data directly from an istream.
Definition: streamReader.h:30