mirror of
https://github.com/pocoproject/poco.git
synced 2024-12-14 02:57:45 +01:00
Run ping with custom data size #1912
This commit is contained in:
parent
9770d4cfd4
commit
2ff645d598
@ -44,7 +44,7 @@ public:
|
|||||||
mutable Poco::BasicEvent<ICMPEventArgs> pingError;
|
mutable Poco::BasicEvent<ICMPEventArgs> pingError;
|
||||||
mutable Poco::BasicEvent<ICMPEventArgs> pingEnd;
|
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.
|
/// Creates an ICMP client.
|
||||||
|
|
||||||
~ICMPClient();
|
~ICMPClient();
|
||||||
@ -62,13 +62,22 @@ public:
|
|||||||
///
|
///
|
||||||
/// Returns the number of valid replies.
|
/// 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.
|
/// Pings the specified address [repeat] times.
|
||||||
/// Notifications are not posted for events.
|
/// Notifications are not posted for events.
|
||||||
///
|
///
|
||||||
/// Returns the number of valid replies.
|
/// 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
|
/// Calls ICMPClient::ping(SocketAddress&, int) and
|
||||||
/// returns the result.
|
/// returns the result.
|
||||||
///
|
///
|
||||||
@ -76,6 +85,9 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
mutable SocketAddress::Family _family;
|
mutable SocketAddress::Family _family;
|
||||||
|
int _dataSize;
|
||||||
|
int _ttl;
|
||||||
|
int _timeout;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -34,8 +34,11 @@ namespace Poco {
|
|||||||
namespace Net {
|
namespace Net {
|
||||||
|
|
||||||
|
|
||||||
ICMPClient::ICMPClient(SocketAddress::Family family):
|
ICMPClient::ICMPClient(SocketAddress::Family family, int dataSize, int ttl, int timeout):
|
||||||
_family(family)
|
_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;
|
if (repeat <= 0) return 0;
|
||||||
|
|
||||||
ICMPSocket icmpSocket(_family);
|
ICMPSocket icmpSocket(_family, _dataSize, _ttl, _timeout);
|
||||||
SocketAddress returnAddress;
|
SocketAddress returnAddress;
|
||||||
|
|
||||||
ICMPEventArgs eventArgs(address, repeat, icmpSocket.dataSize(), icmpSocket.ttl());
|
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;
|
if (repeat <= 0) return 0;
|
||||||
|
|
||||||
SocketAddress a(address, 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;
|
if (repeat <= 0) return 0;
|
||||||
|
|
||||||
ICMPSocket icmpSocket(family);
|
ICMPSocket icmpSocket(family, dataSize, ttl, timeout);
|
||||||
SocketAddress returnAddress;
|
SocketAddress returnAddress;
|
||||||
int received = 0;
|
int received = 0;
|
||||||
|
|
||||||
|
@ -33,8 +33,7 @@ using Poco::AutoPtr;
|
|||||||
|
|
||||||
|
|
||||||
ICMPClientTest::ICMPClientTest(const std::string& name):
|
ICMPClientTest::ICMPClientTest(const std::string& name):
|
||||||
CppUnit::TestCase(name),
|
CppUnit::TestCase(name)
|
||||||
_icmpClient(IPAddress::IPv4)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,31 +47,67 @@ void ICMPClientTest::testPing()
|
|||||||
{
|
{
|
||||||
assert(ICMPClient::pingIPv4("127.0.0.1") > 0);
|
assert(ICMPClient::pingIPv4("127.0.0.1") > 0);
|
||||||
|
|
||||||
assert(_icmpClient.ping("127.0.0.1") > 0);
|
Poco::Net::ICMPClient icmpClient(IPAddress::IPv4);
|
||||||
assert(_icmpClient.ping("www.appinf.com", 4) > 0);
|
|
||||||
|
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
|
// 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)
|
// 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("192.168.243.1"));
|
||||||
assert(0 == _icmpClient.ping("10.11.12.13"));
|
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()
|
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()
|
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::TestSuite* pSuite = new CppUnit::TestSuite("ICMPClientTest");
|
||||||
|
|
||||||
CppUnit_addTest(pSuite, ICMPClientTest, testPing);
|
CppUnit_addTest(pSuite, ICMPClientTest, testPing);
|
||||||
|
CppUnit_addTest(pSuite, ICMPClientTest, testBigPing);
|
||||||
|
|
||||||
return pSuite;
|
return pSuite;
|
||||||
}
|
}
|
||||||
|
@ -27,6 +27,7 @@ public:
|
|||||||
~ICMPClientTest();
|
~ICMPClientTest();
|
||||||
|
|
||||||
void testPing();
|
void testPing();
|
||||||
|
void testBigPing();
|
||||||
|
|
||||||
void setUp();
|
void setUp();
|
||||||
void tearDown();
|
void tearDown();
|
||||||
@ -39,7 +40,8 @@ public:
|
|||||||
void onEnd(const void* pSender, Poco::Net::ICMPEventArgs& args);
|
void onEnd(const void* pSender, Poco::Net::ICMPEventArgs& args);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Poco::Net::ICMPClient _icmpClient;
|
void registerDelegates(const Poco::Net::ICMPClient& icmpClient);
|
||||||
|
void unregisterDelegates(const Poco::Net::ICMPClient& icmpClient);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user