001: // $Id: Utilities.java,v 1.10 2006/10/11 14:33:53 belaban Exp $
002:
003: package org.jgroups.tests.stack;
004:
005: import org.apache.commons.logging.Log;
006: import org.apache.commons.logging.LogFactory;
007: import org.jgroups.stack.GossipClient;
008: import org.jgroups.stack.GossipRouter;
009: import org.jgroups.stack.IpAddress;
010:
011: import java.net.ServerSocket;
012:
013: /**
014: * Utility functions shared by stack tests.
015: *
016: * @author Ovidiu Feodorov <ovidiuf@users.sourceforge.net>
017: * @version $Revision: 1.10 $
018: * @since 2.2.1
019: */
020: public class Utilities {
021:
022: static Log log = LogFactory.getLog(Utilities.class);
023:
024: private static GossipRouter gossipRouter = null;
025:
026: public static int startGossipRouter() throws Exception {
027: return startGossipRouter(GossipRouter.EXPIRY_TIME, "localhost",
028: GossipRouter.GOSSIP_REQUEST_TIMEOUT,
029: GossipRouter.ROUTING_CLIENT_REPLY_TIMEOUT);
030: }
031:
032: public static int startGossipRouter(String bind_addr)
033: throws Exception {
034: return startGossipRouter(GossipRouter.EXPIRY_TIME, bind_addr,
035: GossipRouter.GOSSIP_REQUEST_TIMEOUT,
036: GossipRouter.ROUTING_CLIENT_REPLY_TIMEOUT);
037: }
038:
039: public static int startGossipRouter(long expiryTime)
040: throws Exception {
041: return startGossipRouter(expiryTime, "localhost",
042: GossipRouter.GOSSIP_REQUEST_TIMEOUT,
043: GossipRouter.ROUTING_CLIENT_REPLY_TIMEOUT);
044: }
045:
046: public static int startGossipRouter(long expiryTime,
047: String bind_addr) throws Exception {
048: return startGossipRouter(expiryTime, bind_addr,
049: GossipRouter.GOSSIP_REQUEST_TIMEOUT,
050: GossipRouter.ROUTING_CLIENT_REPLY_TIMEOUT);
051: }
052:
053: /**
054: * Starts the router on a separate thread and makes sure it answers a dummy
055: * GossipRouter.GET request.
056: *
057: * @return the port GossipRouter is listening on.
058: */
059: public static int startGossipRouter(final long expiryTime,
060: final String bind_addr, final long gossipRequestTimeout,
061: final long routingClientReplyTimeout) throws Exception {
062:
063: if (gossipRouter != null)
064: throw new Exception("GossipRouter already started");
065:
066: final int routerPort = getFreePort();
067: try {
068: gossipRouter = new GossipRouter(routerPort, bind_addr,
069: expiryTime, gossipRequestTimeout,
070: routingClientReplyTimeout);
071: gossipRouter.start();
072: } catch (Exception e) {
073: log.error("Failed to start the router on port "
074: + routerPort);
075: gossipRouter = null;
076: throw e;
077: }
078:
079: GossipClient client = null;
080:
081: // verify the router - try for 10 secs to connect
082: long startms = System.currentTimeMillis();
083: Exception lastConnectException = null;
084: long crtms = startms;
085:
086: while (crtms - startms < 10000) {
087: try {
088: client = new GossipClient(new IpAddress(bind_addr,
089: routerPort), 10000);
090: client
091: .getMembers("Utilities:startGossipRouterConnectionTest");
092: lastConnectException = null;
093: break;
094: } catch (Exception e) {
095: if (client != null)
096: client.stop();
097: lastConnectException = e;
098: Thread.sleep(1000);
099: crtms = System.currentTimeMillis();
100: }
101: }
102:
103: if (lastConnectException != null) {
104: lastConnectException.printStackTrace();
105: throw new Exception("Cannot connect to the router");
106: }
107: return routerPort;
108: }
109:
110: public static void stopGossipRouter() throws Exception {
111: if (gossipRouter == null) {
112: throw new Exception("There's no GossipRouter running");
113: }
114: gossipRouter.stop();
115: System.out.println("router stopped");
116: gossipRouter = null;
117: }
118:
119: /**
120: * Returns a port we can bind to.
121: */
122: public static int getFreePort() throws Exception {
123: ServerSocket ss = new ServerSocket(0);
124: int port = ss.getLocalPort();
125: ss.close();
126: return port;
127: }
128:
129: }
|