Commit d8a9c7d1 authored by jehan's avatar jehan

add stun server tester

git-svn-id: svn+ssh://svn.savannah.nongnu.org/linphone/trunk@159 3f6dc0c8-ddfe-455d-9043-3cd528dc4637
parent 87aa93d3
......@@ -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;
}
}
......
......@@ -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<NetworkResourceAdvertisement> lMediaResoureAdvertisements = (List<NetworkResourceAdvertisement>) (mJxtaNetworkManager.getAdvertisementList(null, "Name",MediaResourceService.ADV_NAME, true,2));
String[] lAddresses = new String[lMediaResoureAdvertisements.size()];
for (int i=0;i<lMediaResoureAdvertisements.size();i++) {
lAddresses[i] = lMediaResoureAdvertisements.get(i).getAddress();
}
return lAddresses;
}catch (Exception e) {
throw new P2pProxyException(e);
}
}
}
......@@ -23,6 +23,8 @@ import java.net.InetSocketAddress;
import java.net.SocketException;
import java.net.URI;
import java.net.UnknownHostException;
import java.util.Timer;
import java.util.TimerTask;
import net.jxta.document.AdvertisementFactory;
import net.jxta.id.IDFactory;
......@@ -47,7 +49,10 @@ public class MediaResourceService implements ServiceProvider {
private RtpRelayServerConfig mConfig;
private final JxtaNetworkManager mJxtaNetworkManager;
private NetworkResourceAdvertisement mStunRtpServerAdvertisement;
Timer mPublishTimer = new Timer("MediaResourceService publish timer");
public final static String ADV_NAME = "p2p-proxy-stunrtp";
private final int ADV_LIFE_TIME=6000000;
TimerTask mPublishTask;
public MediaResourceService(Configurator aConfigurator, JxtaNetworkManager aJxtaNetworkManager) throws SocketException, UnknownHostException {
URI lAudioVideoPublicUri = URI.create(aConfigurator.getProperty(RtpRelayService.AUDIO_VIDEO_PUBLIC_URI,RtpRelayService.getDefaultAudioVideoPublicUri()));
int lAudioVideoLocalPort = Integer.valueOf(aConfigurator.getProperty(RtpRelayService.AUDIO_VIDEO_LOCAL_PORT,String.valueOf(lAudioVideoPublicUri.getPort())));
......@@ -62,21 +67,32 @@ public class MediaResourceService implements ServiceProvider {
}
public void start(long timeOut) throws P2pProxyException {
try {
mStunRtpServerAdvertisement = (NetworkResourceAdvertisement) AdvertisementFactory.newAdvertisement(NetworkResourceAdvertisement.getAdvertisementType());
mStunRtpServerAdvertisement.setAddress("udp://"+mConfig.getAudioVideoPublicSocketAddress().getAddress().getHostAddress()+":"+mConfig.getAudioVideoPublicSocketAddress().getPort());
mStunRtpServerAdvertisement.setID(IDFactory.newCodatID(mJxtaNetworkManager.getPeerGroup().getPeerGroupID(), mStunRtpServerAdvertisement.getAddress().toString().getBytes()));
mStunRtpServerAdvertisement.setName(ADV_NAME);
mJxtaNetworkManager.getPeerGroup().getDiscoveryService().publish(mStunRtpServerAdvertisement,60000,30000);
mLog.info(mStunRtpServerAdvertisement + "published");
} catch (Exception e) {
throw new P2pProxyException(e);
}
mPublishTask = new TimerTask() {
@Override
public void run() {
try {
mStunRtpServerAdvertisement = (NetworkResourceAdvertisement) AdvertisementFactory.newAdvertisement(NetworkResourceAdvertisement.getAdvertisementType());
mStunRtpServerAdvertisement.setAddress("udp://"+mConfig.getAudioVideoPublicSocketAddress().getAddress().getHostAddress()+":"+mConfig.getAudioVideoPublicSocketAddress().getPort());
mStunRtpServerAdvertisement.setID(IDFactory.newCodatID(mJxtaNetworkManager.getPeerGroup().getPeerGroupID(), mStunRtpServerAdvertisement.getAddress().toString().getBytes()));
mStunRtpServerAdvertisement.setName(ADV_NAME);
mJxtaNetworkManager.getPeerGroup().getDiscoveryService().publish(mStunRtpServerAdvertisement,ADV_LIFE_TIME,ADV_LIFE_TIME/2);
mLog.info(mStunRtpServerAdvertisement + "published");
} catch (Exception e) {
mLog.error("Cannot publish StunRtpServerAdvertisement", e);
}
}
};
mPublishTimer.scheduleAtFixedRate(mPublishTask, 0, ADV_LIFE_TIME - ADV_LIFE_TIME/10);
}
public void stop() {
try {
mJxtaNetworkManager.getPeerGroup().getDiscoveryService().flushAdvertisement(mStunRtpServerAdvertisement);
mPublishTask.cancel();
mJxtaNetworkManager.getPeerGroup().getDiscoveryService().flushAdvertisement(mStunRtpServerAdvertisement);
mUdpSessionForStunRtp.close();
} catch (Exception e) {
mLog.error("cannot stop MediaResourceService",e);
......
......@@ -39,9 +39,12 @@ import de.javawi.jstun.attribute.ChangeRequest;
import de.javawi.jstun.attribute.ErrorCode;
import de.javawi.jstun.attribute.MappedAddress;
import de.javawi.jstun.attribute.MessageAttribute;
import de.javawi.jstun.attribute.MessageAttributeException;
import de.javawi.jstun.attribute.MessageAttributeParsingException;
import de.javawi.jstun.attribute.ResponseAddress;
import de.javawi.jstun.header.MessageHeader;
import de.javawi.jstun.header.MessageHeaderParsingException;
import de.javawi.jstun.util.Address;
import de.javawi.jstun.util.UtilityException;
public class StunClient {
......@@ -87,12 +90,12 @@ public class StunClient {
try {
DiscoveryInfo lDiscoveryInfo = new DiscoveryInfo((InetSocketAddress) lLocalSocket.getLocalSocketAddress());
//1 bind request
bindRequest(lDiscoveryInfo,lLocalSocket,lLocalSocket, mStunServerList.get(0));
bindRequest(lDiscoveryInfo,lLocalSocket,lLocalSocket,null, mStunServerList.get(0));
//2 bind request
if (mStunServerList.size() > 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);
......
......@@ -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);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment