KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > protocols > ring > UdpRingNode


1 //$Id: UdpRingNode.java,v 1.5 2004/09/23 16:29:40 belaban Exp $
2

3 package org.jgroups.protocols.ring;
4
5 import org.jgroups.Address;
6 import org.jgroups.Event;
7 import org.jgroups.Message;
8 import org.jgroups.protocols.TOTAL_TOKEN;
9 import org.jgroups.stack.IpAddress;
10 import org.jgroups.stack.RpcProtocol;
11
12 import java.io.Serializable JavaDoc;
13 import java.util.Vector JavaDoc;
14
15
16 public class UdpRingNode implements RingNode
17 {
18
19    final Address thisNode;
20      Address nextNode;
21    final RpcProtocol rpcProtocol;
22    Object JavaDoc token;
23    final Object JavaDoc mutex = new Object JavaDoc();
24    final TOTAL_TOKEN.RingTokenHeader tokenHeader;
25    boolean tokenInStack = false;
26
27
28    public UdpRingNode(RpcProtocol owner, Address memberAddress)
29    {
30       rpcProtocol = owner;
31       thisNode = memberAddress;
32       nextNode = null;
33       tokenHeader = new TOTAL_TOKEN.RingTokenHeader();
34    }
35
36    public IpAddress getTokenReceiverAddress()
37    {
38       return (IpAddress) thisNode;
39    }
40
41    public synchronized void tokenArrived(Object JavaDoc token)
42    {
43       tokenInStack = true;
44       this.token = token;
45       this.notifyAll();
46    }
47
48    public Object JavaDoc receiveToken(int timeout) throws TokenLostException
49    {
50       Address wasNext = nextNode;
51
52       try
53       {
54
55          synchronized (this)
56          {
57             while (!tokenInStack)
58             {
59                wait(timeout);
60                break;
61             }
62
63             //we haven't received token for the time of timeout
64
if (!tokenInStack)
65             {
66                throw new TokenLostException("Token wait timout expired", null,
67                                             wasNext, TokenLostException.WHILE_RECEIVING);
68             }
69          }
70       }
71       catch (InterruptedException JavaDoc ie)
72       {
73          throw new TokenLostException("Token thread interrupted", ie,
74                                       wasNext, TokenLostException.WHILE_RECEIVING);
75       }
76       return token;
77    }
78
79    public Object JavaDoc receiveToken() throws TokenLostException
80    {
81       return receiveToken(0);
82    }
83
84    public synchronized void passToken(Object JavaDoc token)
85    {
86        Message t = new Message(nextNode, thisNode, (Serializable JavaDoc) token);
87        t.putHeader(TOTAL_TOKEN.prot_name, tokenHeader);
88        rpcProtocol.passDown(new Event(Event.MSG, t));
89        tokenInStack = false;
90    }
91
92
93    public synchronized void reconfigure(Vector JavaDoc newMembers)
94    {
95       if (isNextNeighbourChanged(newMembers))
96       {
97          nextNode = getNextNode(newMembers);
98       }
99    }
100
101    private boolean isNextNeighbourChanged(Vector JavaDoc newMembers)
102    {
103       Address oldNeighbour = nextNode;
104       Address newNeighbour = getNextNode(newMembers);
105       return !(newNeighbour.equals(oldNeighbour));
106    }
107
108    private Address getNextNode(Vector JavaDoc otherNodes)
109    {
110       int myIndex = otherNodes.indexOf(thisNode);
111       return (myIndex == otherNodes.size() - 1)?
112               (Address) otherNodes.firstElement():
113               (Address) otherNodes.elementAt(myIndex + 1);
114
115    }
116 }
117
Popular Tags