-
Notifications
You must be signed in to change notification settings - Fork 1
/
ManifestManager.cpp
269 lines (219 loc) · 7.03 KB
/
ManifestManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include "ManifestManager.h"
#include "BValue.h"
#include "PeerID.h"
#include "Message.h"
#include "MessageCore.h"
#include "CredentialManager.h"
#include "ManifestService.h"
#include "SystemIDs.h"
using namespace RAIN;
extern RAIN::MessageCore *globalMessageCore;
extern RAIN::CredentialManager *globalCredentialManager;
ManifestManager::ManifestManager()
{
this->tickCount = 0;
}
ManifestManager::~ManifestManager()
{
}
void ManifestManager::HandleMessage(RAIN::Message *msg)
{
BENC_SAFE_CAST(msg->headers["Event"], msgevent, String);
switch (msg->status)
{
case MSG_STATUS_INCOMING:
if (msgevent->getString() == "Manifest-Update")
{
this->MergeManifests(msg);
} else if (msgevent->getString() == "Synchronise") {
this->CheckSynchronise(msg);
}
break;
case MSG_STATUS_LOCAL:
if (msgevent->getString() == "Force-Update")
{
this->Synchronise();
}
break;
default:
// ignore
break;
}
}
void ManifestManager::Tick()
{
this->tickCount--;
if (this->tickCount <= 0)
{
this->Synchronise();
this->tickCount = SYNC_INTERVAL;
}
}
int ManifestManager::GetMessageHandlerID()
{
return MANIFEST_ID;
}
/** merges all the manifests in message <b>m</b> into our local manifest store,
* unless they already exist */
void ManifestManager::MergeManifests(RAIN::Message *m)
{
BENC_SAFE_CAST(m->headers["Manifests"], manifests, List);
if (!manifests)
return;
size_t size = manifests->getListSize();
wxLogVerbose("ManifestManager::MergeManifests with %d manifests", size);
for (size_t i = 0; i < size; i++)
{
BEnc::Value *v = manifests->getListItem(i);
ManifestEntry e(v);
if (e.Verify())
{
wxLogVerbose("manifest %d verified, saving", i);
e.Save();
} else {
wxLogVerbose("manifest %d bad, ignoring", i);
}
}
}
/** sends a message to a random connected peer with our manifest timestamp,
* the remote peer should check this against their manifest timestamp.
*
* if they are newer, they are required to send as many of their manifest entries
* as they like (minimum of 1, no maximum), which are then merged on our end and
* bring us closer up-to-date
*
* if they are later, they simply reply with a synchronise of their own, and we
* do the opposite
*
* if we're the same, they do nothing */
void ManifestManager::Synchronise()
{
Message *m = new Message();
m->status = MSG_STATUS_OUTGOING;
m->subsystem = MANIFEST_ID;
m->headers["From"] = globalCredentialManager->GetMyPeerID()->AsBValue();
m->headers["Destination"] = new BEnc::String("Random");
m->headers["Event"] = new BEnc::String("Synchronise");
m->headers["Timestamp"] = new BEnc::Integer(this->getLatestTimestamp());
globalMessageCore->PushMessage(m);
}
/** returns the manifest directory, after ensuring it exists */
wxString ManifestManager::getManifestDir()
{
wxString dir(CredentialManager::GetCredentialFilename(RAIN::MANIFEST_DIR));
if (!wxDirExists(dir))
{
wxFileName::Mkdir(dir, 0755, wxPATH_MKDIR_FULL);
if (!wxDirExists(dir))
{
wxLogError("Cannot open/create manifest directory");
return "";
}
}
return dir;
}
/** returns the latest timestamp of any manifest entry */
time_t ManifestManager::getLatestTimestamp()
{
time_t latest = 0;
wxDir dir(this->getManifestDir());
wxString filename;
bool cont = dir.GetFirst(&filename, "*.rnm", wxDIR_FILES);
if (cont)
{
wxFileName fn(CredentialManager::GetCredentialFilename(RAIN::MANIFEST_DIR), filename);
ManifestEntry ent(fn);
if (latest < ent.getTimestamp())
{
latest = ent.getTimestamp();
}
cont = dir.GetNext(&filename);
}
wxLogVerbose("ManifestManager::getLatestTimestamp() returning %ld", latest);
return latest;
}
/** handles a "Synchronise" message as outlined in the commentry for Synchronise() */
void ManifestManager::CheckSynchronise(RAIN::Message *m)
{
wxLogVerbose("ManifestManager::CheckSynchronise()");
BENC_SAFE_CAST(m->headers["Timestamp"], theirtimestamp, Integer);
if (!theirtimestamp)
return;
time_t ours = this->getLatestTimestamp();
time_t theirs = theirtimestamp->getNum();
if (ours == theirs)
{
/* we're syncd, do nothing */
wxLogVerbose("ManifestManager::CheckSynchronise() - %d == %d so we are doing no sync", ours, theirs);
return;
} else if (ours > theirs) {
wxLogVerbose("ManifestManager::CheckSynchronise() - %d > %d so we are sending %d manifests", ours, theirs, MANIFEST_CHUNKS);
/* send a few manifests */
std::vector<RAIN::ManifestEntry*> v;
size_t got = this->getManifestsAfter(v, theirs, MANIFEST_CHUNKS);
/* make a new message */
Message *msg = new Message();
msg->status = MSG_STATUS_OUTGOING;
msg->subsystem = MANIFEST_ID;
msg->headers["To"] = m->GetReply();
msg->headers["From"] = globalCredentialManager->GetMyPeerID()->AsBValue();
msg->headers["Destination"] = new BEnc::String("Peer");
msg->headers["Event"] = new BEnc::String("Manifest-Update");
BEnc::List *mans = new BEnc::List();
msg->headers["Manifests"] = mans;
for (size_t i = 0; i < got; i++)
{
mans->list.push_back(v.at(i)->getBenc());
delete v.at(i);
}
globalMessageCore->PushMessage(msg);
} else if (theirs > ours) {
wxLogVerbose("ManifestManager::CheckSynchronise() - %d > %d so we are doing sync right now", theirs, ours);
/* we are out of sync - let's do it right now! */
this->Synchronise();
}
}
/** fills <b>v</b> with <b>n</b> ManifestEntry's immediately after time <b>after</b>
* and returns how many were put into v */
size_t ManifestManager::getManifestsAfter(std::vector<RAIN::ManifestEntry*>& v, time_t after, int n)
{
wxLogVerbose("ManifestManager::getManifestsAfter() called: %d manifests after %d", n, after);
wxDir dir(this->getManifestDir());
wxString filename;
bool cont = dir.GetFirst(&filename, "*.rnm", wxDIR_FILES);
bool enough = false;
bool lateEnough = false;
while (cont && !enough)
{
wxFileName fn(CredentialManager::GetCredentialFilename(RAIN::MANIFEST_DIR), filename);
ManifestEntry ent(fn);
if (!lateEnough && ent.getTimestamp() > after)
{
lateEnough = true;
}
if (lateEnough && !enough)
{
v.push_back(new ManifestEntry(fn));
if (v.size() >= n && n != -1)
enough = true;
}
cont = dir.GetNext(&filename);
}
wxLogVerbose("ManifestManager::getManifestsAfter() returning %d manifests", v.size());
return v.size();
}
/** fills <b>v</b> with all manifests and returns how many were put into v */
size_t ManifestManager::GetAllManifests(std::vector<RAIN::ManifestEntry*>& v)
{
wxDir dir(this->getManifestDir());
wxString filename;
bool cont = dir.GetFirst(&filename, "*.rnm", wxDIR_FILES);
while (cont)
{
wxFileName fn(CredentialManager::GetCredentialFilename(RAIN::MANIFEST_DIR), filename);
v.push_back(new ManifestEntry(fn));
cont = dir.GetNext(&filename);
}
wxLogVerbose("ManifestManager::GetAllManifests() returning %d manifests", v.size());
return v.size();
}