From 346e85101211d38d233750cab092b921b80bf7bf Mon Sep 17 00:00:00 2001 From: emcho Date: Sun, 31 Jan 2010 13:02:31 +0000 Subject: [PATCH] Preparing to merge StunStack and StunProvider as there is no good reason for them to live apart. --- .../attribute/UnknownAttributesAttribute.java | 6 +- src/org/ice4j/message/MessageFactory.java | 118 ++++++++++-------- src/org/ice4j/stack/NetAccessManager.java | 17 +-- src/org/ice4j/stack/RawMessage.java | 2 +- .../ice4j/stack/StunClientTransaction.java | 8 +- src/org/ice4j/stack/StunProvider.java | 9 +- src/org/ice4j/stack/StunStack.java | 9 +- 7 files changed, 96 insertions(+), 73 deletions(-) diff --git a/src/org/ice4j/attribute/UnknownAttributesAttribute.java b/src/org/ice4j/attribute/UnknownAttributesAttribute.java index 505979d..e5038d5 100644 --- a/src/org/ice4j/attribute/UnknownAttributesAttribute.java +++ b/src/org/ice4j/attribute/UnknownAttributesAttribute.java @@ -108,7 +108,7 @@ public class UnknownAttributesAttribute extends Attribute * @return an iterator over the list of attribute IDs contained by this * attribute. */ - public Iterator getAttributes() + public Iterator getAttributes() { return unknownAttributes.iterator(); @@ -205,8 +205,8 @@ public class UnknownAttributesAttribute extends Attribute * @param attributeValue a binary array containing this attribute's field * values and NOT containing the attribute header. * @param offset the position where attribute values begin (most often - * offset is equal to the index of the first byte after - * length) + * offset is equal to the index of the first byte after + * length) * @param length the length of the binary array. * @throws StunException if attrubteValue contains invalid data. */ diff --git a/src/org/ice4j/message/MessageFactory.java b/src/org/ice4j/message/MessageFactory.java index b216eb6..a2a0180 100644 --- a/src/org/ice4j/message/MessageFactory.java +++ b/src/org/ice4j/message/MessageFactory.java @@ -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. - * See terms of license at gnu.org. + * Distributable under LGPL license. See terms of license at gnu.org. */ package org.ice4j.message; -import java.util.List; import java.util.logging.*; import org.ice4j.*; @@ -16,11 +15,8 @@ import org.ice4j.attribute.*; * This class provides factory methods to allow an application to create * STUN Messages from a particular implementation. * - *

Organisation: Louis Pasteur University, Strasbourg, France.

- *

Network Research Team (http://www-r2.u-strasbg.fr)

* @author Emil Ivov * @author Sebastien Vincent - * @version 0.1 */ public class MessageFactory @@ -29,8 +25,8 @@ public class MessageFactory Logger.getLogger(MessageFactory.class.getName()); /** - * Creates a default binding request. - * The request DOES NOT contains a ChangeRequest attribute + * Creates a default binding request. + * The request DOES NOT contains a ChangeRequest attribute * with zero change ip and change port flags. * @return a default binding request. */ @@ -48,7 +44,7 @@ public class MessageFactory } /* do not add this by default */ -/* + /* //add a change request attribute ChangeRequestAttribute attribute = AttributeFactory.createChangeRequestAttribute(); @@ -60,9 +56,10 @@ public class MessageFactory catch (StunException ex) { //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; } @@ -71,35 +68,38 @@ public class MessageFactory * attribute with zero change ip and change port flags. * 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 - * @throws StunException + * @throws StunException if we have a problem creating the request */ public static Request createBindingRequest(long priority) throws StunException { Request bindingRequest = createBindingRequest(); - + PriorityAttribute attribute = AttributeFactory.createPriorityAttribute(priority); bindingRequest.addAttribute(attribute); - - return bindingRequest; - } - /** + return bindingRequest; + } + + /** * Creates a default binding request. The request contains a ChangeReqeust * attribute with zero change ip and change port flags. * It contains the PRIORITY, ICE-CONTROLLED or ICE-CONTROLLING attributes * used for ICE processing * - * @param priority - * @param controlling - * @param tieBreaker - * @return a BindingRequest header with some ICE attributes (PRIORITY, ICE-CONTROLLING / ICE-CONTROLLED) - * @throws StunException + * @param priority the value of the ICE priority attributes + * @param controlling the value of the controlling attribute + * @param tieBreaker the value of the ICE tie breaker attribute + * @return a BindingRequest header with some ICE attributes (PRIORITY, + * 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 { Request bindingRequest = createBindingRequest(); @@ -111,13 +111,13 @@ public class MessageFactory if(controlling) { IceControllingAttribute iceControllingAttribute - = AttributeFactory.createIceControllingAttribute(tieBreaker); + = AttributeFactory.createIceControllingAttribute(tieBreaker); bindingRequest.addAttribute(iceControllingAttribute); } else { IceControlledAttribute iceControlledAttribute - = AttributeFactory.createIceControlledAttribute(tieBreaker); + = AttributeFactory.createIceControlledAttribute(tieBreaker); bindingRequest.addAttribute(iceControlledAttribute); } @@ -135,9 +135,10 @@ public class MessageFactory * headers. * @throws StunException ILLEGAL_ARGUMENT */ - public static Response createBindingResponse(TransportAddress mappedAddress, - TransportAddress sourceAddress, - TransportAddress changedAddress) + public static Response createBindingResponse( + TransportAddress mappedAddress, + TransportAddress sourceAddress, + TransportAddress changedAddress) throws StunException { Response bindingResponse = new Response(); @@ -294,9 +295,10 @@ public class MessageFactory /** * Create an allocate request without attribute. + * * @return an allocate request */ - public static Request createAllocateRequest() throws StunException + public static Request createAllocateRequest() { Request allocateRequest = new Request(); try @@ -314,16 +316,16 @@ public class MessageFactory /** * Create an allocate request to allocate an even port. - * Attention this does not have attributes for long-term + * Attention this does not have attributes for long-term * authentication. * @param protocol requested protocol number * @param rFlag R flag for the EVEN-PORT * @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(); - + try { allocateRequest.setMessageType(Message.ALLOCATE_REQUEST); @@ -336,11 +338,13 @@ public class MessageFactory } /* add a REQUESTED-TRANSPORT attribute */ - RequestedTransportAttribute reqTransport = AttributeFactory.createRequestedTransportAttribute(protocol); + RequestedTransportAttribute reqTransport + = AttributeFactory.createRequestedTransportAttribute(protocol); allocateRequest.addAttribute(reqTransport); - + /* add EVEN-PORT attribute */ - EvenPortAttribute reqProps = AttributeFactory.createEvenPortAttribute(rFlag); + EvenPortAttribute reqProps + = AttributeFactory.createEvenPortAttribute(rFlag); allocateRequest.addAttribute(reqProps); } catch(StunException ex) @@ -359,10 +363,18 @@ public class MessageFactory * @param username username value * @param realm realm 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); NonceAttribute nonceAttr = AttributeFactory.createNonceAttribute(nonce); @@ -378,7 +390,7 @@ public class MessageFactory * @param lifetime lifetime value * @return refresh request */ - public static Request createRefreshRequest(int lifetime) throws StunException + public static Request createRefreshRequest(int lifetime) { Request refreshRequest = new Request(); @@ -387,7 +399,8 @@ public class MessageFactory refreshRequest.setMessageType(Message.REFRESH_REQUEST); /* add a LIFETIME attribute */ - LifetimeAttribute lifetimeReq = AttributeFactory.createLifetimeAttribute(lifetime); + LifetimeAttribute lifetimeReq + = AttributeFactory.createLifetimeAttribute(lifetime); refreshRequest.addAttribute(lifetimeReq); } catch(StunException ex) @@ -402,22 +415,27 @@ public class MessageFactory * Create a ChannelBind request. * @param channelNumber the channel number * @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(); try { channelBindRequest.setMessageType(Message.CHANNELBIND_REQUEST); - + /* add a CHANNEL-NUMBER attribute */ - ChannelNumberAttribute channelNumberAttribute = AttributeFactory.createChannelNumberAttribute(channelNumber); + ChannelNumberAttribute channelNumberAttribute + = AttributeFactory.createChannelNumberAttribute(channelNumber); channelBindRequest.addAttribute(channelNumberAttribute); /* add a XOR-PEER-ADDRESS */ - XorPeerAddressAttribute peerAddressAttribute = AttributeFactory.createXorPeerAddressAttribute(peerAddress); + XorPeerAddressAttribute peerAddressAttribute + = AttributeFactory.createXorPeerAddressAttribute(peerAddress); channelBindRequest.addAttribute(peerAddressAttribute); } catch(StunException ex) @@ -434,16 +452,18 @@ public class MessageFactory * @param data data (could be 0 byte) * @return send indication message */ - public static Indication createSendIndication(TransportAddress peerAddress, byte data[]) + public static Indication createSendIndication(TransportAddress peerAddress, + byte data[]) { Indication sendIndication = new Indication(); try { sendIndication.setMessageType(Message.SEND_INDICATION); - + /* add XOR-PEER-ADDRESS attribute */ - XorPeerAddressAttribute peerAddressAttribute = AttributeFactory.createXorPeerAddressAttribute(peerAddress); + XorPeerAddressAttribute peerAddressAttribute + = AttributeFactory.createXorPeerAddressAttribute(peerAddress); sendIndication.addAttribute(peerAddressAttribute); /* add DATA if data */ diff --git a/src/org/ice4j/stack/NetAccessManager.java b/src/org/ice4j/stack/NetAccessManager.java index 676fb48..105f4ec 100644 --- a/src/org/ice4j/stack/NetAccessManager.java +++ b/src/org/ice4j/stack/NetAccessManager.java @@ -16,10 +16,11 @@ import org.ice4j.*; import org.ice4j.message.*; /** - * Manages NetAccessPoints and MessageProcessor pooling. This class serves as a - * layer that masks network primitives and provides equivalent STUN abstractions. - * Instances that operate with the NetAccessManager are only supposed to - * understand STUN talk and shouldn't be aware of datagrams sockets, and etc. + * Manages Connectors and MessageProcessor pooling. This class + * serves as a layer that masks network primitives and provides equivalent STUN + * abstractions. Instances that operate with the NetAccessManager are only + * supposed to understand STUN talk and shouldn't be aware of datagrams sockets, + * and etc. * * @author Emil Ivov */ @@ -111,8 +112,8 @@ class NetAccessManager Connector ap = (Connector)callingThread; //make sure nothing's left and notify user - removeConnector(ap.getListenAddress()); - logger.log(Level.WARNING, "Removing connector:"+ ap); + removeSocket(ap.getListenAddress()); + logger.log(Level.WARNING, "Removing connector:"+ ap, error); } else if( callingThread instanceof MessageProcessor ) { @@ -164,7 +165,7 @@ class NetAccessManager * * @param address the address of the connector to remove. */ - protected void removeConnector(TransportAddress address) + protected void removeSocket(TransportAddress address) { Connector ap = netAccessPoints.remove(address); @@ -176,7 +177,7 @@ class NetAccessManager /** * Adjusts the number of concurrently running MessageProcessors. * 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. * * @param threadPoolSize the number of MessageProcessors that should be diff --git a/src/org/ice4j/stack/RawMessage.java b/src/org/ice4j/stack/RawMessage.java index 6e9405f..8f17309 100644 --- a/src/org/ice4j/stack/RawMessage.java +++ b/src/org/ice4j/stack/RawMessage.java @@ -61,7 +61,7 @@ class RawMessage this.messageLength = messageLength; System.arraycopy(messageBytes, 0, this.messageBytes, 0, messageBytes.length); - + this.localAddress = localAddress; this.remoteAddress = remoteAddress; } diff --git a/src/org/ice4j/stack/StunClientTransaction.java b/src/org/ice4j/stack/StunClientTransaction.java index fcbea7f..c64ea87 100644 --- a/src/org/ice4j/stack/StunClientTransaction.java +++ b/src/org/ice4j/stack/StunClientTransaction.java @@ -130,7 +130,7 @@ class StunClientTransaction /** * The thread that this transaction runs in. */ - private Thread runningThread = null; + private Thread retransmissionsThread = null; /** * Creates a client transaction @@ -169,7 +169,7 @@ class StunClientTransaction +"generated an invalid transaction ID"); } - runningThread = new Thread(this); + retransmissionsThread = new Thread(this); } /** @@ -182,7 +182,7 @@ class StunClientTransaction */ public void run() { - runningThread.setName("CliTran"); + retransmissionsThread.setName("CliTran"); nextWaitInterval = originalWaitInterval; for (retransmissionCounter = 0; @@ -239,7 +239,7 @@ class StunClientTransaction { sendRequest0(); - runningThread.start(); + retransmissionsThread.start(); } /** diff --git a/src/org/ice4j/stack/StunProvider.java b/src/org/ice4j/stack/StunProvider.java index d0154e9..93f16ca 100644 --- a/src/org/ice4j/stack/StunProvider.java +++ b/src/org/ice4j/stack/StunProvider.java @@ -92,6 +92,7 @@ public class StunProvider clientTransactions.put(clientTransaction.getTransactionID(), clientTransaction); + clientTransaction.sendRequest(); } @@ -99,9 +100,9 @@ public class StunProvider * Sends the specified response message through the specified access point. * * @param transactionID the id of the transaction to use when sending the - * 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 - * parameter as the user might otherwise forget to explicitly set it. + * 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 + * parameter as the user might otherwise forget to explicitly set it. * @param response the message to send. * @param sendThrough the local address to use when sending the message. * @param sendTo the destination of the message. @@ -111,7 +112,6 @@ public class StunProvider * @throws IllegalArgumentException if the apDescriptor references an * access point that had not been installed, * @throws StunException if message encoding fails, - */ public void sendResponse(byte[] transactionID, Response response, @@ -214,6 +214,7 @@ public class StunProvider /** * Called to notify this provider for an incoming message. + * * @param event the event object that contains the new message. */ public void handleMessageEvent(StunMessageEvent event) diff --git a/src/org/ice4j/stack/StunStack.java b/src/org/ice4j/stack/StunStack.java index 89d88fe..a20f030 100644 --- a/src/org/ice4j/stack/StunStack.java +++ b/src/org/ice4j/stack/StunStack.java @@ -10,7 +10,6 @@ import java.io.*; import java.net.*; import org.ice4j.*; - /** * The entry point to the Stun4J stack. The class is used to start, stop and * configure the stack. @@ -40,8 +39,8 @@ public class StunStack private StunProvider stunProvider = null; /** - * Returns a reference to the singleton StunStack insance. If the stack - * had not yet been initialised, a new instance will be created. + * Returns a reference to the singleton StunStack instance. If the stack + * had not yet been initialized, a new instance will be created. * * @return a reference to the StunStack. */ @@ -86,7 +85,9 @@ public class StunStack */ public void removeSocket(TransportAddress localAddr) { - netAccessManager.removeConnector(localAddr); + netAccessManager.removeSocket(localAddr); + + }