001: // $Id: DEADLOCK.java,v 1.7 2005/08/08 12:45:42 belaban Exp $
002:
003: package org.jgroups.protocols;
004:
005: import org.jgroups.Address;
006: import org.jgroups.Event;
007: import org.jgroups.View;
008: import org.jgroups.blocks.GroupRequest;
009: import org.jgroups.blocks.MethodCall;
010: import org.jgroups.stack.RpcProtocol;
011: import org.jgroups.util.RspList;
012:
013: import java.awt.*;
014: import java.awt.event.ActionEvent;
015: import java.awt.event.ActionListener;
016: import java.util.Vector;
017:
018: class MyFrame extends Frame {
019: final List list = new List();
020: final Label result = new Label("Result: ");
021: final Button send = new Button("Send Request");
022: final Button quit = new Button("Quit");
023: final Panel button_panel = new Panel();
024: final Panel main_panel = new Panel();
025: DEADLOCK deadlock = null;
026: Vector members = null;
027:
028: MyFrame(String title, DEADLOCK deadlock) {
029: this .deadlock = deadlock;
030: setSize(300, 200);
031: setTitle(title);
032: setBackground(Color.white);
033: setFont(new Font("Helvetica", Font.PLAIN, 12));
034: setLayout(new BorderLayout());
035: main_panel.setLayout(new GridLayout(0, 2));
036: main_panel.add(result);
037: main_panel.add(list);
038: button_panel.add(send);
039: button_panel.add(quit);
040: add("Center", main_panel);
041: add("South", button_panel);
042: addEventHandlers();
043: }
044:
045: void addEventHandlers() {
046:
047: quit.addActionListener(new ActionListener() {
048: public void actionPerformed(ActionEvent e) {
049: dispose();
050: }
051: });
052:
053: send.addActionListener(new ActionListener() {
054: public void actionPerformed(ActionEvent e) {
055: Address dest;
056: int res;
057: int index = -1;
058:
059: index = list.getSelectedIndex();
060: if (index == -1)
061: return;
062: dest = members != null ? (Address) members
063: .elementAt(index) : null;
064: if (dest != null) {
065: res = deadlock.sendRequest(dest);
066: setResult(res);
067: }
068: }
069: });
070:
071: }
072:
073: void setResult(int res) {
074: result.setText("Result: " + res);
075: }
076:
077: void setMembers(Vector members) {
078: list.removeAll();
079: for (int i = 0; i < members.size(); i++)
080: list.add(members.elementAt(i).toString());
081: this .members = members;
082: }
083:
084: }
085:
086: /**
087: Tests the deadlock detection mechanism of RequestCorrelator.
088: */
089: public class DEADLOCK extends RpcProtocol {
090: MyFrame frame = null;
091:
092: public String getName() {
093: return "DEADLOCK";
094: }
095:
096: public void start() throws Exception {
097: super .start();
098: if (_corr != null)
099: _corr.setDeadlockDetection(true);
100: else
101: log
102: .error("Cannot set deadlock detection in corr, as it is null !");
103: frame = new MyFrame(getName(), this );
104: frame.show();
105: }
106:
107: public void stop() {
108: super .stop();
109: if (frame != null) {
110: frame.dispose();
111: frame = null;
112: }
113: }
114:
115: public int sendRequest(Address dest) {
116: Object retval;
117:
118: try {
119: System.out.println("--> getCombinedResults() to " + dest);
120: retval = callRemoteMethod(dest, "getCombinedResults",
121: GroupRequest.GET_FIRST, 0);
122: } catch (Exception e) {
123: return -1;
124: }
125: if (retval != null && retval instanceof Integer)
126: return ((Integer) retval).intValue();
127: return -1;
128: }
129:
130: /* ------------------------- Request handler methods ----------------------------- */
131:
132: /** Mcasts getResult() to all members (including itself). Returns the sum of all results. */
133: public int getCombinedResults() {
134: RspList rsp_list;
135: Vector results;
136: int retval = 0;
137:
138: System.out.println("<-- getCombinedResults()");
139:
140: System.out.println("--> getResult() to " + members);
141: MethodCall call = new MethodCall("getResult", new Object[] {},
142: new String[] {});
143: rsp_list = callRemoteMethods(members, call,
144: GroupRequest.GET_ALL, 0);
145: results = rsp_list.getResults();
146: for (int i = 0; i < results.size(); i++)
147: retval += ((Integer) results.elementAt(i)).intValue();
148: return retval;
149: }
150:
151: /** Returns a random integer value between 1 and 10 */
152: public static int getResult() {
153: System.out.println("<-- getResult()");
154: return (int) ((Math.random() * 10) % 10) + 1;
155: }
156:
157: /* --------------------- End of Request handler methods -------------------------- */
158:
159: /**
160: * <b>Callback</b>. Called by superclass when event may be handled.<p>
161: * <b>Do not use <code>PassUp</code> in this method as the event is passed up
162: * by default by the superclass after this method returns !</b>
163: * @return boolean Defaults to true. If false, event will not be passed up the stack.
164: */
165: public boolean handleUpEvent(Event evt) {
166: switch (evt.getType()) {
167:
168: case Event.TMP_VIEW:
169: case Event.VIEW_CHANGE:
170: Vector new_members = ((View) evt.getArg()).getMembers();
171: synchronized (members) {
172: members.removeAllElements();
173: if (new_members != null && new_members.size() > 0)
174: for (int i = 0; i < new_members.size(); i++)
175: members.addElement(new_members.elementAt(i));
176: }
177: frame.setMembers(members);
178: break;
179:
180: case Event.SET_LOCAL_ADDRESS:
181: frame.setTitle(frame.getTitle() + ": "
182: + evt.getArg().toString());
183: break;
184:
185: }
186:
187: return true;
188: }
189:
190: /**
191: <b>Callback</b>. Called by superclass when event may be handled.<p>
192: <b>Do not use <code>PassDown</code> in this method as the event is passed down
193: by default by the superclass after this method returns !</b>
194: @return boolean Defaults to true. If false, event will not be passed down the stack.
195: */
196: public boolean handleDownEvent(Event evt) {
197: switch (evt.getType()) {
198: case Event.TMP_VIEW:
199: case Event.VIEW_CHANGE:
200: Vector new_members = ((View) evt.getArg()).getMembers();
201: synchronized (members) {
202: members.removeAllElements();
203: if (new_members != null && new_members.size() > 0)
204: for (int i = 0; i < new_members.size(); i++)
205: members.addElement(new_members.elementAt(i));
206: }
207: System.out.println("Setting members");
208: frame.setMembers(members);
209: System.out.println("done");
210: break;
211: }
212: return true;
213: }
214:
215: }
|