Skip to content

Commit 3cf4ae2

Browse files
committed
add pretty printing for pipe and endpoint names
1 parent 1b6f949 commit 3cf4ae2

File tree

13 files changed

+53
-38
lines changed

13 files changed

+53
-38
lines changed

daemon/daemon.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,6 @@ void HopperDaemon::process_events(struct epoll_event *events, int n_events) {
5959
int HopperDaemon::run() {
6060
int res = 0;
6161

62-
std::cout << "Started event loop, max " << m_max_events
63-
<< " events, timeout " << m_timeout << " ms" << std::endl;
64-
6562
while (res == 0) {
6663
struct epoll_event *events = new struct epoll_event[m_max_events];
6764

daemon/daemon_endpoint.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ uint32_t HopperDaemon::create_endpoint(const std::filesystem::path &path) {
1717
auto *endpoint = new HopperEndpoint(endpoint_id, inotify_watch_fd, path);
1818
m_endpoints[endpoint_id] = endpoint;
1919

20-
std::cout << "CREATE " << endpoint->path() << std::endl;
20+
std::cout << "CREATE " << *endpoint << std::endl;
2121

2222
return endpoint_id;
2323
}
@@ -26,7 +26,7 @@ void HopperDaemon::delete_endpoint(uint32_t id) {
2626
if (!m_endpoints.contains(id))
2727
return;
2828

29-
std::cout << "DELETE " << m_endpoints[id]->path() << std::endl;
29+
std::cout << "DELETE " << *(m_endpoints[id]) << std::endl;
3030

3131
delete m_endpoints[id];
3232
m_endpoints.erase(id);

daemon/daemon_inotify.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void HopperDaemon::handle_endpoint_inotify(struct inotify_event *ev,
6262
: endpoint->add_output_pipe(p));
6363

6464
if (pipe != nullptr)
65-
add_pipe(endpoint, pipe);
65+
add_pipe(pipe);
6666
}
6767

6868
if (ev->mask & IN_DELETE) {

daemon/daemon_pipe.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@ void HopperDaemon::remove_pipe(HopperEndpoint *endpoint, uint64_t pipe_id) {
1616
throw_errno("epoll_ctl DEL");
1717
pipe->close_pipe();
1818

19-
std::cout << "DOWN " << pipe->name() << "(" << endpoint->name()
20-
<< ")\n";
19+
std::cout << "DOWN " << *pipe << "\n";
2120
}
2221
}
2322

24-
void HopperDaemon::add_pipe(HopperEndpoint *endpoint, HopperPipe *pipe) {
23+
void HopperDaemon::add_pipe(HopperPipe *pipe) {
2524
if (pipe == nullptr)
2625
return;
2726

@@ -37,7 +36,7 @@ void HopperDaemon::add_pipe(HopperEndpoint *endpoint, HopperPipe *pipe) {
3736
if (epoll_ctl(m_epoll_fd, EPOLL_CTL_ADD, pipe->fd(), &ev) != 0)
3837
throw_errno("epoll_ctl ADD");
3938

40-
std::cout << "UP " << pipe->name() << "(" << endpoint->name() << ")\n";
39+
std::cout << "UP " << *pipe << "\n";
4140
}
4241

4342
void HopperDaemon::refresh_pipes() {
@@ -46,12 +45,12 @@ void HopperDaemon::refresh_pipes() {
4645
for (const auto &[id, pipe] : endpoint->inputs()) {
4746
if (pipe->status() == PipeStatus::ACTIVE || !pipe->open_pipe())
4847
continue;
49-
add_pipe(endpoint, pipe);
48+
add_pipe(pipe);
5049
}
5150
for (const auto &[id, pipe] : endpoint->outputs()) {
5251
if (pipe->status() == PipeStatus::ACTIVE || !pipe->open_pipe())
5352
continue;
54-
add_pipe(endpoint, pipe);
53+
add_pipe(pipe);
5554
}
5655

5756
// Try to empty buffers into pipes

daemon/endpoint.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,17 @@ void HopperEndpoint::on_pipe_readable(uint64_t id) {
3030
if (res == (size_t)-1)
3131
throw_errno("read");
3232

33-
std::cout << pipe->name() << "(" << m_name << ") -> " << res << " bytes\n";
33+
std::cout << *pipe << " -> " << res << " bytes\n";
3434
}
3535

3636
void HopperEndpoint::flush_pipes() {
3737
for (const auto &[_, pipe] : m_outputs) {
3838
if (pipe->status() == PipeStatus::INACTIVE)
3939
continue;
4040

41-
m_buffer.read(pipe);
41+
size_t res = m_buffer.read(pipe);
42+
if (res > 0)
43+
std::cout << *pipe << " <- " << res << " bytes\n";
4244
}
4345
}
4446

@@ -61,11 +63,11 @@ HopperPipe *HopperEndpoint::add_input_pipe(const std::filesystem::path &path) {
6163
if (id == 0) // ID 0 is never valid
6264
return nullptr;
6365

64-
std::cout << "OPEN IN " << path << "\n";
65-
66-
HopperPipe *p = new HopperPipe(id, PipeType::IN, path, nullptr);
66+
HopperPipe *p = new HopperPipe(id, m_name, PipeType::IN, path, nullptr);
6767
m_inputs[id] = p;
6868

69+
std::cout << "OPEN " << *p << "\n";
70+
6971
return p;
7072
}
7173

@@ -78,11 +80,11 @@ HopperPipe *HopperEndpoint::add_output_pipe(const std::filesystem::path &path) {
7880
if (id == 0)
7981
return nullptr;
8082

81-
std::cout << "OPEN OUT " << path << "\n";
82-
83-
HopperPipe *p = new HopperPipe(id, PipeType::OUT, path, marker);
83+
HopperPipe *p = new HopperPipe(id, m_name, PipeType::OUT, path, marker);
8484
m_outputs[id] = p;
8585

86+
std::cout << "OPEN " << *p << "\n";
87+
8688
return p;
8789
}
8890

@@ -92,15 +94,15 @@ void HopperEndpoint::remove_by_id(uint64_t pipe_id) {
9294
if (type == PipeType::IN && m_inputs.contains(pipe_id)) {
9395
HopperPipe *pipe = m_inputs[pipe_id];
9496
m_buffer.delete_marker(pipe->marker());
95-
std::cout << "CLOSE IN " << pipe->path() << "\n";
96-
97+
std::cout << "CLOSE " << *pipe << "\n";
98+
9799
delete pipe;
98100
m_inputs.erase(pipe_id);
99101
} else if (type == PipeType::OUT && m_outputs.contains(pipe_id)) {
100102
HopperPipe *pipe = m_outputs[pipe_id];
101103
m_buffer.delete_marker(pipe->marker());
102-
std::cout << "CLOSE OUT " << pipe->path() << "\n";
103-
104+
std::cout << "CLOSE " << *pipe << "\n";
105+
104106
delete pipe;
105107
m_outputs.erase(pipe_id);
106108
}

daemon/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ int main(int argc, char *argv[]) {
1010
std::cout << "Usage: hopperd <hopper path>" << std::endl;
1111
return 1;
1212
} else if (argc == 2) {
13-
std::cout << "Using Hopper at " << argv[1] << std::endl;
13+
std::cout << "HOPPER " << argv[1] << std::endl;
1414
auto daemon = hopper::HopperDaemon(argv[1]);
1515
return daemon.run();
1616
} else if (char *p = getenv("HOPPER_PATH")) {
17-
std::cout << "Using Hopper at " << p << std::endl;
17+
std::cout << "HOPPER " << p << std::endl;
1818
auto daemon = hopper::HopperDaemon(p);
1919
return daemon.run();
2020
} else {

daemon/pipe.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include <fcntl.h>
2-
#include <iostream>
32
#include <unistd.h>
43

54
#include "hopper/daemon/pipe.hpp"
@@ -9,8 +8,11 @@ namespace hopper {
98

109
/* HopperPipe */
1110

12-
HopperPipe::HopperPipe(uint64_t id, PipeType type, std::filesystem::path path, BufferMarker *marker)
13-
: m_marker(marker), m_type(type), m_path(path), m_id(id) {
11+
HopperPipe::HopperPipe(uint64_t id, const std::string &endpoint_name,
12+
PipeType type, std::filesystem::path path,
13+
BufferMarker *marker)
14+
: m_marker(marker), m_type(type), m_path(path),
15+
m_endpoint_name(endpoint_name), m_id(id) {
1416
m_name = path.replace_extension("").filename();
1517
open_pipe();
1618
}

daemon/util.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "hopper/daemon/util.hpp"
22

33
namespace hopper {
4-
4+
55
PipeType detect_pipe_type(const std::filesystem::path &path) {
66
if (!path.has_extension())
77
return PipeType::NONE;
@@ -15,4 +15,4 @@ PipeType detect_pipe_type(const std::filesystem::path &path) {
1515
return PipeType::NONE;
1616
}
1717

18-
};
18+
}; // namespace hopper

include/hopper/daemon/buffer.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class HopperBuffer {
2121
HopperBuffer(size_t len = 1024 * 1024)
2222
: m_buf(len) {} // Use 1 MiB size by default
2323

24-
BufferMarker* create_marker();
24+
BufferMarker *create_marker();
2525
void delete_marker(BufferMarker *marker);
2626

2727
size_t write(void *src, size_t len);

include/hopper/daemon/daemon.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ class HopperDaemon {
1919
private:
2020
std::unordered_map<uint32_t, HopperEndpoint *> m_endpoints;
2121

22-
2322
// endpoint IDs are 24 bit
2423
uint32_t m_last_endpoint_id = 1;
2524
uint32_t next_endpoint_id() {
@@ -46,12 +45,14 @@ class HopperDaemon {
4645
void setup_inotify();
4746
void handle_inotify();
4847
void handle_root_inotify(struct inotify_event *ev);
49-
void handle_endpoint_inotify(struct inotify_event *ev, HopperEndpoint *endpoint);
48+
void handle_endpoint_inotify(struct inotify_event *ev,
49+
HopperEndpoint *endpoint);
5050

5151
void process_events(struct epoll_event *events, int n_events);
5252
void remove_pipe(HopperEndpoint *endpoint, uint64_t pipe_id);
53-
void add_pipe(HopperEndpoint *endpoint, HopperPipe *pipe);
53+
void add_pipe(HopperPipe *pipe);
5454
void refresh_pipes();
55+
5556
public:
5657
HopperDaemon(std::filesystem::path path, int max_events = 64,
5758
int m_timeout = 250);

0 commit comments

Comments
 (0)