3 Copyright (C) 2007 Jehan Monnier ()
5 P2pAutoConfigTester.java - .
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 package org.linphone.p2pproxy.test.utils;
24 import java.net.DatagramSocket;
25 import java.net.InetAddress;
26 import java.net.PortUnreachableException;
28 import java.util.Timer;
29 import java.util.TimerTask;
31 import org.apache.log4j.Logger;
32 import org.linphone.p2pproxy.api.P2pProxyException;
33 import org.linphone.p2pproxy.api.P2pProxyResourceManagement;
34 import org.linphone.p2pproxy.core.P2pProxyMain;
35 import org.linphone.p2pproxy.core.media.MediaResoureUnreachableException;
36 import org.linphone.p2pproxy.core.stun.AddressInfo;
37 import org.linphone.p2pproxy.core.stun.StunClient;
38 import org.linphone.p2pproxy.launcher.P2pProxylauncherConstants;
39 import org.linphone.p2pproxy.test.RtpRelayServerTester;
40 import org.zoolu.net.SocketAddress;
41 import org.zoolu.sip.provider.SipProvider;
42 import org.zoolu.sip.provider.SipStack;
44 public class UserInstance {
45 private final Thread mFonisThread;
46 private Timer mTimer = new Timer("Registartion timer");
47 private final SipProvider mProvider;
48 private final SipClient mSipClient;
49 private RtpRelayServerTester mRtpRelayServerTester;
50 private StunClient mStunClient;
51 private final int REGISTRATION_PERIOD=60;
52 private final static Logger mLog = Logger.getLogger(UserInstance.class);
53 private static boolean mIsRegistered = false;
54 DatagramSocket mAudioSocket;
55 public UserInstance(final String userName,final String aPreferedProxyUri) throws P2pProxyException {
57 mAudioSocket = new DatagramSocket();
58 DatagramSocket lSocket = new DatagramSocket();
59 lSocket.setReuseAddress(true);
60 int lSipPort = lSocket.getLocalPort();
62 URI lUserNameUri = URI.create(userName);
63 final String[] lParam = {"-jxta" ,"userinstance-"+lUserNameUri.getSchemeSpecificPart()
65 ,"-seeding", "tcp://91.121.81.212:9701|tcp://91.121.81.212:9702"};
68 Runnable lFonisTask = new Runnable() {
70 P2pProxyMain.main(lParam);
74 mFonisThread = new Thread(lFonisTask,"fonis lib");
77 while (P2pProxyMain.getState() != P2pProxylauncherConstants.P2PPROXY_CONNECTED && lRetry++<20) {
80 if (P2pProxyMain.getState() != P2pProxylauncherConstants.P2PPROXY_CONNECTED) {
81 throw new P2pProxyException("Cannot connect to fonis network");
83 P2pProxyMain.createAccount(userName);
84 SipStack.log_path = "userinstance-"+lUserNameUri.getSchemeSpecificPart()+"/logs";
85 File lFile = new File(SipStack.log_path);
86 if (lFile.exists() == false) lFile.mkdir();
87 //InetAddress[] lAddresses = InetAddress.getAllByName("localhost");
88 mProvider=new SipProvider(null,lSipPort);
89 mSipClient = new SipClient(mProvider,userName,30000);
91 class RegistrarTimerTask extends TimerTask {
96 // 1 get proxy address
97 String [] lProxies = P2pProxyMain.lookupSipProxiesUri(P2pProxyResourceManagement.DOMAINE);
98 if (lProxies.length == 0) {
99 System.out.println("cannot find registrar");
103 lProxyUri = URI.create(lProxies[0]);
105 for (String lProxy: lProxies) {
106 if (lProxy.equals(aPreferedProxyUri)) {
107 lProxyUri = URI.create(lProxy);
111 //2 setOutbound proxy
112 mProvider.setOutboundProxy(new SocketAddress(lProxyUri.getRawSchemeSpecificPart()));
113 mLog.info("use outband proxy ["+mProvider.getOutboundProxy()+"]");
114 //3 setup stun client
116 String [] lMediaServer = P2pProxyMain.lookupMediaServerAddress(P2pProxyResourceManagement.DOMAINE);
118 mStunClient = new StunClient(lMediaServer);
120 AddressInfo lAudioAddressInfo = mStunClient.computeAddressInfo(mAudioSocket);
121 mLog.info("audio socket info ["+lAudioAddressInfo+"]");
122 }catch (MediaResoureUnreachableException pex) {
123 mLog.error("cannot reach media server, flushing ["+pex.getResourceAddress()+"]",pex);
124 P2pProxyMain.revokeMediaServer(pex.getResourceAddress());
128 mSipClient.register(REGISTRATION_PERIOD,userName);
129 }catch(Exception e) {
130 mLog.error("registration error", e);
132 if (lMediaServer.length > 0) {
133 mRtpRelayServerTester = new RtpRelayServerTester(mStunClient.getStrunServerList().get(0));
135 mIsRegistered = true;
138 mLog.error("cannot register user["+userName+"]",e);
140 mTimer.schedule(new RegistrarTimerTask(), 1000 *(REGISTRATION_PERIOD-REGISTRATION_PERIOD/10));
145 mTimer.schedule(new RegistrarTimerTask(), 0);
147 } catch (Exception e) {
148 throw new P2pProxyException("cannot start client",e);
151 public void call(String aTo, int duration) {
152 mSipClient.call(aTo, true, duration);
153 if (mRtpRelayServerTester != null) {
154 mRtpRelayServerTester.testRouting();
155 mLog.info("rtp relay ok");
158 public static void main(String[] args) throws P2pProxyException {
159 String lFrom=null, lTo=null, lPreferedProxyUri=null;
160 int lDuration = 10000, lLoop=0;
161 for (int i=0; i < args.length; i=i+2) {
162 String argument = args[i];
163 if (argument.equals("-from")) {
165 System.out.println("from [" + lFrom + "]");
167 } else if (argument.equals("-to")) {
169 System.out.println("to [" + lTo + "]");
171 } else if (argument.equals("-duration")) {
172 lDuration = Integer.parseInt(args[i + 1]);
173 System.out.println("duration [" + lDuration + "]");
175 } else if (argument.equals("-nb-call")) {
176 lLoop = Integer.parseInt(args[i + 1]);
177 System.out.println("nb-call [" + lLoop + "]");
178 } else if (argument.equals("-prefered-proxy")) {
179 lPreferedProxyUri = args[i + 1];
180 System.out.println("prefered-proxy [" + lLoop + "]");
183 System.out.println("Invalid option: " + args[i]);
190 System.out.println("missing -from ");
194 if (lLoop != 0 && lTo == null) {
195 System.out.println("missing -to ");
200 UserInstance lUserInstance= new UserInstance(lFrom,lPreferedProxyUri);
201 while (mIsRegistered == false) {
204 } catch (InterruptedException e) {
208 for (int i=0;i<lLoop;i++) {
209 lUserInstance.call(lTo, lDuration);
214 } catch (InterruptedException e) {
220 private static void usage() {
221 // TODO Auto-generated method stub