Changeset 337

Show
Ignore:
Timestamp:
03/21/06 00:42:51 (3 years ago)
Author:
alban
Message:

[Bug 69] added STUN support to determinate the right public reference

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/.classpath

    r333 r337  
    2828        <classpathentry kind="lib" path="track/commons-logging/releases/1.1.0/commons-logging-1.1-RC5.jar"/> 
    2929        <classpathentry kind="lib" path="track/log4j/releases/1.2.13/dist/lib/log4j-1.2.13.jar"/> 
     30        <classpathentry sourcepath="track/jstun/releases/0.5.9.2/src" kind="lib" path="track/jstun/releases/0.5.9.2/jstun-0.5.9.2.jar"/> 
    3031        <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> 
    3132        <classpathentry kind="output" path=".eclipse"/> 
  • trunk/build.xml

    r309 r337  
    198198                        </library> 
    199199 
     200                        <library id="lib.jstun"> 
     201                                <lib-entry groupid="jstun" version="0.5.9.2"/> 
     202                        </library> 
     203 
    200204                        <library id="core"> 
    201205                                <lib-entry groupid="commons-collections" version="3.1" /> 
     
    210214                                <lib-entry groupid="concurrent" version="kolaka" /> 
    211215                                <library refid="lib.mina" /> 
     216                                <library refid="lib.jstun" /> 
    212217                        </library> 
    213218 
  • trunk/source/org/kolaka/freecast/peer/AutomaticPeerReferenceFactory.java

    r71 r337  
    2424 
    2525import java.io.IOException; 
    26 import java.net.Inet4Address; 
    2726import java.net.InetAddress; 
    28 import java.net.NetworkInterface; 
    29 import java.util.Enumeration; 
    30 import java.util.HashSet; 
    31 import java.util.Iterator; 
    32 import java.util.Set; 
    3327 
    34 import org.apache.commons.collections.CollectionUtils; 
    35 import org.apache.commons.collections.EnumerationUtils; 
    36 import org.apache.commons.collections.Predicate; 
    37 import org.apache.commons.collections.functors.InstanceofPredicate; 
    38 import org.apache.commons.collections.functors.NotPredicate; 
    3928import org.apache.commons.lang.Validate; 
    4029import org.kolaka.freecast.net.PublicAddressResolver; 
     
    4534public class AutomaticPeerReferenceFactory implements PeerReferenceFactory { 
    4635 
    47         private int defaultPort = UNDEFINED
     36        private final int port
    4837 
    4938        private PublicAddressResolver publicAddressResolver; 
    5039 
    51         private static final int UNDEFINED = -1; 
    52  
    53         public void setDefaultPort(int defaultPort) { 
    54                 Validate.isTrue(InetPeerReference.validatePort(defaultPort), 
    55                                 "Invalid port: " + defaultPort); 
    56                 this.defaultPort = defaultPort; 
     40        public AutomaticPeerReferenceFactory(int port) { 
     41                Validate.isTrue(InetPeerReference.validatePort(port)); 
     42                this.port = port; 
     43                this.localFactory = new LocalPeerReferenceFactory(port); 
    5744        } 
    5845 
    59         public void setPublicAddressResolver( 
    60                         PublicAddressResolver publicAddressResolver) { 
    61                 Validate.notNull(publicAddressResolver, 
    62                                 "No specified PublicAddressResolver"); 
     46        public void setPublicAddressResolver(PublicAddressResolver publicAddressResolver) { 
     47                Validate.notNull(publicAddressResolver); 
    6348                this.publicAddressResolver = publicAddressResolver; 
    6449        } 
     50         
     51        private final LocalPeerReferenceFactory localFactory;  
    6552 
    6653        public PeerReference create() throws PeerReferenceFactoryException { 
    67                 if (defaultPort == UNDEFINED) { 
    68                         throw new IllegalStateException("No defined default port"); 
    69                 } 
    70  
    71                 Set addresses; 
    72                 try { 
    73                         addresses = getLocalInetAddress(); 
    74                 } catch (IOException e) { 
    75                         throw new PeerReferenceFactoryException( 
    76                                         "Can't determinate the local inet addresses", e); 
    77                 } 
     54                InetAddress publicAddress; 
    7855 
    7956                try { 
    80                         addresses.add(getPublicAddress()); 
     57                        publicAddress = getPublicAddress(); 
    8158                } catch (IOException e) { 
    8259                        throw new PeerReferenceFactoryException( 
    8360                                        "Can't determinate the public inet addresses", e); 
    8461                } 
    85  
    86                 Set references = new HashSet(); 
    87                 for (Iterator iter = addresses.iterator(); iter.hasNext();) { 
    88                         InetAddress address = (InetAddress) iter.next(); 
    89                         references.add(InetPeerReference.getInstance(address 
    90                                         .getHostAddress(), defaultPort, false)); 
    91                 } 
    92                 return new MultiplePeerReference(references); 
     62                 
     63                InetPeerReference publicReference = InetPeerReference.getInstance(publicAddress.getHostAddress(), port, false); 
     64                return InetPeerReferences.create(localFactory.create(),publicReference); 
    9365        } 
    9466 
     
    10072        } 
    10173 
    102         private Set getLocalInetAddress() throws IOException { 
    103                 Set inetAddresses = new HashSet(); 
    104  
    105                 Enumeration networkInterfaces = NetworkInterface.getNetworkInterfaces(); 
    106                 while (networkInterfaces.hasMoreElements()) { 
    107                         NetworkInterface networkInterface = (NetworkInterface) networkInterfaces 
    108                                         .nextElement(); 
    109                         inetAddresses.addAll(EnumerationUtils.toList(networkInterface 
    110                                         .getInetAddresses())); 
    111                 } 
    112  
    113                 CollectionUtils.filter(inetAddresses, new InstanceofPredicate( 
    114                                 Inet4Address.class)); 
    115  
    116                 if (inetAddresses.size() > 1) { 
    117                         Predicate loopback = new Predicate() { 
    118  
    119                                 public boolean evaluate(Object object) { 
    120                                         return ((InetAddress) object).isLoopbackAddress(); 
    121                                 } 
    122                         }; 
    123                         CollectionUtils.filter(inetAddresses, new NotPredicate(loopback)); 
    124                 } 
    125  
    126                 return inetAddresses; 
    127         } 
    128  
    12974} 
  • trunk/source/org/kolaka/freecast/peer/PeerReferenceLoader.java

    r71 r337  
    2323package org.kolaka.freecast.peer; 
    2424 
     25import java.net.InetAddress; 
    2526import java.net.InetSocketAddress; 
    2627import java.util.HashSet; 
     
    5960        } 
    6061 
    61         private PeerReference createReference(Configuration configuration) 
     62        private PeerReference createReference(final Configuration configuration) 
    6263                        throws ConfigurationException { 
    63                 PeerReference reference; 
    6464                String referenceClass = configuration.getString("class", "inet"); 
     65                 
     66                PeerReferenceFactory factory; 
     67                 
    6568                if (referenceClass.equals("inet")) { 
    66                         reference = InetPeerReference.getInstance(configuration 
    67                                         .getString("host"), configuration.getInt("port", 
    68                                         listenAddress.getPort()), true); 
     69                        factory = new PeerReferenceFactory() { 
     70                                public PeerReference create() throws PeerReferenceFactoryException { 
     71                                        return InetPeerReference.getInstance(configuration 
     72                                                        .getString("host"), configuration.getInt("port", 
     73                                                        listenAddress.getPort()), true); 
     74                                }; 
     75                        }; 
    6976                } else if (referenceClass.equals("multiple")) { 
     77                        factory = new PeerReferenceFactory() { 
     78                                public PeerReference create() throws PeerReferenceFactoryException { 
    7079                        Set references = new HashSet(); 
    7180                        // TODO horrible limitation of Commons Configuration .. 
     
    7685                                                listenAddress.getPort(), true)); 
    7786                        } 
    78                         reference = new MultiplePeerReference(references); 
     87                        return new MultiplePeerReference(references); 
     88                                }}; 
    7989                } else if (referenceClass.equals("auto")) { 
    80                         AutomaticPeerReferenceFactory factory = new AutomaticPeerReferenceFactory(); 
    81                         factory.setDefaultPort(listenAddress.getPort()); 
    82                         try { 
    83                                 reference = factory.create(); 
    84                         } catch (PeerReferenceFactoryException e) { 
    85                                 throw new ConfigurationException( 
    86                                                 "Can't create the automatic reference", e); 
    87                         } 
     90                        factory = new AutomaticPeerReferenceFactory(listenAddress.getPort()); 
     91                } else if (referenceClass.equals("stun")) { 
     92                        InetSocketAddress stunServer = new InetSocketAddress(  
     93                        configuration.getString("host"), configuration.getInt("port",3478)); 
     94                        factory = new StunPeerReferenceFactory(listenAddress.getPort(), stunServer); 
    8895                } else { 
    8996                        throw new ConfigurationException("Unknow reference class: " 
    9097                                        + referenceClass); 
    9198                } 
    92  
    93                 return reference; 
     99                 
     100                try { 
     101                        return factory.create(); 
     102                } catch (PeerReferenceFactoryException e) { 
     103                        throw new ConfigurationException( 
     104                                        "Can't create the automatic reference", e); 
     105                } 
    94106        } 
    95107 
  • trunk/source/org/kolaka/freecast/peer/test/AutomaticPeerReferenceFactoryTest.java

    r71 r337  
    3535 
    3636        public void testCreate() throws PeerReferenceFactoryException { 
    37                 AutomaticPeerReferenceFactory factory = new AutomaticPeerReferenceFactory(); 
    38  
    39                 try { 
    40                         factory.create(); 
    41                         fail(); 
    42                 } catch (IllegalStateException e) { 
    43                         // expected exception 
    44                 } 
    45  
    46                 factory.setDefaultPort(1000); 
    47  
     37                AutomaticPeerReferenceFactory factory = new AutomaticPeerReferenceFactory(1000); 
    4838                MultiplePeerReference reference = (MultiplePeerReference) factory 
    4939                                .create(); 
  • trunk/source/org/kolaka/freecast/transport/MinaPeerReceivingConnection.java

    r333 r337  
    172172 
    173173        protected void sendNodeStatus(PeerStatus peerStatus) { 
    174                 if (!getStatus().equals(PeerConnection.Status.OPENING)) { 
     174                if (!getStatus().equals(PeerConnection.Status.OPENED)) { 
    175175                        latencyMonitor.statusSent(peerStatus); 
    176176                } 
     
    218218                public void statusReceived(PeerStatus remoteStatus) { 
    219219                        if (sendingTimeStamp == UNKNOWN_TIMESTAMP) { 
    220                                 LogFactory.getLog(getClass()).debug( 
    221                                                 "unexpected remote PeerStatus received: " 
    222                                                                 + remoteStatus); 
     220                                LogFactory.getLog(getClass()).trace("unexpected remote PeerStatus received: " + remoteStatus); 
    223221                                return; 
    224222                        } 
  • trunk/source/org/kolaka/freecast/transport/receiver/PeerReceiverControler.java

    r333 r337  
    150150                         
    151151                        try { 
     152                                final PeerConnectionStatusListener closeListener = new PeerConnectionStatusAdapter() { 
     153                                        protected void connectionClosed(PeerConnection connection) { 
     154                                                LogFactory.getLog(getClass()).debug("lost connection with " + peer); 
     155                                                openedConnections.remove(peer); 
     156                                        } 
     157                                }; 
    152158                                PeerConnectionStatusListener listener = new PeerConnectionStatusAdapter() { 
    153159                                        private boolean registered; 
    154160                                         
    155                                         protected void connectionClosed(PeerConnection connection) { 
    156                                                 LogFactory.getLog(getClass()).trace("lost connection with " + peer); 
    157                                                 openedConnections.remove(peer); 
    158                                         } 
    159161                                        protected synchronized void connectionOpened(PeerConnection connection) { 
    160162                                                if (registered) { 
     
    165167                                                registered = true; 
    166168                                                LogFactory.getLog(getClass()).debug("new opened connection with " + peer); 
     169                                                connection.add(closeListener); 
    167170                                                openedConnections.put(peer, connection); 
    168171                                        } 
     
    174177                } 
    175178        } 
     179         
     180         
     181 
    176182 
    177183        private PeerReceiver receiver;