KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > protocols > LOOPBACK


1 // $Id: LOOPBACK.java,v 1.13 2005/04/23 12:41:49 belaban Exp $
2

3 package org.jgroups.protocols;
4
5
6 import org.jgroups.Address;
7 import org.jgroups.Event;
8 import org.jgroups.Message;
9 import org.jgroups.View;
10 import org.jgroups.stack.IpAddress;
11 import org.jgroups.stack.Protocol;
12 import org.jgroups.util.Util;
13
14 import java.util.Vector JavaDoc;
15
16
17 /**
18  Makes copies of outgoing messages, swaps sender and receiver and sends the message back up the stack.
19  */

20 public class LOOPBACK extends Protocol {
21     private Address local_addr=null;
22     private String JavaDoc group_addr=null;
23     private final Vector JavaDoc members=new Vector JavaDoc();
24
25
26     public LOOPBACK() {
27         ;
28     }
29
30
31     public String JavaDoc toString() {
32         return "Protocol LOOPBACK(local address: " + local_addr + ')';
33     }
34
35
36
37
38     /*------------------------------ Protocol interface ------------------------------ */
39
40     public String JavaDoc getName() {
41         return "LOOPBACK";
42     }
43
44
45
46     public void init() throws Exception JavaDoc {
47 // local_addr=new IpAddress("localhost", 10000) { // fake address
48
// public String toString() {
49
// return "<fake>";
50
// }
51
// };
52

53           //local_addr=new org.jgroups.stack.IpAddress("localhost", 10000); // fake address
54
local_addr = new IpAddress(12345);
55     }
56
57     public void start() throws Exception JavaDoc {
58         passUp(new Event(Event.SET_LOCAL_ADDRESS, local_addr));
59     }
60
61
62     /**
63      * Caller by the layer above this layer. Usually we just put this Message
64      * into the send queue and let one or more worker threads handle it. A worker thread
65      * then removes the Message from the send queue, performs a conversion and adds the
66      * modified Message to the send queue of the layer below it, by calling Down).
67      */

68     public void down(Event evt) {
69         Message msg, rsp;
70         Address dest_addr;
71
72         if(log.isTraceEnabled())
73             log.trace("event is " + evt + ", group_addr=" + group_addr +
74                       ", time is " + System.currentTimeMillis() + ", hdrs: " + Util.printEvent(evt));
75
76         switch(evt.getType()) {
77
78         case Event.MSG:
79             msg=(Message)evt.getArg();
80             if(msg.getSrc() == null)
81                 msg.setSrc(local_addr);
82             rsp=msg.copy();
83             //dest_addr=msg.getDest();
84
//rsp.setDest(local_addr);
85
//rsp.setSrc(dest_addr != null ? dest_addr : local_addr);
86
up(new Event(Event.MSG, rsp));
87             break;
88
89         case Event.TMP_VIEW:
90         case Event.VIEW_CHANGE:
91             synchronized(members) {
92                 members.removeAllElements();
93                 Vector JavaDoc tmpvec=((View)evt.getArg()).getMembers();
94                 for(int i=0; i < tmpvec.size(); i++)
95                     members.addElement(tmpvec.elementAt(i));
96             }
97             break;
98
99         case Event.GET_LOCAL_ADDRESS: // return local address -> Event(SET_LOCAL_ADDRESS, local)
100
passUp(new Event(Event.SET_LOCAL_ADDRESS, local_addr));
101             break;
102
103         case Event.CONNECT:
104             group_addr=(String JavaDoc)evt.getArg();
105             passUp(new Event(Event.CONNECT_OK));
106             break;
107
108         case Event.DISCONNECT:
109             passUp(new Event(Event.DISCONNECT_OK));
110             break;
111
112         case Event.PERF:
113             passUp(evt);
114             break;
115         }
116     }
117
118
119
120     /*--------------------------- End of Protocol interface -------------------------- */
121
122
123 }
124
Popular Tags