KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jgroups.tests.adaptudp;
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.DatagramSocket JavaDoc;
10 import java.net.InetAddress JavaDoc;
11 import java.net.MulticastSocket JavaDoc;
12 import java.util.ArrayList JavaDoc;
13 import java.util.List JavaDoc;
14 import java.util.StringTokenizer JavaDoc;
15
16 /** You start the test by running this class.
17  * It only parses the initial params from the config.txt
18  * file (or any other file you wish to pass as the argument)
19  * and instantiates a new JGroupsTester object.<br>
20  * Use parameters -Xbatch -Xconcurrentio (Solaris specific)
21  * @author Milcan Prica (prica@deei.units.it)
22  * @author Bela Ban (belaban@yahoo.com)
23
24  */

25 public class Test {
26     public static int mcast_port=7777;
27     public static String JavaDoc mcast_addr="228.8.8.8";
28     public static int grpMembers=4;
29
30     public static void main(String JavaDoc[] args) {
31         String JavaDoc config="config.txt";
32         BufferedReader JavaDoc fileReader;
33         String JavaDoc line;
34
35         boolean sender=false;
36         int num_msgs=1000;
37         int msg_size=500;
38
39         int num_senders=1;
40         long log_interval=1000;
41
42
43         MulticastSocket JavaDoc recv_sock;
44         DatagramSocket JavaDoc send_sock;
45
46         for(int i=0; i < args.length; i++) {
47             if("-sender".equals(args[i])) {
48                 sender=true;
49                 continue;
50             }
51             if("-receiver".equals(args[i])) {
52                 sender=false;
53                 continue;
54             }
55             if("-config".equals(args[i])) {
56                 config=args[++i];
57                 continue;
58             }
59             help();
60             return;
61         }
62
63
64
65         try {
66             fileReader=new BufferedReader JavaDoc(new FileReader JavaDoc(config));
67             while((line=fileReader.readLine()) != null) {
68                 if(line.startsWith("#"))
69                     continue;
70                 else if(line.startsWith("NUM_MSGS=")) {
71                     num_msgs=Integer.parseInt(line.substring(line.indexOf('=') + 1,
72                             line.indexOf(';')));
73                 }
74                 else if(line.startsWith("MSG_SIZE=")) {
75                     msg_size=Integer.parseInt(line.substring(line.indexOf('=') + 1,
76                             line.indexOf(';')));
77                 }
78                 else if(line.startsWith("GRPMEMBERS=")) {
79                     grpMembers=Integer.parseInt(line.substring(line.indexOf('=') + 1,
80                             line.indexOf(';')));
81                 }
82                 else if(line.startsWith("NUM_SENDERS=")) {
83                     num_senders=Integer.parseInt(line.substring(line.indexOf('=') + 1,
84                             line.indexOf(';')));
85                 }
86                 else if(line.startsWith("LOG_INTERVAL=")) {
87                     log_interval=Long.parseLong(line.substring(line.indexOf('=') + 1,
88                             line.indexOf(';')));
89                 }
90                 else if(line.startsWith("GNUPLOT_OUTPUT=")) {
91                     // only parse if not yet set by -Dgnuplot_output=true option (overrides file)
92
if(System.getProperty("gnuplot_output") == null) {
93                         String JavaDoc gnuplot_output=line.substring(line.indexOf('=') + 1,
94                                 line.indexOf(';')).trim();
95                         if(gnuplot_output != null)
96                             System.setProperty("gnuplot_output", gnuplot_output);
97                     }
98                 }
99             }
100             fileReader.close();
101
102             System.out.println("Javagroups test:");
103             String JavaDoc s="Initial params parsing completed. Starting test"
104                     + " with these values:\n"
105                     + "Sender:" + sender + " num_msgs:" + num_msgs
106                     + " Size(bytes):" + msg_size + " # Mbrs:" + grpMembers
107                     + " Senders: " + num_senders
108                     + "\nLog interval: " + log_interval + '\n';
109
110             System.out.println(s);
111             Logger.getLogger(Test.class).info("main(): " + s);
112
113             recv_sock=new MulticastSocket JavaDoc(mcast_port);
114             recv_sock.joinGroup(InetAddress.getByName(mcast_addr));
115
116             send_sock=new DatagramSocket JavaDoc();
117
118             new UdpTester(recv_sock, send_sock, sender, num_msgs,
119                     msg_size, grpMembers, num_senders,
120                     log_interval).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