]> sjero.net Git - linphone/blob - p2pproxy/test-src/org/linphone/p2pproxy/test/utils/P2pNetwork.java
remove mediastreamer2 and add it as a submodule instead.
[linphone] / p2pproxy / test-src / org / linphone / p2pproxy / test / utils / P2pNetwork.java
1 /*
2 p2pproxy Copyright (C) 2007  Jehan Monnier ()
3
4 P2pNetwork.java -- create a jxta network.
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 */
20 package org.linphone.p2pproxy.test.utils;
21
22 import java.io.File;
23 import java.io.FileNotFoundException;
24 import java.io.IOException;
25 import java.net.InetAddress;
26 import java.net.URISyntaxException;
27 import java.net.URL;
28 import java.net.URLClassLoader;
29 import java.net.UnknownHostException;
30 import java.util.ArrayList;
31 import java.util.InvalidPropertiesFormatException;
32 import java.util.List;
33
34 import javax.security.cert.CertificateException;
35
36 import org.apache.log4j.Logger;
37 import org.linphone.p2pproxy.api.P2pProxyInstance;
38 import org.linphone.p2pproxy.api.P2pProxyInstance.Mode;
39
40 import net.jxta.exception.JxtaException;
41
42 //import org.linphone.p2pproxy.Configurator;
43 //import org.linphone.p2pproxy.JxtaNetworkManager;
44 //import org.linphone.p2pproxy.P2pProxyException;
45
46
47 public class P2pNetwork {
48         private List<P2pProxyInstance> mEdges = new ArrayList<P2pProxyInstance>();
49     private List<P2pProxyInstance> mSuperPeers = new ArrayList<P2pProxyInstance>();
50     private final static Logger mLog = Logger.getLogger(P2pNetwork.class);
51     Process mSshProcess;
52     //String mPrivateAdress = "192.168.145.1";
53     String mPrivateAdress = "127.0.0.1";
54
55     String mPublicAdress ;
56     String mPublicAdressUser ;
57    /**
58          * create a p2p network with super peers and edges
59          * @param nEdgees number of edge peer
60          * @param mRdv number of super peer
61     * @throws IOException 
62     * @throws FileNotFoundException 
63     * @throws InvalidPropertiesFormatException 
64     * @throws CertificateException 
65     * @throws URISyntaxException 
66     * @throws P2pProxyException 
67     * @throws InterruptedException 
68     * @throws JxtaException 
69          */
70     public P2pNetwork(int nEdge,int mRdv) throws Exception {
71        this(nEdge,mRdv,0,0,100,false,System.getProperty("user.name"),"127.0.0.1");
72     }
73     public P2pNetwork(int nEdge,int mRdv,int pAuto,int aBaseIndex,int aRelayCapacity, boolean isNated, String aUser,String aRemoteHost) throws Exception {
74        mPublicAdress = aRemoteHost;
75        mPublicAdressUser=aUser;
76        try {
77
78           if (isNated == true) {
79
80              //1
81              setupDummySocks(aBaseIndex,nEdge+mRdv+pAuto);
82              //2
83
84              System.setProperty("socksProxyHost", mPrivateAdress);
85              System.setProperty("socksProxyPort", "1080");
86           }
87           String [] lClassPath = {
88                 "./antbuild/p2pproxy"
89                 ,"./dependencies/bcprov-jdk14.jar"
90                 ,"./dependencies/javax.servlet.jar"
91                 ,"./dependencies/org.mortbay.jetty.jar"
92           };
93           URL[] lUrlTab = new URL[lClassPath.length];
94           for (int i=0;i<lClassPath.length;i++) {
95              lUrlTab[i]=  new File(lClassPath[i]).toURL();
96           }
97           // build relays
98           
99           ClassLoader lRootClassLoader = Thread.currentThread().getContextClassLoader();
100           // first relay
101           ClassLoader lFirstClassLoader = new URLClassLoader(lUrlTab,lRootClassLoader);
102           P2pProxyInstance lFirstP2pProxyInstance = (P2pProxyInstance) Class.forName("org.linphone.p2pproxy.core.P2pProxyInstanceImpl",true,lFirstClassLoader).newInstance();
103           lFirstP2pProxyInstance.setIndex(aBaseIndex);
104           lFirstP2pProxyInstance.setMode(Mode.seeding_server);
105           if (isNated == true)  {
106              lFirstP2pProxyInstance.setPrivateHostAdress(mPrivateAdress);
107              lFirstP2pProxyInstance.setPublicHostAdress(mPublicAdress);
108           }
109           lFirstP2pProxyInstance.start();
110           mSuperPeers.add(lFirstP2pProxyInstance);
111           while (lFirstP2pProxyInstance.isStarted() == false) {
112              Thread.sleep(1000);
113           }
114           for (int i=1;i<mRdv;i++) {
115              ClassLoader lClassLoader = new URLClassLoader(lUrlTab,lRootClassLoader);
116              P2pProxyInstance lP2pProxyInstance = (P2pProxyInstance) Class.forName("org.linphone.p2pproxy.core.P2pProxyInstanceImpl",true,lClassLoader).newInstance();
117              lP2pProxyInstance.setIndex(aBaseIndex+i);
118              lP2pProxyInstance.setMode(Mode.relay);
119              lP2pProxyInstance.setRelayCapacity(aRelayCapacity);
120              if (isNated == true)  {
121                 lP2pProxyInstance.setPrivateHostAdress(mPrivateAdress);
122                 lP2pProxyInstance.setPublicHostAdress(mPublicAdress);
123              }
124              mSuperPeers.add(lP2pProxyInstance);
125              lP2pProxyInstance.start();
126           }
127           //edges
128           for (int j=0;j<nEdge;j++) {
129              ClassLoader lClassLoader = new URLClassLoader(lUrlTab,lRootClassLoader);
130              P2pProxyInstance lP2pProxyInstance = (P2pProxyInstance) Class.forName("org.linphone.p2pproxy.core.P2pProxyInstanceImpl",true,lClassLoader).newInstance();
131              lP2pProxyInstance.setIndex(aBaseIndex+mRdv+j);
132              lP2pProxyInstance.setMode(Mode.edge);
133              lP2pProxyInstance.setRelayCapacity(aRelayCapacity);
134              if (isNated == true)  {
135                 lP2pProxyInstance.setPrivateHostAdress(mPrivateAdress);
136                 lP2pProxyInstance.setPublicHostAdress(mPublicAdress);
137              }
138              mEdges.add(lP2pProxyInstance);
139              lP2pProxyInstance.start();
140           }
141           //auto
142           for (int k=0;k<pAuto;k++) {
143              ClassLoader lClassLoader = new URLClassLoader(lUrlTab,lRootClassLoader);
144              P2pProxyInstance lP2pProxyInstance = (P2pProxyInstance) Class.forName("org.linphone.p2pproxy.core.P2pProxyInstanceImpl",true,lClassLoader).newInstance();
145              lP2pProxyInstance.setIndex(aBaseIndex+mRdv+nEdge+k);
146              lP2pProxyInstance.setMode(Mode.auto);
147              lP2pProxyInstance.setRelayCapacity(aRelayCapacity);
148              if (isNated == true)  {
149                 lP2pProxyInstance.setPrivateHostAdress(mPrivateAdress);
150                 lP2pProxyInstance.setPublicHostAdress(mPublicAdress);
151              }
152              mEdges.add(lP2pProxyInstance);
153              lP2pProxyInstance.start();
154           }
155           
156           boolean lstarted;
157           // wait untill all instances are started
158           do {
159              lstarted = true;
160              for (P2pProxyInstance lP2pProxyInstance : mSuperPeers) {
161                 if (lP2pProxyInstance.isStarted() == false) lstarted = false;
162              } 
163              for (P2pProxyInstance lP2pProxyInstance : new ArrayList<P2pProxyInstance>(mEdges)) {
164                 if (lP2pProxyInstance.isStarted() == false) {
165                    lstarted = false;
166                 } else {
167                    if (lP2pProxyInstance.getMode() == Mode.relay) {
168                       // move from edge list to relay
169                       mEdges.remove(lP2pProxyInstance);
170                       mSuperPeers.add(lP2pProxyInstance);
171                       mLog.info("peer [ "+lP2pProxyInstance+"] moved from edge to relay" );
172                    }
173                 }
174              }
175              Thread.sleep(1000);
176           }while (lstarted == false);
177           mLog.info("P2pNetwork started with ["+getEdgesPeers().size()+"] egdges and ["+getSuperPeers().size()+"]super peers");
178           
179        } catch (Exception e) {
180           mLog.fatal("cannot create network",e);
181        }
182        
183     }
184    /**
185     * @return Returns the mEdges.
186     */
187    public List<P2pProxyInstance> getEdgesPeers() {
188       return mEdges;
189    }
190    /**
191     * @return Returns the mSuperPeers.
192     */
193    public List<P2pProxyInstance> getSuperPeers() {
194       return mSuperPeers;
195    }
196    public List<P2pProxyInstance> getAllPeers() {
197       List<P2pProxyInstance> lP2pProxyInstanceList = new ArrayList <P2pProxyInstance>();
198       lP2pProxyInstanceList.addAll(mSuperPeers);
199       lP2pProxyInstanceList.addAll(mEdges);
200       return lP2pProxyInstanceList;
201    }
202    public void stop() {
203       for (P2pProxyInstance lP2pProxyInstance:getAllPeers()) {
204          try {
205             lP2pProxyInstance.stop();
206          } catch (Exception e) {
207             mLog.error("stop error", e);
208             //nop
209          }
210       }
211       mSshProcess.destroy();
212       
213    }
214    private void setupDummySocks(int aBaseIndex, int numberOfPeers) throws InterruptedException, IOException {
215       StringBuffer lCommand =new StringBuffer("ssh -D "+mPrivateAdress+":1080 ");
216       for (int i=aBaseIndex; i<aBaseIndex + numberOfPeers;i++) {
217          lCommand.append("-R"+mPublicAdress+":"+(P2pProxyInstance.BASE_HTTP+i)+":"+mPrivateAdress+":"+(P2pProxyInstance.BASE_HTTP+i)+" ");
218       }
219       lCommand.append(mPublicAdressUser+"@"+mPublicAdress);
220       mLog.info("executing ["+lCommand.toString()+"]");
221       mSshProcess = Runtime.getRuntime().exec(lCommand.toString());
222    }
223         
224 }