edu.uiuc.cs.net.DPRPManager
Class DPRPSocket

java.lang.Object
  |
  +--java.net.DatagramSocket
        |
        +--edu.uiuc.cs.net.DPRPManager.DPRPSocket

public class DPRPSocket
extends java.net.DatagramSocket

This class represents a Socket for sending DPRP Messages. It was adapted from the JDHCP package, available at http://www.dhcp.org/javadhcp/main.html. This package implements a simple DHCP client in Java, and was the inspiration for a lot of the DPRP code base. However, this is the only file that was actually ripped from the JDHCP distribution, and adapted to the DPRP cause. Aside from some major tweaks to receive(), I pretty much just did a search-and-replace job.

Version:
1.0 - September 28th, 2001
Author:
Andy Reitz (areitz@cs.uiuc.edu), Jason Goldschmidt (The DHCP incarnation)
See Also:
DatagramSocket

Field Summary
private  int defaultRcvTIME_OUT
          Default receive socket timeout (defaults to 4 seconds).
private  int defaultSndTIME_OUT
          Default send timeout (defaults to 2 seconds).
private  int defaultSOTIME_OUT
          Default socket timeout (defaults to 4 seconds).
protected static int PACKET_SIZE
          The size of a packet -- defaults to 1500 bytes (Ethernet MTU).
 
Fields inherited from class java.net.DatagramSocket
connected, connectedAddress, connectedPort, factory, impl, implClass
 
Constructor Summary
DPRPSocket()
          Constructor for creating a DPRPSocket on the "next available" local port on the machine.
DPRPSocket(int inPort)
          Constructor for creating DPRPSocket on a specific port on the local machine.
DPRPSocket(int inPort, java.net.InetAddress laddr)
          Constructor, that binds the new socket to a specific IP address & port combination.
 
Method Summary
 int getMTU()
          Returns the set MTU for this socket
 boolean receive(DPRPMessage outMessage)
          Receives a datagram packet containing a DHCP Message into a DHCPMessage object.
 void send(DPRPMessage inMessage)
          Sends a DHCPMessage object to a predifined host.
 void sendError(DPRPMessage errMsg, int errno)
          A method that I crafted, that sends an error message.
 void setMTU(int inSize)
          Sets the Maximum Transfer Unit for the UDP DHCP Packets to be set.
 
Methods inherited from class java.net.DatagramSocket
, close, connect, create, disconnect, getInetAddress, getLocalAddress, getLocalPort, getPort, getReceiveBufferSize, getSendBufferSize, getSoTimeout, receive, send, setDatagramSocketImplFactory, setReceiveBufferSize, setSendBufferSize, setSoTimeout
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

PACKET_SIZE

protected static int PACKET_SIZE
The size of a packet -- defaults to 1500 bytes (Ethernet MTU).

defaultSOTIME_OUT

private int defaultSOTIME_OUT
Default socket timeout (defaults to 4 seconds).

defaultRcvTIME_OUT

private int defaultRcvTIME_OUT
Default receive socket timeout (defaults to 4 seconds). Originally, I was trying to differentiate between send and receive timeouts, but I don't know if the actual code reflects this.

defaultSndTIME_OUT

private int defaultSndTIME_OUT
Default send timeout (defaults to 2 seconds).
Constructor Detail

DPRPSocket

public DPRPSocket()
           throws java.net.SocketException
Constructor for creating a DPRPSocket on the "next available" local port on the machine.
Throws:
java.net.SocketException - If the socket cannot be created/bound.

DPRPSocket

public DPRPSocket(int inPort)
           throws java.net.SocketException
Constructor for creating DPRPSocket on a specific port on the local machine.
Parameters:
inPort - the port for the application to bind.
Throws:
java.net.SocketException - If the socket cannot be created/bound.

DPRPSocket

public DPRPSocket(int inPort,
                  java.net.InetAddress laddr)
           throws java.net.SocketException
Constructor, that binds the new socket to a specific IP address & port combination.
Parameters:
inPort - The port to which we should attempt our bind.
laddr - The local IP address to which we should attempt to bind.
Throws:
java.net.SocketException - If the socket cannot be created/bound.
Method Detail

setMTU

public void setMTU(int inSize)
Sets the Maximum Transfer Unit for the UDP DHCP Packets to be set. Default is 1500, MTU for Ethernet
Parameters:
inSize - integer representing desired MTU

getMTU

public int getMTU()
Returns the set MTU for this socket
Returns:
the Maximum Transfer Unit set for this socket

send

public void send(DPRPMessage inMessage)
          throws java.io.IOException
Sends a DHCPMessage object to a predifined host. In the JDHCP incarnation, this is protected via the synchronized flag, which enforces a monitor lock, and makes all calls to this method ordered. I have lifted this flag, because the way I use this method, only one thread will ever be hitting upon a specific socket at a time.
Parameters:
inMessage - well-formed DHCPMessage to be sent to a server

receive

public boolean receive(DPRPMessage outMessage)
Receives a datagram packet containing a DHCP Message into a DHCPMessage object. Again, I lifted the synchronized restriction here.
Parameters:
outMessage - DHCPMessage object to receive new message into
Returns:
true if message is received, false if timeout occurs.

sendError

public void sendError(DPRPMessage errMsg,
                      int errno)
A method that I crafted, that sends an error message. It's basically a useful short-hand for pumping out error messages in times of trouble.
Parameters:
errMsg - A message to send, with the proper source and destination addresses set.
errno - An error number, to stuff into the message before sending.