dk.i1.diameter
Class Message

Object
  extended by dk.i1.diameter.Message

public class Message
extends Object

A Diameter Message. The Message is a container for the MessageHeader and the AVPs. It supports converting to/from the on-the-wire format, and manipulating the AVPs. The class is lean and mean, and does as little checking as possible.

Example of building a Message:

Message msg = new Message();
msg.hdr.application_id = ProtocolConstants.DIAMETER_APPLICATION_ACCOUNTING;
msg.hdr.command_code = ProtocolConstants.DIAMETER_COMMAND_ACCOUNTING;
msg.hdr.setRequest(true);
msg.hdr.setProxiable(true);
//Add AVPs
...
msg.add(new AVP_UTF8String(ProtocolConstants.DI_USER_NAME,"user@example.net"));
msg.add(new AVP_Unsigned64(ProtocolConstants.DI_ACCOUNTING_INPUT_OCTETS,36758373691049));
...
Example of processing a message:
Message msg ...;
for(AVP avp : msg.subset(ProtocolConstants.DI_FRAMED_IP_ADDRESS)) {
    try {
        InetAddress address = new AVP_Address(avp).queryAddress();
        ..do something useful with the address...
    } catch(InvalidAVPLengthException ex) {
        .. handle when peer sends garbage
    } catch(InvalidAddressTypeException ex) {
        .. handle when peer sends garbage
    }
}
AVP avp_reply_message = msg.find(ProtocolConstants.DI_REPLY_MESSAGE);
if(avp!=null) {
    ..do something sensible with reply-message
}


Nested Class Summary
static class Message.decode_status
          The decode status from decode(byte[])
 
Field Summary
 MessageHeader hdr
          The message header
 
Constructor Summary
Message()
          The default constructor.
Message(Message msg)
          Copy-constructor.
Message(MessageHeader header)
          Construct a message with a specific header.
 
Method Summary
 void add(AVP avp)
          Adds an AVP at the end of the AVP list
 void add(int index, AVP avp)
          Inserts an AVP at the specified posistion (0-based)
 Iterable<AVP> avps()
          Returns an Iterable for the AVPs
 void clear()
          Removes all AVPs from the message
 Message.decode_status decode(byte[] b)
          Decode a message from on-the-wire format.
 Message.decode_status decode(byte[] b, int offset, int bytes)
          Decode a message from on-the-wire format.
static int decodeSize(byte[] b, int offset)
          Determine the complete size of the message from a on-the-wire byte array.
 byte[] encode()
          Encode the message to on-the-wire format
 void encode(byte[] b)
          Encode the message in on-the-wire format to the specified byte array.
 int encodeSize()
          Calculate the size of the message in on-the-wire format
 void ensureCapacity(int minCapacity)
          Ensure that ther is room for at least he specified number of AVPs
 AVP find(int code)
          Finds an AVP with the specified code.
 AVP find(int code, int vendor_id)
          Finds an AVP with the specified code/vendor-id.
 AVP get(int index)
          Gets the AVP at the specified index (0-based)
 java.util.Iterator<AVP> iterator()
          Returns an iterator for the AVP list
 java.util.Iterator<AVP> iterator(int code)
          Returns an iterator for the AVPs with the specified code
 java.util.Iterator<AVP> iterator(int code, int vendor_id)
          Returns an iterator for the AVPs with the specified code and vendor id
 void prepareAnswer(Message request)
          Prepare an answer from the specified request header.
 void prepareResponse(Message request)
          Prepare a response the the supplied request.
 void remove(int index)
          Removes the AVP at the specified position (0-based)
 int size()
          Return the number of AVPs in the message
 Iterable<AVP> subset(int code)
          Returns a iterable subset of the AVPs.
 Iterable<AVP> subset(int code, int vendor_id)
          Returns a iterable subset of the AVPs.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

hdr

public MessageHeader hdr
The message header

Constructor Detail

Message

public Message()
The default constructor. The header is initialized to default values and the AVP list will be empty


