ProtocolHeader.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;
022 
023 import java.nio.ByteBuffer;
024 
025 import org.apache.qpid.transport.network.NetworkDelegate;
026 import org.apache.qpid.transport.network.NetworkEvent;
027 import org.apache.qpid.transport.network.Frame;
028 
029 
030 /**
031  * ProtocolHeader
032  *
033  @author Rafael H. Schloming
034  */
035 
036 public final class ProtocolHeader implements NetworkEvent, ProtocolEvent
037 {
038 
039     private static final byte[] AMQP = {'A''M''Q''P' };
040     private static final byte CLASS = 1;
041 
042     final private byte instance;
043     final private byte major;
044     final private byte minor;
045     private int channel;
046 
047     public ProtocolHeader(byte instance, byte major, byte minor)
048     {
049         this.instance = instance;
050         this.major = major;
051         this.minor = minor;
052     }
053 
054     public ProtocolHeader(int instance, int major, int minor)
055     {
056         this((byteinstance, (bytemajor, (byteminor);
057     }
058 
059     public byte getInstance()
060     {
061         return instance;
062     }
063 
064     public byte getMajor()
065     {
066         return major;
067     }
068 
069     public byte getMinor()
070     {
071         return minor;
072     }
073 
074     public int getChannel()
075     {
076         return channel;
077     }
078 
079     public void setChannel(int channel)
080     {
081         this.channel = channel;
082     }
083 
084     public byte getEncodedTrack()
085     {
086         return Frame.L1;
087     }
088 
089     public ByteBuffer toByteBuffer()
090     {
091         ByteBuffer buf = ByteBuffer.allocate(8);
092         buf.put(AMQP);
093         buf.put(CLASS);
094         buf.put(instance);
095         buf.put(major);
096         buf.put(minor);
097         buf.flip();
098         return buf;
099     }
100 
101     public <C> void delegate(C context, ProtocolDelegate<C> delegate)
102     {
103         delegate.init(context, this);
104     }
105 
106     public void delegate(NetworkDelegate delegate)
107     {
108         delegate.init(this);
109     }
110 
111     public String toString()
112     {
113         return String.format("AMQP.%d %d-%d", instance, major, minor);
114     }
115 
116 }