Skip to content

Commit e37b756

Browse files
committed
* NTP client implementation
1 parent fefdee9 commit e37b756

19 files changed

Lines changed: 1118 additions & 0 deletions

Net/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ objects = \
3030
MailRecipient MailMessage MailStream SMTPClientSession POP3ClientSession \
3131
RawSocket RawSocketImpl ICMPClient ICMPEventArgs ICMPPacket ICMPPacketImpl \
3232
ICMPSocket ICMPSocketImpl ICMPv4PacketImpl \
33+
NTPClient NTPEventArgs NTPPacket \
3334
RemoteSyslogChannel RemoteSyslogListener SMTPChannel \
3435
WebSocket WebSocketImpl
3536

Net/Net_vs100.vcxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,9 @@
274274
<ClInclude Include="include\Poco\Net\Net.h" />
275275
<ClInclude Include="include\Poco\Net\NetException.h" />
276276
<ClInclude Include="include\Poco\Net\NetworkInterface.h" />
277+
<ClInclude Include="include\Poco\Net\NTPClient.h" />
278+
<ClInclude Include="include\Poco\Net\NTPEventArgs.h" />
279+
<ClInclude Include="include\Poco\Net\NTPPacket.h" />
277280
<ClInclude Include="include\Poco\Net\ParallelSocketAcceptor.h" />
278281
<ClInclude Include="include\Poco\Net\ParallelSocketReactor.h" />
279282
<ClInclude Include="include\Poco\Net\PartStore.h" />
@@ -376,6 +379,9 @@
376379
<ClCompile Include="src\Net.cpp" />
377380
<ClCompile Include="src\NetException.cpp" />
378381
<ClCompile Include="src\NetworkInterface.cpp" />
382+
<ClCompile Include="src\NTPClient.cpp" />
383+
<ClCompile Include="src\NTPEventArgs.cpp" />
384+
<ClCompile Include="src\NTPPacket.cpp" />
379385
<ClCompile Include="src\PartStore.cpp" />
380386
<ClCompile Include="src\SocketAddress.cpp" />
381387
<ClCompile Include="src\DatagramSocket.cpp" />

Net/Net_vs100.vcxproj.filters

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,15 @@
127127
<Filter Include="WebSocket\Source Files">
128128
<UniqueIdentifier>{9e8d3dd2-8950-4437-b83d-0f5c2acdf755}</UniqueIdentifier>
129129
</Filter>
130+
<Filter Include="NTPClient">
131+
<UniqueIdentifier>{da44ddca-d7c4-49d7-9d2d-eae118575059}</UniqueIdentifier>
132+
</Filter>
133+
<Filter Include="NTPClient\Header Files">
134+
<UniqueIdentifier>{b1bc5e82-a8e3-4c3c-9bef-f5ebc71a5ffc}</UniqueIdentifier>
135+
</Filter>
136+
<Filter Include="NTPClient\Source Files">
137+
<UniqueIdentifier>{ef2b98c6-9300-404b-b922-f1d79bca1d6b}</UniqueIdentifier>
138+
</Filter>
130139
</ItemGroup>
131140
<ItemGroup>
132141
<ClInclude Include="include\Poco\Net\DNS.h">
@@ -429,6 +438,15 @@
429438
<ClInclude Include="include\Poco\Net\SocketAddressImpl.h">
430439
<Filter>NetCore\Header Files</Filter>
431440
</ClInclude>
441+
<ClInclude Include="include\Poco\Net\NTPClient.h">
442+
<Filter>NTPClient\Header Files</Filter>
443+
</ClInclude>
444+
<ClInclude Include="include\Poco\Net\NTPEventArgs.h">
445+
<Filter>NTPClient\Header Files</Filter>
446+
</ClInclude>
447+
<ClInclude Include="include\Poco\Net\NTPPacket.h">
448+
<Filter>NTPClient\Header Files</Filter>
449+
</ClInclude>
432450
</ItemGroup>
433451
<ItemGroup>
434452
<ClCompile Include="src\DNS.cpp">
@@ -713,6 +731,15 @@
713731
<ClCompile Include="src\SocketAddressImpl.cpp">
714732
<Filter>NetCore\Source Files</Filter>
715733
</ClCompile>
734+
<ClCompile Include="src\NTPClient.cpp">
735+
<Filter>NTPClient\Source Files</Filter>
736+
</ClCompile>
737+
<ClCompile Include="src\NTPEventArgs.cpp">
738+
<Filter>NTPClient\Source Files</Filter>
739+
</ClCompile>
740+
<ClCompile Include="src\NTPPacket.cpp">
741+
<Filter>NTPClient\Source Files</Filter>
742+
</ClCompile>
716743
</ItemGroup>
717744
<ItemGroup>
718745
<ResourceCompile Include="..\DLLVersion.rc" />

