]> sjero.net Git - linphone/blob - p2pproxy/test-src/org/linphone/p2pproxy/test/utils/UserInstance.java
5e589a7dd36de306869d3ad57a24e53669e4b649
[linphone] / p2pproxy / test-src / org / linphone / p2pproxy / test / utils / UserInstance.java
1 /*
2 p2pproxy
3 Copyright (C) 2007  Jehan Monnier ()
4
5 P2pAutoConfigTester.java - .
6
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.
11
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.
16
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.
20 */
21 package org.linphone.p2pproxy.test.utils;
22
23 import java.io.File;
24 import java.net.DatagramSocket;
25 import java.net.InetAddress;
26 import java.net.PortUnreachableException;
27 import java.net.URI;
28 import java.util.Timer;
29 import java.util.TimerTask;
30
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;
43
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 {
56         try {
57                 mAudioSocket  = new DatagramSocket();
58                 DatagramSocket lSocket = new DatagramSocket();
59                 lSocket.setReuseAddress(true);
60                 int lSipPort = lSocket.getLocalPort();
61                 lSocket.close();
62                 URI lUserNameUri = URI.create(userName);
63                 final String[] lParam = {"-jxta" ,"userinstance-"+lUserNameUri.getSchemeSpecificPart()
64                                 ,"-edge-only"
65                                 ,"-seeding", "tcp://91.121.81.212:9701|tcp://91.121.81.212:9702"};
66                 lSocket.close();
67
68                 Runnable lFonisTask = new Runnable() {
69                         public void run() {
70                                 P2pProxyMain.main(lParam);
71                         }
72
73                 };
74                 mFonisThread = new Thread(lFonisTask,"fonis lib");
75                 mFonisThread.start();
76                 int lRetry=0;
77                 while (P2pProxyMain.getState() != P2pProxylauncherConstants.P2PPROXY_CONNECTED && lRetry++<20) {
78                         Thread.sleep(500);
79                 }
80                 if (P2pProxyMain.getState() != P2pProxylauncherConstants.P2PPROXY_CONNECTED) {
81                         throw new P2pProxyException("Cannot connect to fonis network");
82                 }
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);
90
91                 class RegistrarTimerTask extends  TimerTask {
92                         @Override
93                         public void run() {
94                                 try {
95                                         URI     lProxyUri = null;
96                                         // 1 get proxy address
97                                         String [] lProxies = P2pProxyMain.lookupSipProxiesUri(P2pProxyResourceManagement.DOMAINE);
98                                         if (lProxies.length == 0) {
99                                                 System.out.println("cannot find registrar");
100                                                 return;
101                                         }
102                                         //default choice
103                                         lProxyUri = URI.create(lProxies[0]);
104                                         //search
105                                         for (String lProxy: lProxies) {
106                                                 if  (lProxy.equals(aPreferedProxyUri)) {
107                                                         lProxyUri = URI.create(lProxy);
108                                                         break;
109                                                 }
110                                         }
111                                         //2 setOutbound proxy
112                                         mProvider.setOutboundProxy(new SocketAddress(lProxyUri.getRawSchemeSpecificPart()));
113                                         mLog.info("use outband proxy ["+mProvider.getOutboundProxy()+"]");
114                                         //3 setup stun client
115
116                                         String [] lMediaServer = P2pProxyMain.lookupMediaServerAddress(P2pProxyResourceManagement.DOMAINE);
117                                         
118                                         mStunClient =  new StunClient(lMediaServer);
119                                         try {
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());
125                                         }
126                                         
127                                         try{
128                                                 mSipClient.register(REGISTRATION_PERIOD,userName);
129                                         }catch(Exception e) {
130                                                 mLog.error("registration error", e);
131                                         }
132                                         if (lMediaServer.length > 0) {
133                                                 mRtpRelayServerTester = new RtpRelayServerTester(mStunClient.getStrunServerList().get(0));
134                                         }
135                                         mIsRegistered = true;
136                                 }       
137                                         catch(Exception e) {
138                                         mLog.error("cannot register user["+userName+"]",e);
139                                 } finally {
140                                         mTimer.schedule(new  RegistrarTimerTask(), 1000 *(REGISTRATION_PERIOD-REGISTRATION_PERIOD/10));
141                                 }
142                         }
143
144                 };
145                 mTimer.schedule(new  RegistrarTimerTask(), 0);
146                 mSipClient.listen();
147         } catch (Exception e) {
148                 throw new P2pProxyException("cannot start client",e);
149         }
150 }
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");
156         }
157 }
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")) {
164                            lFrom = args[i + 1];
165                            System.out.println("from [" + lFrom + "]");
166                            //nop
167                    } else if (argument.equals("-to")) {
168                            lTo = args[i + 1];
169                            System.out.println("to [" + lTo + "]");
170                            
171                    } else if (argument.equals("-duration")) {
172                            lDuration =  Integer.parseInt(args[i + 1]);
173                            System.out.println("duration [" + lDuration + "]");
174                            
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 + "]");
181                    } 
182                    else {
183                            System.out.println("Invalid option: " + args[i]);
184                            usage();
185                            System.exit(1);
186                    }
187            }    
188         
189    if (lFrom == null) {
190            System.out.println("missing -from ");
191            usage();
192            System.exit(1);
193    }
194    if (lLoop != 0 && lTo == null) {
195            System.out.println("missing -to ");
196            usage();
197            System.exit(1);
198    }
199
200    UserInstance lUserInstance= new UserInstance(lFrom,lPreferedProxyUri);
201    while (mIsRegistered == false) {
202            try {
203                    Thread.sleep(1000);
204            } catch (InterruptedException e) {
205                    //nop;
206            }
207    }
208    for (int i=0;i<lLoop;i++) {
209            lUserInstance.call(lTo, lDuration);
210    }
211    while (true) {
212            try {
213                    Thread.sleep(1000);
214            } catch (InterruptedException e) {
215                    //nop;
216            }
217    }
218            
219 }
220 private static void usage() {
221         // TODO Auto-generated method stub
222         
223 }
224 }