1//
2// ICMPEventArgs.cpp
3//
4// Library: Net
5// Package: ICMP
6// Module: ICMPEventArgs
7//
8// Implementation of ICMPEventArgs
9//
10// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
11// and Contributors.
12//
13// SPDX-License-Identifier: BSL-1.0
14//
15
16
17#include "Poco/Net/ICMPEventArgs.h"
18#include "Poco/Net/SocketAddress.h"
19#include "Poco/Net/DNS.h"
20#include "Poco/Exception.h"
21#include "Poco/Net/NetException.h"
22#include <numeric>
23
24
25using Poco::IOException;
26using Poco::InvalidArgumentException;
27
28
29namespace Poco {
30namespace Net {
31
32
33ICMPEventArgs::ICMPEventArgs(const SocketAddress& address, int repetitions, int dataSize, int ttl):
34 _address(address),
35 _sent(0),
36 _dataSize(dataSize),
37 _ttl(ttl),
38 _rtt(repetitions, 0),
39 _errors(repetitions)
40{
41}
42
43
44ICMPEventArgs::~ICMPEventArgs()
45{
46}
47
48
49std::string ICMPEventArgs::hostName() const
50{
51 try
52 {
53 return DNS::resolve(_address.host().toString()).name();
54 }
55 catch (HostNotFoundException&)
56 {
57 }
58 catch (NoAddressFoundException&)
59 {
60 }
61 catch (DNSException&)
62 {
63 }
64 catch (IOException&)
65 {
66 }
67 return _address.host().toString();
68}
69
70
71std::string ICMPEventArgs::hostAddress() const
72{
73 return _address.host().toString();
74}
75
76
77void ICMPEventArgs::setRepetitions(int repetitions)
78{
79 _rtt.clear();
80 _rtt.resize(repetitions, 0);
81 _errors.assign(repetitions, "");
82}
83
84
85ICMPEventArgs& ICMPEventArgs::operator ++ ()
86{
87 ++_sent;
88 return *this;
89}
90
91
92ICMPEventArgs ICMPEventArgs::operator ++ (int)
93{
94 ICMPEventArgs prev(*this);
95 operator ++ ();
96 return prev;
97}
98
99
100int ICMPEventArgs::received() const
101{
102 int received = 0;
103
104 for (int i = 0; i < _rtt.size(); ++i)
105 {
106 if (_rtt[i]) ++received;
107 }
108 return received;
109}
110
111
112void ICMPEventArgs::setError(int index, const std::string& text)
113{
114 if (index >= _errors.size())
115 throw InvalidArgumentException("Supplied index exceeds vector capacity.");
116
117 _errors[index] = text;
118}
119
120
121const std::string& ICMPEventArgs::error(int index) const
122{
123 if (0 == _errors.size())
124 throw InvalidArgumentException("Supplied index exceeds vector capacity.");
125
126 if (-1 == index) index = _sent - 1;
127
128 return _errors[index];
129}
130
131
132void ICMPEventArgs::setReplyTime(int index, int time)
133{
134 if (index >= _rtt.size())
135 throw InvalidArgumentException("Supplied index exceeds array capacity.");
136 if (0 == time) time = 1;
137 _rtt[index] = time;
138}
139
140
141int ICMPEventArgs::replyTime(int index) const
142{
143 if (0 == _rtt.size())
144 throw InvalidArgumentException("Supplied index exceeds array capacity.");
145
146 if (-1 == index) index = _sent - 1;
147
148 return _rtt[index];
149}
150
151
152int ICMPEventArgs::avgRTT() const
153{
154 if (0 == _rtt.size()) return 0;
155
156 return (int) (std::accumulate(_rtt.begin(), _rtt.end(), 0) / _rtt.size());
157}
158
159
160float ICMPEventArgs::percent() const
161{
162 if (0 == _rtt.size()) return 0;
163
164 return ((float) received() / (float) _rtt.size()) * (float) 100.0;
165}
166
167
168} } // namespace Poco::Net
169