* NTP client implementation

This commit is contained in:
Rangel Reale
2014-01-13 15:04:27 -02:00
parent fefdee93f7
commit e37b756157
19 changed files with 1118 additions and 0 deletions

View File

@@ -0,0 +1,104 @@
//
// NTPClientTest.cpp
//
// $Id: //poco/1.4/Net/testsuite/src/NTPClientTest.cpp#1 $
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// 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 "NTPClientTest.h"
#include "CppUnit/TestCaller.h"
#include "CppUnit/TestSuite.h"
#include "Poco/Net/NTPClient.h"
#include "Poco/Net/NTPEventArgs.h"
#include "Poco/Net/SocketAddress.h"
#include "Poco/Net/NetException.h"
#include "Poco/AutoPtr.h"
#include "Poco/Delegate.h"
#include "Poco/DateTimeFormatter.h"
#include "Poco/DateTimeFormat.h"
#include <sstream>
#include <iostream>
using Poco::Net::NTPClient;
using Poco::Net::NTPEventArgs;
using Poco::Net::SocketAddress;
using Poco::Net::IPAddress;
using Poco::Net::HostNotFoundException;
using Poco::Delegate;
using Poco::AutoPtr;
NTPClientTest::NTPClientTest(const std::string& name):
CppUnit::TestCase(name),
_ntpClient(IPAddress::IPv4)
{
}
NTPClientTest::~NTPClientTest()
{
}
void NTPClientTest::testTimeSync()
{
assert(_ntpClient.request("pool.ntp.br") > 0);
}
void NTPClientTest::setUp()
{
_ntpClient.response += Delegate<NTPClientTest, NTPEventArgs>(this, &NTPClientTest::onResponse);
}
void NTPClientTest::tearDown()
{
_ntpClient.response -= Delegate<NTPClientTest, NTPEventArgs>(this, &NTPClientTest::onResponse);
}
void NTPClientTest::onResponse(const void* pSender, NTPEventArgs& args)
{
std::ostringstream os;
os << std::endl << "Received from " << args.hostName() << " [" << args.hostAddress() << "] with "
<< Poco::DateTimeFormatter::format(args.packet().referenceTime(), Poco::DateTimeFormat::ISO8601_FORMAT) << " reference typestamp"
<< std::endl;
std::cout << os.str() << std::endl;
}
CppUnit::Test* NTPClientTest::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("NTPClientTest");
CppUnit_addTest(pSuite, NTPClientTest, testTimeSync);
return pSuite;
}

View File

@@ -0,0 +1,64 @@
//
// NTPClientTest.h
//
// $Id: //poco/1.4/Net/testsuite/src/NTPClientTest.h#1 $
//
// Definition of the NTPClientTest class.
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// 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.
//
#ifndef NTPClientTest_INCLUDED
#define NTPClientTest_INCLUDED
#include "Poco/Net/Net.h"
#include "CppUnit/TestCase.h"
#include "Poco/Net/NTPClient.h"
#include "Poco/Net/NTPEventArgs.h"
class NTPClientTest: public CppUnit::TestCase
{
public:
NTPClientTest(const std::string& name);
~NTPClientTest();
void testTimeSync();
void setUp();
void tearDown();
static CppUnit::Test* suite();
void onResponse(const void* pSender, Poco::Net::NTPEventArgs& args);
private:
Poco::Net::NTPClient _ntpClient;
};
#endif // NTPClientTest_INCLUDED

View File

@@ -0,0 +1,44 @@
//
// NTPClientTestSuite.cpp
//
// $Id: //poco/1.4/Net/testsuite/src/NTPClientTestSuite.cpp#1 $
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// 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 "NTPClientTestSuite.h"
#include "NTPClientTest.h"
CppUnit::Test* NTPClientTestSuite::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("NTPClientTestSuite");
pSuite->addTest(NTPClientTest::suite());
return pSuite;
}

View File

@@ -0,0 +1,49 @@
//
// NTPClientTestSuite.h
//
// $Id: //poco/1.4/Net/testsuite/src/NTPClientTestSuite.h#1 $
//
// Definition of the NTPClientTestSuite class.
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// 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.
//
#ifndef NTPClientTestSuite_INCLUDED
#define NTPClientTestSuite_INCLUDED
#include "CppUnit/TestSuite.h"
class NTPClientTestSuite
{
public:
static CppUnit::Test* suite();
};
#endif // NTPClientTestSuite_INCLUDED

View File

@@ -43,6 +43,7 @@
#include "FTPClientTestSuite.h"
#include "MailTestSuite.h"
#include "ICMPClientTestSuite.h"
#include "NTPClientTestSuite.h"
#include "WebSocketTestSuite.h"
#include "SyslogTest.h"
@@ -63,6 +64,7 @@ CppUnit::Test* NetTestSuite::suite()
pSuite->addTest(FTPClientTestSuite::suite());
pSuite->addTest(MailTestSuite::suite());
pSuite->addTest(ICMPClientTestSuite::suite());
pSuite->addTest(NTPClientTestSuite::suite());
pSuite->addTest(WebSocketTestSuite::suite());
pSuite->addTest(SyslogTest::suite());