notify-dpml AT lists.ibiblio.org
Subject: DPML Notify
List archive
svn commit: r1903 - development/laboratory/planet/components/protocol/packetimpl/src/main/net/dpml/protocol/packet/impl
- From: niclas AT netcompartner.com
- To: notify-dpml AT lists.ibiblio.org
- Subject: svn commit: r1903 - development/laboratory/planet/components/protocol/packetimpl/src/main/net/dpml/protocol/packet/impl
- Date: Fri, 25 Feb 2005 16:06:09 +0100
Author: niclas
Date: Fri Feb 25 16:06:08 2005
New Revision: 1903
Added:
development/laboratory/planet/components/protocol/packetimpl/src/main/net/dpml/protocol/packet/impl/BitMappedFlags.java
(contents, props changed)
development/laboratory/planet/components/protocol/packetimpl/src/main/net/dpml/protocol/packet/impl/EnumeratedString.java
(contents, props changed)
Log:
Added two more common field types.
Added:
development/laboratory/planet/components/protocol/packetimpl/src/main/net/dpml/protocol/packet/impl/BitMappedFlags.java
==============================================================================
--- (empty file)
+++
development/laboratory/planet/components/protocol/packetimpl/src/main/net/dpml/protocol/packet/impl/BitMappedFlags.java
Fri Feb 25 16:06:08 2005
@@ -0,0 +1,247 @@
+/*
+ * Copyright 2005, Niclas Hedhman
+ *
+ * Licensed 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 net.dpml.protocol.packet.impl;
+
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.StringTokenizer;
+
+import net.dpml.parameters.ParameterException;
+import net.dpml.parameters.Parameters;
+
+import net.dpml.protocol.packet.FieldType;
+import net.dpml.protocol.packet.IllegalPacketFormatException;
+
+/** Handles translation of "flags" into 8, 16 or 32 bit integer bit maps.
+ * The flag list is provided via the parameters or the
+ * add/removeFlag. For each Parameter the name attribute is the
+ * flag name and the value is the bit number of the flag. When the field
+ * is serialized and deserialized the content string contains a
comma-separated
+ * list of the flag names that should be set, i.e. a true bit.
+ *
+ * @metro.component name="enumerated-string" lifestyle="singleton"
+ * @metro.service type="net.dpml.protocol.packet.FieldType"
+ */
+public class BitMappedFlags
+ implements FieldType
+{
+ private HashMap m_names;
+ private HashMap m_values;
+ private int m_width;
+
+ public BitMappedFlags( Parameters params )
+ throws IllegalArgumentException, ParameterException
+ {
+ String[] names = params.getNames();
+ for( int i = 0; i < names.length; i++ )
+ {
+ String name = names[ i ];
+ if( "field-width".equals( name ) )
+ {
+ m_width = params.getParameterAsInteger( name );
+ if( m_width != 8 && m_width != 16
+ && m_width != 24 && m_width != 32
+ )
+ {
+ throw new ParameterException( "field-width has an
illegal value: " + m_width );
+ }
+ }
+ else
+ {
+ int value = params.getParameterAsInteger( name );
+ addFlag( name, value );
+ }
+ }
+ }
+
+ /** Adds a flag into this field type.
+ * @param name the named string for the value.
+ * @param value the value of the named string.
+ * @exception IllegalArgumentException if the value is less than zero
+ * or more than 255 ( 0 <= x <= 255 ).
+ */
+ public void addFlag( String name, int value )
+ throws IllegalArgumentException
+ {
+ if( value < 0 || value > 31 )
+ throw new IllegalArgumentException( "Data out of range: " + name
+ " = " + value );
+ Integer data = new Integer( value );
+ m_names.put( name, data );
+ m_values.put( data, name );
+ }
+
+ /** Removes an enumeration of a string from this field type.
+ * @param name the named string for the value.
+ * @return the value of the named string that was removed.
+ */
+ public int removeFlag( String name )
+ {
+ Integer value = (Integer) m_names.remove( name );
+ m_values.remove( value );
+ return value.intValue();
+ }
+
+ /** Returns the FieldType name.
+ * This method should return the name of the implementation that
+ * handles the serialization/deserialization of the byte stream.
+ * @return the name in english of the FieldType.
+ */
+ public String getName()
+ {
+ return "BitMappedFlags";
+ }
+
+ /** Returns a human-readable description of the FieldType.
+ * @return a description of the field type in the locale provided.
+ */
+ public String getDescription( Locale locale )
+ {
+ return "TODO: Not implemented yet.";
+ }
+
+ /** Returns the size of the field.
+ * This method returns how many bytes that the field type consists of at
a
+ * minimum. Zero-length is not possible, as the field type would then not
+ * be able to deserialize properly. Furher constraint is that the minimum
+ * size of the field type can not be larger than the maximum field size,
and
+ * if the <code>isVariableSize</code> method returns false, then both the
+ * <code>getMinimumSize</code> and <code>getMaximumSize</code> methods
+ * must return the same number.
+ *
+ * @return the minimum number of bytes that the serialized field can
+ * consist of.
+ */
+ public int getMinimumSize()
+ {
+ return 1;
+ }
+
+ /** Returns the size of the field.
+ * This method returns how many bytes that the field type consists of at
a
+ * maximum. A constraint is that the minimum
+ * size of the field type can not be larger than the maximum field size,
and
+ * if the <code>isVariableSize</code> method returns false, then both the
+ * <code>getMinimumSize</code> and <code>getMaximumSize</code> methods
+ * must return the same number.
+ * @return the minimum number of bytes that the serialized field can
+ * consist of.
+ */
+ public int getMaximumSize()
+ {
+ return 4;
+ }
+
+ /** Query whether this field can be varied in length.
+ * If this method returns false, then both the
+ * <code>getMinimumSize</code> and <code>getMaximumSize</code> methods
+ * must return the same number.
+ * @return true if the field is not a fixed length field.
+ */
+ public boolean isVariableSize()
+ {
+ return true;
+ }
+
+ /** Serialize the content.
+ * @param content the data to be stored in the field.
+ * @return the resulting byte array of the serialized data. This byte
array
+ * must be of the exact size and may not contain any
+ * non-significant bytes at the beginning or end of the array.
+ */
+ public byte[] serialize( String content )
+ throws IllegalPacketFormatException
+ {
+ StringTokenizer st = new StringTokenizer( content, ", ", false );
+ int result = 0;
+ while( st.hasMoreTokens() )
+ {
+ String token = st.nextToken();
+ Object obj = m_names.get( content );
+ if( obj == null )
+ throw new IllegalPacketFormatException( "Flag name not
registered: " + token, this );
+ Integer data = (Integer) obj;
+ int value = data.intValue();
+ value = 1 << value;
+ result = result | value;
+ }
+ byte[] retVal = null;
+ switch( m_width )
+ {
+ case 8:
+ retVal = new byte[1];
+ retVal[ 0 ] = (byte) ( result & 255 );
+ break;
+ case 16:
+ retVal = new byte[2];
+ retVal[ 0 ] = (byte) ( result & 255 );
+ retVal[ 1 ] = (byte) ( result >> 8 & 255 );
+ break;
+ case 24:
+ retVal = new byte[3];
+ retVal[ 0 ] = (byte) ( result & 255 );
+ retVal[ 1 ] = (byte) ( result >> 8 & 255 );
+ retVal[ 2 ] = (byte) ( result >> 16 & 255 );
+ break;
+ case 32:
+ retVal = new byte[4];
+ retVal[ 0 ] = (byte) ( result & 255 );
+ retVal[ 1 ] = (byte) ( result >> 8 & 255 );
+ retVal[ 2 ] = (byte) ( result >> 16 & 255 );
+ retVal[ 3 ] = (byte) ( result >> 24 & 255 );
+ break;
+ }
+ return retVal;
+ }
+
+ /** Deserializes the content from the input stream.
+ * @param input the byte[] that contains the content.
+ * @return the deserialized content from the input stream.
+ */
+ public String deserialize( byte[] input )
+ throws IllegalPacketFormatException
+ {
+ if( input.length > 4 )
+ {
+ throw new IllegalPacketFormatException( "Field contains the
wrong length: " + input.length, this );
+ }
+ int value = 0;
+ for( int i = 0; i < input.length ; i++ )
+ {
+ value = value << 8;
+ value = value | input[ i ];
+ }
+ StringBuffer result = new StringBuffer();
+ for( int i = 0; i < 32 ; i++ )
+ {
+ int flag = value & 1;
+ if( flag == 1 )
+ {
+ if( result.length() > 0 )
+ {
+ result.append( ", " );
+ }
+ Integer lookup = new Integer( i );
+ String name = (String) m_values.get( lookup );
+ result.append( name );
+ }
+ }
+ return result.toString();
+ }
+}
+
Added:
development/laboratory/planet/components/protocol/packetimpl/src/main/net/dpml/protocol/packet/impl/EnumeratedString.java
==============================================================================
--- (empty file)
+++
development/laboratory/planet/components/protocol/packetimpl/src/main/net/dpml/protocol/packet/impl/EnumeratedString.java
Fri Feb 25 16:06:08 2005
@@ -0,0 +1,175 @@
+/*
+ * Copyright 2005, Niclas Hedhman
+ *
+ * Licensed 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 net.dpml.protocol.packet.impl;
+
+import java.util.HashMap;
+import java.util.Locale;
+
+import net.dpml.parameters.ParameterException;
+import net.dpml.parameters.Parameters;
+
+import net.dpml.protocol.packet.FieldType;
+import net.dpml.protocol.packet.IllegalPacketFormatException;
+
+/** Handles translation of strings into 8 bit number sequences.
+ * The enumeration list is provided via the parameters or the
+ * add/removeEnumeration. For each Parameter the name attribute is the
+ * enumerated string and the value is the numeric value to be written to
+ * the field.
+ *
+ * @metro.component name="enumerated-string" lifestyle="singleton"
+ * @metro.service type="net.dpml.protocol.packet.FieldType"
+ */
+public class EnumeratedString
+ implements FieldType
+{
+ private HashMap m_names;
+ private HashMap m_values;
+
+ public EnumeratedString( Parameters params )
+ throws IllegalArgumentException, ParameterException
+ {
+ String[] names = params.getNames();
+ for( int i = 0; i < names.length; i++ )
+ {
+ int value = params.getParameterAsInteger( names[ i ] );
+ addEnumeration( names[ i ], value );
+ }
+ }
+
+ /** Adds an enumeration of a string into this field type.
+ * @param name the named string for the value.
+ * @param value the value of the named string.
+ * @exception IllegalArgumentException if the value is less than zero
+ * or more than 255 ( 0 <= x <= 255 ).
+ */
+ public void addEnumeration( String name, int value )
+ throws IllegalArgumentException
+ {
+ if( value < 0 || value > 255 )
+ throw new IllegalArgumentException( "Data out of range." );
+ Integer data = new Integer( value );
+ m_names.put( name, data );
+ m_values.put( data, name );
+ }
+
+ /** Removes an enumeration of a string from this field type.
+ * @param name the named string for the value.
+ * @return the value of the named string that was removed.
+ */
+ public int removeEnumeration( String name )
+ {
+ Integer value = (Integer) m_names.remove( name );
+ m_values.remove( value );
+ return value.intValue();
+ }
+
+ /** Returns the FieldType name.
+ * This method should return the name of the implementation that
+ * handles the serialization/deserialization of the byte stream.
+ * @return the name in english of the FieldType.
+ */
+ public String getName()
+ {
+ return "EnumeratedString";
+ }
+
+ /** Returns a human-readable description of the FieldType.
+ * @return a description of the field type in the locale provided.
+ */
+ public String getDescription( Locale locale )
+ {
+ return "TODO: Not implemented yet.";
+ }
+
+ /** Returns the size of the field.
+ * This method returns how many bytes that the field type consists of at
a
+ * minimum. Zero-length is not possible, as the field type would then not
+ * be able to deserialize properly. Furher constraint is that the minimum
+ * size of the field type can not be larger than the maximum field size,
and
+ * if the <code>isVariableSize</code> method returns false, then both the
+ * <code>getMinimumSize</code> and <code>getMaximumSize</code> methods
+ * must return the same number.
+ *
+ * @return the minimum number of bytes that the serialized field can
+ * consist of.
+ */
+ public int getMinimumSize()
+ {
+ return 1;
+ }
+
+ /** Returns the size of the field.
+ * This method returns how many bytes that the field type consists of at
a
+ * maximum. A constraint is that the minimum
+ * size of the field type can not be larger than the maximum field size,
and
+ * if the <code>isVariableSize</code> method returns false, then both the
+ * <code>getMinimumSize</code> and <code>getMaximumSize</code> methods
+ * must return the same number.
+ * @return the minimum number of bytes that the serialized field can
+ * consist of.
+ */
+ public int getMaximumSize()
+ {
+ return 1;
+ }
+
+ /** Query whether this field can be varied in length.
+ * If this method returns false, then both the
+ * <code>getMinimumSize</code> and <code>getMaximumSize</code> methods
+ * must return the same number.
+ * @return true if the field is not a fixed length field.
+ */
+ public boolean isVariableSize()
+ {
+ return false;
+ }
+
+ /** Serialize the content.
+ * @param content the data to be stored in the field.
+ * @return the resulting byte array of the serialized data. This byte
array
+ * must be of the exact size and may not contain any
+ * non-significant bytes at the beginning or end of the array.
+ */
+ public byte[] serialize( String content )
+ throws IllegalPacketFormatException
+ {
+ Object obj = m_names.get( content );
+ if( obj == null )
+ throw new IllegalPacketFormatException( "The enumerated string
has not been registered: " + content, this );
+ Integer value = (Integer) obj;
+ byte data = value.byteValue();
+ return new byte[] { data };
+ }
+
+ /** Deserializes the content from the input stream.
+ * @param input the byte[] that contains the content.
+ * @return the deserialized content from the input stream.
+ */
+ public String deserialize( byte[] input )
+ throws IllegalPacketFormatException
+ {
+ if( input.length != 1 )
+ {
+ throw new IllegalPacketFormatException( "Field contains the
wrong length: " + input.length, this );
+ }
+ Integer data = new Integer( input[ 0 ] );
+ return (String) m_values.get( data );
+ }
+}
- svn commit: r1903 - development/laboratory/planet/components/protocol/packetimpl/src/main/net/dpml/protocol/packet/impl, niclas, 02/25/2005
Archive powered by MHonArc 2.6.24.