Message

public Message(MessageHeader header)
Construct a message with a specific header. The AVP list will be empty.

Parameters:
header - The message header to use instead of a default one.

Message

public Message(Message msg)
Copy-constructor. Implements a deep copy.

Method Detail

encodeSize

public int encodeSize()
Calculate the size of the message in on-the-wire format

Returns:
The number of bytes the message will use on-the-wire.

encode

public void encode(byte[] b)
Encode the message in on-the-wire format to the specified byte array.

Parameters:
b - The byte array which must be large enough.

encode

public byte[] encode()
Encode the message to on-the-wire format

Returns:
A on-the-wire message byte array

decodeSize

public static int decodeSize(byte[] b,
                             int offset)
Determine the complete size of the message from a on-the-wire byte array. There must be at least 4 bytes available in the array.

Parameters:
b - The byte array
offset - The offset into the byte array where the message is supposed to start.
Returns:
The size (in bytes) of the message

decode

public Message.decode_status decode(byte[] b)
Decode a message from on-the-wire format. Implemented as return decode(b,0,b.length)

Parameters:
b - A byte array which consists of exacly 1 message. Superfluous bytes are not permitted.
Returns:
The result for the decode operation.

decode

public Message.decode_status decode(byte[] b,
                                    int offset,
                                    int bytes)
Decode a message from on-the-wire format. The message is checked to be in valid format and the VPs to be of the correct length etc. Invalid/reserved bits are not checked.

Parameters:
b - A byte array possibly containing a Diameter message
offset - Offset into the array where decoding should start
bytes - The bytes to try to decode
Returns:
The result for the decode operation.

size

public int size()
Return the number of AVPs in the message


ensureCapacity

public void ensureCapacity(int minCapacity)
Ensure that ther is room for at least he specified number of AVPs


get

public AVP get(int index)
Gets the AVP at the specified index (0-based)


clear

public void clear()
Removes all AVPs from the message


add

public void add(AVP avp)
Adds an AVP at the end of the AVP list


add

public void add(int index,
                AVP avp)
Inserts an AVP at the specified posistion (0-based)


remove

public void remove(int index)
Removes the AVP at the specified position (0-based)


avps

public Iterable<AVP> avps()
Returns an Iterable for the AVPs


iterator

public java.util.Iterator<AVP> iterator()
Returns an iterator for the AVP list


iterator

public java.util.Iterator<AVP> iterator(int code)
Returns an iterator for the AVPs with the specified code


iterator

public java.util.Iterator<AVP> iterator(int code,
                                        int vendor_id)
Returns an iterator for the AVPs with the specified code and vendor id


prepareResponse

public void prepareResponse(Message request)
Prepare a response the the supplied request. Implemented as hdr.prepareResponse(request.hdr);

See Also:
MessageHeader.prepareResponse(MessageHeader)

prepareAnswer

public void prepareAnswer(Message request)
Prepare an answer from the specified request header. This is identical to prepareResponse().

Since:
0.9.3

subset

public Iterable<AVP> subset(int code)
Returns a iterable subset of the AVPs. Implemented as
return subset(code,0);


subset

public Iterable<AVP> subset(int code,
                            int vendor_id)
Returns a iterable subset of the AVPs. This is mainly useful for the new-style foreach statement as in
 for(AVP avp : message.subset(...something...)) {
    //process the AVP
 }
 

Parameters:
code - The AVP code
Returns:
An iterable subset

find

public AVP find(int code)
Finds an AVP with the specified code. Implemented as find(code,0);


find

public AVP find(int code,
                int vendor_id)
Finds an AVP with the specified code/vendor-id. Returns an AVP with the specified code (and vendor-id) if any. If there are no AVPs in this message with the specified code/vendor-id then null is returned. It is unspecified which AVP is returned if there are multiple matches.

Parameters:
code - AVP code
vendor_id - Vendor-ID. Use 0 to specify none.
Returns:
AP with the specified code/vendor-id. Null if not found.