View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  
21  package org.apache.directory.server.ntp.protocol;
22  
23  
24  import org.apache.directory.server.ntp.NtpService;
25  import org.apache.directory.server.ntp.messages.NtpMessage;
26  import org.apache.directory.server.ntp.service.NtpServiceImpl;
27  import org.apache.mina.core.service.IoHandlerAdapter;
28  import org.apache.mina.core.session.IoSession;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  
33  /**
34   * The NTP protocol handler. It implements the {@link org.apache.mina.core.service.IoHandler#messageReceived} method,
35   * which returns the NTP reply. The {@link org.apache.mina.core.service.IoHandler#exceptionCaught} is also implemented,
36   * all the other methods are handled by the {@link IoHandlerAdapter} class.<br>
37   * 
38   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
39   */
40  public class NtpProtocolHandler extends IoHandlerAdapter
41  {
42      /** the log for this class */
43      private static final Logger LOG = LoggerFactory.getLogger( NtpProtocolHandler.class );
44  
45      /** The NtpService instance */
46      private NtpService ntpService = new NtpServiceImpl();
47  
48  
49      /**
50       * {@inheritDoc}
51       */
52      @Override
53      public void exceptionCaught( IoSession session, Throwable cause )
54      {
55          LOG.error( session.getRemoteAddress() + " EXCEPTION", cause );
56          session.closeNow();
57      }
58  
59  
60      /**
61       * {@inheritDoc}
62       */
63      @Override
64      public void messageReceived( IoSession session, Object message )
65      {
66          if ( LOG.isDebugEnabled() )
67          {
68              LOG.debug( "{} RCVD:  {}", session.getRemoteAddress(), message );
69          }
70  
71          NtpMessage/directory/server/ntp/messages/NtpMessage.html#NtpMessage">NtpMessage reply = ntpService.getReplyFor( ( NtpMessage ) message );
72  
73          session.write( reply );
74      }
75  }