-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdhtdisplay.cpp
More file actions
74 lines (60 loc) · 2.2 KB
/
dhtdisplay.cpp
File metadata and controls
74 lines (60 loc) · 2.2 KB
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
#include "dhtdisplay.h"
bool display(const DHT& dht, sf::Image& image, sf::Sprite& sprite, sf::Texture& texture) {
ofstream dotFile("graph.dot");
dotFile << "graph G {\n";
for (shared_ptr<DHTConnection> connection : dht.connections) {
dotFile << " " << to_string(connection->a->id) << " -- " << to_string(connection->b->id) << ";\n";
}
vector<string> colors = { "purple", "green", "red", "blue" };
for (shared_ptr<DHTNode> node : dht.nodes) {
if (node->level == -1) {
dotFile << " " << to_string(node->id) << "[color = orange];\n";
}
else if (node->level <= 3) {
dotFile << " " << to_string(node->id) << "[color = " << colors[node->level] << "];\n";
}
}
dotFile << "}\n";
dotFile.close();
string command = "dot -Tpng graph.dot -o graph.png > NUL 2>&1";
int result = system(command.c_str());
if (result == 0) {
if (image.loadFromFile("graph.png")) {
texture.loadFromImage(image);
sprite = sf::Sprite(texture);
return true;
}
}
return false;
}
void dhtDisplay(const DHT& dht) {
int old_version = -1;
shared_ptr<sf::RenderWindow> window;
sf::Image image;
sf::Sprite sprite;
sf::Texture texture;
while (true) {
if (old_version != dht.version) {
if (display(dht, image, sprite, texture)) {
if (window) window->close();
window = make_shared<sf::RenderWindow>(sf::VideoMode(image.getSize().x, image.getSize().y), "DHT Graph");
window->draw(sprite);
window->display();
}
old_version = dht.version;
}
if (window) {
if (window->isOpen()) {
window->draw(sprite);
window->display();
sf::Event event;
while (window->pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window->close();
}
}
}
}
this_thread::sleep_for(chrono::milliseconds(500));
}
};