Preparing to merge StunStack and StunProvider as there is no good reason for them to live apart.

This commit is contained in:
emcho 2010-01-31 13:02:31 +00:00
parent cc345098db
commit 346e851012
7 changed files with 96 additions and 73 deletions

View File

@ -108,7 +108,7 @@ public class UnknownAttributesAttribute extends Attribute
* @return an iterator over the list of attribute IDs contained by this * @return an iterator over the list of attribute IDs contained by this
* attribute. * attribute.
*/ */
public Iterator getAttributes() public Iterator<Character> getAttributes()
{ {
return unknownAttributes.iterator(); return unknownAttributes.iterator();
@ -205,8 +205,8 @@ public class UnknownAttributesAttribute extends Attribute
* @param attributeValue a binary array containing this attribute's field * @param attributeValue a binary array containing this attribute's field
* values and NOT containing the attribute header. * values and NOT containing the attribute header.
* @param offset the position where attribute values begin (most often * @param offset the position where attribute values begin (most often
* offset is equal to the index of the first byte after * offset is equal to the index of the first byte after
* length) * length)
* @param length the length of the binary array. * @param length the length of the binary array.
* @throws StunException if attrubteValue contains invalid data. * @throws StunException if attrubteValue contains invalid data.
*/ */

View File

@ -1,12 +1,11 @@
/* /*
* Ice4j, the OpenSource Java Solution for NAT and Firewall Traversal. * ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
* Maintained by the SIP Communicator community (http://sip-communicator.org).
* *
* Distributable under LGPL license. * Distributable under LGPL license. See terms of license at gnu.org.
* See terms of license at gnu.org.
*/ */
package org.ice4j.message; package org.ice4j.message;
import java.util.List;
import java.util.logging.*; import java.util.logging.*;
import org.ice4j.*; import org.ice4j.*;
@ -16,11 +15,8 @@ import org.ice4j.attribute.*;
* This class provides factory methods to allow an application to create * This class provides factory methods to allow an application to create
* STUN Messages from a particular implementation. * STUN Messages from a particular implementation.
* *
* <p>Organisation: Louis Pasteur University, Strasbourg, France.</p>
* <p>Network Research Team (http://www-r2.u-strasbg.fr)</p></p>
* @author Emil Ivov * @author Emil Ivov
* @author Sebastien Vincent * @author Sebastien Vincent
* @version 0.1
*/ */
public class MessageFactory public class MessageFactory
@ -48,7 +44,7 @@ public class MessageFactory
} }
/* do not add this by default */ /* do not add this by default */
/* /*
//add a change request attribute //add a change request attribute
ChangeRequestAttribute attribute ChangeRequestAttribute attribute
= AttributeFactory.createChangeRequestAttribute(); = AttributeFactory.createChangeRequestAttribute();
@ -60,9 +56,10 @@ public class MessageFactory
catch (StunException ex) catch (StunException ex)
{ {
//shouldn't happen //shouldn't happen
throw new RuntimeException("Failed to add a change request attribute to a binding request!"); throw new RuntimeException("Failed to add a change request "
+"attribute to a binding request!");
} }
*/ */
return bindingRequest; return bindingRequest;
} }
@ -71,9 +68,9 @@ public class MessageFactory
* attribute with zero change ip and change port flags. * attribute with zero change ip and change port flags.
* It also contains the PRIORITY attribute used for ICE processing * It also contains the PRIORITY attribute used for ICE processing
* *
* @param priority * @param priority the value for the priority attribute
* @return a BindingRequest header with ICE PRIORITY attribute * @return a BindingRequest header with ICE PRIORITY attribute
* @throws StunException * @throws StunException if we have a problem creating the request
*/ */
public static Request createBindingRequest(long priority) public static Request createBindingRequest(long priority)
throws StunException throws StunException
@ -93,13 +90,16 @@ public class MessageFactory
* It contains the PRIORITY, ICE-CONTROLLED or ICE-CONTROLLING attributes * It contains the PRIORITY, ICE-CONTROLLED or ICE-CONTROLLING attributes
* used for ICE processing * used for ICE processing
* *
* @param priority * @param priority the value of the ICE priority attributes
* @param controlling * @param controlling the value of the controlling attribute
* @param tieBreaker * @param tieBreaker the value of the ICE tie breaker attribute
* @return a BindingRequest header with some ICE attributes (PRIORITY, ICE-CONTROLLING / ICE-CONTROLLED) * @return a BindingRequest header with some ICE attributes (PRIORITY,
* @throws StunException * ICE-CONTROLLING / ICE-CONTROLLED)
* @throws StunException if we have a problem creating the request
*/ */
public static Request createBindingRequest(long priority, boolean controlling, long tieBreaker) public static Request createBindingRequest(long priority,
boolean controlling,
long tieBreaker)
throws StunException throws StunException
{ {
Request bindingRequest = createBindingRequest(); Request bindingRequest = createBindingRequest();
@ -111,13 +111,13 @@ public class MessageFactory
if(controlling) if(controlling)
{ {
IceControllingAttribute iceControllingAttribute IceControllingAttribute iceControllingAttribute
= AttributeFactory.createIceControllingAttribute(tieBreaker); = AttributeFactory.createIceControllingAttribute(tieBreaker);
bindingRequest.addAttribute(iceControllingAttribute); bindingRequest.addAttribute(iceControllingAttribute);
} }
else else
{ {
IceControlledAttribute iceControlledAttribute IceControlledAttribute iceControlledAttribute
= AttributeFactory.createIceControlledAttribute(tieBreaker); = AttributeFactory.createIceControlledAttribute(tieBreaker);
bindingRequest.addAttribute(iceControlledAttribute); bindingRequest.addAttribute(iceControlledAttribute);
} }
@ -135,9 +135,10 @@ public class MessageFactory
* headers. * headers.
* @throws StunException ILLEGAL_ARGUMENT * @throws StunException ILLEGAL_ARGUMENT
*/ */
public static Response createBindingResponse(TransportAddress mappedAddress, public static Response createBindingResponse(
TransportAddress sourceAddress, TransportAddress mappedAddress,
TransportAddress changedAddress) TransportAddress sourceAddress,
TransportAddress changedAddress)
throws StunException throws StunException
{ {
Response bindingResponse = new Response(); Response bindingResponse = new Response();
@ -294,9 +295,10 @@ public class MessageFactory
/** /**
* Create an allocate request without attribute. * Create an allocate request without attribute.
*
* @return an allocate request * @return an allocate request
*/ */
public static Request createAllocateRequest() throws StunException public static Request createAllocateRequest()
{ {
Request allocateRequest = new Request(); Request allocateRequest = new Request();
try try
@ -320,7 +322,7 @@ public class MessageFactory
* @param rFlag R flag for the EVEN-PORT * @param rFlag R flag for the EVEN-PORT
* @return an allocation request * @return an allocation request
*/ */
public static Request createAllocateRequest(byte protocol, boolean rFlag) throws StunException public static Request createAllocateRequest(byte protocol, boolean rFlag)
{ {
Request allocateRequest = new Request(); Request allocateRequest = new Request();
@ -336,11 +338,13 @@ public class MessageFactory
} }
/* add a REQUESTED-TRANSPORT attribute */ /* add a REQUESTED-TRANSPORT attribute */
RequestedTransportAttribute reqTransport = AttributeFactory.createRequestedTransportAttribute(protocol); RequestedTransportAttribute reqTransport
= AttributeFactory.createRequestedTransportAttribute(protocol);
allocateRequest.addAttribute(reqTransport); allocateRequest.addAttribute(reqTransport);
/* add EVEN-PORT attribute */ /* add EVEN-PORT attribute */
EvenPortAttribute reqProps = AttributeFactory.createEvenPortAttribute(rFlag); EvenPortAttribute reqProps
= AttributeFactory.createEvenPortAttribute(rFlag);
allocateRequest.addAttribute(reqProps); allocateRequest.addAttribute(reqProps);
} }
catch(StunException ex) catch(StunException ex)
@ -359,10 +363,18 @@ public class MessageFactory
* @param username username value * @param username username value
* @param realm realm value * @param realm realm value
* @param nonce nonce value * @param nonce nonce value
*
* @throws StunException in case we have a problem creating the username
* realm or nonce attributes.
*/ */
public static void addLongTermAuthentifcationAttribute(Request request, byte username[], byte realm[], byte nonce[]) throws StunException public static void addLongTermAuthentifcationAttribute(Request request,
byte username[],
byte realm[],
byte nonce[])
throws StunException
{ {
UsernameAttribute usernameAttr = AttributeFactory.createUsernameAttribute(username); UsernameAttribute usernameAttr
= AttributeFactory.createUsernameAttribute(username);
RealmAttribute realmAttr = AttributeFactory.createRealmAttribute(realm); RealmAttribute realmAttr = AttributeFactory.createRealmAttribute(realm);
NonceAttribute nonceAttr = AttributeFactory.createNonceAttribute(nonce); NonceAttribute nonceAttr = AttributeFactory.createNonceAttribute(nonce);
@ -378,7 +390,7 @@ public class MessageFactory
* @param lifetime lifetime value * @param lifetime lifetime value
* @return refresh request * @return refresh request
*/ */
public static Request createRefreshRequest(int lifetime) throws StunException public static Request createRefreshRequest(int lifetime)
{ {
Request refreshRequest = new Request(); Request refreshRequest = new Request();
@ -387,7 +399,8 @@ public class MessageFactory
refreshRequest.setMessageType(Message.REFRESH_REQUEST); refreshRequest.setMessageType(Message.REFRESH_REQUEST);
/* add a LIFETIME attribute */ /* add a LIFETIME attribute */
LifetimeAttribute lifetimeReq = AttributeFactory.createLifetimeAttribute(lifetime); LifetimeAttribute lifetimeReq
= AttributeFactory.createLifetimeAttribute(lifetime);
refreshRequest.addAttribute(lifetimeReq); refreshRequest.addAttribute(lifetimeReq);
} }
catch(StunException ex) catch(StunException ex)
@ -402,9 +415,12 @@ public class MessageFactory
* Create a ChannelBind request. * Create a ChannelBind request.
* @param channelNumber the channel number * @param channelNumber the channel number
* @param peerAddress the peer address * @param peerAddress the peer address
* @return channelbind request *
* @return channel bind request
*/ */
public static Request createChannelBindRequest(char channelNumber, TransportAddress peerAddress) throws StunException public static Request createChannelBindRequest(
char channelNumber,
TransportAddress peerAddress)
{ {
Request channelBindRequest = new Request(); Request channelBindRequest = new Request();
@ -413,11 +429,13 @@ public class MessageFactory
channelBindRequest.setMessageType(Message.CHANNELBIND_REQUEST); channelBindRequest.setMessageType(Message.CHANNELBIND_REQUEST);
/* add a CHANNEL-NUMBER attribute */ /* add a CHANNEL-NUMBER attribute */
ChannelNumberAttribute channelNumberAttribute = AttributeFactory.createChannelNumberAttribute(channelNumber); ChannelNumberAttribute channelNumberAttribute
= AttributeFactory.createChannelNumberAttribute(channelNumber);
channelBindRequest.addAttribute(channelNumberAttribute); channelBindRequest.addAttribute(channelNumberAttribute);
/* add a XOR-PEER-ADDRESS */ /* add a XOR-PEER-ADDRESS */
XorPeerAddressAttribute peerAddressAttribute = AttributeFactory.createXorPeerAddressAttribute(peerAddress); XorPeerAddressAttribute peerAddressAttribute
= AttributeFactory.createXorPeerAddressAttribute(peerAddress);
channelBindRequest.addAttribute(peerAddressAttribute); channelBindRequest.addAttribute(peerAddressAttribute);
} }
catch(StunException ex) catch(StunException ex)
@ -434,7 +452,8 @@ public class MessageFactory
* @param data data (could be 0 byte) * @param data data (could be 0 byte)
* @return send indication message * @return send indication message
*/ */
public static Indication createSendIndication(TransportAddress peerAddress, byte data[]) public static Indication createSendIndication(TransportAddress peerAddress,
byte data[])
{ {
Indication sendIndication = new Indication(); Indication sendIndication = new Indication();
@ -443,7 +462,8 @@ public class MessageFactory
sendIndication.setMessageType(Message.SEND_INDICATION); sendIndication.setMessageType(Message.SEND_INDICATION);
/* add XOR-PEER-ADDRESS attribute */ /* add XOR-PEER-ADDRESS attribute */
XorPeerAddressAttribute peerAddressAttribute = AttributeFactory.createXorPeerAddressAttribute(peerAddress); XorPeerAddressAttribute peerAddressAttribute
= AttributeFactory.createXorPeerAddressAttribute(peerAddress);
sendIndication.addAttribute(peerAddressAttribute); sendIndication.addAttribute(peerAddressAttribute);
/* add DATA if data */ /* add DATA if data */

View File

@ -16,10 +16,11 @@ import org.ice4j.*;
import org.ice4j.message.*; import org.ice4j.message.*;
/** /**
* Manages NetAccessPoints and MessageProcessor pooling. This class serves as a * Manages <tt>Connector</tt>s and <tt>MessageProcessor</tt> pooling. This class
* layer that masks network primitives and provides equivalent STUN abstractions. * serves as a layer that masks network primitives and provides equivalent STUN
* Instances that operate with the NetAccessManager are only supposed to * abstractions. Instances that operate with the NetAccessManager are only
* understand STUN talk and shouldn't be aware of datagrams sockets, and etc. * supposed to understand STUN talk and shouldn't be aware of datagrams sockets,
* and etc.
* *
* @author Emil Ivov * @author Emil Ivov
*/ */
@ -111,8 +112,8 @@ class NetAccessManager
Connector ap = (Connector)callingThread; Connector ap = (Connector)callingThread;
//make sure nothing's left and notify user //make sure nothing's left and notify user
removeConnector(ap.getListenAddress()); removeSocket(ap.getListenAddress());
logger.log(Level.WARNING, "Removing connector:"+ ap); logger.log(Level.WARNING, "Removing connector:"+ ap, error);
} }
else if( callingThread instanceof MessageProcessor ) else if( callingThread instanceof MessageProcessor )
{ {
@ -164,7 +165,7 @@ class NetAccessManager
* *
* @param address the address of the connector to remove. * @param address the address of the connector to remove.
*/ */
protected void removeConnector(TransportAddress address) protected void removeSocket(TransportAddress address)
{ {
Connector ap = netAccessPoints.remove(address); Connector ap = netAccessPoints.remove(address);
@ -176,7 +177,7 @@ class NetAccessManager
/** /**
* Adjusts the number of concurrently running MessageProcessors. * Adjusts the number of concurrently running MessageProcessors.
* If the number is smaller or bigger than the number of threads * If the number is smaller or bigger than the number of threads
* currentlyrunning, then message processors are created/deleted so that * currently running, then message processors are created/deleted so that
* their count matches the new value. * their count matches the new value.
* *
* @param threadPoolSize the number of MessageProcessors that should be * @param threadPoolSize the number of MessageProcessors that should be

View File

@ -61,7 +61,7 @@ class RawMessage
this.messageLength = messageLength; this.messageLength = messageLength;
System.arraycopy(messageBytes, 0, this.messageBytes, System.arraycopy(messageBytes, 0, this.messageBytes,
0, messageBytes.length); 0, messageBytes.length);
this.localAddress = localAddress;
this.remoteAddress = remoteAddress; this.remoteAddress = remoteAddress;
} }

View File

@ -130,7 +130,7 @@ class StunClientTransaction
/** /**
* The thread that this transaction runs in. * The thread that this transaction runs in.
*/ */
private Thread runningThread = null; private Thread retransmissionsThread = null;
/** /**
* Creates a client transaction * Creates a client transaction
@ -169,7 +169,7 @@ class StunClientTransaction
+"generated an invalid transaction ID"); +"generated an invalid transaction ID");
} }
runningThread = new Thread(this); retransmissionsThread = new Thread(this);
} }
/** /**
@ -182,7 +182,7 @@ class StunClientTransaction
*/ */
public void run() public void run()
{ {
runningThread.setName("CliTran"); retransmissionsThread.setName("CliTran");
nextWaitInterval = originalWaitInterval; nextWaitInterval = originalWaitInterval;
for (retransmissionCounter = 0; for (retransmissionCounter = 0;
@ -239,7 +239,7 @@ class StunClientTransaction
{ {
sendRequest0(); sendRequest0();
runningThread.start(); retransmissionsThread.start();
} }
/** /**

View File

@ -92,6 +92,7 @@ public class StunProvider
clientTransactions.put(clientTransaction.getTransactionID(), clientTransactions.put(clientTransaction.getTransactionID(),
clientTransaction); clientTransaction);
clientTransaction.sendRequest(); clientTransaction.sendRequest();
} }
@ -99,9 +100,9 @@ public class StunProvider
* Sends the specified response message through the specified access point. * Sends the specified response message through the specified access point.
* *
* @param transactionID the id of the transaction to use when sending the * @param transactionID the id of the transaction to use when sending the
* response. Actually we are getting kind of redundant here as we already * response. Actually we are getting kind of redundant here as we already
* have the id in the response object, but I am bringing out as an extra * have the id in the response object, but I am bringing out as an extra
* parameter as the user might otherwise forget to explicitly set it. * parameter as the user might otherwise forget to explicitly set it.
* @param response the message to send. * @param response the message to send.
* @param sendThrough the local address to use when sending the message. * @param sendThrough the local address to use when sending the message.
* @param sendTo the destination of the message. * @param sendTo the destination of the message.
@ -111,7 +112,6 @@ public class StunProvider
* @throws IllegalArgumentException if the apDescriptor references an * @throws IllegalArgumentException if the apDescriptor references an
* access point that had not been installed, * access point that had not been installed,
* @throws StunException if message encoding fails, * @throws StunException if message encoding fails,
*/ */
public void sendResponse(byte[] transactionID, public void sendResponse(byte[] transactionID,
Response response, Response response,
@ -214,6 +214,7 @@ public class StunProvider
/** /**
* Called to notify this provider for an incoming message. * Called to notify this provider for an incoming message.
*
* @param event the event object that contains the new message. * @param event the event object that contains the new message.
*/ */
public void handleMessageEvent(StunMessageEvent event) public void handleMessageEvent(StunMessageEvent event)

View File

@ -10,7 +10,6 @@ import java.io.*;
import java.net.*; import java.net.*;
import org.ice4j.*; import org.ice4j.*;
/** /**
* The entry point to the Stun4J stack. The class is used to start, stop and * The entry point to the Stun4J stack. The class is used to start, stop and
* configure the stack. * configure the stack.
@ -40,8 +39,8 @@ public class StunStack
private StunProvider stunProvider = null; private StunProvider stunProvider = null;
/** /**
* Returns a reference to the singleton StunStack insance. If the stack * Returns a reference to the singleton StunStack instance. If the stack
* had not yet been initialised, a new instance will be created. * had not yet been initialized, a new instance will be created.
* *
* @return a reference to the StunStack. * @return a reference to the StunStack.
*/ */
@ -86,7 +85,9 @@ public class StunStack
*/ */
public void removeSocket(TransportAddress localAddr) public void removeSocket(TransportAddress localAddr)
{ {
netAccessManager.removeConnector(localAddr); netAccessManager.removeSocket(localAddr);
} }