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.buffer.ByteBuf;
19  import io.netty.channel.ChannelHandlerContext;
20  import io.netty.handler.codec.MessageToByteEncoder;
21  
22  /**
23   * This object is inserted inside the NETTY pipeline to create serialize a
24   * {@link JNRPEResponse} object.
25   * 
26   * @author Massimiliano Ziccardi
27   *
28   */
29  public class JNRPEResponseEncoder extends MessageToByteEncoder<JNRPEResponse> {
30  
31  	/**
32  	 * Constructor.
33  	 */
34  	public JNRPEResponseEncoder() {
35  	}
36  
37  	@Override
38  	protected final void encode(final ChannelHandlerContext ctx,
39  			final JNRPEResponse msg, final ByteBuf out) throws Exception {
40  		msg.updateCRC();
41  		out.writeShort(msg.getPacketVersion().intValue());
42  		out.writeShort(msg.getPacketType().intValue());
43  		out.writeInt(msg.getCRC());
44  		out.writeShort(msg.getResultCode());
45  		out.writeBytes(msg.getBuffer());
46  		out.writeBytes(msg.getDummy());
47  	}
48  
49  }