com.voytechs.jnetstream.codec
Class HeaderImpl

java.lang.Object
  extended by com.voytechs.jnetstream.codec.HeaderImpl
All Implemented Interfaces:
Header, Identity, MutableHeader
Direct Known Subclasses:
TCP

public class HeaderImpl
extends java.lang.Object
implements Header, MutableHeader

Note: The HeaderImpl class * is usually hidden from the user and the default public JNetStream access interfaces.

Default header interface implementation. This implementation allows header objects to be created from NPL definitions and input byte stream. Also 2 levels of properties are maintained. Permanent and temporary. Permanent properties are shared between all header objects of this type. Only a single instance of the property table exists. These can be static properties such as protocol description of RFC reference.

A second property table is maintained that is unique to each header object. The temporary property table has properties that are specific to each instance of the header. Such as 1-line summary of the important fields in the header, length of the header if variable, etc...


Field Summary
 
Fields inherited from interface com.voytechs.jnetstream.codec.Header
ABREVIATION, ACL, ADMINISTRATOR, ANSI, AUTHOR, COMMON, COMPANY, COPYRIGHT, CREATIONDATE, CREATOR, DESCRIPTION, DOC, HIDEHEADERLINE, HTTP, IEEE, IETF, IMTC, ITU, NAME, OMG, ORGANIZATION, OSILAYER, RFC, SHORTDESCRIPTION, SHORTHEADER, STD, SUMMARY, TRADEMARK, W3C
 
Fields inherited from interface com.voytechs.jnetstream.codec.Identity
SHORT_NAME
 
Constructor Summary
HeaderImpl(java.lang.String name, java.util.Map permProperties)
           
 
Method Summary
 void addField(Field field)
          Adds a new protocol field to the packet.
 void addHeader(Header header)
          Adds a new header to the packet.
 void addNote(Note note)
           Notes are special formatting and cary additional information.
 boolean empty()
          Checks to see if the header has any fields defined or not.
 Field getField(int index)
          Returns the indexed field from the packet.
 Field getField(java.lang.String name)
          Returns the named field from the packet.
 int getFieldCount()
           Returns the number of fields within this header.
 Header getHeader(int index)
          Returns the indexed header from the packet.
 Header getHeader(java.lang.String name)
          Returns the named header from the packet.
 int getHeaderCount()
          Returns the size of this header.
 int getHeaderLength()
          Returns the number of bytes of length of this header as decoded from the input byte stream.
 java.lang.String getName()
          Returns the name of this header as it was originally defined.
 java.util.Map getPermProperties()
           
 Primitive getProperty(java.lang.String name)
          Retrieve a named property from either the permanent or temporary properties tables.
 java.lang.Object getPropertyValue(java.lang.String name)
          Retrieve a named property from either the permanent or temporary properties tables.
 java.util.Map getTempProperties()
           
 java.lang.Object getValue(int index)
          Conveniece method to retrieve a value of a field directly.
 java.lang.Object getValue(java.lang.String name)
          Conveniece method to retrieve a value of a field or property directly.
 boolean hasHeader(java.lang.String name)
          Checks to if the specified header exists in the packet.
protected  int longestName()
           
static void main(java.lang.String[] args)
          Test function for HeaderImpl
 void merge(HeaderImpl header)
          A utitility function to merge the fields of one header with another.
 void setHeaderLength(int headerLength)
          Sets the length of this header.
 void setProperty(java.lang.String name, Primitive property)
          Set a property value in the temporary table.
 java.lang.String toString()
           
 OutputBuffer toString(OutputBuffer s)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

HeaderImpl

public HeaderImpl(java.lang.String name,
                  java.util.Map permProperties)
Parameters:
name - Name for this header.
permProperties - External reference to a global permanent properties table what is shared among all headers of this type.
Method Detail

addHeader

public void addHeader(Header header)
Adds a new header to the packet.

Parameters:
header - A reference to a header to be added to the list of header within this PacketImpl object.

hasHeader

public boolean hasHeader(java.lang.String name)
Description copied from interface: Header
Checks to if the specified header exists in the packet. You can also check for the "Data" header using this method, although there is a convience method hasDataHeader()

Specified by:
hasHeader in interface Header
Parameters:
name - name of the header to check for.
Returns:
true if header exists in the packet, otherwise false.

getHeader

public Header getHeader(java.lang.String name)
Returns the named header from the packet.

Specified by:
getHeader in interface Header
Parameters:
name - Name of the header to retrieve from the PacketImpl list of headers it has.
Returns:
Header object that if name has been found in the list of headers.

getHeader

public Header getHeader(int index)
Returns the indexed header from the packet. As the PacketImpl object is built and the stream decoded, headers are added sequentially to the PacketImpl object. The index parameter is used to return the header at that index in the list of headers.

