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 | 49115e9 | 2015-03-14 10:43:33 -0700 | [diff] [blame^] | 30 | import com.google.common.base.Charsets; |
| 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 | |
Madan Jampani | 2e5f87b | 2015-02-22 10:37:15 -0800 | [diff] [blame] | 41 | private long messageId; |
| 42 | private Version ipVersion; |
| 43 | private IpAddress senderIp; |
| 44 | private int senderPort; |
Madan Jampani | 49115e9 | 2015-03-14 10:43:33 -0700 | [diff] [blame^] | 45 | private int messageTypeLength; |
| 46 | private String messageType; |
Madan Jampani | 938aa43 | 2014-10-04 17:37:23 -0700 | [diff] [blame] | 47 | private int contentLength; |
| 48 | |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 49 | public MessageDecoder(NettyMessagingService messagingService) { |
Madan Jampani | 2e5f87b | 2015-02-22 10:37:15 -0800 | [diff] [blame] | 50 | super(DecoderState.READ_MESSAGE_ID); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 51 | this.messagingService = messagingService; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | @Override |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 55 | protected void decode( |
| 56 | ChannelHandlerContext context, |
| 57 | ByteBuf buffer, |
| 58 | List<Object> out) throws Exception { |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 59 | |
Yuta HIGUCHI | 5e8ceb4 | 2014-11-04 17:22:26 -0800 | [diff] [blame] | 60 | switch (state()) { |
Madan Jampani | 2e5f87b | 2015-02-22 10:37:15 -0800 | [diff] [blame] | 61 | case READ_MESSAGE_ID: |
| 62 | messageId = buffer.readLong(); |
| 63 | checkpoint(DecoderState.READ_SENDER_IP_VERSION); |
| 64 | case READ_SENDER_IP_VERSION: |
| 65 | ipVersion = buffer.readByte() == 0x0 ? Version.INET : Version.INET6; |
| 66 | checkpoint(DecoderState.READ_SENDER_IP); |
| 67 | case READ_SENDER_IP: |
| 68 | byte[] octects = new byte[IpAddress.byteLength(ipVersion)]; |
| 69 | buffer.readBytes(octects); |
| 70 | senderIp = IpAddress.valueOf(ipVersion, octects); |
| 71 | checkpoint(DecoderState.READ_SENDER_PORT); |
| 72 | case READ_SENDER_PORT: |
| 73 | senderPort = buffer.readInt(); |
Madan Jampani | 49115e9 | 2015-03-14 10:43:33 -0700 | [diff] [blame^] | 74 | checkpoint(DecoderState.READ_MESSAGE_TYPE_LENGTH); |
| 75 | case READ_MESSAGE_TYPE_LENGTH: |
| 76 | messageTypeLength = buffer.readInt(); |
Madan Jampani | 2e5f87b | 2015-02-22 10:37:15 -0800 | [diff] [blame] | 77 | checkpoint(DecoderState.READ_MESSAGE_TYPE); |
| 78 | case READ_MESSAGE_TYPE: |
Madan Jampani | 49115e9 | 2015-03-14 10:43:33 -0700 | [diff] [blame^] | 79 | byte[] messageTypeBytes = new byte[messageTypeLength]; |
| 80 | buffer.readBytes(messageTypeBytes); |
| 81 | messageType = new String(messageTypeBytes, Charsets.UTF_8); |
Madan Jampani | 938aa43 | 2014-10-04 17:37:23 -0700 | [diff] [blame] | 82 | checkpoint(DecoderState.READ_CONTENT_LENGTH); |
| 83 | case READ_CONTENT_LENGTH: |
| 84 | contentLength = buffer.readInt(); |
Madan Jampani | 938aa43 | 2014-10-04 17:37:23 -0700 | [diff] [blame] | 85 | checkpoint(DecoderState.READ_CONTENT); |
| 86 | case READ_CONTENT: |
Madan Jampani | 2e5f87b | 2015-02-22 10:37:15 -0800 | [diff] [blame] | 87 | byte[] payload = new byte[contentLength]; |
| 88 | buffer.readBytes(payload); |
| 89 | InternalMessage message = new InternalMessage( |
| 90 | messageId, |
| 91 | new Endpoint(senderIp, senderPort), |
| 92 | messageType, |
| 93 | payload); |
Madan Jampani | 938aa43 | 2014-10-04 17:37:23 -0700 | [diff] [blame] | 94 | message.setMessagingService(messagingService); |
| 95 | out.add(message); |
Madan Jampani | 2e5f87b | 2015-02-22 10:37:15 -0800 | [diff] [blame] | 96 | checkpoint(DecoderState.READ_MESSAGE_ID); |
Madan Jampani | 938aa43 | 2014-10-04 17:37:23 -0700 | [diff] [blame] | 97 | break; |
| 98 | default: |
| 99 | checkState(false, "Must not be here"); |
| 100 | } |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 101 | } |
Madan Jampani | 29e5dfd | 2014-10-07 17:26:25 -0700 | [diff] [blame] | 102 | |
| 103 | @Override |
| 104 | public void exceptionCaught(ChannelHandlerContext context, Throwable cause) { |
| 105 | log.error("Exception inside channel handling pipeline.", cause); |
| 106 | context.close(); |
| 107 | } |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 108 | } |