KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > FragTest2


1 // $Id: FragTest2.java,v 1.6 2005/04/13 10:02:56 belaban Exp $
2

3
4 package org.jgroups.tests;
5
6
7 import org.jgroups.Channel;
8 import org.jgroups.JChannel;
9 import org.jgroups.Message;
10 import org.jgroups.util.Util;
11
12
13 /**
14  * Tests the fragmentation protocol (FRAG). A large message is broadcast to the group (sender does not
15  * receive its own broadcasts, though). The fragmentation protocol fragments messages bigger than
16  * 8K and reassembles them at the receiver. Messages sent are 100K in size (configurable).
17  */

18 public class FragTest2 {
19     int mode=0; // 0=receiver, 1=sender
20
Channel channel;
21     String JavaDoc props;
22     int i=1;
23     Message msg;
24     Object JavaDoc obj;
25     int MSG_SIZE; // bytes
26
String JavaDoc groupname="FragTest2Group";
27     char sendingChar;
28     int num_msgs=5;
29     long timeout=1000;
30     int frag_size=20000;
31
32
33     public FragTest2(char character, int mode, int msg_size, int num_msgs, long timeout, int frag_size) {
34         this.sendingChar=character;
35         this.mode=mode;
36         this.MSG_SIZE=msg_size;
37         this.num_msgs=num_msgs;
38         this.timeout=timeout;
39         this.frag_size=frag_size;
40
41
42         props="UDP(mcast_addr=228.1.2.3;mcast_port=45566;ip_ttl=0;" +
43                 "mcast_recv_buf_size=" + (frag_size * 4) + ";mcast_send_buf_size=" + (frag_size * 2) + "):" +
44                 "PING(timeout=3000;num_initial_members=1):" +
45                 "FD(timeout=5000):" +
46                 "VERIFY_SUSPECT(timeout=1500):" +
47                 "pbcast.NAKACK(gc_lag=20;retransmit_timeout=2000):" +
48                 "UNICAST(timeout=2000):" +
49                 "pbcast.STABLE(desired_avg_gossip=5000):" +
50                 "FRAG(frag_size=" + frag_size + "):" +
51                 "pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;" +
52                 "shun=false;print_local_addr=true)";
53     }
54
55
56     public void start() throws Exception JavaDoc {
57
58         channel=new JChannel(props);
59         if(mode == 1) channel.setOpt(Channel.LOCAL, Boolean.FALSE);
60         channel.connect(groupname);
61
62         if(mode == 1) {
63             for(int j=0; j < num_msgs; j++) {
64                 msg=createBigMessage(MSG_SIZE);
65                 System.out.println("Sending msg (" + MSG_SIZE + " bytes)");
66                 channel.send(msg);
67                 System.out.println("Done Sending msg (" + MSG_SIZE + " bytes)");
68                 Util.sleep(timeout);
69             }
70             System.out.println("Press [return] to exit");
71             System.in.read();
72         }
73         else {
74             System.out.println("Waiting for messages:");
75
76             while(true) {
77                 try {
78                     obj=channel.receive(0);
79                     if(obj instanceof Message) {
80                         System.out.println("Received message: " + obj);
81                         Message tmp=(Message)obj;
82                         byte[] buf=tmp.getBuffer();
83                         for(int i=0; i < (10 < MSG_SIZE ? 10 : MSG_SIZE); i++) {
84                             System.out.print((char)buf[i]);
85                         }
86                         System.out.println();
87
88                     }
89                 }
90                 catch(Exception JavaDoc e) {
91                     System.err.println(e);
92                 }
93             }
94
95         }
96         channel.close();
97     }
98
99
100     Message createBigMessage(int size) {
101         byte[] buf=new byte[size];
102         for(int i=0; i < buf.length; i++) buf[i]=(byte)sendingChar;
103         return new Message(null, null, buf);
104     }
105
106
107     public static void main(String JavaDoc[] args) {
108         char defaultChar='A';
109         int default_mode=0; // receiver
110
int MSG_SIZE=30000;
111         int num_msgs=10;
112         long timeout=3000;
113         int frag_size=20000;
114
115         for(int i=0; i < args.length; i++) {
116             if("-help".equals(args[i])) {
117                 usage();
118                 return;
119             }
120             if("-sender".equals(args[i])) {
121                 default_mode=1;
122                 continue;
123             }
124             if("-size".equals(args[i])) {
125                 MSG_SIZE=Integer.parseInt(args[++i]);
126                 continue;
127             }
128             if("-num_msgs".equals(args[i])) {
129                 num_msgs=Integer.parseInt(args[++i]);
130                 continue;
131             }
132             if("-frag_size".equals(args[i])) {
133                 frag_size=Integer.parseInt(args[++i]);
134                 continue;
135             }
136             if("-timeout".equals(args[i])) {
137                 timeout=Long.parseLong(args[++i]);
138                 continue;
139             }
140             if("-char".equals(args[i])) {
141                 defaultChar=args[++i].charAt(0);
142                 continue;
143             }
144             usage();
145             return;
146         }
147
148         try {
149             new FragTest2(defaultChar, default_mode, MSG_SIZE, num_msgs, timeout, frag_size).start();
150         }
151         catch(Exception JavaDoc e) {
152             System.err.println(e);
153         }
154     }
155
156
157     static void usage() {
158         System.out.println("FragTest2 [-sender] [-size <message size (in bytes)>] [-timeout <msecs>]" +
159                            " [-num_msgs <number of messages>] [-char <frag character>] " +
160                            "[-frag_size <fragmentation size>] [-help]");
161     }
162
163 }
164
165
Popular Tags