Net/include/Poco/Net/NTPClient.h

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
//
2+
// NTPClient.h
3+
//
4+
// $Id: //poco/1.4/Net/include/Poco/Net/NTPClient.h#1 $
5+
//
6+
// Library: Net
7+
// Package: NTP
8+
// Module: NTPClient
9+
//
10+
// Definition of the NTPClient class.
11+
//
12+
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
13+
// and Contributors.
14+
//
15+
// Permission is hereby granted, free of charge, to any person or organization
16+
// obtaining a copy of the software and accompanying documentation covered by
17+
// this license (the "Software") to use, reproduce, display, distribute,
18+
// execute, and transmit the Software, and to prepare derivative works of the
19+
// Software, and to permit third-parties to whom the Software is furnished to
20+
// do so, all subject to the following:
21+
//
22+
// The copyright notices in the Software and this entire statement, including
23+
// the above license grant, this restriction and the following disclaimer,
24+
// must be included in all copies of the Software, in whole or in part, and
25+
// all derivative works of the Software, unless such copies or derivative
26+
// works are solely in the form of machine-executable object code generated by
27+
// a source language processor.
28+
//
29+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31+
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
32+
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
33+
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
34+
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
35+
// DEALINGS IN THE SOFTWARE.
36+
//
37+
38+
39+
#ifndef Net_NTPClient_INCLUDED
40+
#define Net_NTPClient_INCLUDED
41+
42+
43+
#include "Poco/Net/Net.h"
44+
#include "Poco/Net/NTPEventArgs.h"
45+
#include "Poco/Net/SocketAddress.h"
46+
#include "Poco/BasicEvent.h"
47+
48+
49+
namespace Poco {
50+
namespace Net {
51+
52+
53+
class Net_API NTPClient
54+
/// This class provides NTP (Network Time Protocol) client functionality.
55+
{
56+
public:
57+
mutable Poco::BasicEvent<NTPEventArgs> response;
58+
59+
explicit NTPClient(IPAddress::Family family, int timeout = 3000000);
60+
/// Creates an NTP client.
61+
62+
~NTPClient();
63+
/// Destroys the NTP client.
64+
65+
int request(SocketAddress& address) const;
66+
/// Request the time from the server at address.
67+
/// Notifications are posted for events.
68+
///
69+
/// Returns the number of valid replies.
70+
71+
int request(const std::string& address) const;
72+
/// Request the time from the server at address.
73+
/// Notifications are posted for events.
74+
///
75+
/// Returns the number of valid replies.
76+
77+
private:
78+
mutable IPAddress::Family _family;
79+
int _timeout;
80+
};
81+
82+
83+
} } // namespace Poco::Net
84+
85+
86+
#endif // Net_NTPClient_INCLUDED
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
//
2+
// NTPEventArgs.h
3+
//
4+
// $Id: //poco/1.4/Net/include/Poco/Net/NTPEventArgs.h#1 $
5+
//
6+
// Library: Net
7+
// Package: NTP
8+
// Module: NTPEventArgs
9+
//
10+
// Definition of NTPEventArgs.
11+
//
12+
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
13+
// and Contributors.
14+
//
15+
// Permission is hereby granted, free of charge, to any person or organization
16+
// obtaining a copy of the software and accompanying documentation covered by
17+
// this license (the "Software") to use, reproduce, display, distribute,
18+
// execute, and transmit the Software, and to prepare derivative works of the
19+
// Software, and to permit third-parties to whom the Software is furnished to
20+
// do so, all subject to the following:
21+
//
22+
// The copyright notices in the Software and this entire statement, including
23+
// the above license grant, this restriction and the following disclaimer,
24+
// must be included in all copies of the Software, in whole or in part, and
25+
// all derivative works of the Software, unless such copies or derivative
26+
// works are solely in the form of machine-executable object code generated by
27+
// a source language processor.
28+
//
29+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31+
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
32+
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
33+
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
34+
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
35+
// DEALINGS IN THE SOFTWARE.
36+
//
37+
38+
39+
#ifndef Net_NTPEventArgs_INCLUDED
40+
#define Net_NTPEventArgs_INCLUDED
41+
42+
43+
#include "Poco/Net/Net.h"
44+
#include "Poco/Net/SocketAddress.h"
45+
#include "Poco/Net/NTPPacket.h"
46+
47+
48+
namespace Poco {
49+
namespace Net {
50+
51+
52+
class Net_API NTPEventArgs
53+
/// The purpose of the NTPEventArgs class is to be used as template parameter
54+
/// to instantiate event members in NTPClient class.
55+
/// When clients register for an event notification, the reference to the class is
56+
/// passed to the handler function to provide information about the event.
57+
{
58+
public:
59+
NTPEventArgs(const SocketAddress& address);
60+
/// Creates NTPEventArgs.
61+
62+
virtual ~NTPEventArgs();
63+
/// Destroys NTPEventArgs.
64+
65+
std::string hostName() const;
66+
/// Tries to resolve the target IP address into host name.
67+
/// If unsuccessful, all exceptions are silently ignored and
68+
/// the IP address is returned.
69+
70+
std::string hostAddress() const;
71+
/// Returns the target IP address.
72+
73+
const NTPPacket &packet();
74+
/// Returns the NTP packet.
75+
76+
private:
77+
NTPEventArgs();
78+
79+
void setPacket(NTPPacket &packet);
80+
81+
SocketAddress _address;
82+
NTPPacket _packet;
83+
84+
friend class NTPClient;
85+
};
86+
87+
88+
//
89+
// inlines
90+
//
91+
inline const NTPPacket &NTPEventArgs::packet()
92+
{
93+
return _packet;
94+
}
95+
96+
97+
inline void NTPEventArgs::setPacket(NTPPacket &packet)
98+
{
99+
_packet = packet;
100+
}
101+
102+
103+
} } // namespace Poco::Net
104+
105+
106+
#endif

0 commit comments

Comments
 (0)