KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > protocols > DEADLOCK


1 // $Id: DEADLOCK.java,v 1.5 2004/09/23 16:29:41 belaban Exp $
2

3 package org.jgroups.protocols;
4
5
6 import org.jgroups.Address;
7 import org.jgroups.Event;
8 import org.jgroups.View;
9 import org.jgroups.blocks.GroupRequest;
10 import org.jgroups.blocks.MethodCall;
11 import org.jgroups.stack.RpcProtocol;
12 import org.jgroups.util.RspList;
13
14 import java.awt.*;
15 import java.awt.event.ActionEvent JavaDoc;
16 import java.awt.event.ActionListener JavaDoc;
17 import java.util.Vector JavaDoc;
18
19
20
21
22 class MyFrame extends Frame {
23     final List list=new List();
24     final Label result=new Label("Result: ");
25     final Button send=new Button("Send Request");
26     final Button quit=new Button("Quit");
27     final Panel button_panel=new Panel();
28     final Panel main_panel=new Panel();
29     DEADLOCK deadlock=null;
30     Vector JavaDoc members=null;
31     
32     
33     MyFrame(String JavaDoc title, DEADLOCK deadlock) {
34     this.deadlock=deadlock;
35     setSize(300, 200);
36     setTitle(title);
37     setBackground(Color.white);
38     setFont(new Font("Helvetica", Font.PLAIN, 12));
39     setLayout(new BorderLayout());
40     main_panel.setLayout(new GridLayout(0, 2));
41     main_panel.add(result);
42     main_panel.add(list);
43     button_panel.add(send);
44     button_panel.add(quit);
45     add("Center", main_panel);
46     add("South", button_panel);
47     addEventHandlers();
48     }
49
50
51     void addEventHandlers() {
52
53     quit.addActionListener(
54                    new ActionListener JavaDoc() {
55         public void actionPerformed(ActionEvent JavaDoc e) {dispose();}
56     });
57
58     send.addActionListener(
59                    new ActionListener JavaDoc() {
60         public void actionPerformed(ActionEvent JavaDoc e) {
61         Address dest;
62         int res;
63         int index=-1;
64         
65         index=list.getSelectedIndex();
66         if(index == -1)
67             return;
68         dest=members != null ? (Address)members.elementAt(index) : null;
69         if(dest != null) {
70             res=deadlock.sendRequest(dest);
71             setResult(res);
72         }
73         }
74     });
75
76     
77     }
78
79
80     void setResult(int res) {
81     result.setText("Result: " + res);
82     }
83
84     void setMembers(Vector JavaDoc members) {
85     list.removeAll();
86     for(int i=0; i < members.size(); i++)
87         list.add(members.elementAt(i).toString());
88     this.members=members;
89     }
90
91 }
92
93
94
95
96
97 /**
98    Tests the deadlock detection mechanism of RequestCorrelator.
99  */

100 public class DEADLOCK extends RpcProtocol {
101     MyFrame frame=null;
102     
103
104
105
106     public String JavaDoc getName() {return "DEADLOCK";}
107
108
109     public void start() throws Exception JavaDoc {
110         super.start();
111         if(_corr != null)
112             _corr.setDeadlockDetection(true);
113         else
114             System.err.println("Cannot set deadlock detection in corr, as it is null !");
115         frame=new MyFrame(getName(), this);
116         frame.show();
117     }
118
119     public void stop() {
120         super.stop();
121         if(frame != null) {
122             frame.dispose();
123             frame=null;
124         }
125     }
126     
127
128     public int sendRequest(Address dest) {
129     Object JavaDoc retval;
130     
131     try {
132         System.out.println("--> getCombinedResults() to " + dest);
133         retval=callRemoteMethod(dest, "getCombinedResults", GroupRequest.GET_FIRST, 0);
134     }
135     catch(Exception JavaDoc e) {
136         return -1;
137     }
138     if(retval != null && retval instanceof Integer JavaDoc)
139         return ((Integer JavaDoc)retval).intValue();
140     return -1;
141     }
142
143
144     /* ------------------------- Request handler methods ----------------------------- */
145
146
147     /** Mcasts getResult() to all members (including itself). Returns the sum of all results. */
148     public int getCombinedResults() {
149     RspList rsp_list;
150     Vector JavaDoc results;
151     int retval=0;
152
153     System.out.println("<-- getCombinedResults()");
154
155     System.out.println("--> getResult() to " + members);
156     MethodCall call = new MethodCall("getResult", new Object JavaDoc[] {}, new String JavaDoc[] {});
157     rsp_list=callRemoteMethods(members, call, GroupRequest.GET_ALL, 0);
158     results=rsp_list.getResults();
159     for(int i=0; i < results.size(); i++)
160         retval+=((Integer JavaDoc)results.elementAt(i)).intValue();
161     return retval;
162     }
163
164
165     /** Returns a random integer value between 1 and 10 */
166     public int getResult() {
167
168     System.out.println("<-- getResult()");
169
170     return (int)((Math.random() * 10) % 10) + 1;
171     }
172
173
174     /* --------------------- End of Request handler methods -------------------------- */
175
176
177
178
179     
180     /**
181      * <b>Callback</b>. Called by superclass when event may be handled.<p>
182      * <b>Do not use <code>PassUp</code> in this method as the event is passed up
183      * by default by the superclass after this method returns !</b>
184      * @return boolean Defaults to true. If false, event will not be passed up the stack.
185      */

186     public boolean handleUpEvent(Event evt) {
187     switch(evt.getType()) {
188
189     case Event.TMP_VIEW:
190     case Event.VIEW_CHANGE:
191         Vector JavaDoc new_members=((View)evt.getArg()).getMembers();
192         synchronized(members) {
193         members.removeAllElements();
194         if(new_members != null && new_members.size() > 0)
195             for(int i=0; i < new_members.size(); i++)
196             members.addElement(new_members.elementAt(i));
197         }
198         frame.setMembers(members);
199         break;
200
201
202     case Event.SET_LOCAL_ADDRESS:
203         frame.setTitle(frame.getTitle() + ": " + evt.getArg().toString());
204         break;
205         
206     }
207
208     return true;
209     }
210
211
212     /**
213        <b>Callback</b>. Called by superclass when event may be handled.<p>
214        <b>Do not use <code>PassDown</code> in this method as the event is passed down
215        by default by the superclass after this method returns !</b>
216        @return boolean Defaults to true. If false, event will not be passed down the stack.
217     */

218     public boolean handleDownEvent(Event evt) {
219     switch(evt.getType()) {
220     case Event.TMP_VIEW:
221     case Event.VIEW_CHANGE:
222         Vector JavaDoc new_members=((View)evt.getArg()).getMembers();
223         synchronized(members) {
224         members.removeAllElements();
225         if(new_members != null && new_members.size() > 0)
226             for(int i=0; i < new_members.size(); i++)
227             members.addElement(new_members.elementAt(i));
228         }
229         System.out.println("Setting members");
230         frame.setMembers(members);
231         System.out.println("done");
232         break;
233     }
234     return true;
235     }
236
237
238
239 }
240
Popular Tags