Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 16 | package org.onlab.netty; |
| 17 | |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 18 | import static com.google.common.base.Preconditions.checkState; |
| 19 | import io.netty.buffer.ByteBuf; |
| 20 | import io.netty.channel.ChannelHandlerContext; |
| 21 | import io.netty.handler.codec.ReplayingDecoder; |
| 22 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 23 | import java.util.List; |
| 24 | |
Madan Jampani | 2e5f87b | 2015-02-22 10:37:15 -0800 | [diff] [blame^] | 25 | import org.onlab.packet.IpAddress; |
| 26 | import org.onlab.packet.IpAddress.Version; |
Madan Jampani | 29e5dfd | 2014-10-07 17:26:25 -0700 | [diff] [blame] | 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
Madan Jampani | 938aa43 | 2014-10-04 17:37:23 -0700 | [diff] [blame] | 30 | /** |
| 31 | * Decoder for inbound messages. |
| 32 | */ |
| 33 | public class MessageDecoder extends ReplayingDecoder<DecoderState> { |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 34 | |
Madan Jampani | 29e5dfd | 2014-10-07 17:26:25 -0700 | [diff] [blame] | 35 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 36 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 37 | private final NettyMessagingService messagingService; |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 38 | |
Madan Jampani | 2e5f87b | 2015-02-22 10:37:15 -0800 | [diff] [blame^] | 39 | private long messageId; |
| 40 | private Version ipVersion; |
| 41 | private IpAddress senderIp; |
| 42 | private int senderPort; |
Madan Jampani | 938aa43 | 2014-10-04 17:37:23 -0700 | [diff] [blame] | 43 | private int contentLength; |
Madan Jampani | 2e5f87b | 2015-02-22 10:37:15 -0800 | [diff] [blame^] | 44 | private long messageType; |
Madan Jampani | 938aa43 | 2014-10-04 17:37:23 -0700 | [diff] [blame] | 45 | |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 46 | public MessageDecoder(NettyMessagingService messagingService) { |
Madan Jampani | 2e5f87b | 2015-02-22 10:37:15 -0800 | [diff] [blame^] | 47 | super(DecoderState.READ_MESSAGE_ID); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 48 | this.messagingService = messagingService; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | @Override |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 52 | protected void decode( |
| 53 | ChannelHandlerContext context, |
| 54 | ByteBuf buffer, |
| 55 | List<Object> out) throws Exception { |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 56 | |
Yuta HIGUCHI | 5e8ceb4 | 2014-11-04 17:22:26 -0800 | [diff] [blame] | 57 | switch (state()) { |
Madan Jampani | 2e5f87b | 2015-02-22 10:37:15 -0800 | [diff] [blame^] | 58 | case READ_MESSAGE_ID: |
| 59 | messageId = buffer.readLong(); |
| 60 | checkpoint(DecoderState.READ_SENDER_IP_VERSION); |
| 61 | case READ_SENDER_IP_VERSION: |
| 62 | ipVersion = buffer.readByte() == 0x0 ? Version.INET : Version.INET6; |
| 63 | checkpoint(DecoderState.READ_SENDER_IP); |
| 64 | case READ_SENDER_IP: |
| 65 | byte[] octects = new byte[IpAddress.byteLength(ipVersion)]; |
| 66 | buffer.readBytes(octects); |
| 67 | senderIp = IpAddress.valueOf(ipVersion, octects); |
| 68 | checkpoint(DecoderState.READ_SENDER_PORT); |
| 69 | case READ_SENDER_PORT: |
| 70 | senderPort = buffer.readInt(); |
| 71 | checkpoint(DecoderState.READ_MESSAGE_TYPE); |
| 72 | case READ_MESSAGE_TYPE: |
| 73 | messageType = buffer.readLong(); |
Madan Jampani | 938aa43 | 2014-10-04 17:37:23 -0700 | [diff] [blame] | 74 | checkpoint(DecoderState.READ_CONTENT_LENGTH); |
| 75 | case READ_CONTENT_LENGTH: |
| 76 | contentLength = buffer.readInt(); |
Madan Jampani | 938aa43 | 2014-10-04 17:37:23 -0700 | [diff] [blame] | 77 | checkpoint(DecoderState.READ_CONTENT); |
| 78 | case READ_CONTENT: |
Madan Jampani | 2e5f87b | 2015-02-22 10:37:15 -0800 | [diff] [blame^] | 79 | byte[] payload = new byte[contentLength]; |
| 80 | buffer.readBytes(payload); |
| 81 | InternalMessage message = new InternalMessage( |
| 82 | messageId, |
| 83 | new Endpoint(senderIp, senderPort), |
| 84 | messageType, |
| 85 | payload); |
Madan Jampani | 938aa43 | 2014-10-04 17:37:23 -0700 | [diff] [blame] | 86 | message.setMessagingService(messagingService); |
| 87 | out.add(message); |
Madan Jampani | 2e5f87b | 2015-02-22 10:37:15 -0800 | [diff] [blame^] | 88 | checkpoint(DecoderState.READ_MESSAGE_ID); |
Madan Jampani | 938aa43 | 2014-10-04 17:37:23 -0700 | [diff] [blame] | 89 | break; |
| 90 | default: |
| 91 | checkState(false, "Must not be here"); |
| 92 | } |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 93 | } |
Madan Jampani | 29e5dfd | 2014-10-07 17:26:25 -0700 | [diff] [blame] | 94 | |
| 95 | @Override |
| 96 | public void exceptionCaught(ChannelHandlerContext context, Throwable cause) { |
| 97 | log.error("Exception inside channel handling pipeline.", cause); |
| 98 | context.close(); |
| 99 | } |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 100 | } |