Skip to content
This repository
Newer
Older
100644 199 lines (171 sloc) 5.288 kb
5a639074 » aleks-f
2012-04-29 change EOL to 'native'
1 //
2 // Ping.cpp
3 //
4 // $Id: //poco/1.4/Net/samples/Ping/src/Ping.cpp#1 $
5 //
6 // This sample demonstrates the Application class.
7 //
8 // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
9 // and Contributors.
10 //
628a06f7 » bufferoverflow
2014-05-04 LICENSE: add info about SPDX-License-Identifier usage and use it
11 // SPDX-License-Identifier: BSL-1.0
5a639074 » aleks-f
2012-04-29 change EOL to 'native'
12 //
13
14
15 #include "Poco/Util/Application.h"
16 #include "Poco/Util/Option.h"
17 #include "Poco/Util/OptionSet.h"
18 #include "Poco/Util/HelpFormatter.h"
19 #include "Poco/Util/AbstractConfiguration.h"
20 #include "Poco/Net/ICMPSocket.h"
21 #include "Poco/Net/ICMPClient.h"
22 #include "Poco/Net/IPAddress.h"
23 #include "Poco/Net/ICMPEventArgs.h"
24 #include "Poco/AutoPtr.h"
25 #include "Poco/NumberParser.h"
26 #include "Poco/Delegate.h"
27 #include <iostream>
28 #include <sstream>
29
30
31 using Poco::Util::Application;
32 using Poco::Util::Option;
33 using Poco::Util::OptionSet;
34 using Poco::Util::HelpFormatter;
35 using Poco::Util::AbstractConfiguration;
36 using Poco::Net::ICMPSocket;
37 using Poco::Net::ICMPClient;
38 using Poco::Net::IPAddress;
39 using Poco::Net::ICMPEventArgs;
40 using Poco::AutoPtr;
41 using Poco::NumberParser;
42 using Poco::Delegate;
43
44
45 class Ping: public Application
46 /// This sample demonstrates the Poco::Net::ICMPClient in conjunction with
47 /// Poco Foundation C#-like events functionality.
48 ///
49 /// Try Ping --help (on Unix platforms) or Ping /help (elsewhere) for
50 /// more information.
51 {
52 public:
53 Ping():
54 _helpRequested(false),
55 _icmpClient(IPAddress::IPv4),
56 _repetitions(4),
57 _target("localhost")
58 {
59 }
60
61 protected:
62 void initialize(Application& self)
63 {
64 loadConfiguration(); // load default configuration files, if present
65 Application::initialize(self);
66
ba9d8574 » aleks-f
2013-02-02 few more fixes and refactoring
67 _icmpClient.pingBegin += delegate(this, &Ping::onBegin);
68 _icmpClient.pingReply += delegate(this, &Ping::onReply);
69 _icmpClient.pingError += delegate(this, &Ping::onError);
70 _icmpClient.pingEnd += delegate(this, &Ping::onEnd);
5a639074 » aleks-f
2012-04-29 change EOL to 'native'
71 }
72
73 void uninitialize()
74 {
ba9d8574 » aleks-f
2013-02-02 few more fixes and refactoring
75 _icmpClient.pingBegin -= delegate(this, &Ping::onBegin);
76 _icmpClient.pingReply -= delegate(this, &Ping::onReply);
77 _icmpClient.pingError -= delegate(this, &Ping::onError);
78 _icmpClient.pingEnd -= delegate(this, &Ping::onEnd);
5a639074 » aleks-f
2012-04-29 change EOL to 'native'
79
80 Application::uninitialize();
81 }
82
83 void defineOptions(OptionSet& options)
84 {
85 Application::defineOptions(options);
86
87 options.addOption(
88 Option("help", "h", "display help information on command line arguments")
89 .required(false)
90 .repeatable(false));
91
92 options.addOption(
93 Option("repetitions", "r", "define the number of repetitions")
94 .required(false)
95 .repeatable(false)
96 .argument("repetitions"));
97
98 options.addOption(
99 Option("target", "t", "define the target address")
100 .required(false)
101 .repeatable(false)
102 .argument("target"));
103 }
104
105 void handleOption(const std::string& name, const std::string& value)
106 {
107 Application::handleOption(name, value);
108
109 if (name == "help")
110 _helpRequested = true;
111 else if (name == "repetitions")
112 _repetitions = NumberParser::parse(value);
113 else if (name == "target")
114 _target = value;
115 }
116
117 void displayHelp()
118 {
119 HelpFormatter helpFormatter(options());
120 helpFormatter.setCommand(commandName());
121 helpFormatter.setUsage("OPTIONS");
122 helpFormatter.setHeader(
123 "A sample application that demonstrates the functionality of the "
124 "Poco::Net::ICMPClient class in conjunction with Poco::Events package functionality.");
125 helpFormatter.format(std::cout);
126 }
127
128
129 int main(const std::vector<std::string>& args)
130 {
131 if (_helpRequested)
132 displayHelp();
133 else
134 _icmpClient.ping(_target, _repetitions);
135
136 return Application::EXIT_OK;
137 }
138
139
140 void onBegin(const void* pSender, ICMPEventArgs& args)
141 {
142 std::ostringstream os;
143 os << "Pinging " << args.hostName() << " [" << args.hostAddress() << "] with " << args.dataSize() << " bytes of data:"
144 << std::endl << "---------------------------------------------" << std::endl;
145 logger().information(os.str());
146 }
147
148 void onReply(const void* pSender, ICMPEventArgs& args)
149 {
150 std::ostringstream os;
151 os << "Reply from " << args.hostAddress()
152 << " bytes=" << args.dataSize()
153 << " time=" << args.replyTime() << "ms"
154 << " TTL=" << args.ttl();
155 logger().information(os.str());
156 }
157
158 void onError(const void* pSender, ICMPEventArgs& args)
159 {
160 std::ostringstream os;
161 os << args.error();
162 logger().information(os.str());
163 }
164
165 void onEnd(const void* pSender, ICMPEventArgs& args)
166 {
167 std::ostringstream os;
168 os << std::endl << "--- Ping statistics for " << args.hostName() << " ---"
169 << std::endl << "Packets: Sent=" << args.sent() << ", Received=" << args.received()
170 << " Lost=" << args.repetitions() - args.received() << " (" << 100.0 - args.percent() << "% loss),"
171 << std::endl << "Approximate round trip times in milliseconds: " << std::endl
172 << "Minimum=" << args.minRTT() << "ms, Maximum=" << args.maxRTT()
173 << "ms, Average=" << args.avgRTT() << "ms"
174 << std::endl << "------------------------------------------";
175 logger().information(os.str());
176 }
177
178 private:
179 bool _helpRequested;
180 ICMPClient _icmpClient;
181 int _repetitions;
182 std::string _target;
183 };
184
185
186 int main(int argc, char** argv)
187 {
188 AutoPtr<Ping> pApp = new Ping;
189 try
190 {
191 pApp->init(argc, argv);
192 }
193 catch (Poco::Exception& exc)
194 {
195 pApp->logger().log(exc);
196 return Application::EXIT_CONFIG;
197 }
198 return pApp->run();
199 }
Something went wrong with that request. Please try again.