KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jgroups.tests;
2
3 import org.jgroups.*;
4 import org.jgroups.blocks.GroupRequest;
5 import org.jgroups.blocks.RpcDispatcher;
6
7 /**
8  * Tests the "NAKACK retransmit message lost" problem. Start 2 members, then a third one, and you should never see
9  * the problem with messages than cannot be retransmitted because they were already garbage-collected.
10  * @author Bela Ban Apr 4, 2004
11  * @version $Id: NAKACKTest2.java,v 1.4 2004/07/05 14:15:22 belaban Exp $
12  */

13 public class NAKACKTest2 {
14     Channel ch;
15     Address local_addr;
16     Receiver receiver;
17     RpcDispatcher disp;
18
19     class Receiver extends Thread JavaDoc {
20         public void run() {
21             Object JavaDoc obj;
22             Message msg;
23             boolean running=true;
24             while(running) {
25                 try {
26                     obj=ch.receive(0);
27                     if(obj instanceof Message) {
28                         msg=(Message)obj;
29                         System.out.println(msg.getSrc() + "::" + msg.getObject());
30                     }
31                     else
32                         System.out.println("received " + obj);
33                 }
34                 catch(ChannelNotConnectedException e) {
35                     running=false;
36                 }
37                 catch(ChannelClosedException e) {
38                     running=false;
39                 }
40                 catch(TimeoutException e) {
41                     ;
42                 }
43             }
44         }
45     }
46
47     public void receive(Address sender, Long JavaDoc i) {
48         System.out.println(sender + "::" + i);
49     }
50
51     void start(String JavaDoc props, boolean use_rpc) throws Exception JavaDoc {
52         long i=0;
53         Message msg;
54         ch=new JChannel(props);
55         if(use_rpc)
56            disp=new RpcDispatcher(ch, null, null, this);
57         ch.connect("NAKACKTest");
58         local_addr=ch.getLocalAddress();
59         if(use_rpc == false) {
60             receiver=new Receiver();
61             receiver.start();
62         }
63         while(true) {
64         // for(int j=0; j < 10000; j++) {
65
if(use_rpc) {
66                 disp.callRemoteMethods(null, "receive", new Object JavaDoc[]{local_addr, new Long JavaDoc(i++)},
67                         new Class JavaDoc[]{Address.class, Long JavaDoc.class}, GroupRequest.GET_ALL, 10000);
68             }
69             else {
70                 msg=new Message(null, null, new Long JavaDoc(i++));
71                 ch.send(msg);
72             }
73             //Util.sleep(1);
74
}
75
76         // Util.sleep(3600000);
77
}
78
79     public static void main(String JavaDoc[] args) {
80         String JavaDoc props=null;
81         boolean use_rpc=false;
82
83         for(int i=0; i < args.length; i++) {
84             if("-props".equals(args[i])) {
85                 props=args[++i];
86                 continue;
87             }
88             if("-use_rpc".equals(args[i])) {
89                 use_rpc=true;
90                 continue;
91             }
92             help();
93             return;
94         }
95
96
97         try {
98             new NAKACKTest2().start(props, use_rpc);
99         }
100         catch(Exception JavaDoc e) {
101             e.printStackTrace();
102         }
103     }
104
105     private static void help() {
106         System.out.println("NAKACKTest [-help] [-props properties] [-use_rpc]");
107     }
108 }
109
Popular Tags