1 3 package org.jgroups.tests.stack; 4 5 import org.apache.commons.logging.Log; 6 import org.apache.commons.logging.LogFactory; 7 import org.jgroups.stack.GossipClient; 8 import org.jgroups.stack.GossipRouter; 9 import org.jgroups.stack.IpAddress; 10 11 import java.net.ServerSocket ; 12 13 20 public class Utilities { 21 22 static Log log=LogFactory.getLog(Utilities.class); 23 24 25 private static GossipRouter gossipRouter=null; 26 27 public static int startGossipRouter() throws Exception { 28 return startGossipRouter(GossipRouter.EXPIRY_TIME, 29 "localhost", 30 GossipRouter.GOSSIP_REQUEST_TIMEOUT, 31 GossipRouter.ROUTING_CLIENT_REPLY_TIMEOUT); 32 } 33 34 35 public static int startGossipRouter(String bind_addr) throws Exception { 36 return startGossipRouter(GossipRouter.EXPIRY_TIME, 37 bind_addr, 38 GossipRouter.GOSSIP_REQUEST_TIMEOUT, 39 GossipRouter.ROUTING_CLIENT_REPLY_TIMEOUT); 40 } 41 42 43 public static int startGossipRouter(long expiryTime) throws Exception { 44 return startGossipRouter(expiryTime, 45 "localhost", 46 GossipRouter.GOSSIP_REQUEST_TIMEOUT, 47 GossipRouter.ROUTING_CLIENT_REPLY_TIMEOUT); 48 } 49 50 51 public static int startGossipRouter(long expiryTime, String bind_addr) throws Exception { 52 return startGossipRouter(expiryTime, 53 bind_addr, 54 GossipRouter.GOSSIP_REQUEST_TIMEOUT, 55 GossipRouter.ROUTING_CLIENT_REPLY_TIMEOUT); 56 } 57 58 64 public static int startGossipRouter(final long expiryTime, 65 final String bind_addr, 66 final long gossipRequestTimeout, 67 final long routingClientReplyTimeout) throws Exception { 68 69 if(gossipRouter != null) 70 throw new Exception ("GossipRouter already started"); 71 72 final int routerPort=getFreePort(); 73 try { 74 gossipRouter=new GossipRouter(routerPort, bind_addr, expiryTime, gossipRequestTimeout, routingClientReplyTimeout); 75 gossipRouter.start(); 76 } 77 catch(Exception e) { 78 log.error("Failed to start the router on port " + routerPort); 79 gossipRouter=null; 80 throw e; 81 } 82 83 GossipClient client=null; 84 85 long startms=System.currentTimeMillis(); 87 Exception lastConnectException=null; 88 long crtms=startms; 89 90 while(crtms - startms < 10000) { 91 try { 92 client=new GossipClient(new IpAddress(bind_addr, routerPort), 10000); 93 client.getMembers("Utilities:startGossipRouterConnectionTest"); 94 lastConnectException=null; 95 break; 96 } 97 catch(Exception e) { 98 if(client != null) 99 client.stop(); 100 lastConnectException=e; 101 Thread.sleep(1000); 102 crtms=System.currentTimeMillis(); 103 } 104 } 105 106 if(lastConnectException != null) { 107 lastConnectException.printStackTrace(); 108 throw new Exception ("Cannot connect to the router"); 109 } 110 return routerPort; 111 } 112 113 114 public static void stopGossipRouter() throws Exception { 115 if(gossipRouter == null) { 116 throw new Exception ("There's no GossipRouter running"); 117 } 118 gossipRouter.stop(); 119 System.out.println("router stopped"); 120 gossipRouter=null; 121 } 122 123 124 125 128 public static int getFreePort() throws Exception { 129 ServerSocket ss=new ServerSocket (0); 130 int port=ss.getLocalPort(); 131 ss.close(); 132 return port; 133 } 134 135 } 136 | Popular Tags |