-
Notifications
You must be signed in to change notification settings - Fork 13
/
harness.cpp
83 lines (63 loc) · 2.64 KB
/
harness.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
#include <iostream>
#include <sstream>
#include <memory>
#include <hoytech/error.h>
#include <hoytech/hex.h>
#include "negentropy.h"
#include "negentropy/storage/BTreeMem.h"
#include "negentropy/storage/Vector.h"
std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> result;
std::stringstream ss (s);
std::string item;
while (getline (ss, item, delim)) {
result.push_back (item);
}
return result;
}
int main() {
uint64_t frameSizeLimit = 0;
if (::getenv("FRAMESIZELIMIT")) frameSizeLimit = std::stoull(::getenv("FRAMESIZELIMIT"));
negentropy::storage::Vector storage;
std::unique_ptr<Negentropy<negentropy::storage::Vector>> ne;
std::string line;
while (std::cin) {
std::getline(std::cin, line);
if (!line.size()) continue;
auto items = split(line, ',');
if (items[0] == "item") {
if (items.size() != 3) throw hoytech::error("wrong num of fields");
uint64_t created = std::stoull(items[1]);
auto id = hoytech::from_hex(items[2]);
storage.insert(created, id);
} else if (items[0] == "seal") {
storage.seal();
ne = std::make_unique<Negentropy<negentropy::storage::Vector>>(storage, frameSizeLimit);
} else if (items[0] == "initiate") {
auto q = ne->initiate();
if (frameSizeLimit && q.size() > frameSizeLimit) throw hoytech::error("initiate frameSizeLimit exceeded: ", q.size(), " > ", frameSizeLimit);
std::cout << "msg," << hoytech::to_hex(q) << std::endl;
} else if (items[0] == "msg") {
std::string q;
if (items.size() >= 2) q = hoytech::from_hex(items[1]);
if (ne->isInitiator) {
std::vector<std::string> have, need;
auto resp = ne->reconcile(q, have, need);
for (auto &id : have) std::cout << "have," << hoytech::to_hex(id) << "\n";
for (auto &id : need) std::cout << "need," << hoytech::to_hex(id) << "\n";
if (!resp) {
std::cout << "done" << std::endl;
continue;
}
q = *resp;
} else {
q = ne->reconcile(q);
}
if (frameSizeLimit && q.size() > frameSizeLimit) throw hoytech::error("frameSizeLimit exceeded: ", q.size(), " > ", frameSizeLimit, ": from ", (ne->isInitiator ? "initiator" : "non-initiator"));
std::cout << "msg," << hoytech::to_hex(q) << std::endl;
} else {
throw hoytech::error("unknown cmd: ", items[0]);
}
}
return 0;
}