Run ping with custom data size #1912

This commit is contained in:
Alex Fabijanic 2017-09-28 11:18:45 -05:00
parent 9770d4cfd4
commit 2ff645d598
4 changed files with 81 additions and 25 deletions

View File

@ -44,7 +44,7 @@ public:
mutable Poco::BasicEvent<ICMPEventArgs> pingError;
mutable Poco::BasicEvent<ICMPEventArgs> pingEnd;
explicit ICMPClient(SocketAddress::Family family);
explicit ICMPClient(SocketAddress::Family family, int dataSize = 48, int ttl = 128, int timeout = 50000);
/// Creates an ICMP client.
~ICMPClient();
@ -62,13 +62,22 @@ public:
///
/// Returns the number of valid replies.
static int ping(SocketAddress& address, SocketAddress::Family family, int repeat = 1);
static int ping(SocketAddress& address,
SocketAddress::Family family,
int repeat = 1,
int dataSize = 48,
int ttl = 128,
int timeout = 50000);
/// Pings the specified address [repeat] times.
/// Notifications are not posted for events.
///
/// Returns the number of valid replies.
static int pingIPv4(const std::string& address, int repeat = 1);
static int pingIPv4(const std::string& address,
int repeat = 1,
int dataSize = 48,
int ttl = 128,
int timeout = 50000);
/// Calls ICMPClient::ping(SocketAddress&, int) and
/// returns the result.
///
@ -76,6 +85,9 @@ public:
private:
mutable SocketAddress::Family _family;
int _dataSize;
int _ttl;
int _timeout;
};

View File

