KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > stack > Utilities


1 // $Id: Utilities.java,v 1.10 2006/10/11 14:33:53 belaban Exp $
2

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 JavaDoc;
12
13 /**
14  * Utility functions shared by stack tests.
15  *
16  * @author Ovidiu Feodorov <ovidiuf@users.sourceforge.net>
17  * @version $Revision: 1.10 $
18  * @since 2.2.1
19  */

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 JavaDoc {
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 JavaDoc bind_addr) throws Exception JavaDoc {
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 JavaDoc {
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 JavaDoc bind_addr) throws Exception JavaDoc {
52         return startGossipRouter(expiryTime,
53                                  bind_addr,
54                                  GossipRouter.GOSSIP_REQUEST_TIMEOUT,
55                                  GossipRouter.ROUTING_CLIENT_REPLY_TIMEOUT);
56     }
57
58     /**
59      * Starts the router on a separate thread and makes sure it answers a dummy
60      * GossipRouter.GET request.
61      *
62      * @return the port GossipRouter is listening on.
63      */

64     public static int startGossipRouter(final long expiryTime,
65                                         final String JavaDoc bind_addr,
66                                         final long gossipRequestTimeout,
67                                         final long routingClientReplyTimeout) throws Exception JavaDoc {
68
69         if(gossipRouter != null)
70             throw new Exception JavaDoc("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 JavaDoc 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         // verify the router - try for 10 secs to connect
86
long startms=System.currentTimeMillis();
87         Exception JavaDoc 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 JavaDoc 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 JavaDoc("Cannot connect to the router");
109         }
110         return routerPort;
111     }
112
113
114     public static void stopGossipRouter() throws Exception JavaDoc {
115         if(gossipRouter == null) {
116             throw new Exception JavaDoc("There's no GossipRouter running");
117         }
118         gossipRouter.stop();
119         System.out.println("router stopped");
120         gossipRouter=null;
121     }
122
123
124
125     /**
126      * Returns a port we can bind to.
127      */

128     public static int getFreePort() throws Exception JavaDoc {
129         ServerSocket JavaDoc ss=new ServerSocket JavaDoc(0);
130         int port=ss.getLocalPort();
131         ss.close();
132         return port;
133     }
134
135 }
136
Popular Tags