1/*
2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25package sun.management.jdp;
26
27/**
28 * JdpGenericPacket responsible to provide fields
29 * common for all Jdp packets
30 */
31public abstract class JdpGenericPacket implements JdpPacket {
32
33    /**
34     * JDP protocol magic. Magic allows a reader to quickly select
35     * JDP packets from a bunch of broadcast packets addressed to the same port
36     * and broadcast group. Any packet intended to be parsed by JDP client
37     * has to start from this  magic.
38     */
39    private static final int MAGIC = 0xC0FFEE42;
40
41    /**
42     * Current version of protocol. Any implementation of this protocol has to
43     * conform with the packet structure and the flow described in JEP-168
44     */
45    private static final short PROTOCOL_VERSION = 1;
46
47    /**
48     * Default do-nothing constructor
49     */
50    protected  JdpGenericPacket(){
51        // do nothing
52    }
53
54
55    /**
56     * Validate protocol header magic field
57     *
58     * @param magic - value to validate
59     * @throws JdpException
60     */
61    public static void checkMagic(int magic)
62            throws JdpException {
63        if (magic != MAGIC) {
64            throw new JdpException("Invalid JDP magic header: " + magic);
65        }
66    }
67
68    /**
69     * Validate protocol header version field
70     *
71     * @param version - value to validate
72     * @throws JdpException
73     */
74    public static void checkVersion(short version)
75            throws JdpException {
76
77        if (version > PROTOCOL_VERSION) {
78            throw new JdpException("Unsupported protocol version: " + version);
79        }
80    }
81
82    /**
83     *
84     * @return protocol magic
85     */
86    public static int getMagic() {
87        return MAGIC;
88    }
89
90    /**
91     *
92     * @return current protocol version
93     */
94    public static short getVersion() {
95        return PROTOCOL_VERSION;
96    }
97}
98