|
2 | 2 |
|
3 | 3 | #include <memory>
|
4 | 4 | #include <string>
|
| 5 | +#include <vector> |
5 | 6 |
|
6 | 7 | #include "absl/base/thread_annotations.h"
|
7 | 8 | #include "glog/logging.h"
|
8 | 9 | #include "absl/status/status.h"
|
| 10 | +#include "absl/strings/match.h" |
| 11 | +#include "absl/strings/numbers.h" |
9 | 12 | #include "absl/strings/str_cat.h"
|
| 13 | +#include "absl/strings/string_view.h" |
| 14 | +#include "absl/strings/strip.h" |
10 | 15 | #include "absl/synchronization/mutex.h"
|
11 | 16 | #include "absl/time/clock.h"
|
12 | 17 | #include "absl/time/time.h"
|
13 | 18 | #include "proto/gnpsi/gnpsi.pb.h"
|
14 | 19 |
|
15 | 20 | namespace gnpsi {
|
16 | 21 |
|
| 22 | +absl::Status GnpsiConnection::InitializeStats() { |
| 23 | + std::string uri = this->GetPeerName(); |
| 24 | + LOG(INFO) << "uri: " << uri; |
| 25 | + absl::string_view ip; |
| 26 | + int port; |
| 27 | + if (absl::StartsWith(uri, kIpv4Indicator)) { |
| 28 | + absl::string_view path = |
| 29 | + absl::StripPrefix(uri, absl::StrCat(kIpv4Indicator, ":")); |
| 30 | + if (int colon = path.find(':'); colon != absl::string_view::npos) { |
| 31 | + ip = path.substr(0, colon); |
| 32 | + if (!absl::SimpleAtoi(path.substr(colon + 1), &port)) { |
| 33 | + return absl::InternalError( |
| 34 | + "Error retrieving port information from uri string"); |
| 35 | + } |
| 36 | + this->stats_ = GnpsiStats(ip, port); |
| 37 | + return absl::OkStatus(); |
| 38 | + } |
| 39 | + return absl::NotFoundError("Port not found in uri string"); |
| 40 | + } |
| 41 | + if (absl::StartsWith(uri, kIpv6Indicator)) { |
| 42 | + absl::string_view path = |
| 43 | + absl::StripPrefix(uri, absl::StrCat(kIpv6Indicator, ":%5B")); |
| 44 | + if (int closing_bracket = path.find("%5D"); |
| 45 | + closing_bracket != absl::string_view::npos) { |
| 46 | + ip = path.substr(0, closing_bracket); |
| 47 | + if (!absl::SimpleAtoi(path.substr(closing_bracket + 4), &port)) { |
| 48 | + return absl::InternalError( |
| 49 | + "Error retrieving port information from uri string"); |
| 50 | + } |
| 51 | + this->stats_ = GnpsiStats(ip, port); |
| 52 | + return absl::OkStatus(); |
| 53 | + } |
| 54 | + return absl::NotFoundError("Port not found in uri string"); |
| 55 | + } |
| 56 | + return absl::InvalidArgumentError("The passed URI format is not supported"); |
| 57 | +} |
| 58 | + |
17 | 59 | void GnpsiConnection::WaitUntilClosed() {
|
18 | 60 | absl::MutexLock l(&mu_);
|
19 | 61 | auto stream_disconnected = [this]() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu_) {
|
@@ -71,6 +113,10 @@ absl::Status GnpsiServiceImpl::AddConnection(GnpsiConnection* connection) {
|
71 | 113 | client_max_number_, " clients."));
|
72 | 114 | }
|
73 | 115 | LOG(INFO) << "Add " << connection->GetPeerName() << " to client list.";
|
| 116 | + if (absl::Status status = connection->InitializeStats(); !status.ok()) { |
| 117 | + LOG(ERROR) << "Error while creating stats object for peer - " |
| 118 | + << status.message(); |
| 119 | + } |
74 | 120 | gnpsi_connections_.push_back(connection);
|
75 | 121 | return absl::OkStatus();
|
76 | 122 | }
|
@@ -113,13 +159,25 @@ void GnpsiServiceImpl::SendSamplePacket(
|
113 | 159 | connection->SendResponse(response)) {
|
114 | 160 | VLOG(1) << "Successfully sent sample packet to "
|
115 | 161 | << connection->GetPeerName() << ".";
|
| 162 | + connection->IncrementDatagramCount(); |
| 163 | + connection->IncrementBytesSampled(sample_packet.size()); |
116 | 164 | } else {
|
117 | 165 | // If it fails to send response to any client, close this connection.
|
118 | 166 | LOG(ERROR) << "Failed to send sample packet to "
|
119 | 167 | << connection->GetPeerName() << ".";
|
| 168 | + connection->IncrementWriteErrorCount(); |
120 | 169 | connection->CloseStream();
|
121 | 170 | }
|
122 | 171 | }
|
123 | 172 | }
|
124 | 173 |
|
| 174 | +std::vector<GnpsiStats> GnpsiServiceImpl::GetStats() { |
| 175 | + absl::MutexLock l(&mu_); |
| 176 | + std::vector<GnpsiStats> stats; |
| 177 | + for (auto it = gnpsi_connections_.begin(), end = gnpsi_connections_.end(); |
| 178 | + it != end; it++) { |
| 179 | + stats.push_back((*it)->GetConnectionStats()); |
| 180 | + } |
| 181 | + return stats; |
| 182 | +} |
125 | 183 | } // namespace gnpsi
|
0 commit comments