View Javadoc

1   package org.xvsm.remote.freepastry;
2   
3   import java.io.IOException;
4   import java.net.InetAddress;
5   import java.net.InetSocketAddress;
6   import java.net.UnknownHostException;
7   import java.util.Hashtable;
8   
9   import org.xvsm.core.Capi;
10  import org.xvsm.internal.exceptions.FatalException;
11  import org.xvsm.internal.exceptions.XCoreException;
12  import org.xvsm.internal.exceptions.XCoreRemoteException;
13  
14  import rice.environment.Environment;
15  import rice.p2p.commonapi.Endpoint;
16  import rice.p2p.replication.Replication;
17  import rice.pastry.NodeHandle;
18  import rice.pastry.NodeIdFactory;
19  import rice.pastry.PastryNode;
20  import rice.pastry.PastryNodeFactory;
21  import rice.pastry.commonapi.PastryIdFactory;
22  import rice.pastry.socket.SocketPastryNodeFactory;
23  import rice.pastry.standard.RandomNodeIdFactory;
24  import rice.persistence.LRUCache;
25  import rice.persistence.MemoryStorage;
26  import rice.persistence.PersistentStorage;
27  import rice.persistence.Storage;
28  import rice.persistence.StorageManagerImpl;
29  
30  /***
31   * connects the peer to the FreePastry-network
32   * 
33   * @author Hannu-Daniel Goiss
34   */
35  public class FreePastryConnection {
36  	private static FreePastryConnection connectionInstance;
37  	
38      private NodeIdFactory noteIdFactory;
39      private PastryNodeFactory pastryNodeFactory;
40      private NodeHandle noteHandle;
41      private PastryNode pastryNode;
42      private PastryIdFactory pastryIdFactory;
43      private MyPastImpl past;
44      private Endpoint endpoint;
45      private int localport;
46      private int numberOfContainers;
47      
48      /***
49  	 * @return the numberOfContainers
50  	 */
51  	public int getNumberOfContainers() {
52  		return numberOfContainers;
53  	}
54  
55  	public int getLocalport() {
56      	return localport;
57      }
58  	
59  	/***
60  	 * @return the endpoint
61  	 */
62  	public Endpoint getEndpoint() {
63  		return endpoint;
64  	}
65  
66  	/***
67  	 * @param endpoint the endpoint to set
68  	 */
69  	public void setEndpoint(Endpoint endpoint) {
70  		this.endpoint = endpoint;
71  	}
72  
73  	public FreePastryConnection(Hashtable<String, String> env) {
74  	    Environment fpenv = new Environment();
75  		
76  	    fpenv.getParameters().setString("nat_search_policy","never");
77  		
78  		try {
79  			String remoteUri = env.get("RemoteURI");
80  			int port = Integer.parseInt(env.get("Port"));
81  			this.numberOfContainers = Integer.parseInt(env.get("Containers"));
82  			
83  			this.localport = port;
84  			
85  			InetSocketAddress bootaddress = null;
86  			try {
87  				if(remoteUri.startsWith("localhost") == false)
88  					bootaddress = new InetSocketAddress(remoteUri.substring(0, remoteUri.indexOf(":")), Integer.parseInt(remoteUri.substring(remoteUri.indexOf(":")+1)));
89  				else
90  					bootaddress = new InetSocketAddress(InetAddress.getLocalHost().getHostAddress(), Integer.parseInt(remoteUri.substring(remoteUri.indexOf(":")+1)));
91  			}
92  			catch(UnknownHostException uhe) {
93  				throw new FatalException(uhe);
94  			}
95  //			InetSocketAddress bootaddress = new InetSocketAddress(remoteUri.substring(0, remoteUri.indexOf(":")), Integer.parseInt(remoteUri.substring(remoteUri.indexOf(":")+1)));
96  
97  		    this.noteIdFactory = new RandomNodeIdFactory(fpenv);
98  		    this.pastryNodeFactory = new SocketPastryNodeFactory(this.noteIdFactory, port, fpenv);
99  		    this.noteHandle = ((SocketPastryNodeFactory)this.pastryNodeFactory).getNodeHandle(bootaddress);
100 		    this.pastryNode = this.pastryNodeFactory.newNode(this.noteHandle);
101 			
102 		    synchronized(this.pastryNode) {
103 		      while(!this.pastryNode.isReady() && !this.pastryNode.joinFailed()) {
104 		        this.pastryNode.wait(500);
105 		        
106 		        if (this.pastryNode.joinFailed()) {
107 		          throw new FatalException("Could not join the FreePastry ring.  Reason:"+pastryNode.joinFailedReason()); 
108 		        }
109 		      }       
110 		    }
111 		    
112 		    System.err.println("Finished creating new node "+pastryNode);    
113 		    
114 		    pastryIdFactory = new rice.pastry.commonapi.PastryIdFactory(fpenv);
115 
116 		    String storageDirectory = "/tmp/storage"+this.pastryNode.getId().hashCode();
117 //		    String storageDirectory = "./MYstorage";
118 		    
119 		    Storage stor = new PersistentStorage(this.pastryIdFactory, storageDirectory, 4 * 1024 * 1024, this.pastryNode.getEnvironment());
120 		    
121 //		    PastImpl needs the following:
122 //		        * The Node
123 //		        * StorageManager
124 //		        * The number of Replicas to maintain — this is a parameter that you can tune to adjust overhead vs. chance of data-loss
125 //		        * An instance name
126 
127 //		    MyPastImpl test = new MyPastImpl(this.pastryNode, new StorageManagerImpl(this.pastryIdFactory, stor, new LRUCache(new MemoryStorage(this.pastryIdFactory), 512 * 1024, this.pastryNode.getEnvironment())), 2, "");
128 
129 		    MyPastImpl test;
130 		    if(numberOfContainers == 0) {
131 			    test = new MyPastImpl(this.pastryNode, new MyStorageManagerImpl(this.pastryIdFactory, stor), 0, "");
132 		    }
133 		    else
134 		    	test = new MyPastImpl(this.pastryNode, new MyStorageManagerImpl(this.pastryIdFactory, stor), this.numberOfContainers-1, "");
135 	
136 		    Replication rep = test.getReplication();
137 		    
138 		    this.past = test;
139 		    
140 //		    this.past = new PastImpl(this.pastryNode, new StorageManagerImpl(this.pastryIdFactory, stor, new LRUCache(new MemoryStorage(this.pastryIdFactory), 512 * 1024, this.pastryNode.getEnvironment())), 3, "");
141 		}
142 		catch(IOException ioe) {
143 			new FatalException(ioe);
144 		}
145 		catch(InterruptedException ie) {
146 			new FatalException(ie);
147 		}
148 	}
149 	
150 	public synchronized static FreePastryConnection getInstance(Hashtable<String, String> env) {
151 		if (connectionInstance == null) {
152 			connectionInstance = new FreePastryConnection(env);
153 		}
154 		return connectionInstance;
155 	}
156 	
157 	public synchronized static FreePastryConnection getInstance() throws XCoreException {
158 		if (connectionInstance == null) {
159 			throw new FatalException("FreePastryConnection not instantiated");
160 		}
161 		return connectionInstance;
162 	}
163 
164 	public void stop() throws XCoreRemoteException {
165 /*		try {
166 			if(delete == true) {
167 				manager.deleteAccount();
168 			}
169 		}
170 		catch(XMPPException xe) {
171 			throw new FatalException(xe);
172 		}
173 		connection.disconnect();
174 */		
175 	}
176 
177 	/***
178 	 * @return the noteIdFactory
179 	 */
180 	public NodeIdFactory getNoteIdFactory() {
181 		return noteIdFactory;
182 	}
183 
184 	/***
185 	 * @return the pastryNodeFactory
186 	 */
187 	public PastryNodeFactory getPastryNodeFactory() {
188 		return pastryNodeFactory;
189 	}
190 
191 	/***
192 	 * @return the noteHandle
193 	 */
194 	public NodeHandle getNoteHandle() {
195 		return noteHandle;
196 	}
197 
198 	/***
199 	 * @return the pastryNode
200 	 */
201 	public PastryNode getPastryNode() {
202 		return pastryNode;
203 	}
204 	
205 	/***
206 	 * @return the pastryIdFactory
207 	 */
208 	public PastryIdFactory getPastryIdFactory() {
209 		return pastryIdFactory;
210 	}
211 
212 	/***
213 	 * @return the past
214 	 */
215 	public MyPastImpl getPast() {
216 		return past;
217 	}
218 
219 }