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.common.IdleStatus;
28  import org.apache.mina.common.IoHandler;
29  import org.apache.mina.common.IoSession;
30  import org.apache.mina.filter.codec.ProtocolCodecFilter;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  
35  /**
36   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37   * @version $Rev: 550363 $, $Date: 2007-06-25 07:44:58 +0200 (Mo, 25 Jun 2007) $
38   */
39  public class NtpProtocolHandler implements IoHandler
40  {
41      /** the log for this class */
42      private static final Logger log = LoggerFactory.getLogger( NtpProtocolHandler.class );
43  
44      private NtpService ntpService = new NtpServiceImpl();
45  
46  
47      public void sessionCreated( IoSession session ) throws Exception
48      {
49          if ( log.isDebugEnabled() )
50          {
51              log.debug( "{} CREATED", session.getRemoteAddress() );
52          }
53  
54          session.getFilterChain().addFirst( "codec", new ProtocolCodecFilter( NtpProtocolCodecFactory.getInstance() ) );
55      }
56  
57  
58      public void sessionOpened( IoSession session )
59      {
60          if ( log.isDebugEnabled() )
61          {
62              log.debug( "{} OPENED", session.getRemoteAddress() );
63          }
64      }
65  
66  
67      public void sessionClosed( IoSession session )
68      {
69          if ( log.isDebugEnabled() )
70          {
71              log.debug( "{} CLOSED", session.getRemoteAddress() );
72          }
73      }
74  
75  
76      public void sessionIdle( IoSession session, IdleStatus status )
77      {
78          if ( log.isDebugEnabled() )
79          {
80              log.debug( "{} IDLE ({})", session.getRemoteAddress(), status );
81          }
82      }
83  
84  
85      public void exceptionCaught( IoSession session, Throwable cause )
86      {
87          log.error( session.getRemoteAddress() + " EXCEPTION", cause );
88          session.close();
89      }
90  
91  
92      public void messageReceived( IoSession session, Object message )
93      {
94          if ( log.isDebugEnabled() )
95          {
96              log.debug( "{} RCVD:  {}", session.getRemoteAddress(), message );
97          }
98  
99          NtpMessage reply = ntpService.getReplyFor( ( NtpMessage ) message );
100 
101         session.write( reply );
102     }
103 
104 
105     public void messageSent( IoSession session, Object message )
106     {
107         if ( log.isDebugEnabled() )
108         {
109             log.debug( "{} SENT:  {}", session.getRemoteAddress(), message );
110         }
111     }
112 }