~ubuntu-branches/ubuntu/gutsy/poco/gutsy

« back to all changes in this revision

Viewing changes to Net/samples/Ping/src/Ping.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Krzysztof Burghardt
  • Date: 2007-04-27 18:33:48 UTC
  • Revision ID: james.westby@ubuntu.com-20070427183348-xgnpct0qd6a2ip34
Tags: upstream-1.2.9
ImportĀ upstreamĀ versionĀ 1.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// Ping.cpp
 
3
//
 
4
// $Id: //poco/1.2/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
//
 
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:
 
17
// 
 
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.
 
24
// 
 
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.
 
32
//
 
33
 
 
34
 
 
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"
 
47
#include <iostream>
 
48
#include <sstream>
 
49
 
 
50
 
 
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;
 
60
using Poco::AutoPtr;
 
61
using Poco::NumberParser;
 
62
using Poco::Delegate;
 
63
 
 
64
 
 
65
class Ping: public Application
 
66
        /// This sample demonstrates the Poco::Net::ICMPClient in conjunction with 
 
67
        /// Poco Foundation C#-like events functionality.
 
68
        ///
 
69
        /// Try Ping --help (on Unix platforms) or Ping /help (elsewhere) for
 
70
        /// more information.
 
71
{
 
72
public:
 
73
        Ping(): 
 
74
                _helpRequested(false), 
 
75
                _icmpClient(IPAddress::IPv4),
 
76
                _repetitions(4), 
 
77
                _target("localhost")
 
78
        {
 
79
        }
 
80
 
 
81
protected:      
 
82
        void initialize(Application& self)
 
83
        {
 
84
                loadConfiguration(); // load default configuration files, if present
 
85
                Application::initialize(self);
 
86
                
 
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);
 
91
        }
 
92
        
 
93
        void uninitialize()
 
94
        {
 
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);
 
99
 
 
100
                Application::uninitialize();
 
101
        }
 
102
 
 
103
        void defineOptions(OptionSet& options)
 
104
        {
 
105
                Application::defineOptions(options);
 
106
 
 
107
                options.addOption(
 
108
                        Option("help", "h", "display help information on command line arguments")
 
109
                                .required(false)
 
110
                                .repeatable(false));
 
111
 
 
112
                options.addOption(
 
113
                        Option("repetitions", "r", "define the number of repetitions")
 
114
                                .required(false)
 
115
                                .repeatable(false)
 
116
                                .argument("repetitions"));
 
117
                                
 
118
                options.addOption(
 
119
                        Option("target", "t", "define the target address")
 
120
                                .required(false)
 
121
                                .repeatable(false)
 
122
                                .argument("target"));
 
123
        }
 
124
        
 
125
        void handleOption(const std::string& name, const std::string& value)
 
126
        {
 
127
                Application::handleOption(name, value);
 
128
 
 
129
                if (name == "help")
 
130
                        _helpRequested = true;
 
131
                else if (name == "repetitions")
 
132
                        _repetitions = NumberParser::parse(value);
 
133
                else if (name == "target")
 
134
                        _target = value;
 
135
        }
 
136
        
 
137
        void displayHelp()
 
138
        {
 
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);
 
146
        }
 
147
 
 
148
 
 
149
        int main(const std::vector<std::string>& args)
 
150
        {
 
151
                if (_helpRequested) 
 
152
                        displayHelp();
 
153
                else 
 
154
                        _icmpClient.ping(_target, _repetitions);
 
155
                
 
156
                return Application::EXIT_OK;
 
157
        }
 
158
 
 
159
 
 
160
        void onBegin(const void* pSender, ICMPEventArgs& args)
 
161
        {
 
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());
 
166
        }
 
167
 
 
168
        void onReply(const void* pSender, ICMPEventArgs& args)
 
169
        {
 
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());
 
176
        }
 
177
 
 
178
        void onError(const void* pSender, ICMPEventArgs& args)
 
179
        {
 
180
                std::ostringstream os;
 
181
                os << args.error();
 
182
                logger().information(os.str());
 
183
        }
 
184
 
 
185
        void onEnd(const void* pSender, ICMPEventArgs& args)
 
186
        {
 
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());
 
196
        }
 
197
 
 
198
private:
 
199
        bool        _helpRequested;
 
200
        ICMPClient  _icmpClient;
 
201
        int         _repetitions;
 
202
        std::string _target;
 
203
};
 
204
 
 
205
 
 
206
int main(int argc, char** argv)
 
207
{
 
208
        AutoPtr<Ping> pApp = new Ping;
 
209
        try
 
210
        {
 
211
                pApp->init(argc, argv);
 
212
        }
 
213
        catch (Poco::Exception& exc)
 
214
        {
 
215
                pApp->logger().log(exc);
 
216
                return Application::EXIT_CONFIG;
 
217
        }
 
218
        return pApp->run();
 
219
}