net.tomp2p.natpmp
Class NatPmpDevice

java.lang.Object
  extended by net.tomp2p.natpmp.NatPmpDevice

public class NatPmpDevice
extends Object

This class manages a NAT-PMP device. This class is thread-safe. This manages communication with a NAT-PMP device on the network. There are two types of messages that can be sent to the device. ExternalAddressRequestMessage can be sent to get the external IP of the gateway. MapRequestMessage can be sent to map a port for a certain amount of time. These two messages can be put into the message queue through the enqueueMessage(Message) method. As this class manages a message queue to the NAT-PMP device, it is important to shut it down correctly. Any mapped ports that are no longer desired should be unmapped before shutdown occurs. Refer to #NatPmpDevice(boolean) for details about the shutdown mechanism.

Author:
flszen

Constructor Summary
NatPmpDevice(InetAddress gateway)
          Constructs a new NatPmpDevice.
 
Method Summary
 void clearQueue()
          Clears the queue of messages to send.
 void enqueueMessage(Message message)
          Enqueues a message for sending.
 boolean isShutdown()
          Flag indicates if this NatPmpDevice is shutdown.
 void shutdown()
          Shuts down this NatPmpDevice.
 void waitUntilQueueEmpty()
          Synchronously waits until the queue is empty before returning.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NatPmpDevice

public NatPmpDevice(InetAddress gateway)
             throws NatPmpException
Constructs a new NatPmpDevice.

Parameters:
shutdownHookEnabled - Shutting down existing port mappings is a desired behavior; therefore, this value is required! Refer to #setShutdownHookEnabled(boolean) for details about what value should be provided here and how it alters the object's behavior.
Throws:
NatPmpException - A NatPmpException may be thrown if the local network is not using addresses defined in RFC1918. A NatPmpException may also be thrown if the the network gateway cannot be determined, which may rarely be due to the network not using IPv4. NAT-PMP should only be used on RFC1918 networks using IPv4.
See Also:
#setShutdownHookEnabled(boolean), shutdown()
Method Detail

enqueueMessage

public void enqueueMessage(Message message)
Enqueues a message for sending.

Parameters:
message - The Message to send.
See Also:
clearQueue()

clearQueue

public void clearQueue()
Clears the queue of messages to send. If a message is currently sending, it is not interrupted.

See Also:
enqueueMessage(Message)

waitUntilQueueEmpty

public void waitUntilQueueEmpty()
Synchronously waits until the queue is empty before returning.


shutdown

public void shutdown()
Shuts down this NatPmpDevice. If the shutdown hook is disabled, this method should be called manually at the time port mappings through the NAT-PMP gateway are no longer needed. If the shutdown hook is enabled, this method is called automatically during Java VM shutdown. When this method is called, if the shutdown hook is enabled, it is automatically disabled. It should be noted that when this method is called manually, it blocks until it completes. If it is desired to shutdown asynchronously, the #shutdownAsync(boolean) method should be called.

See Also:
isShutdown(), #setShutdownHookEnabled(boolean), #shutdownAsync(boolean)

isShutdown

public boolean isShutdown()
Flag indicates if this NatPmpDevice is shutdown. This method will block if a shutdown is in progress. If you desire to wait for an asynchronous shutdown to complete, please monitor the returned Thread instead.

Returns:
True if this NatPmpDeviceTest is shutdown, false if it is not.
See Also:
shutdown()


Copyright © 2013. All Rights Reserved.