X-Git-Url: https://git.sesse.net/?p=cubemap;a=blobdiff_plain;f=httpinput.cpp;h=688f321744ff5084d9f03da33e0e76ebae5eb76c;hp=3bee5d675fde7385fc61307ac1ad14d8648f9edc;hb=bd694fdd3dd1417399aecead2c8b91fc4fe95ce8;hpb=4a33511c426ae90abb261f09fda1e31e0c30ca16 diff --git a/httpinput.cpp b/httpinput.cpp index 3bee5d6..688f321 100644 --- a/httpinput.cpp +++ b/httpinput.cpp @@ -18,6 +18,7 @@ #include "httpinput.h" #include "log.h" #include "metacube.h" +#include "mutexlock.h" #include "parse.h" #include "serverpool.h" #include "state.pb.h" @@ -34,6 +35,10 @@ HTTPInput::HTTPInput(const string &url) has_metacube_header(false), sock(-1) { + pthread_mutex_init(&stats_mutex, NULL); + stats.url = url; + stats.bytes_received = 0; + stats.data_bytes_received = 0; } HTTPInput::HTTPInput(const InputProto &serialized) @@ -46,6 +51,8 @@ HTTPInput::HTTPInput(const InputProto &serialized) has_metacube_header(serialized.has_metacube_header()), sock(serialized.sock()) { + pthread_mutex_init(&stats_mutex, NULL); + pending_data.resize(serialized.pending_data().size()); memcpy(&pending_data[0], serialized.pending_data().data(), serialized.pending_data().size()); @@ -58,6 +65,11 @@ HTTPInput::HTTPInput(const InputProto &serialized) memcmp(http_header.data() + http_header.size() - 4, "\r\n\r\n", 4) == 0) { http_header.resize(http_header.size() - 2); } + + pthread_mutex_init(&stats_mutex, NULL); + stats.url = url; + stats.bytes_received = serialized.bytes_received(); + stats.data_bytes_received = serialized.data_bytes_received(); } void HTTPInput::close_socket() @@ -79,6 +91,8 @@ InputProto HTTPInput::serialize() const serialized.set_pending_data(string(pending_data.begin(), pending_data.end())); serialized.set_has_metacube_header(has_metacube_header); serialized.set_sock(sock); + serialized.set_bytes_received(stats.bytes_received); + serialized.set_data_bytes_received(stats.data_bytes_received); return serialized; } @@ -420,6 +434,10 @@ void HTTPInput::do_work() void HTTPInput::process_data(char *ptr, size_t bytes) { pending_data.insert(pending_data.end(), ptr, ptr + bytes); + { + MutexLock mutex(&stats_mutex); + stats.bytes_received += bytes; + } for ( ;; ) { // If we don't have enough data (yet) for even the Metacube header, just return. @@ -468,7 +486,11 @@ void HTTPInput::process_data(char *ptr, size_t bytes) return; } - // Send this block on to the data. + // Send this block on to the servers. + { + MutexLock lock(&stats_mutex); + stats.data_bytes_received += size; + } char *inner_data = pending_data.data() + sizeof(metacube_block_header); if (flags & METACUBE_FLAGS_HEADER) { string header(inner_data, inner_data + size); @@ -506,3 +528,8 @@ void HTTPInput::drop_pending_data(size_t num_bytes) pending_data.erase(pending_data.begin(), pending_data.begin() + num_bytes); } +InputStats HTTPInput::get_stats() const +{ + MutexLock lock(&stats_mutex); + return stats; +}