From d8a9c7d12211060e8dc15458f34411e2d035934b Mon Sep 17 00:00:00 2001 From: jehan Date: Tue, 4 Nov 2008 21:25:53 +0000 Subject: [PATCH] add stun server tester git-svn-id: svn+ssh://svn.savannah.nongnu.org/linphone/trunk@159 3f6dc0c8-ddfe-455d-9043-3cd528dc4637 --- .../linphone/p2pproxy/core/P2pProxyMain.java | 3 +- .../core/P2pProxyResourceManagementImpl.java | 13 +- .../core/media/MediaResourceService.java | 38 +++-- .../p2pproxy/core/stun/StunClient.java | 20 ++- .../p2pproxy/test/utils/UserInstance.java | 146 +++++++++--------- 5 files changed, 134 insertions(+), 86 deletions(-) diff --git a/p2pproxy/src/org/linphone/p2pproxy/core/P2pProxyMain.java b/p2pproxy/src/org/linphone/p2pproxy/core/P2pProxyMain.java index 146c3c3e4..9c78e20d7 100644 --- a/p2pproxy/src/org/linphone/p2pproxy/core/P2pProxyMain.java +++ b/p2pproxy/src/org/linphone/p2pproxy/core/P2pProxyMain.java @@ -491,7 +491,8 @@ public static String[] lookupMediaServerAddress(String aDomaine) { isReady(); return mP2pProxyManagement.getMediaServerList(); } catch (Exception e) { - return null; + mLog.error("cannot find media resource",e); + return null; } } diff --git a/p2pproxy/src/org/linphone/p2pproxy/core/P2pProxyResourceManagementImpl.java b/p2pproxy/src/org/linphone/p2pproxy/core/P2pProxyResourceManagementImpl.java index 6fef9e499..9b78c92e2 100644 --- a/p2pproxy/src/org/linphone/p2pproxy/core/P2pProxyResourceManagementImpl.java +++ b/p2pproxy/src/org/linphone/p2pproxy/core/P2pProxyResourceManagementImpl.java @@ -5,6 +5,7 @@ import java.util.List; import org.apache.log4j.Logger; import org.linphone.p2pproxy.api.P2pProxyException; import org.linphone.p2pproxy.api.P2pProxyResourceManagement; +import org.linphone.p2pproxy.core.media.MediaResourceService; import org.linphone.p2pproxy.core.sipproxy.NetworkResourceAdvertisement; import org.linphone.p2pproxy.core.sipproxy.SipProxyRegistrar; @@ -42,7 +43,17 @@ public class P2pProxyResourceManagementImpl implements P2pProxyResourceManagemen } } public String[] getMediaServerList() throws P2pProxyException { - throw new RuntimeException("not implmented yet"); + try { + + List lMediaResoureAdvertisements = (List) (mJxtaNetworkManager.getAdvertisementList(null, "Name",MediaResourceService.ADV_NAME, true,2)); + String[] lAddresses = new String[lMediaResoureAdvertisements.size()]; + for (int i=0;i 1) { //open new socket DatagramSocket lDatagramSocket = new DatagramSocket(); - bindRequest(lDiscoveryInfo,lLocalSocket,lDatagramSocket, mStunServerList.get(1)); + bindRequest(lDiscoveryInfo,lDatagramSocket, lLocalSocket, lDiscoveryInfo.getPublicSocketAddress(),mStunServerList.get(1)); lDatagramSocket.close(); } //analyse @@ -104,7 +107,7 @@ public class StunClient { } return lAddressInfo; } - private void bindRequest(DiscoveryInfo aDiscoveryInfo,DatagramSocket aLocalSocket, DatagramSocket aResponseSocket,InetSocketAddress aStunAddress) throws UtilityException, SocketException, UnknownHostException, IOException, MessageAttributeParsingException, MessageHeaderParsingException, P2pProxyException { + private void bindRequest(DiscoveryInfo aDiscoveryInfo,DatagramSocket aLocalSocket, DatagramSocket aResponseSocket,InetSocketAddress aResponseAddress, InetSocketAddress aStunAddress) throws UtilityException, SocketException, UnknownHostException, IOException, MessageAttributeParsingException, MessageHeaderParsingException, P2pProxyException { int timeSinceFirstTransmission = 0; int lSoTimeOut = SO_TIME_OUT; while (true) { @@ -118,6 +121,17 @@ public class StunClient { ChangeRequest changeRequest = new ChangeRequest(); sendMH.addMessageAttribute(changeRequest); + if (!((InetSocketAddress)aLocalSocket.getLocalSocketAddress()).equals((InetSocketAddress)aResponseSocket.getLocalSocketAddress()) && aResponseAddress != null) { + // add response address + ResponseAddress lResponseAddress = new ResponseAddress(); + lResponseAddress.setAddress(new Address(aResponseAddress.getAddress().getHostAddress())); + try { + lResponseAddress.setPort(aResponseAddress.getPort()); + sendMH.addMessageAttribute(lResponseAddress); + } catch (MessageAttributeException e) { + mLog.info("Cannot set Response address ["+lResponseAddress+"]"); + } + } byte[] data = sendMH.getBytes(); DatagramPacket send = new DatagramPacket(data, data.length); diff --git a/p2pproxy/test-src/org/linphone/p2pproxy/test/utils/UserInstance.java b/p2pproxy/test-src/org/linphone/p2pproxy/test/utils/UserInstance.java index ae69f4a64..1c5258d3f 100644 --- a/p2pproxy/test-src/org/linphone/p2pproxy/test/utils/UserInstance.java +++ b/p2pproxy/test-src/org/linphone/p2pproxy/test/utils/UserInstance.java @@ -31,6 +31,7 @@ import org.apache.log4j.Logger; import org.linphone.p2pproxy.api.P2pProxyException; import org.linphone.p2pproxy.api.P2pProxyResourceManagement; import org.linphone.p2pproxy.core.P2pProxyMain; +import org.linphone.p2pproxy.core.stun.AddressInfo; import org.linphone.p2pproxy.core.stun.StunClient; import org.linphone.p2pproxy.launcher.P2pProxylauncherConstants; import org.zoolu.net.SocketAddress; @@ -46,81 +47,86 @@ private StunClient mStunClient; private final int REGISTRATION_PERIOD=60; private final static Logger mLog = Logger.getLogger(UserInstance.class); private static boolean mIsRegistered = false; +DatagramSocket mAudioSocket; public UserInstance(final String userName,final String aPreferedProxyUri) throws P2pProxyException { try { - DatagramSocket lSocket = new DatagramSocket(); - lSocket.setReuseAddress(true); - int lSipPort = lSocket.getLocalPort(); - lSocket.close(); - URI lUserNameUri = URI.create(userName); - final String[] lParam = {"-jxta" ,"userinstance-"+lUserNameUri.getSchemeSpecificPart() - ,"-edge-only" - ,"-seeding-rdv", "tcp://82.67.74.86:9701" - ,"-seeding-relay", "tcp://82.67.74.86:9701"}; - lSocket.close(); - - Runnable lFonisTask = new Runnable() { - public void run() { - P2pProxyMain.main(lParam); - } - - }; - mFonisThread = new Thread(lFonisTask,"fonis lib"); - mFonisThread.start(); - int lRetry=0; - while (P2pProxyMain.getState() != P2pProxylauncherConstants.P2PPROXY_CONNECTED && lRetry++<20) { - Thread.sleep(500); - } - if (P2pProxyMain.getState() != P2pProxylauncherConstants.P2PPROXY_CONNECTED) { - throw new P2pProxyException("Cannot connect to fonis network"); - } - P2pProxyMain.createAccount(userName); - SipStack.log_path = "userinstance-"+lUserNameUri.getSchemeSpecificPart()+"/logs"; - File lFile = new File(SipStack.log_path); - if (lFile.exists() == false) lFile.mkdir(); - //InetAddress[] lAddresses = InetAddress.getAllByName("localhost"); - mProvider=new SipProvider(null,lSipPort); - mSipClient = new SipClient(mProvider,userName,30000); - - class RegistrarTimerTask extends TimerTask { - @Override - public void run() { - try { - URI lProxyUri = null; - // 1 get proxy address - String [] lProxies = P2pProxyMain.lookupSipProxiesUri(P2pProxyResourceManagement.DOMAINE); - if (lProxies.length == 0) { - System.out.println("cannot find registrar"); - return; + mAudioSocket = new DatagramSocket(); + DatagramSocket lSocket = new DatagramSocket(); + lSocket.setReuseAddress(true); + int lSipPort = lSocket.getLocalPort(); + lSocket.close(); + URI lUserNameUri = URI.create(userName); + final String[] lParam = {"-jxta" ,"userinstance-"+lUserNameUri.getSchemeSpecificPart() + ,"-edge-only" + ,"-seeding-rdv", "tcp://82.67.74.86:9701" + ,"-seeding-relay", "tcp://82.67.74.86:9701"}; + lSocket.close(); + + Runnable lFonisTask = new Runnable() { + public void run() { + P2pProxyMain.main(lParam); } - //default choice - lProxyUri = URI.create(lProxies[0]); - //search - for (String lProxy: lProxies) { - if (lProxy.equals(aPreferedProxyUri)) { - lProxyUri = URI.create(lProxy); - break; + + }; + mFonisThread = new Thread(lFonisTask,"fonis lib"); + mFonisThread.start(); + int lRetry=0; + while (P2pProxyMain.getState() != P2pProxylauncherConstants.P2PPROXY_CONNECTED && lRetry++<20) { + Thread.sleep(500); + } + if (P2pProxyMain.getState() != P2pProxylauncherConstants.P2PPROXY_CONNECTED) { + throw new P2pProxyException("Cannot connect to fonis network"); + } + P2pProxyMain.createAccount(userName); + SipStack.log_path = "userinstance-"+lUserNameUri.getSchemeSpecificPart()+"/logs"; + File lFile = new File(SipStack.log_path); + if (lFile.exists() == false) lFile.mkdir(); + //InetAddress[] lAddresses = InetAddress.getAllByName("localhost"); + mProvider=new SipProvider(null,lSipPort); + mSipClient = new SipClient(mProvider,userName,30000); + + class RegistrarTimerTask extends TimerTask { + @Override + public void run() { + try { + URI lProxyUri = null; + // 1 get proxy address + String [] lProxies = P2pProxyMain.lookupSipProxiesUri(P2pProxyResourceManagement.DOMAINE); + if (lProxies.length == 0) { + System.out.println("cannot find registrar"); + return; + } + //default choice + lProxyUri = URI.create(lProxies[0]); + //search + for (String lProxy: lProxies) { + if (lProxy.equals(aPreferedProxyUri)) { + lProxyUri = URI.create(lProxy); + break; + } + } + //2 setOutbound proxy + mProvider.setOutboundProxy(new SocketAddress(lProxyUri.getRawSchemeSpecificPart())); + mLog.info("use outband proxy ["+mProvider.getOutboundProxy()+"]"); + //3 setup stun client + + String [] lMediaServer = P2pProxyMain.lookupMediaServerAddress(P2pProxyResourceManagement.DOMAINE); + + mStunClient = new StunClient(lMediaServer); + AddressInfo lAudioAddressInfo = mStunClient.computeAddressInfo(mAudioSocket); + mLog.info("audio socket info ["+lAudioAddressInfo+"]"); + mSipClient.register(REGISTRATION_PERIOD,userName); + mIsRegistered = true; + } catch(Exception e) { + mLog.error("cannot register user["+userName+"]",e); + } finally { + mTimer.schedule(new RegistrarTimerTask(), 1000 *(REGISTRATION_PERIOD-REGISTRATION_PERIOD/10)); } } - //2 setOutbound proxy - mProvider.setOutboundProxy(new SocketAddress(lProxyUri.getRawSchemeSpecificPart())); - mLog.info("use outband proxy ["+mProvider.getOutboundProxy()+"]"); - //3 setup stun client - - String [] lMediaServer = P2pProxyMain.lookupMediaServerAddress(P2pProxyResourceManagement.DOMAINE); - mStunClient = new StunClient(lMediaServer); - mSipClient.register(REGISTRATION_PERIOD,userName); - mIsRegistered = true; - } catch(Exception e) { - mLog.error("cannot register user["+userName+"]",e); - } finally { - mTimer.schedule(new RegistrarTimerTask(), 1000 *(REGISTRATION_PERIOD-REGISTRATION_PERIOD/10)); - } - } - - }; - mTimer.schedule(new RegistrarTimerTask(), 0); - mSipClient.listen(); + + }; + mTimer.schedule(new RegistrarTimerTask(), 0); + mSipClient.listen(); } catch (Exception e) { throw new P2pProxyException("cannot start client",e); }