KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: UnicastTest2.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
9 import java.io.Serializable JavaDoc;
10 import java.util.Enumeration JavaDoc;
11 import java.util.Hashtable JavaDoc;
12 import java.util.Vector JavaDoc;
13
14
15
16
17 /**
18  * Demos the UNICAST protocol. As soon as we have 2 members in a group, a destination member is randomly
19  * chosen (not self !). Then, as long as that destination member is member of the group, we send NUM_MSGS
20  * unicast messages to it. The receiver checks that it receives messages in order (monotonically increasing).<p>
21  * The sample protocol stack below has a DISCARD protocol in it, which randomly discards
22  * both unicast and multicast messages (in the example below, down messages are discarded with a probability
23  * of 10%, i.e. 1 out of 10 messages is discarded)).<p>
24  * If you want to see the informational messages for DISCARD and UNICAST, you have to enable them in trace, e.g.
25  * by adding the following statements to your jgroups.properties file (in your home directory):
26  * <pre>
27  * trace1=DISCARD DEBUG STDOUT
28  * trace2=UNICAST DEBUG STDOUT
29  * </pre>
30  * @author Bela Ban
31  */

32 public class UnicastTest2 implements Runnable JavaDoc {
33     Channel channel;
34     String JavaDoc groupname="UnicastTest2Group";
35     String JavaDoc props="UDP:PING:FD:DISCARD(down=0.1):NAKACK(retransmit_timeout=1000):"+
36                 "UNICAST:FRAG:FLUSH:GMS:VIEW_ENFORCER:QUEUE";
37     Thread JavaDoc writer=null;
38     Vector JavaDoc mbrs=new Vector JavaDoc();
39     Hashtable JavaDoc senders=new Hashtable JavaDoc();
40     boolean running=true;
41     final int NUM_MSGS=100;
42
43
44
45
46     public void start() throws Exception JavaDoc {
47     Object JavaDoc obj;
48     Message msg;
49     View view;
50     Vector JavaDoc tmp;
51     UnicastTest2Info info, myinfo;
52     Object JavaDoc sender;
53
54     channel=new JChannel(props);
55     channel.connect(groupname);
56     System.out.println("[ready]");
57
58     while(true) {
59         try {
60         obj=channel.receive(0);
61         if(obj instanceof View) {
62             view=(View)obj;
63             tmp=view.getMembers();
64             mbrs.removeAllElements();
65             for(int i=0; i < tmp.size(); i++)
66             mbrs.addElement(tmp.elementAt(i));
67             
68             for(Enumeration JavaDoc e=senders.keys(); e.hasMoreElements();) {
69             sender=e.nextElement();
70             if(!mbrs.contains(sender)) {
71                 mbrs.removeElement(sender);
72             }
73             }
74             
75             if(mbrs.size() > 1) {
76             if(writer == null) {
77                 writer=new Thread JavaDoc(this, "WriterThread");
78                 writer.start();
79             }
80             }
81             else {
82             if(writer != null) {
83                 running=false;
84                 writer.interrupt();
85             }
86             writer=null;
87             }
88         }
89         else if(obj instanceof Message) {
90             msg=(Message)obj;
91             info=(UnicastTest2Info)msg.getObject();
92             System.out.println("Received msg: " + info);
93
94             myinfo=(UnicastTest2Info)senders.get(info.sender);
95             if(myinfo == null) { // first msg
96
if(info.msgno == 1) {
97                 // must be 1
98
senders.put(info.sender, info);
99             }
100             else {
101                 // error
102
System.err.println("UnicastTest2.start(): first seqno must be 1");
103             }
104
105             }
106             else {
107             if(info.msgno -1 != myinfo.msgno) {
108                 System.err.println("UnicastTest2.start(): received msg " + info.sender + ':' +
109                            info.msgno + ", but last received was " +
110                            myinfo.sender + ':' + myinfo.msgno);
111             }
112             else {
113                 System.out.println("UnicastTest2.start(): OK received " + info.sender + ':' +
114                            info.msgno + ", prev seqno=" + myinfo.sender + ':' + myinfo.msgno);
115                 myinfo.msgno++;
116             }
117             }
118
119         }
120         else
121             ;
122         }
123         catch(ChannelClosedException closed) {
124         System.err.println("Channel closed");
125         break;
126         }
127         catch(ChannelNotConnectedException not_conn) {
128         System.err.println("Channel not connected");
129         break;
130         }
131         catch(Exception JavaDoc e) {
132         System.err.println(e);
133         }
134     }
135
136
137
138
139     }
140
141
142     Address selectTarget() {
143     Vector JavaDoc tmp=new Vector JavaDoc();
144     Address ret;
145     int t;
146
147     if(mbrs == null || mbrs.size() < 2)
148         return null;
149
150     for(int i=0; i < mbrs.size(); i++) {
151         if(!(mbrs.elementAt(i).equals(channel.getLocalAddress())))
152         tmp.addElement(mbrs.elementAt(i));
153     }
154     t=(int)((Math.random() * 100));
155     ret=(Address)tmp.elementAt(t % tmp.size());
156     return ret;
157     }
158
159
160
161     public void run() {
162     Address target=selectTarget();
163     UnicastTest2Info info=null;
164     int msgno=1;
165     
166     if(target == null)
167         return;
168
169     while(running && msgno <= NUM_MSGS) {
170         try {
171         info=new UnicastTest2Info(msgno++, channel.getLocalAddress());
172         System.out.println("Sending message #" + (msgno-1) + " to " + target);
173         channel.send(new Message(target, null, info));
174         Thread.sleep(500);
175         }
176         catch(ChannelClosedException closed) {
177         System.err.println(closed);
178         break;
179         }
180         catch(ChannelNotConnectedException not_conn) {
181         System.err.println(not_conn);
182         }
183         catch(Exception JavaDoc e) {
184         System.err.println(e);
185         }
186     }
187     System.out.println("UnicastTest2Info.run(): writer thread terminated");
188     }
189
190
191
192
193     public static void main(String JavaDoc[] args) {
194     try {
195
196         new UnicastTest2().start();
197     }
198     catch(Exception JavaDoc e) {
199         System.err.println(e);
200     }
201     }
202
203
204     private static class UnicastTest2Info implements Serializable JavaDoc {
205     int msgno=0;
206     Object JavaDoc sender=null;
207     
208     public UnicastTest2Info() {
209         
210     }
211     
212     public UnicastTest2Info(int msgno, Object JavaDoc sender) {
213         this.msgno=msgno;
214         this.sender=sender;
215     }
216     
217     
218     public String JavaDoc toString() {
219         return "#" + msgno + " (sender=" + sender + ')';
220     }
221     }
222
223
224
225
226 }
227
228
229
230
Popular Tags