root/trunk/0060_ICMPClient/ICMPClientTest.cpp

User picture

Author: Setsu

Revision: 348 («Previous)


File Size: 6.31 KB

(March 20, 2011 02:36 UTC) About 1 year ago

ICMPClientTest.cpp updated

 
Show/hide line numbers
//
//  ICMPClientTest.cpp
//
//  Created by Setsu on Mar 20, 2011.
//  Copyright 2011 RoundSquare Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
// 
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//

//----------------------------------------
//	include
//----------------------------------------
#include <Poco/Logger.h>
#include <Poco/PatternFormatter.h>
#include <Poco/FormattingChannel.h>
#include <Poco/ConsoleChannel.h>
#include <Poco/Format.h>
#include <Poco/Net/ICMPClient.h>
#include <Poco/Net/IPAddress.h>
#include <Poco/Net/ICMPEventArgs.h>
#include <Poco/Delegate.h>

#include <string>

#include "ScopedLogMessage.h"

//----------------------------------------
//	PrepareConsoleLogger
//----------------------------------------
void PrepareConsoleLogger(const std::string& name, int level=Poco::Message::PRIO_INFORMATION)
{
	Poco::FormattingChannel* pFCConsole = new Poco::FormattingChannel(new Poco::PatternFormatter("%t"));
	pFCConsole->setChannel(new Poco::ConsoleChannel);
	pFCConsole->open();

	Poco::Logger::create(name, pFCConsole, level);
}

//----------------------------------------
//	IsOK
//----------------------------------------
const std::string IsOK(bool isOK)
{
	return isOK ? "OK":"NG";
}

//----------------------------------------
//	ICMPClientTest
//----------------------------------------
class ICMPClientTest
{
public:
	ICMPClientTest(	ScopedLogMessage& msg,
					Poco::Net::ICMPClient& client) :
		m_msg		(msg)
	,	m_client	(client)
	{
		m_client.pingBegin += Poco::Delegate<ICMPClientTest, Poco::Net::ICMPEventArgs>(this, &ICMPClientTest::onBegin);
		m_client.pingReply += Poco::Delegate<ICMPClientTest, Poco::Net::ICMPEventArgs>(this, &ICMPClientTest::onReply);
		m_client.pingError += Poco::Delegate<ICMPClientTest, Poco::Net::ICMPEventArgs>(this, &ICMPClientTest::onError);
		m_client.pingEnd   += Poco::Delegate<ICMPClientTest, Poco::Net::ICMPEventArgs>(this, &ICMPClientTest::onEnd);
	}
	~ICMPClientTest()
	{
		m_client.pingBegin -= Poco::Delegate<ICMPClientTest, Poco::Net::ICMPEventArgs>(this, &ICMPClientTest::onBegin);
		m_client.pingReply -= Poco::Delegate<ICMPClientTest, Poco::Net::ICMPEventArgs>(this, &ICMPClientTest::onReply);
		m_client.pingError -= Poco::Delegate<ICMPClientTest, Poco::Net::ICMPEventArgs>(this, &ICMPClientTest::onError);
		m_client.pingEnd   -= Poco::Delegate<ICMPClientTest, Poco::Net::ICMPEventArgs>(this, &ICMPClientTest::onEnd);
	}
	int ping(Poco::Net::SocketAddress& address, int repeat) const
	{
		return m_client.ping(address, repeat);
	}
	void onBegin(const void* pSender, Poco::Net::ICMPEventArgs& args)
	{
		m_msg.Message(Poco::format("  Pinging %s [%s] with %d bytes of data:",
									args.hostName(),
									args.hostAddress(),
									args.dataSize()));
	}
	void onReply(const void* pSender, Poco::Net::ICMPEventArgs& args)
	{
		m_msg.Message(Poco::format("   Reply from %s bytes=%d time=%dms TTL=%d",
									args.hostName(),
									args.dataSize(),
									args.replyTime(),
									args.ttl()));
	}
	void onError(const void* pSender, Poco::Net::ICMPEventArgs& args)
	{
		m_msg.Message(Poco::format("   Error: %s ", args.error()));
	}
	void onEnd(const void* pSender, Poco::Net::ICMPEventArgs& args)
	{
		int received = args.received();
		m_msg.Message(Poco::format("  Ping statistics for %s", args.hostName()));
		m_msg.Message(Poco::format("   Packets: Sent=%d, Received=%d, Lost=%d (%3.3f%% loss)",
									args.sent(),
									received,
									args.repetitions() - received,
									100.0 - args.percent()));
		m_msg.Message(Poco::format("   Approximate round trip times: Minimum=%dms, Maximum=%dms, Average=%dms",
									args.minRTT(),
									args.maxRTT(),
									args.avgRTT()));
	}

private:
	ScopedLogMessage&		m_msg;
	Poco::Net::ICMPClient&	m_client;
};

//----------------------------------------
//	main
//----------------------------------------
int main(int /*argc*/, char** /*argv*/)
{
	PrepareConsoleLogger(Poco::Logger::ROOT, Poco::Message::PRIO_INFORMATION);

	ScopedLogMessage msg("ICMPClientTest ", "start", "end");

	try
	{
		const std::string url("poco.roundsquare.net");
		const std::string urlWithPort(url+":80");
		const int numRepeat = 5;
		Poco::Net::SocketAddress socketAddress(urlWithPort);

		msg.Message("*** static methods ***");
		msg.Message(Poco::format(" Poco::Net::ICMPClient::ping() - %s",
					IsOK(numRepeat == Poco::Net::ICMPClient::ping(socketAddress, Poco::Net::IPAddress::IPv4, numRepeat))));
		msg.Message(Poco::format(" Poco::Net::ICMPClient::pingIPv4() - %s",
					IsOK(numRepeat == Poco::Net::ICMPClient::pingIPv4(url, numRepeat))));

		msg.Message("*** non-static methods ***");
		Poco::Net::ICMPClient icmpClient(Poco::Net::IPAddress::IPv4);
		msg.Message(Poco::format(" Poco::Net::ICMPClient::ping(const std::string& address, int repeat) - %s",
					IsOK(numRepeat == icmpClient.ping(url, numRepeat))));
		ICMPClientTest test(msg, icmpClient);
		msg.Message(" ICMPClientTest::ping() start");
		msg.Message(Poco::format(" ICMPClientTest::ping() - %s",
					IsOK(numRepeat == test.ping(socketAddress, numRepeat))));
	}
	catch(Poco::IOException&)
	{
		msg.Message(" CAUTION: You need to sudo to run this app.");
	}

	return 0;
}