Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 1 | package org.projectfloodlight.openflow.util; |
| 2 | |
| 3 | import java.util.List; |
| 4 | |
| 5 | import org.jboss.netty.buffer.ChannelBuffer; |
| 6 | import org.projectfloodlight.openflow.exceptions.OFParseError; |
| 7 | import org.projectfloodlight.openflow.protocol.OFMessageReader; |
| 8 | import org.projectfloodlight.openflow.protocol.Writeable; |
Andreas Wundsam | 70aa549 | 2013-10-23 15:26:53 -0700 | [diff] [blame] | 9 | import org.slf4j.Logger; |
| 10 | import org.slf4j.LoggerFactory; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 11 | |
| 12 | import com.google.common.base.Charsets; |
| 13 | import com.google.common.collect.ImmutableList; |
| 14 | import com.google.common.collect.ImmutableList.Builder; |
| 15 | |
| 16 | /** |
| 17 | * Collection of helper functions for reading and writing into ChannelBuffers |
| 18 | * |
| 19 | * @author capveg |
| 20 | */ |
| 21 | |
| 22 | public class ChannelUtils { |
Andreas Wundsam | 70aa549 | 2013-10-23 15:26:53 -0700 | [diff] [blame] | 23 | private static final Logger logger = LoggerFactory.getLogger(ChannelUtils.class); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 24 | public static String readFixedLengthString(ChannelBuffer bb, int length) { |
| 25 | byte[] dst = new byte[length]; |
| 26 | bb.readBytes(dst, 0, length); |
Rob Vaterlaus | 64d06df | 2013-10-11 19:54:26 -0700 | [diff] [blame] | 27 | int validLength = 0; |
| 28 | for (validLength = 0; validLength < length; validLength++) { |
| 29 | if (dst[validLength] == 0) |
| 30 | break; |
| 31 | } |
| 32 | return new String(dst, 0, validLength, Charsets.US_ASCII); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | public static void writeFixedLengthString(ChannelBuffer bb, String string, |
| 36 | int length) { |
| 37 | int l = string.length(); |
| 38 | if (l > length) { |
| 39 | throw new IllegalArgumentException("Error writing string: length=" |
| 40 | + l + " > max Length=" + length); |
| 41 | } |
| 42 | bb.writeBytes(string.getBytes(Charsets.US_ASCII)); |
| 43 | if (l < length) { |
| 44 | bb.writeZero(length - l); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | static public byte[] readBytes(final ChannelBuffer bb, final int length) { |
| 49 | byte byteArray[] = new byte[length]; |
| 50 | bb.readBytes(byteArray); |
| 51 | return byteArray; |
| 52 | } |
| 53 | |
| 54 | static public void writeBytes(final ChannelBuffer bb, |
| 55 | final byte byteArray[]) { |
| 56 | bb.writeBytes(byteArray); |
| 57 | } |
| 58 | |
| 59 | public static <T> List<T> readList(ChannelBuffer bb, int length, OFMessageReader<T> reader) throws OFParseError { |
| 60 | int end = bb.readerIndex() + length; |
| 61 | Builder<T> builder = ImmutableList.<T>builder(); |
Andreas Wundsam | e8802ac | 2013-11-05 11:47:04 -0800 | [diff] [blame] | 62 | if(logger.isTraceEnabled()) |
| 63 | logger.trace("readList(length={}, reader={})", length, reader.getClass()); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 64 | while(bb.readerIndex() < end) { |
Andreas Wundsam | 70aa549 | 2013-10-23 15:26:53 -0700 | [diff] [blame] | 65 | T read = reader.readFrom(bb); |
Andreas Wundsam | e8802ac | 2013-11-05 11:47:04 -0800 | [diff] [blame] | 66 | if(logger.isTraceEnabled()) |
| 67 | logger.trace("readList: read={}, left={}", read, end - bb.readerIndex()); |
Andreas Wundsam | 70aa549 | 2013-10-23 15:26:53 -0700 | [diff] [blame] | 68 | builder.add(read); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 69 | } |
| 70 | if(bb.readerIndex() != end) { |
| 71 | throw new IllegalStateException("Overread length: length="+length + " overread by "+ (bb.readerIndex() - end) + " reader: "+reader); |
| 72 | } |
| 73 | return builder.build(); |
| 74 | } |
| 75 | |
| 76 | public static void writeList(ChannelBuffer bb, List<? extends Writeable> writeables) { |
| 77 | for(Writeable w: writeables) |
| 78 | w.writeTo(bb); |
| 79 | } |
| 80 | } |