-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
257 lines (216 loc) · 9.86 KB
/
main.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
#include "include/manager.hpp"
#include <random>
#define USERNAME "user1"
#define URL "ws://localhost:8080/"
#define CONNECTION_URL "ws://localhost:8080/"
using namespace std;
vector<string> split(const string &s, char delim) {
vector<string> parts;
stringstream ss(s);
string part;
while (getline(ss, part, delim)) {
parts.push_back(part);
}
return parts;
}
void run_interactive(Manager &manager, const string &username) {
cout << "\nInteractive shell for managing your node \n";
cout << "run \"help\" for a list of possible commands \n";
string input;
vector<string> arguments;
while (true) {
cout << "(" << username << ") >> ";
getline(cin, input);
arguments = split(input, ' ');
if (arguments.size() == 0)
continue;
string command = arguments[0];
if (command == "help") {
if (arguments.size() != 1) {
cout << "Command \"help\" accepts no arguments \n";
cout << "For help on a specific command run \"COMMAND help\" \n";
continue;
}
cout << "Commands: \n";
cout << " blocks Manage blocks \n";
cout << " connections Manages connections \n";
cout << " keys Manager RSA keys \n";
cout << " send Send message to blocks \n";
cout << " help Shows this help message \n";
cout << " exit Quits program and closes connections \n";
cout << "For help on a specific command run \"COMMAND help\" \n";
} else if (command == "blocks") {
if (arguments.size() == 1) {
cout << "Command \"blocks\" requires an argument \n";
cout << "For help on usage run \"blocks help\" \n";
continue;
}
string sub_command = arguments[1];
if (sub_command == "help") {
if (arguments.size() != 2) {
cout << "Command \"blocks help\" accepts no arguments \n";
continue;
}
cout << "Commands: \n";
cout << " list Lists all blocks \n";
cout << " new Creates a new block \n";
cout << " remove Removes a block \n";
cout << " set Sets information in a block \n";
cout << " help Shows this help message \n";
} else if (sub_command == "list") {
if (arguments.size() != 2) {
cout << "Command \"blocks list\" accepts no arguments \n";
continue;
}
manager.print_blocks();
} else if (sub_command == "new") {
if (arguments.size() != 5) {
cout << "Command \"blocks new\" requires 3 arguments \n";
cout << "Usage: blocks new USERNAME URL DESCRIPTION \n";
continue;
}
manager.make_new_block(arguments[2], arguments[3], arguments[4]);
cout << "Successfully created block \n";
} else if (sub_command == "remove") {
if (arguments.size() != 3) {
cout << "Command \"blocks remove\" requires an argument \n";
cout << "Usage: blocks remove USERNAME \n";
continue;
}
string username = arguments[2];
bool result = manager.delete_block_by_username(username);
if (result) {
cout << "Successfully removed block with username " << username << '\n';
} else {
cout << "Block with username \"" << username << "\" not found \n";
}
} else if (sub_command == "set") {
if (arguments.size() != 5) {
cout << "Command \"blocks set\" requires 3 arguments \n";
cout << "Usage: blocks set USERNAME {username, url, location, description} NEW_DATA \n";
continue;
}
auto *block = manager.get_block_by_username(arguments[2]);
if (block == nullptr) {
cout << "Block with username \"" + arguments[2] + "\" not found \n";
} else {
if (arguments[3] == "username") {
block->set_username(arguments[4]);
cout << "Successfully updated username \n";
} else if (arguments[3] == "url") {
block->set_url(arguments[4]);
cout << "Successfully updated url \n";
} else if (arguments[3] == "location") {
block->set_location(arguments[4]);
cout << "Successfully updated location \n";
} else if (arguments[3] == "description") {
block->set_description(arguments[4]);
cout << "Successfully updated description \n";
} else {
cout << "Invalid option \"" + arguments[3] + "\" \n";
cout << "Usage: blocks set {username, url, location, description} NEW_DATA \n";
}
}
} else {
cout << "Invalid command \"blocks" + sub_command + "\" \n";
cout << "For information on commands, run \"blocks help\" \n";
}
} else if (command == "connections") {
if (arguments.size() == 1) {
cout << "Command \"connections\" requires an argument \n";
cout << "For help on usage run \"connections help\" \n";
continue;
}
string sub_command = arguments[1];
if (sub_command == "help") {
if (arguments.size() != 2) {
cout << "Command \"connections help\" accepts no arguments \n";
continue;
}
cout << "Commands: \n";
cout << " list Lists all connections \n";
cout << " open Creates a new connection \n";
cout << " close Ends a connection \n";
cout << " help Shows this help message \n";
} else if (sub_command == "list") {
if (arguments.size() != 2) {
cout << "Command \"connections list\" accepts no arguments \n";
continue;
}
manager.list_connections();
} else if (sub_command == "open") {
if (arguments.size() != 3) {
cout << "Command \"connections open\" requires an argument \n";
cout << "Usage: \"connections open URL\" \n";
continue;
}
manager.open_connection(arguments[2]);
cout << "Connecting to " << arguments[2] << '\n';
} else if (sub_command == "close") {
if (arguments.size() != 3) {
cout << "Command \"connections close\" requires an argument \n";
cout << "Usage: \"connections close URL\" \n";
continue;
}
bool result = manager.close_connection(arguments[2]);
if (result) {
cout << "Successfully closed connection with " << arguments[2] << '\n';
} else {
cout << "Failed to close connection with " << arguments[2] << '\n';
}
} else {
cout << "Invalid command \"connections " + sub_command + "\" \n";
cout << "For information on commands, run \"blocks help\" \n";
}
} else if (command == "keys") {
} else if (command == "send") {
} else if (command == "exit") {
if (arguments.size() != 1) {
cout << "Command \"exit\" accepts no arguments \n";
continue;
}
exit(0);
} else {
cout << "Invalid command \"" + command + "\" \n";
cout << "For information on commands, run \"help\" \n";
}
}
}
int main(int argc, char **argv) {
string username = USERNAME;
string url = URL;
string connection_url = CONNECTION_URL;
if (argc >= 4) {
username = argv[1];
url = argv[2];
connection_url = argv[3];
}
DEBUG_PRINT("Starting block chain ( Username - " + username + ", Url - " + url + ", Connection url - " + connection_url + " )");
RSA *keypair = Crypto::generate_rsa_keys();
Crypto::export_private_key(keypair, username + "_private");
Crypto::export_public_key(keypair, username + "_public");
Crypto::free(keypair);
Json::Value random_foods = Utils::load_json("random-foods");
Json::Value random_addresses = Utils::load_json("random-addresses");
random_device dev;
mt19937 rng(dev());
uniform_int_distribution<mt19937::result_type> food_dist(0, random_foods.size());
uniform_int_distribution<mt19937::result_type> address_dist(0, random_addresses.size());
Json::Value food = random_foods[static_cast<int>(food_dist(rng))];
Block block(username, url, food.asString());
Manager manager(&block);
manager.run();
if (url != connection_url)
manager.open_connection(connection_url);
if (username == "user1") {
run_interactive(manager, username);
} else {
while (true) {
// sleep for a random time up to 30 seconds
this_thread::sleep_for(chrono::milliseconds(rng() % 30'000));
Json::Value location = random_addresses[static_cast<int>(address_dist(rng))];
manager.update_location(location["address"].asString());
}
}
return 0;
}