next up previous contents
Next: Advanced Concepts Up: Peer protocols Previous: MessageProtocol

   
RpcProtocol

This class is similar to MessageProtocol, but instead of sending and receiving messages, it invokes remote methods in its peer protocols. It is derived from MessageProtocol and makes use of the above mentioned methods.

Fig. 4.4 shows how the RpcProtocol class works conceptually.


  
Figure 4.4: Remote method invocations between peer protocols
\begin{figure}
\center{\epsfig{file=/home/bba/JavaGroups/Papers/UsersGuide/figs/RpcProtocol.eps,width=.5\textwidth} }
\end{figure}

In the example, the group of peer protocols would consist of the 3 members P, Q and R. Each is a protocol layer in a different protocol stack (e.g. on a different machine). P now wants to call method A of all members in the group and collect the responses. It does so by invoking one of the CallRemoteMethods methods of RpcProtocol (discussed below). A MethodCall object (see 3.4.2) will be constructed, inserted in the buffer of a message and sent to all peers. Each peer receives the message, extracts the method call object, invokes it against itself and returns a result in the form of an object (or null, if the method has a void return value). The results will be received by the calling method as a response list.

Note that, since the sender is invoking methods in all members of the group, its own A method will also be invoked. Therefore special care has to be taken to avoid deadlocks (see 5.1).

A typical protocol based on RpcProtocol would define a number of methods and then use one of the CallRemoteMethod(s) methods to invoke them. The CallRemoteMethod series invokes a method in a single receiver, while the CallRemoteMethods does so in all group members. The former takes a destination address, the method name, a number of arguments (may be zero), a mode and a timeout. The last 2 parameters are the same as for MessageProtocol. If the number of parameters is too small, a MethodCall object may be created directly and given as parameter.

The implementation of CallRemoteMethods is as follows:

       public RspList CallRemoteMethods(Vector dests, MethodCall call, 
                                        int mode, long timeout) {
           byte[]   buf=null;
           Message  msg=null;

           try {
               buf=Util.ObjectToByteBuffer(method_call);
           }
           catch(Exception e) {
               System.err.println("RpcProtocol.CallRemoteMethods(): " + e);
               return null;
           }
           msg=new Message(null, null, buf);
           return CastMessage(dests, msg, mode, timeout);
       }

The method name and arguments of the other CallRemoteMethods methods are used to create a MethodCall object. It is then serialized into a byte buffer which is inserted into a message. MessageProtocol.CastMessage is then used to send the message to all destinations and receive the responses.

Method Handle of MessageProtocol is overridden in RpcProtocol to invoke a method in the current peer:

       public Object Handle(Message req) {
           Object      body=null, retval=null;
           MethodCall  method_call;

           if(req == null || req.GetBuffer() == null) {
               System.err.println("RpcProtocol.Handle(): msg or msg buffer is null !");
               return null;
           }

           try {
               body=Util.ObjectFromByteBuffer(req.GetBuffer());
           }
           catch(Exception e) {
               System.err.println("RpcProtocol.Handle(): " + e);
               return null;
           }

           if(body == null || !(body instanceof MethodCall)) {
               System.err.println("RpcProtocol.Handle(): msg not a MethodCall !");
               return null;
           }

           method_call=(MethodCall)body;
           retval=method_call.Invoke(this, method_lookup_clos);
           return retval;
       }
First, the message's byte buffer is extracted and converted into a MethodCall object. If this fails, null will be returned as result4.7. Then the method call is invoked against the current RpcProtocol instance (the one that received the method call) and the result returned.


next up previous contents
Next: Advanced Concepts Up: Peer protocols Previous: MessageProtocol

1999-08-19