Frame.java
001 /*
002  *
003  * Licensed to the Apache Software Foundation (ASF) under one
004  * or more contributor license agreements.  See the NOTICE file
005  * distributed with this work for additional information
006  * regarding copyright ownership.  The ASF licenses this file
007  * to you under the Apache License, Version 2.0 (the
008  * "License"); you may not use this file except in compliance
009  * with the License.  You may obtain a copy of the License at
010  *
011  *   http://www.apache.org/licenses/LICENSE-2.0
012  *
013  * Unless required by applicable law or agreed to in writing,
014  * software distributed under the License is distributed on an
015  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016  * KIND, either express or implied.  See the License for the
017  * specific language governing permissions and limitations
018  * under the License.
019  *
020  */
021 package org.apache.qpid.transport.network;
022 
023 import org.apache.qpid.transport.SegmentType;
024 import org.apache.qpid.transport.util.SliceIterator;
025 
026 import java.nio.ByteBuffer;
027 
028 import java.util.ArrayList;
029 import java.util.List;
030 import java.util.Iterator;
031 
032 import static org.apache.qpid.transport.util.Functions.*;
033 
034 
035 /**
036  * Frame
037  *
038  @author Rafael H. Schloming
039  */
040 
041 public final class Frame implements NetworkEvent
042 {
043     public static final int HEADER_SIZE = 12;
044 
045     // XXX: enums?
046     public static final byte L1 = 0;
047     public static final byte L2 = 1;
048     public static final byte L3 = 2;
049     public static final byte L4 = 3;
050 
051     public static final byte RESERVED = 0x0;
052 
053     public static final byte VERSION = 0x0;
054 
055     public static final byte FIRST_SEG = 0x8;
056     public static final byte LAST_SEG = 0x4;
057     public static final byte FIRST_FRAME = 0x2;
058     public static final byte LAST_FRAME = 0x1;
059 
060     final private byte flags;
061     final private SegmentType type;
062     final private byte track;
063     final private int channel;
064     final private ByteBuffer body;
065 
066     public Frame(byte flags, SegmentType type, byte track, int channel,
067                  ByteBuffer body)
068     {
069         this.flags = flags;
070         this.type = type;
071         this.track = track;
072         this.channel = channel;
073         this.body = body;
074     }
075 
076     public ByteBuffer getBody()
077     {
078         return body.slice();
079     }
080 
081     public byte getFlags()
082     {
083         return flags;
084     }
085 
086     public int getChannel()
087     {
088         return channel;
089     }
090 
091     public int getSize()
092     {
093         return body.remaining();
094     }
095 
096     public SegmentType getType()
097     {
098         return type;
099     }
100 
101     public byte getTrack()
102     {
103         return track;
104     }
105 
106     private boolean flag(byte mask)
107     {
108         return (flags & mask!= 0;
109     }
110 
111     public boolean isFirstSegment()
112     {
113         return flag(FIRST_SEG);
114     }
115 
116     public boolean isLastSegment()
117     {
118         return flag(LAST_SEG);
119     }
120 
121     public boolean isFirstFrame()
122     {
123         return flag(FIRST_FRAME);
124     }
125 
126     public boolean isLastFrame()
127     {
128         return flag(LAST_FRAME);
129     }
130 
131     public void delegate(NetworkDelegate delegate)
132     {
133         delegate.frame(this);
134     }
135 
136     public String toString()
137     {
138         StringBuilder str = new StringBuilder();
139 
140         str.append(String.format
141                    ("[%05d %05d %1d %s %d%d%d%d] ", getChannel(), getSize(),
142                     getTrack(), getType(),
143                     isFirstSegment() 0, isLastSegment() 0,
144                     isFirstFrame() 0, isLastFrame() 0));
145 
146         str.append(str(body));
147 
148         return str.toString();
149     }
150 
151 }