KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > blocks > RpcDispatcherAnycastServerObject


1 package org.jgroups.blocks;
2
3 import org.jgroups.MessageListener;
4 import org.jgroups.MembershipListener;
5 import org.jgroups.Channel;
6 import org.jgroups.ChannelException;
7 import org.jgroups.Message;
8 import org.jgroups.View;
9 import org.jgroups.Address;
10 import org.jgroups.util.RspList;
11 import org.jgroups.util.Rsp;
12
13 import java.util.Vector JavaDoc;
14 import java.util.Map JavaDoc;
15 import java.util.Iterator JavaDoc;
16
17 public class RpcDispatcherAnycastServerObject implements MessageListener, MembershipListener
18 {
19    int i = 0;
20    private Channel c;
21    private RpcDispatcher d;
22
23    public RpcDispatcherAnycastServerObject(Channel channel) throws ChannelException
24    {
25       c = channel;
26       c.connect("test");
27       d = new RpcDispatcher(c, this, this, this);
28    }
29
30    public void doSomething()
31    {
32       System.out.println("doSomething invoked on " + c.getLocalAddress() + ". i = " + i);
33       i++;
34       System.out.println("Now i = " + i);
35    }
36
37    public void callRemote(boolean useAnycast, boolean excludeSelf)
38    {
39        // we need to copy the vector, otherwise the modification below will throw an exception because the underlying
40
// vector is unmodifiable
41
Vector JavaDoc v = new Vector JavaDoc(c.getView().getMembers());
42       if (excludeSelf) v.remove(c.getLocalAddress());
43       RspList rsps=d.callRemoteMethods(v, "doSomething", new Object JavaDoc[]{}, new Class JavaDoc[]{}, GroupRequest.GET_ALL, 10000, useAnycast);
44       Map.Entry JavaDoc entry;
45        for(Iterator JavaDoc it=rsps.entrySet().iterator(); it.hasNext();) {
46            entry=(Map.Entry JavaDoc)it.next();
47            Address member=(Address)entry.getKey();
48            Rsp rsp=(Rsp)entry.getValue();
49            if(!rsp.wasReceived())
50                throw new RuntimeException JavaDoc("response from " + member + " was not received, rsp=" + rsp);
51        }
52
53    }
54
55    public void shutdown()
56    {
57       c.close();
58    }
59
60
61    public void receive(Message msg)
62    {
63    }
64
65    public byte[] getState()
66    {
67       return new byte[0];
68    }
69
70    public void setState(byte[] state)
71    {
72    }
73
74    public void viewAccepted(View new_view)
75    {
76    }
77
78    public void suspect(Address suspected_mbr)
79    {
80    }
81
82    public void block()
83    {
84    }
85 }
Popular Tags