1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.ntp;
21
22
23 import org.apache.directory.server.ntp.protocol.NtpProtocolHandler;
24 import org.apache.directory.server.protocol.shared.AbstractProtocolService;
25 import org.apache.mina.transport.socket.nio.DatagramAcceptorConfig;
26 import org.apache.mina.transport.socket.nio.SocketAcceptorConfig;
27
28 import java.io.IOException;
29 import java.net.InetSocketAddress;
30
31
32
33
34
35
36
37
38
39 public class NtpServer extends AbstractProtocolService
40 {
41
42
43
44 private static final int IP_PORT_DEFAULT = 123;
45
46
47
48
49 private static final String SERVICE_PID_DEFAULT = "org.apache.directory.server.ntp";
50
51
52
53
54 private static final String SERVICE_NAME_DEFAULT = "ApacheDS NTP Service";
55
56
57
58
59
60 public NtpServer()
61 {
62 super.setIpPort( IP_PORT_DEFAULT );
63 super.setServiceId( SERVICE_PID_DEFAULT );
64 super.setServiceName( SERVICE_NAME_DEFAULT );
65 }
66
67
68
69
70
71 public void start() throws IOException
72 {
73
74 if ( getDatagramAcceptor() != null )
75 {
76 DatagramAcceptorConfig udpConfig = new DatagramAcceptorConfig();
77 getDatagramAcceptor().bind( new InetSocketAddress( getIpPort() ), new NtpProtocolHandler(), udpConfig );
78 }
79
80 if ( getSocketAcceptor() != null )
81 {
82 SocketAcceptorConfig tcpConfig = new SocketAcceptorConfig();
83 tcpConfig.setDisconnectOnUnbind( false );
84 tcpConfig.setReuseAddress( true );
85 getSocketAcceptor().bind( new InetSocketAddress( getIpPort() ), new NtpProtocolHandler(), tcpConfig );
86 }
87 }
88
89
90 public void stop()
91 {
92 if ( getDatagramAcceptor() != null )
93 {
94 getDatagramAcceptor().unbind( new InetSocketAddress( getIpPort() ));
95 }
96
97 if ( getSocketAcceptor() != null )
98 {
99 getSocketAcceptor().unbind( new InetSocketAddress( getIpPort() ));
100 }
101 }
102 }