Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 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 | */ |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 19 | package org.onlab.netty; |
| 20 | |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 21 | import static com.google.common.base.Preconditions.checkState; |
| 22 | import io.netty.buffer.ByteBuf; |
| 23 | import io.netty.channel.ChannelHandlerContext; |
| 24 | import io.netty.handler.codec.ReplayingDecoder; |
| 25 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 26 | import java.util.Arrays; |
| 27 | import java.util.List; |
| 28 | |
Madan Jampani | 29e5dfd | 2014-10-07 17:26:25 -0700 | [diff] [blame] | 29 | import org.slf4j.Logger; |
| 30 | import org.slf4j.LoggerFactory; |
| 31 | |
Madan Jampani | 938aa43 | 2014-10-04 17:37:23 -0700 | [diff] [blame] | 32 | /** |
| 33 | * Decoder for inbound messages. |
| 34 | */ |
| 35 | public class MessageDecoder extends ReplayingDecoder<DecoderState> { |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 36 | |
Madan Jampani | 29e5dfd | 2014-10-07 17:26:25 -0700 | [diff] [blame] | 37 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 38 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 39 | private final NettyMessagingService messagingService; |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 40 | |
| 41 | private static final KryoSerializer SERIALIZER = new KryoSerializer(); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 42 | |
Madan Jampani | 938aa43 | 2014-10-04 17:37:23 -0700 | [diff] [blame] | 43 | private int contentLength; |
| 44 | |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 45 | public MessageDecoder(NettyMessagingService messagingService) { |
Madan Jampani | 938aa43 | 2014-10-04 17:37:23 -0700 | [diff] [blame] | 46 | super(DecoderState.READ_HEADER_VERSION); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 47 | this.messagingService = messagingService; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | @Override |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 51 | protected void decode( |
| 52 | ChannelHandlerContext context, |
| 53 | ByteBuf buffer, |
| 54 | List<Object> out) throws Exception { |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 55 | |
Madan Jampani | 938aa43 | 2014-10-04 17:37:23 -0700 | [diff] [blame] | 56 | switch(state()) { |
| 57 | case READ_HEADER_VERSION: |
| 58 | int headerVersion = buffer.readInt(); |
| 59 | checkState(headerVersion == MessageEncoder.HEADER_VERSION, "Unexpected header version"); |
| 60 | checkpoint(DecoderState.READ_PREAMBLE); |
| 61 | case READ_PREAMBLE: |
| 62 | byte[] preamble = new byte[MessageEncoder.PREAMBLE.length]; |
| 63 | buffer.readBytes(preamble); |
| 64 | checkState(Arrays.equals(MessageEncoder.PREAMBLE, preamble), "Message has wrong preamble"); |
| 65 | checkpoint(DecoderState.READ_CONTENT_LENGTH); |
| 66 | case READ_CONTENT_LENGTH: |
| 67 | contentLength = buffer.readInt(); |
| 68 | checkpoint(DecoderState.READ_SERIALIZER_VERSION); |
| 69 | case READ_SERIALIZER_VERSION: |
| 70 | int serializerVersion = buffer.readInt(); |
| 71 | checkState(serializerVersion == MessageEncoder.SERIALIZER_VERSION, "Unexpected serializer version"); |
| 72 | checkpoint(DecoderState.READ_CONTENT); |
| 73 | case READ_CONTENT: |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 74 | InternalMessage message = SERIALIZER.decode(buffer.readBytes(contentLength).nioBuffer()); |
Madan Jampani | 938aa43 | 2014-10-04 17:37:23 -0700 | [diff] [blame] | 75 | message.setMessagingService(messagingService); |
| 76 | out.add(message); |
| 77 | checkpoint(DecoderState.READ_HEADER_VERSION); |
| 78 | break; |
| 79 | default: |
| 80 | checkState(false, "Must not be here"); |
| 81 | } |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 82 | } |
Madan Jampani | 29e5dfd | 2014-10-07 17:26:25 -0700 | [diff] [blame] | 83 | |
| 84 | @Override |
| 85 | public void exceptionCaught(ChannelHandlerContext context, Throwable cause) { |
| 86 | log.error("Exception inside channel handling pipeline.", cause); |
| 87 | context.close(); |
| 88 | } |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 89 | } |