summaryrefslogtreecommitdiff
path: root/java/java/common/src/org/apache/qpid/framing/ProtocolInitiation.java
blob: f0d5489527d50c597f7a6fc6e77bea2534fd6cf1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */
package org.apache.qpid.framing;

import org.apache.mina.common.ByteBuffer;
import org.apache.mina.common.IoSession;
import org.apache.mina.filter.codec.ProtocolDecoderOutput;
import org.apache.qpid.AMQException;

public class ProtocolInitiation extends AMQDataBlock implements EncodableAMQDataBlock
{
    public char[] header = new char[]{'A','M','Q','P'};
    // TODO: generate these constants automatically from the xml protocol spec file

    private static byte CURRENT_PROTOCOL_CLASS = 1;
    private static final int CURRENT_PROTOCOL_INSTANCE = 1;

    public byte protocolClass = CURRENT_PROTOCOL_CLASS;
    public byte protocolInstance = CURRENT_PROTOCOL_INSTANCE;
    public byte protocolMajor;
    public byte protocolMinor;

//    public ProtocolInitiation() {}

    public ProtocolInitiation(byte major, byte minor)
    {
        protocolMajor = major;
        protocolMinor = minor;
    }

    public long getSize()
    {
        return 4 + 1 + 1 + 1 + 1;
    }

    public void writePayload(ByteBuffer buffer)
    {
        for (int i = 0; i < header.length; i++)
        {
            buffer.put((byte) header[i]);
        }
        buffer.put(protocolClass);
        buffer.put(protocolInstance);
        buffer.put(protocolMajor);
        buffer.put(protocolMinor);
    }

    public void populateFromBuffer(ByteBuffer buffer) throws AMQException
    {
        throw new AMQException("Method not implemented");
    }

    public boolean equals(Object o)
    {
        if (!(o instanceof ProtocolInitiation))
        {
            return false;
        }

        ProtocolInitiation pi = (ProtocolInitiation) o;
        if (pi.header == null)
        {
            return false;
        }

        if (header.length != pi.header.length)
        {
            return false;
        }

        for (int i = 0; i < header.length; i++)
        {
            if (header[i] != pi.header[i])
            {
                return false;
            }
        }

        return (protocolClass == pi.protocolClass &&
                protocolInstance == pi.protocolInstance &&
                protocolMajor == pi.protocolMajor &&
                protocolMinor == pi.protocolMinor);
    }

    public static class Decoder //implements MessageDecoder
    {
        /**
         *
         * @param session
         * @param in
         * @return true if we have enough data to decode the PI frame fully, false if more
         * data is required
         */
        public boolean decodable(IoSession session, ByteBuffer in)
        {
            return (in.remaining() >= 8);
        }

        public void decode(IoSession session, ByteBuffer in, ProtocolDecoderOutput out)
            throws Exception
        {
            byte[] theHeader = new byte[4];
            in.get(theHeader);
            ProtocolInitiation pi = new ProtocolInitiation((byte)0, (byte)0);
            pi.header = new char[]{(char) theHeader[0],(char) theHeader[CURRENT_PROTOCOL_INSTANCE],(char) theHeader[2], (char) theHeader[3]};
            String stringHeader = new String(pi.header);
            if (!"AMQP".equals(stringHeader))
            {
                throw new AMQProtocolHeaderException("Invalid protocol header - read " + stringHeader);
            }
            pi.protocolClass = in.get();
            pi.protocolInstance = in.get();
            pi.protocolMajor = in.get();
            pi.protocolMinor = in.get();
            out.write(pi);
        }
    }

    public void checkVersion(ProtocolVersionList pvl) throws AMQException
    {
        if (protocolClass != CURRENT_PROTOCOL_CLASS)
        {
            throw new AMQProtocolClassException("Protocol class " + CURRENT_PROTOCOL_CLASS + " was expected; received " +
                    protocolClass);
        }
        if (protocolInstance != CURRENT_PROTOCOL_INSTANCE)
        {
            throw new AMQProtocolInstanceException("Protocol instance " + CURRENT_PROTOCOL_INSTANCE + " was expected; received " +
                    protocolInstance);
        }
        /*
        if (protocolMajor != CURRENT_PROTOCOL_VERSION_MAJOR)
        {
            throw new AMQProtocolVersionException("Protocol major version " + CURRENT_PROTOCOL_VERSION_MAJOR +
                    " was expected; received " + protocolMajor);
        }
        if (protocolMinor != CURRENT_PROTOCOL_VERSION_MINOR)
        {
            throw new AMQProtocolVersionException("Protocol minor version " + CURRENT_PROTOCOL_VERSION_MINOR +
                    " was expected; received " + protocolMinor);
        }
        */
        
        /* Look through list of available protocol versions */
        boolean found = false;
        for (int i=0; i<pvl.pv.length; i++)
        {
            if (pvl.pv[i][pvl.PROTOCOL_MAJOR] == protocolMajor &&
                pvl.pv[i][pvl.PROTOCOL_MINOR] == protocolMinor)
            {
                found = true;
            }
        }
        if (!found)
        {
            // TODO: add list of available versions in list to msg...
            throw new AMQProtocolVersionException("Protocol version " +
                protocolMajor + "." +  protocolMinor + " not found in protocol version list.");
        }
    }
}