-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathping.cpp
276 lines (242 loc) · 8.08 KB
/
ping.cpp
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/***************************************************************************
The Base Framework
A framework for developing platform independent applications
See COPYRIGHT.txt for details.
This framework is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
For the licensing terms refer to the file 'LICENSE'.
***************************************************************************/
#include <base/Application.h>
#include <base/Timer.h>
#include <base/UnsignedInteger.h>
#include <base/math/Math.h>
#include <base/net/StreamSocket.h>
#include <base/net/InetInterface.h>
#include <base/net/InetService.h>
#include <base/net/InetEndPoint.h>
#include <base/concurrency/Thread.h>
#include <base/string/FormatOutputStream.h>
#include <base/string/StringOutputStream.h>
using namespace com::azure::dev::base;
class PingApplication : public Application {
private:
static const unsigned int MAJOR_VERSION = 1;
static const unsigned int MINOR_VERSION = 0;
/** The echo service port. */
static const unsigned short ECHO_SERVICE_PORT = 7;
/** The server port. */
unsigned short port = 0;
/** The bytes to send. */
unsigned int dataSize = 0;
unsigned int timeout = 0;
unsigned int packetsToTransmit = 0;
enum Command {
COMMAND_HELP,
COMMAND_VERSION,
COMMAND_PING
};
public:
PingApplication()
: Application("ping")
{
port = ECHO_SERVICE_PORT;
dataSize = 32;
timeout = 1000000;
packetsToTransmit = 0;
}
void onTermination() noexcept {
}
String getTimeAsString2(uint64 microseconds) noexcept {
return Literal("");
}
String getTimeAsString(uint64 microseconds) noexcept {
StringOutputStream stream;
if (microseconds < 1000) {
stream << microseconds << "us";
} else if (microseconds < 1000000) {
stream << FIXED
<< setPrecision(3) << microseconds/1000.0 << "ms";
} else {
stream << FIXED
<< setPrecision(3) << microseconds/1000000.0 << "s";
}
stream << FLUSH;
return stream.getString();
}
void ping(const String& host) noexcept {
bool byName = true;
InetAddress address;
// if (InetAddress.isIPAddress(host)) {
// address = InetAddress(host);
// byName = false;
// }
try {
address = InetAddress::getAddressByName(host); // the address of the remote host
} catch (HostNotFound&) {
ferr << "Error: " << "Unable to resolve host." << ENDL;
setExitCode(EXIT_CODE_ERROR);
return;
}
String name = host;
try {
name = address.getHostName(true);
} catch (...) {
}
if (byName) {
fout << "Pinging " << name << ' ' << '(' << address << ')' << " with "
<< dataSize << " bytes of data" << EOL
<< ENDL;
} else {
fout << "Pinging " << ' ' << '(' << address << ')' << " with "
<< dataSize << " bytes of data" << EOL
<< ENDL;
}
InetEndPoint endPoint(address, port);
StreamSocket socket;
try {
socket.connect(endPoint.getAddress(), endPoint.getPort());
} catch (IOException&) {
ferr << "Error: " << "Unable to connect." << ENDL;
setExitCode(EXIT_CODE_ERROR);
return;
}
socket.setReceiveBufferSize(dataSize);
socket.setSendBufferSize(dataSize);
socket.setTcpNoDelay(true);
Timer timeoutTimer;
Timer timer;
unsigned int packetsTransmitted = 0;
unsigned int packetsReceived = 0;
uint64 minimumTime = PrimitiveTraits<uint64>::MAXIMUM;
uint64 maximumTime = 0;
uint64 totalTime = 0;
Allocator<uint8> outgoing(dataSize);
Allocator<uint8> incoming(dataSize);
fill<uint8>(outgoing.getElements(), outgoing.getSize(), 0);
while (!isTerminated() &&
((packetsToTransmit == 0) ||
(packetsTransmitted < packetsToTransmit))) {
uint8* dest = outgoing.getElements();
*dest++ = packetsTransmitted;
timeoutTimer.start();
timer.start();
socket.write(outgoing.getElements(), outgoing.getSize());
++packetsTransmitted;
socket.wait(timeout); // minimum(250, timeout)
// TAG: use timer to check for timeout
unsigned int bytesAvailable = socket.available();
if (bytesAvailable < incoming.getSize()) {
fout << name << ' ' << '(' << address << ')'
<< ':' << " request timed out" << ENDL;
} else {
timer.stop();
minimumTime = minimum(minimumTime, timer.getMicroseconds());
maximumTime = maximum(maximumTime, timer.getMicroseconds());
totalTime += timer.getMicroseconds();
while (bytesAvailable >= incoming.getSize()) {
socket.read(incoming.getElements(), incoming.getSize());
if (compare(incoming.getElements(), outgoing.getElements(), dataSize) == 0) {
++packetsReceived;
fout << incoming.getSize()
<< " bytes from " << name << ' '
<< '(' << address << ')' << ':' << ' '
<< "n=" << packetsReceived << ' '
<< "time=" << getTimeAsString(timer.getMicroseconds())
<< ENDL;
break;
}
}
}
timeoutTimer.stop();
if (timeoutTimer.getMicroseconds() < timeout) {
Thread::microsleep(timeout - timeoutTimer.getMicroseconds());
}
}
socket.close();
unsigned int packetsLost = packetsTransmitted - packetsReceived;
double meanTime = (packetsReceived > 0) ? totalTime/packetsReceived : 0;
fout << EOL
<< "--- statistics for " << name << " ---" << EOL
<< "Packets transmitted: " << packetsTransmitted << EOL
<< "Packets received: " << packetsReceived << EOL
<< "Packets lost: " << packetsLost << ' '
<< '(' << static_cast<int>((packetsTransmitted > 0) ? (100*packetsLost/packetsTransmitted) : 0) << '%' << ')' << EOL
<< "Time minimum/maximum/mean: "
<< getTimeAsString(minimumTime) << '/'
<< getTimeAsString(maximumTime) << '/'
<< getTimeAsString(static_cast<uint64>(meanTime)) << EOL
<< ENDL;
}
void version() noexcept
{
fout << getFormalName() << " version "
<< MAJOR_VERSION << '.' << MINOR_VERSION << EOL
<< "The Base Framework (Test Suite)" << EOL
<< ENDL;
}
void help() noexcept
{
version();
fout << getFormalName()
<< " [--help] [--port PORT] [--data SIZE] [--time MS] host" << ENDL;
}
void main()
{
Command command = COMMAND_PING;
String host;
const Array<String> arguments = getArguments();
if (arguments.getSize() > 0) {
Array<String>::ReadEnumerator enu = arguments.getReadEnumerator();
while (enu.hasNext()) {
String argument = enu.next();
if (argument == "--help") {
command = COMMAND_HELP;
break;
} else if (argument == "--version") {
command = COMMAND_VERSION;
break;
} else if (argument == "--port") {
String temp = enu.next();
try {
UnsignedInteger value(temp);
if (value > 0xffff) {
ferr << "Error: " << "Invalid port" << ENDL;
setExitCode(EXIT_CODE_ERROR);
return;
}
port = value;
} catch (InvalidFormat&) {
try {
InetService service(temp);
port = service.getPort();
} catch (ServiceNotFound& e) {
ferr << "Error: " << e.getMessage() << ENDL;
setExitCode(EXIT_CODE_ERROR);
return;
}
}
} else {
host = argument;
}
}
}
if (host.isEmpty()) {
ferr << "Error: " << "Host not specified" << ENDL;
setExitCode(EXIT_CODE_ERROR);
return;
}
switch (command) {
case COMMAND_HELP:
help();
break;
case COMMAND_VERSION:
version();
break;
case COMMAND_PING:
ping(host);
break;
}
}
};
APPLICATION_STUB(PingApplication);