View Javadoc

1   /*******************************************************************************
2    * Copyright (c) 2007, 2014 Massimiliano Ziccardi
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *******************************************************************************/
16  package it.jnrpe.net;
17  
18  import io.netty.channel.ChannelDuplexHandler;
19  import io.netty.channel.ChannelHandlerContext;
20  import io.netty.handler.timeout.IdleState;
21  import io.netty.handler.timeout.IdleStateEvent;
22  import it.jnrpe.JNRPEExecutionContext;
23  import it.jnrpe.events.EventsUtil;
24  import it.jnrpe.events.LogEvent;
25  
26  /**
27   * Idle timeout handler.
28   * 
29   * @author Massimiliano Ziccardi
30   */
31  public final class JNRPEIdleStateHandler extends ChannelDuplexHandler {
32  
33  	/**
34  	 * The JNRPE execution context.
35  	 */
36  	private final JNRPEExecutionContext jnrpeContext;
37  
38  	/**
39  	 * The constructor.
40  	 * 
41  	 * @param ctx
42  	 *            The JNRPE execution context.
43  	 */
44  	public JNRPEIdleStateHandler(final JNRPEExecutionContext ctx) {
45  		this.jnrpeContext = ctx;
46  	}
47  
48  	@Override
49  	public void userEventTriggered(final ChannelHandlerContext ctx,
50  			final Object evt) throws Exception {
51  		if (evt instanceof IdleStateEvent) {
52  			IdleStateEvent e = (IdleStateEvent) evt;
53  			if (e.state() == IdleState.READER_IDLE) {
54  				ctx.close();
55  				EventsUtil.sendEvent(this.jnrpeContext, this, LogEvent.INFO,
56  						"Read Timeout");
57  			} else if (e.state() == IdleState.WRITER_IDLE) {
58  				EventsUtil.sendEvent(jnrpeContext, this, LogEvent.INFO,
59  						"Write Timeout");
60  				ctx.close();
61  			}
62  		}
63  	}
64  }