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
|
//
// ICMPEventArgs.cpp
//
// Library: Net
// Package: ICMP
// Module: ICMPEventArgs
//
// Implementation of ICMPEventArgs
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/Net/ICMPEventArgs.h"
#include "Poco/Net/SocketAddress.h"
#include "Poco/Net/DNS.h"
#include "Poco/Exception.h"
#include "Poco/Net/NetException.h"
#include <numeric>
using Poco::IOException;
using Poco::InvalidArgumentException;
namespace Poco {
namespace Net {
ICMPEventArgs::ICMPEventArgs(const SocketAddress& address, int repetitions, int dataSize, int ttl):
_address(address),
_sent(0),
_dataSize(dataSize),
_ttl(ttl),
_rtt(repetitions, 0),
_errors(repetitions)
{
}
ICMPEventArgs::~ICMPEventArgs()
{
}
std::string ICMPEventArgs::hostName() const
{
try
{
return DNS::resolve(_address.host().toString()).name();
}
catch (HostNotFoundException&)
{
}
catch (NoAddressFoundException&)
{
}
catch (DNSException&)
{
}
catch (IOException&)
{
}
return _address.host().toString();
}
std::string ICMPEventArgs::hostAddress() const
{
return _address.host().toString();
}
void ICMPEventArgs::setRepetitions(int repetitions)
{
_rtt.clear();
_rtt.resize(repetitions, 0);
_errors.assign(repetitions, "");
}
ICMPEventArgs& ICMPEventArgs::operator ++ ()
{
++_sent;
return *this;
}
ICMPEventArgs ICMPEventArgs::operator ++ (int)
{
ICMPEventArgs prev(*this);
operator ++ ();
return prev;
}
int ICMPEventArgs::received() const
{
int received = 0;
for (int i = 0; i < _rtt.size(); ++i)
{
if (_rtt[i]) ++received;
}
return received;
}
void ICMPEventArgs::setError(int index, const std::string& text)
{
if (index >= _errors.size())
throw InvalidArgumentException("Supplied index exceeds vector capacity.");
_errors[index] = text;
}
const std::string& ICMPEventArgs::error(int index) const
{
if (0 == _errors.size())
throw InvalidArgumentException("Supplied index exceeds vector capacity.");
if (-1 == index) index = _sent - 1;
return _errors[index];
}
void ICMPEventArgs::setReplyTime(int index, int time)
{
if (index >= _rtt.size())
throw InvalidArgumentException("Supplied index exceeds array capacity.");
if (0 == time) time = 1;
_rtt[index] = time;
}
int ICMPEventArgs::replyTime(int index) const
{
if (0 == _rtt.size())
throw InvalidArgumentException("Supplied index exceeds array capacity.");
if (-1 == index) index = _sent - 1;
return _rtt[index];
}
int ICMPEventArgs::avgRTT() const
{
if (0 == _rtt.size()) return 0;
return (int) (std::accumulate(_rtt.begin(), _rtt.end(), 0) / _rtt.size());
}
float ICMPEventArgs::percent() const
{
if (0 == _rtt.size()) return 0;
return ((float) received() / (float) _rtt.size()) * (float) 100.0;
}
} } // namespace Poco::Net
|