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
|
#include <erebos/identity.h>
#include <erebos/network.h>
#include <erebos/storage.h>
#include <filesystem>
#include <functional>
#include <iostream>
#include <memory>
#include <optional>
#include <sstream>
#include <string>
#include <vector>
using std::cerr;
using std::cout;
using std::endl;
using std::function;
using std::optional;
using std::ostringstream;
using std::string;
using std::unique_ptr;
using std::vector;
namespace fs = std::filesystem;
using namespace erebos;
namespace {
fs::path getErebosDir()
{
const char * value = getenv("EREBOS_DIR");
if (value)
return value;
return "./.erebos";
}
Storage st(getErebosDir());
optional<Head<LocalState>> h;
optional<Server> server;
struct Command
{
string name;
function<void(const vector<string> &)> action;
};
void createIdentity(const vector<string> & args)
{
optional<Identity> identity;
for (const auto & name : args) {
auto builder = Identity::create(st);
builder.name(name);
if (identity)
builder.owner(*identity);
identity = builder.commit();
}
if (identity) {
auto nh = h->update([&identity] (const auto & loc) {
return st.store(loc->identity(*identity));
});
if (nh)
*h = *nh;
}
}
void startServer(const vector<string> &)
{
vector<unique_ptr<Service>> services;
server.emplace(*h, move(services));
server->peerList().onUpdate([](size_t idx, const Peer * peer) {
ostringstream ss;
ss << "peer " << idx;
if (peer) {
ss << " " << peer->name();
if (peer->identity() && peer->identity()->name())
ss << " " << *peer->identity()->name();
} else {
ss << " deleted";
}
cout << ss.str() << endl;
});
}
void stopServer(const vector<string> &)
{
server.reset();
}
vector<Command> commands = {
{ "create-identity", createIdentity },
{ "start-server", startServer },
{ "stop-server", stopServer },
};
}
int main(int argc, char * argv[])
{
h.emplace([] {
auto hs = st.heads<LocalState>();
if (!hs.empty())
return hs[0];
else
return st.storeHead(LocalState());
}());
char * line = nullptr;
size_t size = 0;
if (argc > 1) {
vector<string> args;
for (int i = 2; i < argc; i++)
args.emplace_back(argv[i]);
for (const auto & cmd : commands) {
if (cmd.name == argv[1]) {
cmd.action(args);
return 0;
}
}
cerr << "Unknown command: '" << argv[1] << "'" << endl;
return 1;
}
while (getline(&line, &size, stdin) > 0) {
optional<string> command;
vector<string> args;
const char * last = line;
for (const char * cur = line;; cur++) {
if (isspace(*cur) || *cur == '\0') {
if (last < cur) {
if (!command)
command.emplace(last, cur);
else
args.emplace_back(last, cur);
}
last = cur + 1;
if (*cur == '\0')
break;
}
}
if (!command)
continue;
bool found = false;
for (const auto & cmd : commands) {
if (cmd.name == *command) {
found = true;
cmd.action(args);
break;
}
}
if (!found)
cerr << "Unknown command: '" << *command << "'" << endl;
}
free(line);
return 0;
}
|