KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: FragTestUnicast.java,v 1.5 2004/07/05 14:15:11 belaban Exp $
2

3
4 package org.jgroups.tests;
5
6
7 import org.jgroups.*;
8 import org.jgroups.util.Util;
9
10 import java.util.Vector JavaDoc;
11
12
13
14
15 /**
16  * Tests the fragmentation protocol (FRAG) for large unicast messages. The first member needs to be started as
17  * receiver. The second member needs to be started as sender (-sender). The latter then starts sending unicast
18  * messages to the receiver, which will be fragmented. If the relation between message size and fragmentation
19  * size results in many fragments to be sent, then the receive and send buffer sizes in UDP have to be
20  * adjusted accordingly, otherwise packets will be dropped and retransmitted.
21  * @author Bela Ban April 19 2001
22  */

23 public class FragTestUnicast {
24     int mode=0; // 0=receiver, 1=sender
25
Channel channel;
26     String JavaDoc props;
27     int i=1;
28     Message msg;
29     Object JavaDoc obj;
30     int MSG_SIZE; // bytes
31
String JavaDoc groupname="FragTestUnicastGroup";
32     char sendingChar;
33     int num_msgs=10;
34     long timeout=3000;
35     int frag_size=20000;
36     Address local_addr;
37
38
39
40     public FragTestUnicast(char character , int mode, int msg_size, int num_msgs, long timeout, int frag_size) {
41     this.sendingChar = character;
42     this.mode=mode;
43     this.MSG_SIZE=msg_size;
44     this.num_msgs=num_msgs;
45     this.timeout=timeout;
46     this.frag_size=frag_size;
47
48
49     
50     props="UDP(mcast_addr=224.10.20.3;mcast_port=45566;ip_ttl=32;" +
51             "ucast_send_buf_size=" + (frag_size*2) + ";ucast_recv_buf_size=" + (frag_size*4) + "):" +
52             "PING(timeout=3000;num_initial_members=2):" +
53             "FD(timeout=5000):" +
54             "VERIFY_SUSPECT(timeout=1500):" +
55             "pbcast.NAKACK(gc_lag=20;retransmit_timeout=2000):" +
56             "UNICAST(timeout=4000):" +
57             "pbcast.STABLE(desired_avg_gossip=10000):" +
58             "FRAG(frag_size=" + frag_size + "):" +
59             "pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;" +
60             "shun=false;print_local_addr=true)";
61     }
62     
63     
64
65
66     public void start() throws Exception JavaDoc {
67     Vector JavaDoc mbrs;
68     Object JavaDoc obj;
69     View view;
70     Address receiver=null;
71
72
73     channel=new JChannel(props);
74     // if(mode == 1) channel.setOpt(Channel.LOCAL, Boolean.FALSE);
75
channel.connect(groupname);
76     local_addr=channel.getLocalAddress();
77
78     if(mode == 1) {
79
80         // waiting for receiver
81
mbrs=channel.getView().getMembers();
82         if(mbrs.size() < 2) {
83         // loop until a new member has joined
84
System.out.println("Waiting for receiver to join group");
85         while(true) {
86             obj=channel.receive(0);
87             if(obj instanceof View) {
88             view=(View)obj;
89             mbrs=view.getMembers();
90             if(mbrs.size() >= 2)
91                 break;
92             }
93         }
94         }
95         for(int i=0; i < mbrs.size(); i++) {
96         if(mbrs.elementAt(i).equals(local_addr))
97             continue;
98         else {
99             receiver=(Address)mbrs.elementAt(i);
100             break;
101         }
102         }
103         System.out.println("Receiver joined group: I'm starting to send messages to " + receiver);
104
105         for(int j=0; j < num_msgs; j++) {
106         msg=createBigMessage(MSG_SIZE);
107         msg.setDest(receiver);
108         System.out.println("Sending msg (" + MSG_SIZE + " bytes) to " + receiver);
109         channel.send(msg);
110         System.out.println("Done Sending msg (" + MSG_SIZE + " bytes)");
111         Util.sleep(timeout);
112         }
113         System.out.println("Press [return] to exit");
114         System.in.read();
115     }
116     else {
117         System.out.println("Waiting for messages:");
118
119         while(true) {
120         try {
121             obj=channel.receive(0);
122             if(obj instanceof Message)
123             {
124                 System.out.println("Received message: " + obj);
125                 Message tmp = (Message)obj;
126                 byte[] buf = tmp.getBuffer();
127                 for (int i=0; i<(10 < MSG_SIZE ? 10 : MSG_SIZE); i++)
128                 {
129                     System.out.print((char)buf[i]);
130                 }
131                 System.out.println();
132
133             }
134         }
135         catch(Exception JavaDoc e) {
136             System.err.println(e);
137         }
138         }
139     
140     }
141
142     channel.close();
143     }
144
145
146
147     Message createBigMessage(int size) {
148     byte[] buf=new byte[size];
149     for(int i=0; i < buf.length; i++) buf[i]=(byte)sendingChar;
150     return new Message(null, null, buf);
151     }
152
153
154
155     public static void main(String JavaDoc[] args) {
156     char defaultChar = 'A';
157     int default_mode=0; // receiver
158
int MSG_SIZE=30000;
159     int num_msgs=5;
160     long timeout=1000;
161     int frag_size=8195;
162
163     for(int i=0; i < args.length; i++) {
164         if("-help".equals(args[i])) {
165         usage();
166         return;
167         }
168         if("-sender".equals(args[i])) {
169         default_mode=1;
170         continue;
171         }
172         if("-size".equals(args[i])) {
173         MSG_SIZE=Integer.parseInt(args[++i]);
174         continue;
175         }
176         if("-num_msgs".equals(args[i])) {
177         num_msgs=Integer.parseInt(args[++i]);
178         continue;
179         }
180         if("-frag_size".equals(args[i])) {
181         frag_size=Integer.parseInt(args[++i]);
182         continue;
183         }
184         if("-timeout".equals(args[i])) {
185         timeout=Long.parseLong(args[++i]);
186         continue;
187         }
188         if("-char".equals(args[i])) {
189         defaultChar=args[++i].charAt(0);
190         continue;
191         }
192         usage();
193         return;
194     }
195
196     try {
197         new FragTestUnicast(defaultChar , default_mode, MSG_SIZE, num_msgs, timeout, frag_size).start();
198     }
199     catch(Exception JavaDoc e) {
200         System.err.println(e);
201     }
202     }
203
204
205     static void usage() {
206     System.out.println("FragTestUnicast [-sender] [-size <message size (in bytes)>] [-timeout <msecs>]"+
207                " [-num_msgs <number of messages>] [-char <frag character>] " +
208                "[-frag_size <fragmentation size>] [-help]");
209     }
210
211 }
212
213
Popular Tags