4
// $Id: //poco/1.2/Net/samples/Ping/src/Ping.cpp#1 $
6
// This sample demonstrates the Application class.
8
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
11
// Permission is hereby granted, free of charge, to any person or organization
12
// obtaining a copy of the software and accompanying documentation covered by
13
// this license (the "Software") to use, reproduce, display, distribute,
14
// execute, and transmit the Software, and to prepare derivative works of the
15
// Software, and to permit third-parties to whom the Software is furnished to
16
// do so, all subject to the following:
18
// The copyright notices in the Software and this entire statement, including
19
// the above license grant, this restriction and the following disclaimer,
20
// must be included in all copies of the Software, in whole or in part, and
21
// all derivative works of the Software, unless such copies or derivative
22
// works are solely in the form of machine-executable object code generated by
23
// a source language processor.
25
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
28
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
29
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
30
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
31
// DEALINGS IN THE SOFTWARE.
35
#include "Poco/Util/Application.h"
36
#include "Poco/Util/Option.h"
37
#include "Poco/Util/OptionSet.h"
38
#include "Poco/Util/HelpFormatter.h"
39
#include "Poco/Util/AbstractConfiguration.h"
40
#include "Poco/Net/ICMPSocket.h"
41
#include "Poco/Net/ICMPClient.h"
42
#include "Poco/Net/IPAddress.h"
43
#include "Poco/Net/ICMPEventArgs.h"
44
#include "Poco/AutoPtr.h"
45
#include "Poco/NumberParser.h"
46
#include "Poco/Delegate.h"
51
using Poco::Util::Application;
52
using Poco::Util::Option;
53
using Poco::Util::OptionSet;
54
using Poco::Util::HelpFormatter;
55
using Poco::Util::AbstractConfiguration;
56
using Poco::Net::ICMPSocket;
57
using Poco::Net::ICMPClient;
58
using Poco::Net::IPAddress;
59
using Poco::Net::ICMPEventArgs;
61
using Poco::NumberParser;
65
class Ping: public Application
66
/// This sample demonstrates the Poco::Net::ICMPClient in conjunction with
67
/// Poco Foundation C#-like events functionality.
69
/// Try Ping --help (on Unix platforms) or Ping /help (elsewhere) for
74
_helpRequested(false),
75
_icmpClient(IPAddress::IPv4),
82
void initialize(Application& self)
84
loadConfiguration(); // load default configuration files, if present
85
Application::initialize(self);
87
_icmpClient.pingBegin += Delegate<Ping, ICMPEventArgs>(this, &Ping::onBegin);
88
_icmpClient.pingReply += Delegate<Ping, ICMPEventArgs>(this, &Ping::onReply);
89
_icmpClient.pingError += Delegate<Ping, ICMPEventArgs>(this, &Ping::onError);
90
_icmpClient.pingEnd += Delegate<Ping, ICMPEventArgs>(this, &Ping::onEnd);
95
_icmpClient.pingBegin -= Delegate<Ping, ICMPEventArgs>(this, &Ping::onBegin);
96
_icmpClient.pingReply -= Delegate<Ping, ICMPEventArgs>(this, &Ping::onReply);
97
_icmpClient.pingError -= Delegate<Ping, ICMPEventArgs>(this, &Ping::onError);
98
_icmpClient.pingEnd -= Delegate<Ping, ICMPEventArgs>(this, &Ping::onEnd);
100
Application::uninitialize();
103
void defineOptions(OptionSet& options)
105
Application::defineOptions(options);
108
Option("help", "h", "display help information on command line arguments")
113
Option("repetitions", "r", "define the number of repetitions")
116
.argument("repetitions"));
119
Option("target", "t", "define the target address")
122
.argument("target"));
125
void handleOption(const std::string& name, const std::string& value)
127
Application::handleOption(name, value);
130
_helpRequested = true;
131
else if (name == "repetitions")
132
_repetitions = NumberParser::parse(value);
133
else if (name == "target")
139
HelpFormatter helpFormatter(options());
140
helpFormatter.setCommand(commandName());
141
helpFormatter.setUsage("OPTIONS");
142
helpFormatter.setHeader(
143
"A sample application that demonstrates the functionality of the "
144
"Poco::Net::ICMPClient class in conjunction with Poco::Events package functionality.");
145
helpFormatter.format(std::cout);
149
int main(const std::vector<std::string>& args)
154
_icmpClient.ping(_target, _repetitions);
156
return Application::EXIT_OK;
160
void onBegin(const void* pSender, ICMPEventArgs& args)
162
std::ostringstream os;
163
os << "Pinging " << args.hostName() << " [" << args.hostAddress() << "] with " << args.dataSize() << " bytes of data:"
164
<< std::endl << "---------------------------------------------" << std::endl;
165
logger().information(os.str());
168
void onReply(const void* pSender, ICMPEventArgs& args)
170
std::ostringstream os;
171
os << "Reply from " << args.hostAddress()
172
<< " bytes=" << args.dataSize()
173
<< " time=" << args.replyTime() << "ms"
174
<< " TTL=" << args.ttl();
175
logger().information(os.str());
178
void onError(const void* pSender, ICMPEventArgs& args)
180
std::ostringstream os;
182
logger().information(os.str());
185
void onEnd(const void* pSender, ICMPEventArgs& args)
187
std::ostringstream os;
188
os << std::endl << "--- Ping statistics for " << args.hostName() << " ---"
189
<< std::endl << "Packets: Sent=" << args.sent() << ", Received=" << args.received()
190
<< " Lost=" << args.repetitions() - args.received() << " (" << 100.0 - args.percent() << "% loss),"
191
<< std::endl << "Approximate round trip times in milliseconds: " << std::endl
192
<< "Minimum=" << args.minRTT() << "ms, Maximum=" << args.maxRTT()
193
<< "ms, Average=" << args.avgRTT() << "ms"
194
<< std::endl << "------------------------------------------";
195
logger().information(os.str());
200
ICMPClient _icmpClient;
206
int main(int argc, char** argv)
208
AutoPtr<Ping> pApp = new Ping;
211
pApp->init(argc, argv);
213
catch (Poco::Exception& exc)
215
pApp->logger().log(exc);
216
return Application::EXIT_CONFIG;