|
Java example source code file (SnmpMessage.java)
The SnmpMessage.java Java example source code
/*
* Copyright (c) 1998, 2007, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.jmx.snmp;
// java imports
//
import java.util.logging.Level;
import java.util.Vector;
import java.net.InetAddress;
import static com.sun.jmx.defaults.JmxProperties.SNMP_LOGGER;
/**
* Is a partially decoded representation of an SNMP packet.
* <P>
* You will not normally need to use this class unless you decide to
* implement your own {@link com.sun.jmx.snmp.SnmpPduFactory SnmpPduFactory} object.
* <P>
* The <CODE>SnmpMessage class is directly mapped onto the
* <CODE>Message syntax defined in RFC1157 and RFC1902.
* <BLOCKQUOTE>
* <PRE>
* Message ::= SEQUENCE {
* version INTEGER { version(1) }, -- for SNMPv2
* community OCTET STRING, -- community name
* data ANY -- an SNMPv2 PDU
* }
* </PRE>
* </BLOCKQUOTE>
*
* <p>This API is a Sun Microsystems internal API and is subject
* to change without notice.</b>
* @see SnmpPduFactory
* @see SnmpPduPacket
*
*/
public class SnmpMessage extends SnmpMsg implements SnmpDefinitions {
/**
* Community name.
*/
public byte[] community ;
/**
* Encodes this message and puts the result in the specified byte array.
* For internal use only.
*
* @param outputBytes An array to receive the resulting encoding.
*
* @exception ArrayIndexOutOfBoundsException If the result does not fit
* into the specified array.
*/
public int encodeMessage(byte[] outputBytes) throws SnmpTooBigException {
int encodingLength = 0 ;
if (data == null)
throw new IllegalArgumentException("Data field is null") ;
//
// Reminder: BerEncoder does backward encoding !
//
try {
BerEncoder benc = new BerEncoder(outputBytes) ;
benc.openSequence() ;
benc.putAny(data, dataLength) ;
benc.putOctetString((community != null) ? community : new byte[0]) ;
benc.putInteger(version) ;
benc.closeSequence() ;
encodingLength = benc.trim() ;
}
catch(ArrayIndexOutOfBoundsException x) {
throw new SnmpTooBigException() ;
}
return encodingLength ;
}
/**
* Returns the associated request ID.
* @param inputBytes The flat message.
* @return The request ID.
*
* @since 1.5
*/
public int getRequestId(byte[] inputBytes) throws SnmpStatusException {
int requestId = 0;
BerDecoder bdec = null;
BerDecoder bdec2 = null;
byte[] any = null;
try {
bdec = new BerDecoder(inputBytes);
bdec.openSequence();
bdec.fetchInteger();
bdec.fetchOctetString();
any = bdec.fetchAny();
bdec2 = new BerDecoder(any);
int type = bdec2.getTag();
bdec2.openSequence(type);
requestId = bdec2.fetchInteger();
}
catch(BerException x) {
throw new SnmpStatusException("Invalid encoding") ;
}
try {
bdec.closeSequence();
}
catch(BerException x) {
}
try {
bdec2.closeSequence();
}
catch(BerException x) {
}
return requestId;
}
/**
* Decodes the specified bytes and initializes this message.
* For internal use only.
*
* @param inputBytes The bytes to be decoded.
*
* @exception SnmpStatusException If the specified bytes are not a valid encoding.
*/
public void decodeMessage(byte[] inputBytes, int byteCount)
throws SnmpStatusException {
try {
BerDecoder bdec = new BerDecoder(inputBytes/*, byteCount */) ; // FIXME
bdec.openSequence() ;
version = bdec.fetchInteger() ;
community = bdec.fetchOctetString() ;
data = bdec.fetchAny() ;
dataLength = data.length ;
bdec.closeSequence() ;
}
catch(BerException x) {
throw new SnmpStatusException("Invalid encoding") ;
}
}
/**
* Initializes this message with the specified <CODE>pdu.
* <P>
* This method initializes the data field with an array of
* <CODE>maxDataLength bytes. It encodes the
Other Java examples (source code examples)Here is a short list of links related to this Java SnmpMessage.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.