2 p2pproxy Copyright (C) 2007 Jehan Monnier ()
4 P2pNetwork.java -- create a jxta network.
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.
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.
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.
20 package org.linphone.p2pproxy.test.utils;
23 import java.io.FileNotFoundException;
24 import java.io.IOException;
25 import java.net.InetAddress;
26 import java.net.URISyntaxException;
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;
34 import javax.security.cert.CertificateException;
36 import org.apache.log4j.Logger;
37 import org.linphone.p2pproxy.api.P2pProxyInstance;
38 import org.linphone.p2pproxy.api.P2pProxyInstance.Mode;
40 import net.jxta.exception.JxtaException;
42 //import org.linphone.p2pproxy.Configurator;
43 //import org.linphone.p2pproxy.JxtaNetworkManager;
44 //import org.linphone.p2pproxy.P2pProxyException;
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);
52 //String mPrivateAdress = "192.168.145.1";
53 String mPrivateAdress = "127.0.0.1";
55 String mPublicAdress ;
56 String mPublicAdressUser ;
58 * create a p2p network with super peers and edges
59 * @param nEdgees number of edge peer
60 * @param mRdv number of super peer
62 * @throws FileNotFoundException
63 * @throws InvalidPropertiesFormatException
64 * @throws CertificateException
65 * @throws URISyntaxException
66 * @throws P2pProxyException
67 * @throws InterruptedException
68 * @throws JxtaException
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");
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;
78 if (isNated == true) {
81 setupDummySocks(aBaseIndex,nEdge+mRdv+pAuto);
84 System.setProperty("socksProxyHost", mPrivateAdress);
85 System.setProperty("socksProxyPort", "1080");
87 String [] lClassPath = {
89 ,"./dependencies/bcprov-jdk14.jar"
90 ,"./dependencies/javax.servlet.jar"
91 ,"./dependencies/org.mortbay.jetty.jar"
93 URL[] lUrlTab = new URL[lClassPath.length];
94 for (int i=0;i<lClassPath.length;i++) {
95 lUrlTab[i]= new File(lClassPath[i]).toURL();
99 ClassLoader lRootClassLoader = Thread.currentThread().getContextClassLoader();
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);
109 lFirstP2pProxyInstance.start();
110 mSuperPeers.add(lFirstP2pProxyInstance);
111 while (lFirstP2pProxyInstance.isStarted() == false) {
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);
124 mSuperPeers.add(lP2pProxyInstance);
125 lP2pProxyInstance.start();
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);
138 mEdges.add(lP2pProxyInstance);
139 lP2pProxyInstance.start();
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);
152 mEdges.add(lP2pProxyInstance);
153 lP2pProxyInstance.start();
157 // wait untill all instances are started
160 for (P2pProxyInstance lP2pProxyInstance : mSuperPeers) {
161 if (lP2pProxyInstance.isStarted() == false) lstarted = false;
163 for (P2pProxyInstance lP2pProxyInstance : new ArrayList<P2pProxyInstance>(mEdges)) {
164 if (lP2pProxyInstance.isStarted() == false) {
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" );
176 }while (lstarted == false);
177 mLog.info("P2pNetwork started with ["+getEdgesPeers().size()+"] egdges and ["+getSuperPeers().size()+"]super peers");
179 } catch (Exception e) {
180 mLog.fatal("cannot create network",e);
185 * @return Returns the mEdges.
187 public List<P2pProxyInstance> getEdgesPeers() {
191 * @return Returns the mSuperPeers.
193 public List<P2pProxyInstance> getSuperPeers() {
196 public List<P2pProxyInstance> getAllPeers() {
197 List<P2pProxyInstance> lP2pProxyInstanceList = new ArrayList <P2pProxyInstance>();
198 lP2pProxyInstanceList.addAll(mSuperPeers);
199 lP2pProxyInstanceList.addAll(mEdges);
200 return lP2pProxyInstanceList;
203 for (P2pProxyInstance lP2pProxyInstance:getAllPeers()) {
205 lP2pProxyInstance.stop();
206 } catch (Exception e) {
207 mLog.error("stop error", e);
211 mSshProcess.destroy();
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)+" ");
219 lCommand.append(mPublicAdressUser+"@"+mPublicAdress);
220 mLog.info("executing ["+lCommand.toString()+"]");
221 mSshProcess = Runtime.getRuntime().exec(lCommand.toString());