|
1 /* |
|
2 * Copyright 2004-2006 Intel Corporation |
|
3 * |
|
4 * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 * you may not use this file except in compliance with the License. |
|
6 * You may obtain a copy of the License at |
|
7 * |
|
8 * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 * |
|
10 * Unless required by applicable law or agreed to in writing, software |
|
11 * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 * See the License for the specific language governing permissions and |
|
14 * limitations under the License. |
|
15 */ |
|
16 |
|
17 #ifndef _GLOBAL_STORE_H_ |
|
18 #define _GLOBAL_STORE_H_ |
|
19 |
|
20 #include <oasys/debug/DebugUtils.h> |
|
21 #include <oasys/debug/Log.h> |
|
22 #include <oasys/serialize/Serialize.h> |
|
23 |
|
24 // forward decl |
|
25 namespace oasys { |
|
26 template<typename _Type> class SingleTypeDurableTable; |
|
27 class DurableStore; |
|
28 class Mutex; |
|
29 class StorageConfig; |
|
30 } |
|
31 |
|
32 namespace dtn { |
|
33 |
|
34 class Globals; |
|
35 |
|
36 /** |
|
37 * Class for those elements of the router that need to be persistently |
|
38 * stored but are singleton global values. Examples include the |
|
39 * running sequence number for bundles and registrations, as well as |
|
40 * any other persistent configuration settings. |
|
41 */ |
|
42 class GlobalStore : public oasys::Logger { |
|
43 public: |
|
44 static const u_int32_t CURRENT_VERSION; |
|
45 |
|
46 /** |
|
47 * Singleton instance accessor. |
|
48 */ |
|
49 static GlobalStore* instance() { |
|
50 if (instance_ == NULL) { |
|
51 PANIC("GlobalStore::init not called yet"); |
|
52 } |
|
53 return instance_; |
|
54 } |
|
55 |
|
56 /** |
|
57 * Boot time initializer |
|
58 * |
|
59 */ |
|
60 static int init(const oasys::StorageConfig& cfg, |
|
61 oasys::DurableStore* store); |
|
62 |
|
63 /** |
|
64 * Constructor. |
|
65 */ |
|
66 GlobalStore(); |
|
67 |
|
68 /** |
|
69 * Real initialization method. |
|
70 */ |
|
71 int do_init(const oasys::StorageConfig& cfg, |
|
72 oasys::DurableStore* store); |
|
73 |
|
74 /** |
|
75 * Return true if initialization has completed. |
|
76 */ |
|
77 static bool initialized() { return (instance_ != NULL); } |
|
78 |
|
79 /** |
|
80 * Destructor. |
|
81 */ |
|
82 ~GlobalStore(); |
|
83 |
|
84 /** |
|
85 * Get a new bundle id, updating the value in the store |
|
86 * |
|
87 * (was db_update_bundle_id, db_restore_bundle_id) |
|
88 */ |
|
89 u_int32_t next_bundleid(); |
|
90 |
|
91 /** |
|
92 * Get a new unique registration id, updating the running value in |
|
93 * the persistent table. |
|
94 * |
|
95 * (was db_new_regID, db_update_registration_id, |
|
96 * db_retable_registration_id) |
|
97 */ |
|
98 u_int32_t next_regid(); |
|
99 |
|
100 /** |
|
101 * Load in the globals. |
|
102 */ |
|
103 bool load(); |
|
104 |
|
105 /** |
|
106 * Close (and flush) the data store. |
|
107 */ |
|
108 void close(); |
|
109 |
|
110 protected: |
|
111 /** |
|
112 * Update the globals in the store. |
|
113 */ |
|
114 void update(); |
|
115 |
|
116 /** |
|
117 * Calculate a digest of on-disk serialized objects. |
|
118 */ |
|
119 void calc_digest(u_char* digest); |
|
120 |
|
121 bool loaded_; |
|
122 Globals* globals_; |
|
123 oasys::SingleTypeDurableTable<Globals>* store_; |
|
124 |
|
125 oasys::Mutex* lock_; |
|
126 |
|
127 static GlobalStore* instance_; ///< singleton instance |
|
128 }; |
|
129 } // namespace dtn |
|
130 |
|
131 #endif /* _GLOBAL_STORE_H_ */ |