Author: erodriguez Date: Fri Jan 7 21:53:35 2005 New Revision: 124628 URL: http://svn.apache.org/viewcvs?view=rev&rev=124628 Log: DHCP message value object with mutable companion and typesafe enumerator. Added: incubator/directory/dhcp/trunk/core/src/java/org/apache/dhcp/messages/ incubator/directory/dhcp/trunk/core/src/java/org/apache/dhcp/messages/DhcpMessage.java incubator/directory/dhcp/trunk/core/src/java/org/apache/dhcp/messages/DhcpMessageModifier.java incubator/directory/dhcp/trunk/core/src/java/org/apache/dhcp/messages/MessageType.java
Added: incubator/directory/dhcp/trunk/core/src/java/org/apache/dhcp/messages/DhcpMessage.java Url: http://svn.apache.org/viewcvs/incubator/directory/dhcp/trunk/core/src/java/org/apache/dhcp/messages/DhcpMessage.java?view=auto&rev=124628 ============================================================================== --- (empty file) +++ incubator/directory/dhcp/trunk/core/src/java/org/apache/dhcp/messages/DhcpMessage.java Fri Jan 7 21:53:35 2005 @@ -0,0 +1,207 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * 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 org.apache.dhcp.messages; + +import org.apache.dhcp.options.OptionsField; + + +public class DhcpMessage +{ + private MessageType messageType; + + private byte opCode; + private byte hardwareAddressType; + private byte hardwareAddressLength; + private byte hardwareOptions; + private int transactionId; + private short seconds; + private short flags; + private byte actualClientAddress[] = new byte[4]; + private byte assignedClientAddress[] = new byte[4]; + private byte nextServerAddress[] = new byte[4]; + private byte relayAgentAddress[] = new byte[4]; + private byte clientHardwareAddress[] = new byte[16]; + private byte serverHostname[] = new byte[64]; + private byte bootFileName[] = new byte[128]; + + private OptionsField options = new OptionsField(); + + public DhcpMessage( MessageType messageType, byte opCode, byte hardwareAddressType, + byte hardwareAddressLength, byte hardwareOptions, int transactionId, + short seconds, short flags, byte[] actualClientAddress, + byte[] assignedClientAddress, byte[] nextServerAddress, byte[] relayAgentAddress, + byte[] clientHardwareAddress, byte[] serverHostname, byte[] bootFileName, + OptionsField options ) + { + this.messageType = messageType; + this.opCode = opCode; + this.hardwareAddressType = hardwareAddressType; + this.hardwareAddressLength = hardwareAddressLength; + this.hardwareOptions = hardwareOptions; + this.transactionId = transactionId; + this.seconds = seconds; + this.flags = flags; + this.actualClientAddress = actualClientAddress; + this.assignedClientAddress = assignedClientAddress; + this.nextServerAddress = nextServerAddress; + this.relayAgentAddress = relayAgentAddress; + this.clientHardwareAddress = clientHardwareAddress; + this.serverHostname = serverHostname; + this.bootFileName = bootFileName; + this.options = options; + } + + /** + * Message type. + */ + public MessageType getMessageType() + { + return messageType; + } + + /** + * Message op code / message type. + * 1 = BOOTREQUEST, 2 = BOOTREPLY + */ + public byte getOpCode() + { + return opCode; + } + + /** + * Hardware address type, see ARP section in + * "Assigned Numbers" RFC; e.g., '1' = 10mb ethernet. + */ + public byte getHardwareAddressType() + { + return hardwareAddressType; + } + + /** + * Hardware address length (e.g. '6' for 10mb ethernet). + */ + public byte getHardwareAddressLength() + { + return hardwareAddressLength; + } + + /** + * Client sets to zero, optionally used by relay agents + * when booting via a relay agent. + */ + public byte getHardwareOptions() + { + return hardwareOptions; + } + + /** + * Transaction ID, a random number chosen by the client, + * used by the client and server to associate messages + * and responses between a client and a server. + */ + public int getTransactionId() + { + return transactionId; + } + + /** + * Filled in by client, seconds elapsed since client + * began address acquisition or renewal process. + */ + public short getSeconds() + { + return seconds; + } + + /** + * Flags. + */ + public short getFlags() + { + return flags; + } + + /** + * Client IP address; only filled in if client is in BOUND, + * RENEW or REBINDING state and can respond to ARP requests. + */ + public byte[] getActualClientAddress() + { + return actualClientAddress; + } + + /** + * Get 'your' (client) IP address. + */ + public byte[] getAssignedClientAddress() + { + return assignedClientAddress; + } + + /** + * IP address of next server to use in bootstrap; + * returned in DHCPOFFER, DHCPACK by server. + */ + public byte[] getNextServerAddress() + { + return nextServerAddress; + } + + /** + * Relay agent IP address, used in booting via a relay agent. + */ + public byte[] getRelayAgentAddress() + { + return relayAgentAddress; + } + + /** + * Client hardware address. + */ + public byte[] getClientHardwareAddress() + { + return clientHardwareAddress; + } + + /** + * Optional server host name, null terminated string. + */ + public byte[] getServerHostname() + { + return serverHostname; + } + + /** + * Boot file name, null terminated string; "generic" name or null + * in DHCPDISCOVER, fully qualified directory-path name in DHCPOFFER. + */ + public byte[] getBootFileName() + { + return bootFileName; + } + + /** + * Optional parameters field. See the options + * documents for a list of defined options. + */ + public OptionsField getOptions() + { + return options; + } +} + Added: incubator/directory/dhcp/trunk/core/src/java/org/apache/dhcp/messages/DhcpMessageModifier.java Url: http://svn.apache.org/viewcvs/incubator/directory/dhcp/trunk/core/src/java/org/apache/dhcp/messages/DhcpMessageModifier.java?view=auto&rev=124628 ============================================================================== --- (empty file) +++ incubator/directory/dhcp/trunk/core/src/java/org/apache/dhcp/messages/DhcpMessageModifier.java Fri Jan 7 21:53:35 2005 @@ -0,0 +1,190 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * 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 org.apache.dhcp.messages; + +import org.apache.dhcp.options.OptionsField; + + +public class DhcpMessageModifier +{ + private MessageType messageType; + + private byte opCode; + private byte hardwareAddressType; + private byte hardwareAddressLength; + private byte hardwareOptions; + private int transactionId; + private short seconds; + private short flags; + private byte actualClientAddress[] = new byte[4]; + private byte assignedClientAddress[] = new byte[4]; + private byte nextServerAddress[] = new byte[4]; + private byte relayAgentAddress[] = new byte[4]; + private byte clientHardwareAddress[] = new byte[16]; + private byte serverHostname[] = new byte[64]; + private byte bootFileName[] = new byte[128]; + + private OptionsField options = new OptionsField(); + + public DhcpMessage getDhcpMessage() + { + return new DhcpMessage( messageType, opCode, hardwareAddressType, hardwareAddressLength, + hardwareOptions, transactionId, seconds, flags, actualClientAddress, + assignedClientAddress, nextServerAddress, relayAgentAddress, + clientHardwareAddress, serverHostname, bootFileName, options ); + } + + /** + * Message type. + */ + public void setMessageType( MessageType messageType ) + { + this.messageType = messageType; + } + + /** + * Message op code / message type. + * 1 = BOOTREQUEST, 2 = BOOTREPLY + */ + public void setOpCode( byte opCode ) + { + this.opCode = opCode; + } + + /** + * Hardware address type, see ARP section in + * "Assigned Numbers" RFC; e.g., '1' = 10mb ethernet. + */ + public void setHardwareAddressType( byte hardwareAddressType ) + { + this.hardwareAddressType = hardwareAddressType; + } + + /** + * Hardware address length (e.g. '6' for 10mb ethernet). + */ + public void setHardwareAddressLength( byte hardwareAddressLength ) + { + this.hardwareAddressLength = hardwareAddressLength; + } + + /** + * Set hops field. + * + * @param inHops hops field + */ + public void setHardwareOptions( byte hardwareOptions ) + { + this.hardwareOptions = hardwareOptions; + } + + /** + * Transaction ID, a random number chosen by the client, + * used by the client and server to associate messages + * and responses between a client and a server. + */ + public void setTransactionId( int transactionId ) + { + this.transactionId = transactionId; + } + + /** + * Filled in by client, seconds elapsed since client + * began address acquisition or renewal process. + */ + public void setSeconds( short seconds ) + { + this.seconds = seconds; + } + + /** + * Flags. + */ + public void setFlags( short flags ) + { + this.flags = flags; + } + + /** + * Client IP address; only filled in if client is in BOUND, + * RENEW or REBINDING state and can respond to ARP requests. + */ + public void setActualClientAddress( byte[] actualClientAddress ) + { + this.actualClientAddress = actualClientAddress; + } + + /** + * Get 'your' (client) IP address. + */ + public void setAssignedClientAddress( byte[] assignedClientAddress ) + { + this.assignedClientAddress = assignedClientAddress; + } + + /** + * IP address of next server to use in bootstrap; + * returned in DHCPOFFER, DHCPACK by server. + */ + public void setNextServerAddress( byte[] nextServerAddress ) + { + this.nextServerAddress = nextServerAddress; + } + + /** + * Relay agent IP address, used in booting via a relay agent. + */ + public void setRelayAgentAddress( byte[] relayAgentAddress ) + { + this.relayAgentAddress = relayAgentAddress; + } + + /** + * Client hardware address. + */ + public void setClientHardwareAddress( byte[] clientHardwareAddress ) + { + this.clientHardwareAddress = clientHardwareAddress; + } + + /** + * Optional server host name, null terminated string. + */ + public void setServerHostname( byte[] serverHostname ) + { + this.serverHostname = serverHostname; + } + + /** + * Boot file name, null terminated string; "generic" name or null + * in DHCPDISCOVER, fully qualified directory-path name in DHCPOFFER. + */ + public void setBootFileName( byte[] bootFileName ) + { + this.bootFileName = bootFileName; + } + + /** + * Optional parameters field. See the options + * documents for a list of defined options. + */ + public void setOptions( OptionsField options ) + { + this.options = options; + } +} + Added: incubator/directory/dhcp/trunk/core/src/java/org/apache/dhcp/messages/MessageType.java Url: http://svn.apache.org/viewcvs/incubator/directory/dhcp/trunk/core/src/java/org/apache/dhcp/messages/MessageType.java?view=auto&rev=124628 ============================================================================== --- (empty file) +++ incubator/directory/dhcp/trunk/core/src/java/org/apache/dhcp/messages/MessageType.java Fri Jan 7 21:53:35 2005 @@ -0,0 +1,85 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * 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 org.apache.dhcp.messages; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + + +public final class MessageType implements Comparable +{ + /** + * Enumeration elements are constructed once upon class loading. + * Order of appearance here determines the order of compareTo. + */ + public static final MessageType NULL = new MessageType( 0, "Null" ); + public static final MessageType DHCPDISCOVER = new MessageType( 1, "DHCP Discover" ); + public static final MessageType DHCPOFFER = new MessageType( 2, "DHCP Offer" ); + public static final MessageType DHCPREQUEST = new MessageType( 3, "DHCP Request" ); + public static final MessageType DHCPDECLINE = new MessageType( 4, "DHCP Decline" ); + public static final MessageType DHCPACK = new MessageType( 5, "DHCP Acknowledge" ); + public static final MessageType DHCPNAK = new MessageType( 6, "DHCP Not Acknowledge" ); + public static final MessageType DHCPRELEASE = new MessageType( 7, "DHCP Release" ); + public static final MessageType DHCPINFORM = new MessageType( 8, "DHCP Inform" ); + + public String toString() + { + return name; + } + + public int compareTo( Object that ) + { + return ordinal - ( (MessageType) that ).ordinal; + } + + public static MessageType getTypeByOrdinal( int type ) + { + for ( int ii = 0; ii < values.length; ii++ ) + if ( values[ ii ].ordinal == type ) + return values[ ii ]; + return NULL; + } + + public int getOrdinal() + { + return ordinal; + } + + /// PRIVATE ///// + private final String name; + private final int ordinal; + + /** + * Private constructor prevents construction outside of this class. + */ + private MessageType( int ordinal, String name ) + { + this.ordinal = ordinal; + this.name = name; + } + + /** + * These two lines are all that's necessary to export a List of VALUES. + */ + private static final MessageType[] values = { NULL, DHCPDISCOVER, DHCPOFFER, + DHCPREQUEST, DHCPDECLINE, DHCPACK, DHCPNAK, DHCPRELEASE, DHCPINFORM }; + // VALUES needs to be located here, otherwise illegal forward reference + public static final List VALUES = Collections.unmodifiableList( Arrays.asList( values ) ); +} +