@ -34,8 +34,11 @@ namespace Poco {
namespace Net {
ICMPClient::ICMPClient(SocketAddress::Family family):
_family(family)
ICMPClient::ICMPClient(SocketAddress::Family family, int dataSize, int ttl, int timeout):
_family(family),
_dataSize(dataSize),
_ttl(ttl),
_timeout(timeout)
{
}
@ -58,7 +61,7 @@ int ICMPClient::ping(SocketAddress& address, int repeat) const
{
if (repeat <= 0) return 0;
ICMPSocket icmpSocket(_family);
ICMPSocket icmpSocket(_family, _dataSize, _ttl, _timeout);
SocketAddress returnAddress;
ICMPEventArgs eventArgs(address, repeat, icmpSocket.dataSize(), icmpSocket.ttl());
@ -105,20 +108,23 @@ int ICMPClient::ping(SocketAddress& address, int repeat) const
}
int ICMPClient::pingIPv4(const std::string& address, int repeat)
int ICMPClient::pingIPv4(const std::string& address, int repeat,
int dataSize, int ttl, int timeout)
{
if (repeat <= 0) return 0;
SocketAddress a(address, 0);
return ping(a, IPAddress::IPv4, repeat);
return ping(a, IPAddress::IPv4, repeat, dataSize, ttl, timeout);
}
int ICMPClient::ping(SocketAddress& address, IPAddress::Family family, int repeat)
int ICMPClient::ping(SocketAddress& address,
IPAddress::Family family, int repeat,
int dataSize, int ttl, int timeout)
{
if (repeat <= 0) return 0;
ICMPSocket icmpSocket(family);
ICMPSocket icmpSocket(family, dataSize, ttl, timeout);
SocketAddress returnAddress;
int received = 0;

View File

@ -33,8 +33,7 @@ using Poco::AutoPtr;
ICMPClientTest::ICMPClientTest(const std::string& name):
CppUnit::TestCase(name),
_icmpClient(IPAddress::IPv4)
CppUnit::TestCase(name)
{
}
@ -48,31 +47,67 @@ void ICMPClientTest::testPing()
{
assert(ICMPClient::pingIPv4("127.0.0.1") > 0);
assert(_icmpClient.ping("127.0.0.1") > 0);
assert(_icmpClient.ping("www.appinf.com", 4) > 0);
Poco::Net::ICMPClient icmpClient(IPAddress::IPv4);
registerDelegates(icmpClient);
assert(icmpClient.ping("127.0.0.1") > 0);
assert(icmpClient.ping("www.appinf.com", 4) > 0);
// warning: may fail depending on the existence of the addresses at test site
// if so, adjust accordingly (i.e. specify non-existent or unreachable IP addresses)
assert(0 == _icmpClient.ping("192.168.243.1"));
assert(0 == _icmpClient.ping("10.11.12.13"));
assert(0 == icmpClient.ping("192.168.243.1"));
assert(0 == icmpClient.ping("10.11.12.13"));
unregisterDelegates(icmpClient);
}
void ICMPClientTest::testBigPing()
{
assert(ICMPClient::pingIPv4("127.0.0.1", 1, 96) > 0);
Poco::Net::ICMPClient icmpClient(IPAddress::IPv4, 96);
registerDelegates(icmpClient);
assert(icmpClient.ping("127.0.0.1", 1) > 0);
assert(icmpClient.ping("www.appinf.com", 4) > 0);
// warning: may fail depending on the existence of the addresses at test site
// if so, adjust accordingly (i.e. specify non-existent or unreachable IP addresses)
assert(0 == icmpClient.ping("192.168.243.1"));
assert(0 == icmpClient.ping("10.11.12.13"));
unregisterDelegates(icmpClient);
}
void ICMPClientTest::registerDelegates(const ICMPClient& icmpClient)
{
icmpClient.pingBegin += Delegate<ICMPClientTest, ICMPEventArgs>(this, &ICMPClientTest::onBegin);
icmpClient.pingReply += Delegate<ICMPClientTest, ICMPEventArgs>(this, &ICMPClientTest::onReply);
icmpClient.pingError += Delegate<ICMPClientTest, ICMPEventArgs>(this, &ICMPClientTest::onError);
icmpClient.pingEnd += Delegate<ICMPClientTest, ICMPEventArgs>(this, &ICMPClientTest::onEnd);
}
void ICMPClientTest::unregisterDelegates(const ICMPClient& icmpClient)
{
icmpClient.pingBegin -= Delegate<ICMPClientTest, ICMPEventArgs>(this, &ICMPClientTest::onBegin);
icmpClient.pingReply -= Delegate<ICMPClientTest, ICMPEventArgs>(this, &ICMPClientTest::onReply);
icmpClient.pingError -= Delegate<ICMPClientTest, ICMPEventArgs>(this, &ICMPClientTest::onError);
icmpClient.pingEnd -= Delegate<ICMPClientTest, ICMPEventArgs>(this, &ICMPClientTest::onEnd);
}
void ICMPClientTest::setUp()
{
_icmpClient.pingBegin += Delegate<ICMPClientTest, ICMPEventArgs>(this, &ICMPClientTest::onBegin);
_icmpClient.pingReply += Delegate<ICMPClientTest, ICMPEventArgs>(this, &ICMPClientTest::onReply);
_icmpClient.pingError += Delegate<ICMPClientTest, ICMPEventArgs>(this, &ICMPClientTest::onError);
_icmpClient.pingEnd += Delegate<ICMPClientTest, ICMPEventArgs>(this, &ICMPClientTest::onEnd);
}
void ICMPClientTest::tearDown()
{
_icmpClient.pingBegin -= Delegate<ICMPClientTest, ICMPEventArgs>(this, &ICMPClientTest::onBegin);
_icmpClient.pingReply -= Delegate<ICMPClientTest, ICMPEventArgs>(this, &ICMPClientTest::onReply);
_icmpClient.pingError -= Delegate<ICMPClientTest, ICMPEventArgs>(this, &ICMPClientTest::onError);
_icmpClient.pingEnd -= Delegate<ICMPClientTest, ICMPEventArgs>(this, &ICMPClientTest::onEnd);
}
@ -125,6 +160,7 @@ CppUnit::Test* ICMPClientTest::suite()
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ICMPClientTest");
CppUnit_addTest(pSuite, ICMPClientTest, testPing);
CppUnit_addTest(pSuite, ICMPClientTest, testBigPing);
return pSuite;
}

View File

@ -27,6 +27,7 @@ public:
~ICMPClientTest();
void testPing();
void testBigPing();
void setUp();
void tearDown();
@ -39,7 +40,8 @@ public:
void onEnd(const void* pSender, Poco::Net::ICMPEventArgs& args);
private:
Poco::Net::ICMPClient _icmpClient;
void registerDelegates(const Poco::Net::ICMPClient& icmpClient);
void unregisterDelegates(const Poco::Net::ICMPClient& icmpClient);
};