simplified the Ping sample

This commit is contained in:
Aleksandar Fabijanic 2009-05-18 19:38:35 +00:00
parent 810cfd2ff4
commit c76ec8b854

View File

@ -59,7 +59,7 @@ using Poco::Net::IPAddress;
using Poco::Net::ICMPEventArgs;
using Poco::AutoPtr;
using Poco::NumberParser;
using Poco::Delegate;
using Poco::delegate;
class Ping: public Application
@ -84,18 +84,21 @@ protected:
loadConfiguration(); // load default configuration files, if present
Application::initialize(self);
_icmpClient.pingBegin += Delegate<Ping, ICMPEventArgs>(this, &Ping::onBegin);
_icmpClient.pingReply += Delegate<Ping, ICMPEventArgs>(this, &Ping::onReply);
_icmpClient.pingError += Delegate<Ping, ICMPEventArgs>(this, &Ping::onError);
_icmpClient.pingEnd += Delegate<Ping, ICMPEventArgs>(this, &Ping::onEnd);
// Note: Handlers are static member functions in this example.
// A non-static handler would require this pointer explicitly
// specified as the first argument, e.g. delegate(this, &Ping::onBegin);
_icmpClient.pingBegin += delegate(&Ping::onBegin);
_icmpClient.pingReply += delegate(&Ping::onReply);
_icmpClient.pingError += delegate(&Ping::onError);
_icmpClient.pingEnd += delegate(&Ping::onEnd);
}
void uninitialize()
{
_icmpClient.pingBegin -= Delegate<Ping, ICMPEventArgs>(this, &Ping::onBegin);
_icmpClient.pingReply -= Delegate<Ping, ICMPEventArgs>(this, &Ping::onReply);
_icmpClient.pingError -= Delegate<Ping, ICMPEventArgs>(this, &Ping::onError);
_icmpClient.pingEnd -= Delegate<Ping, ICMPEventArgs>(this, &Ping::onEnd);
_icmpClient.pingBegin -= delegate(&Ping::onBegin);
_icmpClient.pingReply -= delegate(&Ping::onReply);
_icmpClient.pingError -= delegate(&Ping::onError);
_icmpClient.pingEnd -= delegate(&Ping::onEnd);
Application::uninitialize();
}
@ -157,42 +160,38 @@ protected:
}
void onBegin(const void* pSender, ICMPEventArgs& args)
static void onBegin(ICMPEventArgs& args)
{
std::ostringstream os;
os << "Pinging " << args.hostName() << " [" << args.hostAddress() << "] with " << args.dataSize() << " bytes of data:"
<< std::endl << "---------------------------------------------" << std::endl;
logger().information(os.str());
std::cout << "Pinging " << args.hostName()
<< " [" << args.hostAddress() << "] with "
<< args.dataSize() << " bytes of data:"
<< std::endl << "---------------------------------------------"
<< std::endl;
}
void onReply(const void* pSender, ICMPEventArgs& args)
static void onReply(ICMPEventArgs& args)
{
std::ostringstream os;
os << "Reply from " << args.hostAddress()
<< " bytes=" << args.dataSize()
<< " time=" << args.replyTime() << "ms"
<< " TTL=" << args.ttl();
logger().information(os.str());
std::cout << "Reply from " << args.hostAddress()
<< " bytes=" << args.dataSize()
<< " time=" << args.replyTime() << "ms"
<< " TTL=" << args.ttl() << std::endl;
}
void onError(const void* pSender, ICMPEventArgs& args)
static void onError(ICMPEventArgs& args)
{
std::ostringstream os;
os << args.error();
logger().information(os.str());
std::cout << args.error() << std::endl;
}
void onEnd(const void* pSender, ICMPEventArgs& args)
static void onEnd(ICMPEventArgs& args)
{
std::ostringstream os;
os << std::endl << "--- Ping statistics for " << args.hostName() << " ---"
<< std::endl << "Packets: Sent=" << args.sent() << ", Received=" << args.received()
<< " Lost=" << args.repetitions() - args.received() << " (" << 100.0 - args.percent() << "% loss),"
<< std::endl << "Approximate round trip times in milliseconds: " << std::endl
<< "Minimum=" << args.minRTT() << "ms, Maximum=" << args.maxRTT()
<< "ms, Average=" << args.avgRTT() << "ms"
<< std::endl << "------------------------------------------";
logger().information(os.str());
std::cout << std::endl << "--- Ping statistics for " << args.hostName() << " ---"
<< std::endl << "Packets: Sent=" << args.sent() << ", Received=" << args.received()
<< " Lost=" << args.repetitions() - args.received() << " (" << 100.0 - args.percent() << "% loss),"
<< std::endl << "Approximate round trip times in milliseconds: " << std::endl
<< "Minimum=" << args.minRTT() << "ms, Maximum=" << args.maxRTT()
<< "ms, Average=" << args.avgRTT() << "ms"
<< std::endl << "------------------------------------------"
<< std::endl;
}
private: