blob: f5a8d2ef25301ca0fd3d4d45318e10fce1241c83 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska24c849c2014-10-27 09:53:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska24c849c2014-10-27 09:53:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska24c849c2014-10-27 09:53:05 -070015 */
Madan Jampaniab6d3112014-10-02 16:30:14 -070016package org.onlab.netty;
17
18import io.netty.buffer.ByteBuf;
Madan Jampaniddf76222014-10-04 23:48:44 -070019import io.netty.channel.ChannelHandler.Sharable;
Madan Jampaniab6d3112014-10-02 16:30:14 -070020import io.netty.channel.ChannelHandlerContext;
21import io.netty.handler.codec.MessageToByteEncoder;
22
Madan Jampani2e5f87b2015-02-22 10:37:15 -080023import java.io.IOException;
24
25import org.onlab.packet.IpAddress;
26import org.onlab.packet.IpAddress.Version;
27import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
Madan Jampani49115e92015-03-14 10:43:33 -070030import com.google.common.base.Charsets;
31
Madan Jampaniab6d3112014-10-02 16:30:14 -070032/**
33 * Encode InternalMessage out into a byte buffer.
34 */
Madan Jampaniddf76222014-10-04 23:48:44 -070035@Sharable
Madan Jampaniab6d3112014-10-02 16:30:14 -070036public class MessageEncoder extends MessageToByteEncoder<InternalMessage> {
37
Madan Jampani29e5dfd2014-10-07 17:26:25 -070038 private final Logger log = LoggerFactory.getLogger(getClass());
39
Madan Jampaniab6d3112014-10-02 16:30:14 -070040 @Override
Madan Jampani86ed0552014-10-03 16:45:42 -070041 protected void encode(
42 ChannelHandlerContext context,
43 InternalMessage message,
Madan Jampaniab6d3112014-10-02 16:30:14 -070044 ByteBuf out) throws Exception {
45
Madan Jampani2e5f87b2015-02-22 10:37:15 -080046 // write message id
47 out.writeLong(message.id());
Madan Jampani938aa432014-10-04 17:37:23 -070048
Madan Jampani2e5f87b2015-02-22 10:37:15 -080049 Endpoint sender = message.sender();
Madan Jampaniab6d3112014-10-02 16:30:14 -070050
Madan Jampani2e5f87b2015-02-22 10:37:15 -080051 IpAddress senderIp = sender.host();
52 if (senderIp.version() == Version.INET) {
53 out.writeByte(0);
54 } else {
55 out.writeByte(1);
56 }
57 out.writeBytes(senderIp.toOctets());
58
59 // write sender port
60 out.writeInt(sender.port());
61
Madan Jampani49115e92015-03-14 10:43:33 -070062 byte[] messageTypeBytes = message.type().getBytes(Charsets.UTF_8);
63
64 // write length of message type
65 out.writeInt(messageTypeBytes.length);
66
67 // write message type bytes
68 out.writeBytes(messageTypeBytes);
Madan Jampani2e5f87b2015-02-22 10:37:15 -080069
70 byte[] payload = message.payload();
Madan Jampaniab6d3112014-10-02 16:30:14 -070071
Madan Jampani86ed0552014-10-03 16:45:42 -070072 // write payload length
Madan Jampaniab6d3112014-10-02 16:30:14 -070073 out.writeInt(payload.length);
74
Madan Jampani86ed0552014-10-03 16:45:42 -070075 // write payload.
Madan Jampaniab6d3112014-10-02 16:30:14 -070076 out.writeBytes(payload);
77 }
Madan Jampani29e5dfd2014-10-07 17:26:25 -070078
79 @Override
80 public void exceptionCaught(ChannelHandlerContext context, Throwable cause) {
Yuta HIGUCHI813fb902014-11-07 01:49:33 -080081 if (cause instanceof IOException) {
82 log.debug("IOException inside channel handling pipeline.", cause);
83 } else {
84 log.error("non-IOException inside channel handling pipeline.", cause);
85 }
Madan Jampani29e5dfd2014-10-07 17:26:25 -070086 context.close();
87 }
Madan Jampaniab6d3112014-10-02 16:30:14 -070088}