KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > adapttcp > Test


1 package org.jgroups.tests.adapttcp;
2
3 import org.apache.log4j.Logger;
4
5 import java.io.BufferedReader JavaDoc;
6 import java.io.FileNotFoundException JavaDoc;
7 import java.io.FileReader JavaDoc;
8 import java.io.IOException JavaDoc;
9 import java.net.InetAddress JavaDoc;
10 import java.net.ServerSocket JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.StringTokenizer JavaDoc;
14
15 /** You start the test by running this class.
16  * It only parses the initial params from the config.txt
17  * file (or any other file you wish to pass as the argument)
18  * and instantiates a new JGroupsTester object.<br>
19  * Use parameters -Xbatch -Xconcurrentio (Solaris specific)
20  * @author Milcan Prica (prica@deei.units.it)
21  * @author Bela Ban (belaban@yahoo.com)
22
23  */

24 public class Test {
25     public static int srv_port=7777;
26     public static int grpMembers=4;
27
28     public static void main(String JavaDoc[] args) {
29         String JavaDoc config="config.txt";
30         BufferedReader JavaDoc fileReader;
31         String JavaDoc line;
32
33         boolean sender=false;
34         int num_msgs=1000;
35         int msg_size=500;
36
37         int num_senders=1;
38         long log_interval=1000;
39
40
41         ServerSocket JavaDoc srv_sock;
42         List JavaDoc nodes=new ArrayList JavaDoc(); // list of cluster nodes (InetAddresses)
43

44         for(int i=0; i < args.length; i++) {
45             if("-sender".equals(args[i])) {
46                 sender=true;
47                 continue;
48             }
49             if("-receiver".equals(args[i])) {
50                 sender=false;
51                 continue;
52             }
53             if("-config".equals(args[i])) {
54                 config=args[++i];
55                 continue;
56             }
57             help();
58             return;
59         }
60
61
62
63         try {
64             fileReader=new BufferedReader JavaDoc(new FileReader JavaDoc(config));
65             while((line=fileReader.readLine()) != null) {
66                 if(line.startsWith("#"))
67                     continue;
68                 else if(line.startsWith("NUM_MSGS=")) {
69                     num_msgs=Integer.parseInt(line.substring(line.indexOf('=') + 1,
70                             line.indexOf(';')));
71                 }
72                 else if(line.startsWith("MSG_SIZE=")) {
73                     msg_size=Integer.parseInt(line.substring(line.indexOf('=') + 1,
74                             line.indexOf(';')));
75                 }
76                 else if(line.startsWith("GRPMEMBERS=")) {
77                     grpMembers=Integer.parseInt(line.substring(line.indexOf('=') + 1,
78                             line.indexOf(';')));
79                 }
80                 else if(line.startsWith("NUM_SENDERS=")) {
81                     num_senders=Integer.parseInt(line.substring(line.indexOf('=') + 1,
82                             line.indexOf(';')));
83                 }
84                 else if(line.startsWith("LOG_INTERVAL=")) {
85                     log_interval=Long.parseLong(line.substring(line.indexOf('=') + 1,
86                             line.indexOf(';')));
87                 }
88                 else if(line.startsWith("CLUSTER=")) {
89                     nodes=parseCommaDelimitedList(line.substring(line.indexOf('=') + 1,
90                             line.indexOf(';')).trim());
91
92                 }
93                 else if(line.startsWith("GNUPLOT_OUTPUT=")) {
94                     // only parse if not yet set by -Dgnuplot_output=true option (overrides file)
95
if(System.getProperty("gnuplot_output") == null) {
96                         String JavaDoc gnuplot_output=line.substring(line.indexOf('=') + 1,
97                                 line.indexOf(';')).trim();
98                         if(gnuplot_output != null)
99                             System.setProperty("gnuplot_output", gnuplot_output);
100                     }
101                 }
102             }
103             fileReader.close();
104
105             System.out.println("Javagroups test:");
106             String JavaDoc s="Initial params parsing completed. Starting test"
107                     + " with these values:\n"
108                     + "Sender:" + sender + " num_msgs:" + num_msgs
109                     + " Size(bytes):" + msg_size + " # Mbrs:" + grpMembers
110                     + " Senders: " + num_senders
111                     + "\nLog interval: " + log_interval + '\n';
112
113             System.out.println(s);
114             Logger.getLogger(Test.class).info("main(): " + s);
115
116             srv_sock=new ServerSocket JavaDoc(srv_port);
117
118             new TcpTester(sender, num_msgs,
119                     msg_size, grpMembers, num_senders,
120                     log_interval, srv_sock, nodes).initialize();
121         }
122         catch(FileNotFoundException JavaDoc notFound) {
123             System.err.println("File not found.\n" + notFound);
124         }
125         catch(IOException JavaDoc ioError) {
126             System.err.println(ioError);
127         }
128         catch(Exception JavaDoc ex) {
129             ex.printStackTrace();
130         }
131     }
132
133
134
135     public static List JavaDoc parseCommaDelimitedList(String JavaDoc s) throws Exception JavaDoc {
136         List JavaDoc retval=new ArrayList JavaDoc();
137         StringTokenizer JavaDoc tok;
138         InetAddress JavaDoc host;
139
140         if(s == null) return null;
141         tok=new StringTokenizer JavaDoc(s, ",");
142         while(tok.hasMoreTokens()) {
143             host=InetAddress.getByName(tok.nextToken());
144             retval.add(host);
145         }
146         return retval;
147     }
148
149     static void help() {
150         System.out.println("Test [-help] ([-sender] | [-receiver]) [-config <config file>]");
151     }
152 }
153
Popular Tags