KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jgroups.protocols;
2
3 import org.jgroups.Address;
4 import org.jgroups.Event;
5 import org.jgroups.Message;
6 import org.jgroups.stack.IpAddress;
7
8 import java.util.Map JavaDoc;
9 import java.util.concurrent.ConcurrentHashMap JavaDoc;
10
11
12 /**
13  * Loopback transport shared by all channels within the same VM. Property for testing is that no messages are lost. Allows
14  * us to test various protocols (with ProtocolTester) at maximum speed.
15  * @author Bela Ban
16  * @version $Id: SHARED_LOOPBACK.java,v 1.3 2007/04/27 07:59:20 belaban Exp $
17  */

18 public class SHARED_LOOPBACK extends TP {
19     private static int next_port=10000;
20
21     /** Map of cluster names and address-protocol mappings. Used for routing messages to all or single members */
22     private static final Map JavaDoc<String JavaDoc,Map JavaDoc<Address,SHARED_LOOPBACK>> routing_table=new ConcurrentHashMap JavaDoc<String JavaDoc,Map JavaDoc<Address,SHARED_LOOPBACK>>();
23
24
25     public SHARED_LOOPBACK() {
26     }
27
28     
29
30     public String JavaDoc toString() {
31         return "SHARED_LOOPBACK(local address: " + local_addr + ')';
32     }
33
34     public void sendToAllMembers(byte[] data, int offset, int length) throws Exception JavaDoc {
35         Map JavaDoc<Address,SHARED_LOOPBACK> dests=routing_table.get(channel_name);
36         if(dests == null) {
37             if(log.isWarnEnabled())
38                 log.warn("no destination found for " + channel_name);
39             return;
40         }
41         for(Map.Entry JavaDoc<Address,SHARED_LOOPBACK> entry: dests.entrySet()) {
42             Address dest=entry.getKey();
43             SHARED_LOOPBACK target=entry.getValue();
44             try {
45                 target.receive(dest, local_addr, data, offset, length);
46             }
47             catch(Throwable JavaDoc t) {
48                 log.error("failed sending message to " + dest, t);
49             }
50         }
51     }
52
53     public void sendToSingleMember(Address dest, byte[] data, int offset, int length) throws Exception JavaDoc {
54         Map JavaDoc<Address,SHARED_LOOPBACK> dests=routing_table.get(channel_name);
55         if(dests == null) {
56             if(log.isWarnEnabled())
57                 log.warn("no destination found for " + channel_name);
58             return;
59         }
60         SHARED_LOOPBACK target=dests.get(dest);
61         if(target == null) {
62             if(log.isWarnEnabled())
63                 log.warn("destination address " + dest + " not found");
64             return;
65         }
66         target.receive(dest, local_addr, data, offset, length);
67     }
68
69     public String JavaDoc getInfo() {
70         return toString();
71     }
72
73     public void postUnmarshalling(Message msg, Address dest, Address src, boolean multicast) {
74     }
75
76     public void postUnmarshallingList(Message msg, Address dest, boolean multicast) {
77     }
78
79     /*------------------------------ Protocol interface ------------------------------ */
80
81     public String JavaDoc getName() {
82         return "SHARED_LOOPBACK";
83     }
84
85 // public boolean setProperties(Properties props) {
86
// super.setProperties(props);
87
// if(!props.isEmpty()) {
88
// log.error("the following properties are not recognized: " + props);
89
// return false;
90
// }
91
// return true;
92
// }
93

94
95     public void init() throws Exception JavaDoc {
96         local_addr=new IpAddress("127.0.0.1", next_port++);
97         super.init();
98     }
99
100     public void start() throws Exception JavaDoc {
101         super.start();
102     }
103
104     public void stop() {
105         super.stop();
106     }
107
108
109     public Object JavaDoc down(Event evt) {
110         Object JavaDoc retval=super.down(evt);
111
112         switch(evt.getType()) {
113             case Event.CONNECT:
114                 register(channel_name, local_addr, this);
115                 break;
116
117             case Event.DISCONNECT:
118                 unregister(channel_name, local_addr);
119                 break;
120         }
121
122         return retval;
123     }
124
125     private void register(String JavaDoc channel_name, Address local_addr, SHARED_LOOPBACK shared_loopback) {
126         Map JavaDoc<Address,SHARED_LOOPBACK> map=routing_table.get(channel_name);
127         if(map == null) {
128             map=new ConcurrentHashMap JavaDoc<Address,SHARED_LOOPBACK>();
129             routing_table.put(channel_name, map);
130         }
131         map.put(local_addr, shared_loopback);
132     }
133
134     private void unregister(String JavaDoc channel_name, Address local_addr) {
135         Map JavaDoc<Address,SHARED_LOOPBACK> map=routing_table.get(channel_name);
136         if(map != null) {
137             map.remove(local_addr);
138             if(map.isEmpty()) {
139                 routing_table.remove(channel_name);
140             }
141         }
142     }
143
144 }
145
Popular Tags