Specified by:
getHeader in interface Header
Parameters:
index - Index of the header to return from the list of headers.
Returns:
The header found at this header index within the packet. Header are added and stored within the packet in the order they were decoded. null is returned if index is out of range.

getHeaderCount

public int getHeaderCount()
Returns the size of this header. Size refers to number of headers decoded. Only headers that this codec understands are decoded. There may be additional undecoded headers of other protocoal still in the stack.

Specified by:
getHeaderCount in interface Header
Returns:
Number of headers in the packet.

setProperty

public void setProperty(java.lang.String name,
                        Primitive property)
Set a property value in the temporary table.

Specified by:
setProperty in interface MutableHeader
Parameters:
name - Name of the property.
property - Value of the property.

empty

public boolean empty()
Checks to see if the header has any fields defined or not.

Returns:
true if atleast one protocol field exists within the header. Othewise false.

getProperty

public Primitive getProperty(java.lang.String name)
Retrieve a named property from either the permanent or temporary properties tables. First the temporary table is looked up and if property not found then a lookup in the permanent table is done. Permanent tables are shared accross all packets while temporary properties table is unique to each packet.

Specified by:
getProperty in interface Identity
Returns:
Value of the property.

getPropertyValue

public java.lang.Object getPropertyValue(java.lang.String name)
Retrieve a named property from either the permanent or temporary properties tables. First the temporary table is looked up and if property not found then a lookup in the permanent table is done. Permanent tables are shared accross all packets while temporary properties table is unique to each packet.

Returns:
Value of the property as taken from the Primitive.

addField

public void addField(Field field)
Adds a new protocol field to the packet.

Specified by:
addField in interface MutableHeader
Parameters:
field - The protocol Field object to add to this header.

addNote

public void addNote(Note note)

Notes are special formatting and cary additional information. They are added to the same list as the fields and thus allow the user to insert text and other information along just like a field does when converted to text output using the toString() method.

Notes are not accessible in any otherway after they have been added. They only show up when the toString() method is called.

NPL equivelent

The NPL equivelent of a note is the print statement. It actually adds a note to the header being defined.

Specified by:
addNote in interface MutableHeader
Parameters:
note - The Note object to add to the list of fields.

getField

public Field getField(java.lang.String name)
Returns the named field from the packet. After which the fields value can be retrieved. It is recommended that the getValue() method is used as its more convenient or Packet.getValue(String headerName, String fieldName) be used instead of de-referencing through header.

Specified by:
getField in interface Header
Parameters:
name - Name of the field to retrieve.
Returns:
The named field is returned. Null if not found.

getField

public Field getField(int index)
Returns the indexed field from the packet. After which the fields value can be retrieved. It is recommended that the getValue() method is used as its more convenient or Packet.getValue(String headerName, String fieldName) be used instead of de-referencing through header.

Specified by:
getField in interface Header
Parameters:
name - Name of the field to retrieve.
Returns:
The named field is returned. Null if not found.

getValue

public java.lang.Object getValue(java.lang.String name)
Conveniece method to retrieve a value of a field or property directly.

Specified by:
getValue in interface Header
Parameters:
name - Field or property name to retrieve the value from.
Returns:
Opaque value of the field or property.
Since:
JNetStream 0.2.2

getValue

public java.lang.Object getValue(int index)
Conveniece method to retrieve a value of a field directly.

Specified by:
getValue in interface Header
Parameters:
index - Index of the field within the header to retrieve the value from.
Returns:
Opaque value of the field.
Since:
JNetStream 0.2.2

getFieldCount

public int getFieldCount()

Returns the number of fields within this header.

Specified by:
getFieldCount in interface Header
Returns:
Number of fields in the packet.

getHeaderLength

public int getHeaderLength()
Returns the number of bytes of length of this header as decoded from the input byte stream.

Specified by:
getHeaderLength in interface Header
Returns:
Number of bytes in the stream read when processing this header.

setHeaderLength

public void setHeaderLength(int headerLength)
Sets the length of this header. This method is usually invoked by the decoder to set the number of bytes that were read when processing this header.

Parameters:
headerLength - Length in bytes of this header.

merge

public void merge(HeaderImpl header)
A utitility function to merge the fields of one header with another. All the fields from the passed in header are added to the field list of this header. In effect merging the contents of both.

Parameters:
header - The header from which to take the fields to merge with this header.

getName

public java.lang.String getName()
Returns the name of this header as it was originally defined.

Specified by:
getName in interface Identity
Returns:
Name of this header.

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

toString

public OutputBuffer toString(OutputBuffer s)

longestName

protected int longestName()

main

public static void main(java.lang.String[] args)
Test function for HeaderImpl

Parameters:
args - command line arguments

getPermProperties

public java.util.Map getPermProperties()

getTempProperties

public java.util.Map getTempProperties()