001: // $Id: RpcProtocol.java,v 1.6 2005/11/03 11:42:59 belaban Exp $
002:
003: package org.jgroups.stack;
004:
005: import org.jgroups.*;
006: import org.jgroups.blocks.MethodCall;
007: import org.jgroups.util.RspList;
008: import org.jgroups.util.Util;
009:
010: import java.util.Vector;
011:
012: /**
013: * Base class for group RMC peer protocols.
014: * @author Bela Ban
015: */
016: public class RpcProtocol extends MessageProtocol {
017:
018: public String getName() {
019: return "RpcProtocol";
020: }
021:
022: public RspList callRemoteMethods(Vector dests, String method_name,
023: Object[] args, Class[] types, int mode, long timeout) {
024: MethodCall method_call = new MethodCall(method_name, args,
025: types);
026: return callRemoteMethods(dests, method_call, mode, timeout);
027: }
028:
029: public RspList callRemoteMethods(Vector dests, String method_name,
030: Object[] args, String[] signature, int mode, long timeout) {
031: MethodCall method_call = new MethodCall(method_name, args,
032: signature);
033: return callRemoteMethods(dests, method_call, mode, timeout);
034: }
035:
036: public RspList callRemoteMethods(Vector dests,
037: MethodCall method_call, int mode, long timeout) {
038: byte[] buf = null;
039: Message msg = null;
040:
041: try {
042: buf = Util.objectToByteBuffer(method_call);
043: } catch (Exception e) {
044: if (log.isErrorEnabled())
045: log.error("exception=" + e);
046: return null;
047: }
048:
049: msg = new Message(null, null, buf);
050: return castMessage(dests, msg, mode, timeout);
051: }
052:
053: public Object callRemoteMethod(Address dest, String method_name,
054: int mode, long timeout) throws TimeoutException,
055: SuspectedException {
056: return callRemoteMethod(dest, method_name, new Object[] {},
057: new Class[] {}, mode, timeout);
058: }
059:
060: public Object callRemoteMethod(Address dest, String method_name,
061: Object[] args, Class[] types, int mode, long timeout)
062: throws TimeoutException, SuspectedException {
063: MethodCall method_call = new MethodCall(method_name, args,
064: types);
065: return callRemoteMethod(dest, method_call, mode, timeout);
066: }
067:
068: public Object callRemoteMethod(Address dest, String method_name,
069: Object[] args, String[] signature, int mode, long timeout)
070: throws TimeoutException, SuspectedException {
071: MethodCall method_call = new MethodCall(method_name, args,
072: signature);
073: return callRemoteMethod(dest, method_call, mode, timeout);
074: }
075:
076: public Object callRemoteMethod(Address dest,
077: MethodCall method_call, int mode, long timeout)
078: throws TimeoutException, SuspectedException {
079: byte[] buf = null;
080: Message msg = null;
081:
082: try {
083: buf = Util.objectToByteBuffer(method_call);
084: } catch (Exception e) {
085: if (log.isErrorEnabled())
086: log.error("exception=" + e);
087: return null;
088: }
089:
090: msg = new Message(dest, null, buf);
091: return sendMessage(msg, mode, timeout);
092: }
093:
094: /**
095: Message contains MethodCall. Execute it against *this* object and return result.
096: Use MethodCall.invoke() to do this. Return result.
097: */
098: public Object handle(Message req) {
099: Object body = null;
100: MethodCall method_call;
101:
102: if (req == null || req.getLength() == 0) {
103: if (log.isErrorEnabled())
104: log.error("message or message buffer is null");
105: return null;
106: }
107:
108: try {
109: body = req.getObject();
110: } catch (Exception e) {
111: if (log.isErrorEnabled())
112: log.error("exception=" + e);
113: return e;
114: }
115:
116: if (body == null || !(body instanceof MethodCall)) {
117: if (log.isErrorEnabled())
118: log
119: .error("message does not contain a MethodCall object");
120: return null;
121: }
122:
123: method_call = (MethodCall) body;
124: try {
125: return method_call.invoke(this );
126: } catch (Throwable x) {
127: if (log.isErrorEnabled())
128: log.error("failed invoking method "
129: + method_call.getName(), x);
130: return x;
131: }
132: }
133:
134: /**
135: Handle up event. Return false if it should not be passed up the stack.
136: */
137: public boolean handleUpEvent(Event evt) {
138: return true;
139: }
140:
141: /**
142: Handle down event. Return false if it should not be passed down the stack.
143: */
144: public boolean handleDownEvent(Event evt) {
145: return true;
146: }
147:
148: }
|