KickJava   Java API By Example, From Geeks To Geeks.

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


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

3 package org.jgroups.tests;
4
5
6 import org.jgroups.*;
7 import org.jgroups.blocks.GroupRequest;
8 import org.jgroups.blocks.PullPushAdapter;
9 import org.jgroups.util.RspList;
10
11 import java.io.IOException JavaDoc;
12 import java.io.ObjectInput JavaDoc;
13 import java.io.ObjectOutput JavaDoc;
14 import java.util.Vector JavaDoc;
15
16
17
18
19 /**
20  *
21  * @author Bela Ban
22  */

23 public class GroupRequestPull implements MessageListener, MembershipListener, Transport {
24     PullPushAdapter adapter=null;
25     Channel ch=null;
26     String JavaDoc props=null;
27     GroupRequest group_req=null;
28     static final String JavaDoc HDRNAME="GroupRequestPullHeader";
29     Vector JavaDoc members=new Vector JavaDoc();
30
31
32
33     
34
35     GroupRequestPull(String JavaDoc props) {
36     this.props=props;
37     }
38     
39
40     void start() throws Exception JavaDoc {
41     ch=new JChannel(props);
42     ch.connect("GroupRequestPull-Group");
43     adapter=new PullPushAdapter(ch, this, this);
44     loop();
45     adapter.stop();
46     ch.close();
47     }
48     
49
50     void loop() throws Exception JavaDoc {
51     boolean looping=true;
52     int c;
53
54     while(looping) {
55         System.out.println("Members are " + ch.getView().getMembers() + "\n<enter to send a new group request>");
56         System.out.flush();
57         c=System.in.read();
58         if(c == 'q')
59         looping=false;
60         System.in.skip(System.in.available());
61         sendGroupRequest();
62     }
63     }
64
65
66     void sendGroupRequest() throws Exception JavaDoc {
67     Message msg=new Message();
68     RspList lst;
69     
70     msg.putHeader(HDRNAME, new MyHeader(MyHeader.REQUEST));
71     group_req=new GroupRequest(msg,
72                    this, // as Transport
73
members, // all current members
74
GroupRequest.GET_ALL);
75
76     group_req.execute();
77     lst=group_req.getResults();
78     System.out.println("-- received " + lst.size() + " results:");
79     for(int i=0; i < lst.size(); i++) {
80         System.out.println(lst.elementAt(i));
81     }
82     System.out.println();
83     }
84     
85     /* --------------------------- Interface MessageListener -------------------------- */
86
87     public void receive(Message msg) {
88         MyHeader hdr=(MyHeader)msg.removeHeader(HDRNAME);
89         Message rsp;
90
91         if(hdr == null) {
92             System.err.println("GroupRequestPull.receive(): header for " + HDRNAME + " was null");
93             return;
94         }
95         if(hdr.type == MyHeader.RESPONSE) {
96             if(group_req != null)
97                 group_req.receiveResponse(msg);
98         }
99         else if(hdr.type == MyHeader.REQUEST) {
100             // System.out.println("-- received REQUEST from " + msg.getSrc());
101
rsp=new Message(msg.getSrc(), null, null);
102             rsp.putHeader(HDRNAME, new MyHeader(MyHeader.RESPONSE));
103             rsp.setObject("Hello from member " + ch.getLocalAddress());
104             try {
105                 adapter.send(rsp);
106             }
107             catch(Exception JavaDoc ex) {
108                 System.err.println("GroupRequestPull.receive(): failure sending response: " + ex);
109             }
110         }
111         else {
112             System.err.println("GroupRequestPull.receive(): header type of " + hdr.type + " not known");
113         }
114     }
115
116     public byte[] getState() {
117     return null;
118     }
119
120     public void setState(byte[] state) {
121     ;
122     }
123
124     /* ----------------------- End of Interface MessageListener ------------------------ */
125
126
127
128
129     /* --------------------------- Interface MembershipListener -------------------------- */
130
131     public void viewAccepted(View new_view) {
132     System.out.println("** viewAccepted(): " + new_view);
133     if(new_view != null && new_view.getMembers().size() > 0) {
134         members.removeAllElements();
135         members.addAll(new_view.getMembers());
136     }
137     if(group_req != null)
138         group_req.viewChange(new_view);
139     }
140
141     public void suspect(Address suspected_mbr) {
142     System.out.println("** suspect(): " + suspected_mbr);
143     if(group_req != null)
144         group_req.suspect(suspected_mbr);
145     }
146
147     public void block() {
148     
149     }
150
151     /* ----------------------- End of Interface MembershipListener ----------------------- */
152
153
154     /* --------------------------- Interface Transport ------------------------------------ */
155     /** Used by GroupRequest to send messages */
156     public void send(Message msg) throws Exception JavaDoc {
157     if(adapter == null) {
158         System.err.println("GroupRequestPull.send(): adapter is null, cannot send message");
159     }
160     else
161         adapter.send(msg);
162     }
163
164     public Object JavaDoc receive(long timeout) throws Exception JavaDoc {
165     return null;
166     }
167     /* ------------------------ End of Interface Transport -------------------------------- */
168
169
170     public static void main(String JavaDoc[] args) {
171     String JavaDoc props=null;
172
173     for(int i=0; i < args.length; i++) {
174         if("-props".equals(args[i])) {
175         props=args[++i];
176         continue;
177         }
178         help();
179         return;
180     }
181
182
183
184
185     try {
186         new GroupRequestPull(props).start();
187     }
188     catch(Exception JavaDoc ex) {
189         ex.printStackTrace();
190     }
191     }
192
193
194     static void help() {
195     System.out.println("GroupRequestPull [-help] [-props <properties>]");
196     }
197
198
199
200
201     public static class MyHeader extends Header {
202     public static final int REQUEST = 1;
203     public static final int RESPONSE = 2;
204
205     int type=0;
206
207
208     public MyHeader() {
209         
210     }
211
212     public MyHeader(int type) {
213         this.type=type;
214     }
215     
216     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
217         out.writeInt(type);
218     }
219     
220     
221     
222     public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
223         type=in.readInt();
224     }
225     }
226
227 }
228
229
230
231
Popular Tags