diff --git a/p2pproxy/src/org/linphone/p2pproxy/api/P2pProxyResourceManagement.java b/p2pproxy/src/org/linphone/p2pproxy/api/P2pProxyResourceManagement.java index 44f11504d..e0c5fcbd8 100644 --- a/p2pproxy/src/org/linphone/p2pproxy/api/P2pProxyResourceManagement.java +++ b/p2pproxy/src/org/linphone/p2pproxy/api/P2pProxyResourceManagement.java @@ -25,14 +25,16 @@ public interface P2pProxyResourceManagement { public final String DOMAINE="p2p.linphone.org"; /** * - * @return the SIP uri of an available sip proxy registrar for a given domaine + * @return the SIP uris of an available sip proxy registrar for a given domaine */ - public String lookupSipProxyUri(String aDomaine) throws P2pProxyException ; + public String[] lookupSipProxiesUri(String aDomaine) throws P2pProxyException ; public void revokeSipProxy(String aProxy) throws P2pProxyException; /** * return 2 adresses where to contact media server (stun/rtprelay) */ public String[] getMediaServerList() throws P2pProxyException; + + } diff --git a/p2pproxy/src/org/linphone/p2pproxy/core/JxtaNetworkManager.java b/p2pproxy/src/org/linphone/p2pproxy/core/JxtaNetworkManager.java index bd7a3b18a..a2c96ddee 100644 --- a/p2pproxy/src/org/linphone/p2pproxy/core/JxtaNetworkManager.java +++ b/p2pproxy/src/org/linphone/p2pproxy/core/JxtaNetworkManager.java @@ -271,15 +271,19 @@ public class JxtaNetworkManager { } public List getAdvertisementList(String aPeerId, String anAttributeName,String anAttributeValue, boolean isTryFromLocal) throws InterruptedException, IOException, P2pProxyAdvertisementNotFoundException { + return getAdvertisementList(aPeerId, anAttributeName, anAttributeValue, isTryFromLocal, 1); + } + public List getAdvertisementList(String aPeerId, String anAttributeName,String anAttributeValue, boolean isTryFromLocal, int numberOfexpectedAdv) throws InterruptedException, IOException, P2pProxyAdvertisementNotFoundException { DiscoveryService lDiscoveryService = getPeerGroup().getDiscoveryService(); - final Semaphore lSemaphore = new Semaphore(0); + final Semaphore lSemaphore = new Semaphore(1-numberOfexpectedAdv); final List lReturnList = new ArrayList(); DiscoveryListener lDiscoveryListener = new DiscoveryListener() { public void discoveryEvent(DiscoveryEvent event) { DiscoveryResponseMsg lRes = event.getResponse(); + int lOrigListSize = lReturnList.size(); enumeration2List(lRes.getAdvertisements(), lReturnList); - lSemaphore.release(); + lSemaphore.release(lReturnList.size()-lOrigListSize); } }; @@ -288,10 +292,10 @@ public class JxtaNetworkManager { Enumeration lEnumeration = lDiscoveryService.getLocalAdvertisements(DiscoveryService.ADV, anAttributeName,anAttributeValue); enumeration2List(lEnumeration, lReturnList); } - if (lReturnList.isEmpty() == true) { - mLog.info("no advertisement found in local, trying remote..."); + if (lReturnList.size() < numberOfexpectedAdv) { + mLog.info(lReturnList.size() +" of ["+numberOfexpectedAdv+"] advertisements found in local, trying remote..."); lDiscoveryService.getRemoteAdvertisements(aPeerId, DiscoveryService.ADV, anAttributeName,anAttributeValue, 10,lDiscoveryListener); - if (lSemaphore.tryAcquire(ADV_DISCOVERY_TIMEOUT_INT,TimeUnit.MILLISECONDS) == false) { + if (lSemaphore.tryAcquire(ADV_DISCOVERY_TIMEOUT_INT,TimeUnit.MILLISECONDS) == false && lReturnList.isEmpty()) { throw new P2pProxyAdvertisementNotFoundException( anAttributeName+"="+anAttributeValue+ " not found"); } lSemaphore.release(); @@ -354,14 +358,26 @@ public class JxtaNetworkManager { mNetworkPeerGroup.stopApp(); //mNetworkPeerGroup.unref(); } - private List enumeration2List(Enumeration lEnumeration,List lList) { - if (lList == null) { - lList = new ArrayList(); + private List enumeration2List(Enumeration lEnumeration,List aList) { + if (aList == null) { + aList = new ArrayList(); } while (lEnumeration.hasMoreElements()) { - lList.add((Advertisement) lEnumeration.nextElement()) ; + Advertisement lNewAdv = lEnumeration.nextElement(); + //1 check if already exist + for (Advertisement lAdv: aList) { + if (lAdv.equals(lNewAdv)) { + if (mLog.isDebugEnabled()) mLog.debug("adv ["+lNewAdv.getID()+"]already gathered"); + lNewAdv = null; + break; + } + } + + if (lNewAdv != null) { + aList.add((lNewAdv)) ; + } } - return lList; + return aList; } } diff --git a/p2pproxy/src/org/linphone/p2pproxy/core/P2pProxyMain.java b/p2pproxy/src/org/linphone/p2pproxy/core/P2pProxyMain.java index eff98dc2b..f49b0b6b7 100644 --- a/p2pproxy/src/org/linphone/p2pproxy/core/P2pProxyMain.java +++ b/p2pproxy/src/org/linphone/p2pproxy/core/P2pProxyMain.java @@ -466,11 +466,25 @@ public static int isValidAccount(String aUserName){ public static String lookupSipProxyUri(String aDomaine) { try { isReady(); - return mP2pProxySipProxyRegistrarManagement.lookupSipProxyUri(aDomaine); + String[] lProxies = mP2pProxySipProxyRegistrarManagement.lookupSipProxiesUri(aDomaine); + if (lProxies.length != 0) { + return lProxies[0]; + } else { + return null; + } } catch (Exception e) { return null; } } +public static String[] lookupSipProxiesUri(String aDomaine) { + try { + isReady(); + return mP2pProxySipProxyRegistrarManagement.lookupSipProxiesUri(aDomaine); + } catch (Exception e) { + return null; + } + } + public static int getState() { try { isReady(); diff --git a/p2pproxy/src/org/linphone/p2pproxy/core/P2pProxyResourceManagementImpl.java b/p2pproxy/src/org/linphone/p2pproxy/core/P2pProxyResourceManagementImpl.java index 998acdcbb..6fef9e499 100644 --- a/p2pproxy/src/org/linphone/p2pproxy/core/P2pProxyResourceManagementImpl.java +++ b/p2pproxy/src/org/linphone/p2pproxy/core/P2pProxyResourceManagementImpl.java @@ -1,5 +1,7 @@ package org.linphone.p2pproxy.core; +import java.util.List; + import org.apache.log4j.Logger; import org.linphone.p2pproxy.api.P2pProxyException; import org.linphone.p2pproxy.api.P2pProxyResourceManagement; @@ -13,14 +15,18 @@ public class P2pProxyResourceManagementImpl implements P2pProxyResourceManagemen P2pProxyResourceManagementImpl(JxtaNetworkManager aJxtaNetworkManager) { mJxtaNetworkManager = aJxtaNetworkManager; } - public String lookupSipProxyUri(String aDomaine) throws P2pProxyException { + public String[] lookupSipProxiesUri(String aDomaine) throws P2pProxyException { try { if (!DOMAINE.equals(aDomaine)) { //unknown domaine - return null; + return new String[0]; } - NetworkResourceAdvertisement lSipProxyRegistrarAdvertisement = (NetworkResourceAdvertisement) (mJxtaNetworkManager.getAdvertisement(null, SipProxyRegistrar.ADV_NAME, true)); - return lSipProxyRegistrarAdvertisement.getAddress(); + List lSipProxyRegistrarAdvertisements = (List) (mJxtaNetworkManager.getAdvertisementList(null, "Name",SipProxyRegistrar.ADV_NAME, true,2)); + String[] lAddresses = new String[lSipProxyRegistrarAdvertisements.size()]; + for (int i=0;i