Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present 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 | */ |
Aaron Kruglikov | 1b72738 | 2016-02-09 16:17:47 -0800 | [diff] [blame] | 16 | package org.onosproject.store.cluster.messaging.impl; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 17 | |
Jordan Halterman | e3813a9 | 2017-07-29 14:10:31 -0700 | [diff] [blame^] | 18 | import java.io.IOException; |
| 19 | |
Aaron Kruglikov | 1b72738 | 2016-02-09 16:17:47 -0800 | [diff] [blame] | 20 | import com.google.common.base.Charsets; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 21 | import io.netty.buffer.ByteBuf; |
| 22 | import io.netty.channel.ChannelHandlerContext; |
| 23 | import io.netty.handler.codec.MessageToByteEncoder; |
Madan Jampani | 2e5f87b | 2015-02-22 10:37:15 -0800 | [diff] [blame] | 24 | import org.onlab.packet.IpAddress; |
| 25 | import org.onlab.packet.IpAddress.Version; |
Madan Jampani | c26eede | 2015-04-16 11:42:16 -0700 | [diff] [blame] | 26 | import org.onosproject.store.cluster.messaging.Endpoint; |
Madan Jampani | 2e5f87b | 2015-02-22 10:37:15 -0800 | [diff] [blame] | 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 30 | /** |
| 31 | * Encode InternalMessage out into a byte buffer. |
| 32 | */ |
Yuta HIGUCHI | c012dda | 2016-08-17 00:43:46 -0700 | [diff] [blame] | 33 | public class MessageEncoder extends MessageToByteEncoder<Object> { |
| 34 | // Effectively MessageToByteEncoder<InternalMessage>, |
| 35 | // had to specify <Object> to avoid Class Loader not being able to find some classes. |
| 36 | |
| 37 | private final Logger log = LoggerFactory.getLogger(getClass()); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 38 | |
Jordan Halterman | e3813a9 | 2017-07-29 14:10:31 -0700 | [diff] [blame^] | 39 | private final Endpoint endpoint; |
Aaron Kruglikov | eb0ae4e | 2015-11-10 19:16:16 -0800 | [diff] [blame] | 40 | private final int preamble; |
Jordan Halterman | e3813a9 | 2017-07-29 14:10:31 -0700 | [diff] [blame^] | 41 | private boolean endpointWritten; |
Aaron Kruglikov | eb0ae4e | 2015-11-10 19:16:16 -0800 | [diff] [blame] | 42 | |
Jordan Halterman | e3813a9 | 2017-07-29 14:10:31 -0700 | [diff] [blame^] | 43 | public MessageEncoder(Endpoint endpoint, int preamble) { |
Aaron Kruglikov | eb0ae4e | 2015-11-10 19:16:16 -0800 | [diff] [blame] | 44 | super(); |
Jordan Halterman | e3813a9 | 2017-07-29 14:10:31 -0700 | [diff] [blame^] | 45 | this.endpoint = endpoint; |
Aaron Kruglikov | eb0ae4e | 2015-11-10 19:16:16 -0800 | [diff] [blame] | 46 | this.preamble = preamble; |
| 47 | } |
| 48 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 49 | @Override |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 50 | protected void encode( |
| 51 | ChannelHandlerContext context, |
Yuta HIGUCHI | c012dda | 2016-08-17 00:43:46 -0700 | [diff] [blame] | 52 | Object rawMessage, |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 53 | ByteBuf out) throws Exception { |
Jordan Halterman | e3813a9 | 2017-07-29 14:10:31 -0700 | [diff] [blame^] | 54 | if (rawMessage instanceof InternalRequest) { |
| 55 | encodeRequest((InternalRequest) rawMessage, out); |
| 56 | } else if (rawMessage instanceof InternalReply) { |
| 57 | encodeReply((InternalReply) rawMessage, out); |
| 58 | } |
| 59 | } |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 60 | |
Jordan Halterman | e3813a9 | 2017-07-29 14:10:31 -0700 | [diff] [blame^] | 61 | private void encodeMessage(InternalMessage message, ByteBuf out) { |
| 62 | // If the endpoint hasn't been written to the channel, write it. |
| 63 | if (!endpointWritten) { |
| 64 | IpAddress senderIp = endpoint.host(); |
| 65 | if (senderIp.version() == Version.INET) { |
| 66 | out.writeByte(0); |
| 67 | } else { |
| 68 | out.writeByte(1); |
| 69 | } |
| 70 | out.writeBytes(senderIp.toOctets()); |
Yuta HIGUCHI | c012dda | 2016-08-17 00:43:46 -0700 | [diff] [blame] | 71 | |
Jordan Halterman | e3813a9 | 2017-07-29 14:10:31 -0700 | [diff] [blame^] | 72 | // write sender port |
| 73 | out.writeInt(endpoint.port()); |
| 74 | |
| 75 | endpointWritten = true; |
| 76 | } |
| 77 | |
| 78 | out.writeByte(message.type().id()); |
Aaron Kruglikov | eb0ae4e | 2015-11-10 19:16:16 -0800 | [diff] [blame] | 79 | out.writeInt(this.preamble); |
| 80 | |
Madan Jampani | 0583387 | 2016-07-12 23:01:39 -0700 | [diff] [blame] | 81 | // write time |
| 82 | out.writeLong(message.time().logicalTime()); |
| 83 | out.writeLong(message.time().logicalCounter()); |
| 84 | |
Madan Jampani | 2e5f87b | 2015-02-22 10:37:15 -0800 | [diff] [blame] | 85 | // write message id |
| 86 | out.writeLong(message.id()); |
Madan Jampani | 938aa43 | 2014-10-04 17:37:23 -0700 | [diff] [blame] | 87 | |
Madan Jampani | 2e5f87b | 2015-02-22 10:37:15 -0800 | [diff] [blame] | 88 | byte[] payload = message.payload(); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 89 | |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 90 | // write payload length |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 91 | out.writeInt(payload.length); |
| 92 | |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 93 | // write payload. |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 94 | out.writeBytes(payload); |
| 95 | } |
Madan Jampani | 29e5dfd | 2014-10-07 17:26:25 -0700 | [diff] [blame] | 96 | |
Jordan Halterman | e3813a9 | 2017-07-29 14:10:31 -0700 | [diff] [blame^] | 97 | private void encodeRequest(InternalRequest request, ByteBuf out) { |
| 98 | encodeMessage(request, out); |
| 99 | |
| 100 | byte[] messageTypeBytes = request.subject().getBytes(Charsets.UTF_8); |
| 101 | |
| 102 | // write length of message type |
| 103 | out.writeShort(messageTypeBytes.length); |
| 104 | |
| 105 | // write message type bytes |
| 106 | out.writeBytes(messageTypeBytes); |
| 107 | |
| 108 | } |
| 109 | |
| 110 | private void encodeReply(InternalReply reply, ByteBuf out) { |
| 111 | encodeMessage(reply, out); |
| 112 | |
| 113 | // write message status value |
| 114 | out.writeByte(reply.status().id()); |
| 115 | } |
| 116 | |
Madan Jampani | 29e5dfd | 2014-10-07 17:26:25 -0700 | [diff] [blame] | 117 | @Override |
| 118 | public void exceptionCaught(ChannelHandlerContext context, Throwable cause) { |
Yuta HIGUCHI | 813fb90 | 2014-11-07 01:49:33 -0800 | [diff] [blame] | 119 | if (cause instanceof IOException) { |
| 120 | log.debug("IOException inside channel handling pipeline.", cause); |
| 121 | } else { |
| 122 | log.error("non-IOException inside channel handling pipeline.", cause); |
| 123 | } |
Madan Jampani | 29e5dfd | 2014-10-07 17:26:25 -0700 | [diff] [blame] | 124 | context.close(); |
| 125 | } |
Yuta HIGUCHI | c012dda | 2016-08-17 00:43:46 -0700 | [diff] [blame] | 126 | |
| 127 | // Effectively same result as one generated by MessageToByteEncoder<InternalMessage> |
| 128 | @Override |
| 129 | public final boolean acceptOutboundMessage(Object msg) throws Exception { |
| 130 | return msg instanceof InternalMessage; |
| 131 | } |
Jordan Halterman | e3813a9 | 2017-07-29 14:10:31 -0700 | [diff] [blame^] | 132 | } |