KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > stack > RpcProtocol


1 // $Id: RpcProtocol.java,v 1.5 2004/05/15 00:20:34 belaban Exp $
2

3 package org.jgroups.stack;
4
5
6 import org.jgroups.*;
7 import org.jgroups.blocks.MethodCall;
8 import org.jgroups.util.RspList;
9 import org.jgroups.util.Util;
10
11 import java.util.Vector JavaDoc;
12
13
14
15
16 /**
17  * Base class for group RMC peer protocols.
18  * @author Bela Ban
19  */

20 public class RpcProtocol extends MessageProtocol {
21
22
23     public String JavaDoc getName() {
24         return "RpcProtocol";
25     }
26
27
28
29     public RspList callRemoteMethods(Vector JavaDoc dests, String JavaDoc method_name, Object JavaDoc[] args,
30                                      Class JavaDoc[] types, int mode, long timeout) {
31         MethodCall method_call=new MethodCall(method_name, args, types);
32         return callRemoteMethods(dests, method_call, mode, timeout);
33     }
34
35     public RspList callRemoteMethods(Vector JavaDoc dests, String JavaDoc method_name, Object JavaDoc[] args,
36                                      String JavaDoc[] signature, int mode, long timeout) {
37         MethodCall method_call=new MethodCall(method_name, args, signature);
38         return callRemoteMethods(dests, method_call, mode, timeout);
39     }
40
41
42     public RspList callRemoteMethods(Vector JavaDoc dests, MethodCall method_call, int mode, long timeout) {
43         byte[] buf=null;
44         Message msg=null;
45
46         try {
47             buf=Util.objectToByteBuffer(method_call);
48         }
49         catch(Exception JavaDoc e) {
50             if(log.isErrorEnabled()) log.error("exception=" + e);
51             return null;
52         }
53
54         msg=new Message(null, null, buf);
55         return castMessage(dests, msg, mode, timeout);
56     }
57
58
59     public Object JavaDoc callRemoteMethod(Address dest, String JavaDoc method_name, int mode, long timeout)
60             throws TimeoutException, SuspectedException {
61         return callRemoteMethod(dest, method_name, new Object JavaDoc[]{}, new Class JavaDoc[]{}, mode, timeout);
62     }
63
64
65     public Object JavaDoc callRemoteMethod(Address dest, String JavaDoc method_name, Object JavaDoc[] args,
66                                    Class JavaDoc[] types, int mode, long timeout)
67             throws TimeoutException, SuspectedException {
68         MethodCall method_call=new MethodCall(method_name, args, types);
69         return callRemoteMethod(dest, method_call, mode, timeout);
70     }
71
72     public Object JavaDoc callRemoteMethod(Address dest, String JavaDoc method_name, Object JavaDoc[] args,
73                                    String JavaDoc[] signature, int mode, long timeout)
74             throws TimeoutException, SuspectedException {
75         MethodCall method_call=new MethodCall(method_name, args, signature);
76         return callRemoteMethod(dest, method_call, mode, timeout);
77     }
78
79
80     public Object JavaDoc callRemoteMethod(Address dest, MethodCall method_call, int mode, long timeout)
81             throws TimeoutException, SuspectedException {
82         byte[] buf=null;
83         Message msg=null;
84
85         try {
86             buf=Util.objectToByteBuffer(method_call);
87         }
88         catch(Exception JavaDoc e) {
89             if(log.isErrorEnabled()) log.error("exception=" + e);
90             return null;
91         }
92
93         msg=new Message(dest, null, buf);
94         return sendMessage(msg, mode, timeout);
95     }
96
97
98     /**
99      Message contains MethodCall. Execute it against *this* object and return result.
100      Use MethodCall.invoke() to do this. Return result.
101      */

102     public Object JavaDoc handle(Message req) {
103         Object JavaDoc body=null;
104         MethodCall method_call;
105
106         if(req == null || req.getLength() == 0) {
107             if(log.isErrorEnabled()) log.error("message or message buffer is null");
108             return null;
109         }
110
111         try {
112             body=req.getObject();
113         }
114         catch(Exception JavaDoc e) {
115             if(log.isErrorEnabled()) log.error("exception=" + e);
116             return e;
117         }
118
119         if(body == null || !(body instanceof MethodCall)) {
120             if(log.isErrorEnabled()) log.error("message does not contain a MethodCall object");
121             return null;
122         }
123
124         method_call=(MethodCall)body;
125         try {
126             return method_call.invoke(this);
127         }
128         catch(Throwable JavaDoc x) {
129             if(log.isErrorEnabled()) log.error(Util.getStackTrace(x));
130             return x;
131         }
132     }
133
134
135     /**
136      Handle up event. Return false if it should not be passed up the stack.
137      */

138     public boolean handleUpEvent(Event evt) {
139         return true;
140     }
141
142
143     /**
144      Handle down event. Return false if it should not be passed down the stack.
145      */

146     public boolean handleDownEvent(Event evt) {
147         return true;
148     }
149
150
151 }
152
Popular Tags