PageRenderTime 119ms CodeModel.GetById 45ms RepoModel.GetById 1ms app.codeStats 0ms

/org.kevoree.extra.freePastry/src/main/java/rice/tutorial/prioritylistener/DistTutorial.java

https://github.com/dukeboard/kevoree-extra
Java | 265 lines | 139 code | 39 blank | 87 comment | 19 complexity | f97702d85ed6e767b52825c05a2c85a5 MD5 | raw file
  1. /*******************************************************************************
  2. "FreePastry" Peer-to-Peer Application Development Substrate
  3. Copyright 2002-2007, Rice University. Copyright 2006-2007, Max Planck Institute
  4. for Software Systems. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions are
  7. met:
  8. - Redistributions of source code must retain the above copyright
  9. notice, this list of conditions and the following disclaimer.
  10. - Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions and the following disclaimer in the
  12. documentation and/or other materials provided with the distribution.
  13. - Neither the name of Rice University (RICE), Max Planck Institute for Software
  14. Systems (MPI-SWS) nor the names of its contributors may be used to endorse or
  15. promote products derived from this software without specific prior written
  16. permission.
  17. This software is provided by RICE, MPI-SWS and the contributors on an "as is"
  18. basis, without any representations or warranties of any kind, express or implied
  19. including, but not limited to, representations or warranties of
  20. non-infringement, merchantability or fitness for a particular purpose. In no
  21. event shall RICE, MPI-SWS or contributors be liable for any direct, indirect,
  22. incidental, special, exemplary, or consequential damages (including, but not
  23. limited to, procurement of substitute goods or services; loss of use, data, or
  24. profits; or business interruption) however caused and on any theory of
  25. liability, whether in contract, strict liability, or tort (including negligence
  26. or otherwise) arising in any way out of the use of this software, even if
  27. advised of the possibility of such damage.
  28. *******************************************************************************/
  29. package rice.tutorial.prioritylistener;
  30. import java.io.IOException;
  31. import java.net.InetAddress;
  32. import java.net.InetSocketAddress;
  33. import java.util.Iterator;
  34. import java.util.Map;
  35. import java.util.Vector;
  36. import org.mpisws.p2p.transport.TransportLayerListener;
  37. import org.mpisws.p2p.transport.commonapi.CommonAPITransportLayerImpl;
  38. import org.mpisws.p2p.transport.multiaddress.MultiInetSocketAddress;
  39. import org.mpisws.p2p.transport.priority.PriorityTransportLayer;
  40. import org.mpisws.p2p.transport.priority.PriorityTransportLayerListener;
  41. import rice.environment.Environment;
  42. import rice.p2p.commonapi.Id;
  43. import rice.pastry.NodeHandle;
  44. import rice.pastry.NodeIdFactory;
  45. import rice.pastry.PastryNode;
  46. import rice.pastry.PastryNodeFactory;
  47. import rice.pastry.leafset.LeafSet;
  48. import rice.pastry.socket.SocketPastryNodeFactory;
  49. import rice.pastry.standard.RandomNodeIdFactory;
  50. /**
  51. * This tutorial shows how to setup a FreePastry node using the Socket Protocol.
  52. *
  53. * @author Jeff Hoye
  54. */
  55. public class DistTutorial {
  56. // this will keep track of our applications
  57. Vector<MyApp> apps = new Vector<MyApp>();
  58. /**
  59. * This constructor launches numNodes PastryNodes. They will bootstrap
  60. * to an existing ring if one exists at the specified location, otherwise
  61. * it will start a new ring.
  62. *
  63. * @param bindport the local port to bind to
  64. * @param bootaddress the IP:port of the node to boot from
  65. * @param numNodes the number of nodes to create in this JVM
  66. * @param env the environment for these nodes
  67. */
  68. public DistTutorial(int bindport, InetSocketAddress bootaddress, int numNodes, Environment env) throws Exception {
  69. // Generate the NodeIds Randomly
  70. NodeIdFactory nidFactory = new RandomNodeIdFactory(env);
  71. // construct the PastryNodeFactory, this is how we use rice.pastry.socket
  72. PastryNodeFactory factory = new SocketPastryNodeFactory(nidFactory, bindport, env);
  73. // loop to construct the nodes/apps
  74. for (int curNode = 0; curNode < numNodes; curNode++) {
  75. // construct a node, passing the null boothandle on the first loop will cause the node to start its own ring
  76. final PastryNode node = factory.newNode();
  77. // construct a new MyApp
  78. MyApp app = new MyApp(node);
  79. apps.add(app);
  80. // boot the node
  81. node.boot(bootaddress);
  82. PriorityTransportLayer<MultiInetSocketAddress> ptl = (PriorityTransportLayer<MultiInetSocketAddress>)node.getVars().get(SocketPastryNodeFactory.PRIORITY_TL);
  83. ptl.addPriorityTransportLayerListener(new PriorityTransportLayerListener<MultiInetSocketAddress>() {
  84. public void wrote(int bytes, MultiInetSocketAddress i,
  85. Map<String, Object> options, boolean passthrough, boolean socket) {
  86. int address = 0;
  87. Integer addressI = (Integer)options.get(CommonAPITransportLayerImpl.MSG_ADDR);
  88. if (addressI != null) {
  89. address = addressI.intValue();
  90. }
  91. String messageClass = (String)options.get(CommonAPITransportLayerImpl.MSG_CLASS);
  92. String toString = (String)options.get(CommonAPITransportLayerImpl.MSG_STRING);
  93. short type = 0;
  94. Short typeS = (Short)options.get(CommonAPITransportLayerImpl.MSG_TYPE);
  95. if (typeS != null) {
  96. type = typeS.shortValue();
  97. }
  98. System.out.println(node+" wrote a "+messageClass+" of size "+bytes+" to "+i+" addr:"+address+" type:"+type+" "+toString);
  99. }
  100. public void read(int bytes, MultiInetSocketAddress i,
  101. Map<String, Object> options, boolean passthrough, boolean socket) {
  102. System.out.println(node+" read a message of size "+bytes+" from "+i);
  103. }
  104. public void enqueued(int bytes, MultiInetSocketAddress i,
  105. Map<String, Object> options, boolean passthrough, boolean socket) {
  106. int address = 0;
  107. Integer addressI = (Integer)options.get(CommonAPITransportLayerImpl.MSG_ADDR);
  108. if (addressI != null) {
  109. address = addressI.intValue();
  110. }
  111. String messageClass = (String)options.get(CommonAPITransportLayerImpl.MSG_CLASS);
  112. String toString = (String)options.get(CommonAPITransportLayerImpl.MSG_STRING);
  113. short type = 0;
  114. Short typeS = (Short)options.get(CommonAPITransportLayerImpl.MSG_TYPE);
  115. if (typeS != null) {
  116. type = typeS.shortValue();
  117. }
  118. System.out.println(node+" enqueued a "+messageClass+" of size "+bytes+" to "+i+" addr:"+address+" type:"+type+" "+toString);
  119. }
  120. public void dropped(int bytes, MultiInetSocketAddress i,
  121. Map<String, Object> options, boolean passthrough, boolean socket) {
  122. int address = 0;
  123. Integer addressI = (Integer)options.get(CommonAPITransportLayerImpl.MSG_ADDR);
  124. if (addressI != null) {
  125. address = addressI.intValue();
  126. }
  127. String messageClass = (String)options.get(CommonAPITransportLayerImpl.MSG_CLASS);
  128. String toString = (String)options.get(CommonAPITransportLayerImpl.MSG_STRING);
  129. short type = 0;
  130. Short typeS = (Short)options.get(CommonAPITransportLayerImpl.MSG_TYPE);
  131. if (typeS != null) {
  132. type = typeS.shortValue();
  133. }
  134. System.out.println(node+" dropped a "+messageClass+" of size "+bytes+" to "+i+" addr:"+address+" type:"+type+" "+toString);
  135. }
  136. });
  137. // the node may require sending several messages to fully boot into the ring
  138. synchronized(node) {
  139. while(!node.isReady() && !node.joinFailed()) {
  140. // delay so we don't busy-wait
  141. node.wait(500);
  142. // abort if can't join
  143. if (node.joinFailed()) {
  144. throw new IOException("Could not join the FreePastry ring. Reason:"+node.joinFailedReason());
  145. }
  146. }
  147. }
  148. System.out.println("Finished creating new node "+node);
  149. }
  150. // wait 10 seconds
  151. env.getTimeSource().sleep(10000);
  152. // route 10 messages
  153. for (int i = 0; i < 10; i++) {
  154. // for each app
  155. Iterator<MyApp> appIterator = apps.iterator();
  156. while(appIterator.hasNext()) {
  157. MyApp app = (MyApp)appIterator.next();
  158. // pick a key at random
  159. Id randId = nidFactory.generateNodeId();
  160. // send to that key
  161. app.routeMyMsg(randId);
  162. // wait a bit
  163. env.getTimeSource().sleep(100);
  164. }
  165. }
  166. // wait 1 second
  167. env.getTimeSource().sleep(1000);
  168. // for each app
  169. Iterator<MyApp> appIterator = apps.iterator();
  170. while(appIterator.hasNext()) {
  171. MyApp app = (MyApp)appIterator.next();
  172. PastryNode node = (PastryNode)app.getNode();
  173. // send directly to my leafset
  174. LeafSet leafSet = node.getLeafSet();
  175. // this is a typical loop to cover your leafset. Note that if the leafset
  176. // overlaps, then duplicate nodes will be sent to twice
  177. for (int i=-leafSet.ccwSize(); i<=leafSet.cwSize(); i++) {
  178. if (i != 0) { // don't send to self
  179. // select the item
  180. NodeHandle nh = leafSet.get(i);
  181. // send the message directly to the node
  182. app.routeMyMsgDirect(nh);
  183. // wait a bit
  184. env.getTimeSource().sleep(100);
  185. }
  186. }
  187. }
  188. }
  189. /**
  190. * Usage:
  191. * java [-cp FreePastry-<version>.jar] rice.tutorial.lesson4.DistTutorial localbindport bootIP bootPort numNodes
  192. * example java rice.tutorial.DistTutorial 9001 pokey.cs.almamater.edu 9001 10
  193. */
  194. public static void main(String[] args) throws Exception {
  195. // Loads pastry settings
  196. Environment env = new Environment();
  197. // disable the UPnP setting (in case you are testing this on a NATted LAN)
  198. env.getParameters().setString("nat_search_policy","never");
  199. try {
  200. // the port to use locally
  201. int bindport = Integer.parseInt(args[0]);
  202. // build the bootaddress from the command line args
  203. InetAddress bootaddr = InetAddress.getByName(args[1]);
  204. int bootport = Integer.parseInt(args[2]);
  205. InetSocketAddress bootaddress = new InetSocketAddress(bootaddr,bootport);
  206. // the number of nodes to use
  207. int numNodes = Integer.parseInt(args[3]);
  208. // launch our node!
  209. DistTutorial dt = new DistTutorial(bindport, bootaddress, numNodes, env);
  210. } catch (Exception e) {
  211. // remind user how to use
  212. System.out.println("Usage:");
  213. System.out.println("java [-cp FreePastry-<version>.jar] rice.tutorial.lesson4.DistTutorial localbindport bootIP bootPort numNodes");
  214. System.out.println("example java rice.tutorial.DistTutorial 9001 pokey.cs.almamater.edu 9001 10");
  215. throw e;
  216. }
  217. }
  218. }