|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| package org.codehaus.wadi.group.vm; |
|
17 |
| |
|
18 |
| import java.util.HashMap; |
|
19 |
| import java.util.Iterator; |
|
20 |
| import java.util.Map; |
|
21 |
| |
|
22 |
| import org.codehaus.wadi.group.Address; |
|
23 |
| import org.codehaus.wadi.group.ClusterException; |
|
24 |
| import org.codehaus.wadi.group.Dispatcher; |
|
25 |
| import org.codehaus.wadi.group.Envelope; |
|
26 |
| import org.codehaus.wadi.group.LocalPeer; |
|
27 |
| import org.codehaus.wadi.group.MessageExchangeException; |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| public class VMBroker { |
|
34 |
| protected final long inactiveTime = 5000; |
|
35 |
| |
|
36 |
| private final String name; |
|
37 |
| private final Address address; |
|
38 |
| private final Map nodeNameToDispatcher = new HashMap(); |
|
39 |
| private final ClusterListenerSupport listenerSupport; |
|
40 |
| private MessageRecorder messageRecorder; |
|
41 |
| private EnvelopeTransformer messageTransformer; |
|
42 |
| |
|
43 |
4
| public VMBroker(String name) {
|
|
44 |
4
| this.name = name;
|
|
45 |
| |
|
46 |
4
| address = new VMClusterAddress(this);
|
|
47 |
4
| listenerSupport = new ClusterListenerSupport(this);
|
|
48 |
4
| messageTransformer = new SerializeMessageTransformer(this);
|
|
49 |
| } |
|
50 |
| |
|
51 |
0
| public VMBroker(String name, boolean serializeMessages) {
|
|
52 |
0
| this.name = name;
|
|
53 |
| |
|
54 |
0
| address = new VMClusterAddress(this);
|
|
55 |
0
| listenerSupport = new ClusterListenerSupport(this);
|
|
56 |
0
| if (serializeMessages) {
|
|
57 |
0
| messageTransformer = new SerializeMessageTransformer(this);
|
|
58 |
| } else { |
|
59 |
0
| messageTransformer = new NoOpMessageTransformer();
|
|
60 |
| } |
|
61 |
| } |
|
62 |
| |
|
63 |
4
| String getName() {
|
|
64 |
4
| return name;
|
|
65 |
| } |
|
66 |
| |
|
67 |
8
| void registerDispatcher(VMDispatcher dispatcher) {
|
|
68 |
8
| LocalPeer localPeer = dispatcher.getCluster().getLocalPeer();
|
|
69 |
8
| String nodeName = localPeer.getName();
|
|
70 |
8
| synchronized (nodeNameToDispatcher) {
|
|
71 |
8
| nodeNameToDispatcher.put(nodeName, dispatcher);
|
|
72 |
| } |
|
73 |
| |
|
74 |
| |
|
75 |
8
| listenerSupport.notifyMembershipChanged(localPeer, true);
|
|
76 |
| } |
|
77 |
| |
|
78 |
4
| void unregisterDispatcher(VMDispatcher dispatcher) {
|
|
79 |
4
| LocalPeer localPeer = dispatcher.getCluster().getLocalPeer();
|
|
80 |
4
| String nodeName = localPeer.getName();
|
|
81 |
4
| Object object;
|
|
82 |
4
| synchronized (nodeNameToDispatcher) {
|
|
83 |
4
| object = nodeNameToDispatcher.remove(nodeName);
|
|
84 |
| } |
|
85 |
4
| if (null == object) {
|
|
86 |
0
| throw new IllegalArgumentException("unknown dispatcher");
|
|
87 |
| } |
|
88 |
| |
|
89 |
4
| listenerSupport.notifyMembershipChanged(localPeer, false);
|
|
90 |
| } |
|
91 |
| |
|
92 |
3
| void send(Address to, Envelope message) throws MessageExchangeException {
|
|
93 |
3
| if (null != messageRecorder) {
|
|
94 |
0
| messageRecorder.record(to, message);
|
|
95 |
| } |
|
96 |
| |
|
97 |
3
| message = messageTransformer.transform(message);
|
|
98 |
| |
|
99 |
3
| if (to.equals(address)) {
|
|
100 |
0
| sendToClusterDestination(message);
|
|
101 |
| } else { |
|
102 |
3
| sendToAddress(to, message);
|
|
103 |
| } |
|
104 |
| } |
|
105 |
| |
|
106 |
0
| int getPeerCount() {
|
|
107 |
0
| synchronized (nodeNameToDispatcher) {
|
|
108 |
0
| return nodeNameToDispatcher.size();
|
|
109 |
| } |
|
110 |
| } |
|
111 |
| |
|
112 |
3
| Address getAddress(String name) {
|
|
113 |
3
| Map snapshotMap = snapshotDispatcherMap();
|
|
114 |
| |
|
115 |
3
| for (Iterator iter = snapshotMap.entrySet().iterator(); iter.hasNext();) {
|
|
116 |
3
| Map.Entry entry = (Map.Entry) iter.next();
|
|
117 |
3
| if (entry.getKey().equals(name)) {
|
|
118 |
3
| Dispatcher dispatcher = (Dispatcher) entry.getValue();
|
|
119 |
3
| return dispatcher.getCluster().getLocalPeer().getAddress();
|
|
120 |
| } |
|
121 |
| } |
|
122 |
| |
|
123 |
0
| throw new IllegalArgumentException("Node node having the name:" + name);
|
|
124 |
| } |
|
125 |
| |
|
126 |
38
| Map getPeers() {
|
|
127 |
38
| Map snapshotMap = snapshotDispatcherMap();
|
|
128 |
38
| for (Iterator iter = snapshotMap.entrySet().iterator(); iter.hasNext();) {
|
|
129 |
57
| Map.Entry entry = (Map.Entry) iter.next();
|
|
130 |
57
| Dispatcher dispatcher = (Dispatcher) entry.getValue();
|
|
131 |
57
| entry.setValue(dispatcher.getCluster().getLocalPeer());
|
|
132 |
| } |
|
133 |
38
| return snapshotMap;
|
|
134 |
| } |
|
135 |
| |
|
136 |
0
| Address getAddress() {
|
|
137 |
0
| return address;
|
|
138 |
| } |
|
139 |
| |
|
140 |
5
| void addClusterListener(VMLocalClusterListener listener) {
|
|
141 |
5
| listenerSupport.addClusterListener(listener);
|
|
142 |
| } |
|
143 |
| |
|
144 |
2
| void removeClusterListener(VMLocalClusterListener listener) {
|
|
145 |
2
| listenerSupport.removeClusterListener(listener);
|
|
146 |
| } |
|
147 |
| |
|
148 |
0
| long getInactiveTime() {
|
|
149 |
0
| return inactiveTime;
|
|
150 |
| } |
|
151 |
| |
|
152 |
0
| public void start() throws ClusterException {
|
|
153 |
| } |
|
154 |
| |
|
155 |
0
| public void stop() throws ClusterException {
|
|
156 |
0
| synchronized (nodeNameToDispatcher) {
|
|
157 |
0
| nodeNameToDispatcher.clear();
|
|
158 |
| } |
|
159 |
| } |
|
160 |
| |
|
161 |
0
| public void failNode(String nodeName) {
|
|
162 |
0
| VMDispatcher dispatcher;
|
|
163 |
0
| synchronized (nodeNameToDispatcher) {
|
|
164 |
0
| dispatcher = (VMDispatcher) nodeNameToDispatcher.remove(nodeName);
|
|
165 |
| } |
|
166 |
0
| if (null == dispatcher) {
|
|
167 |
0
| throw new IllegalArgumentException("Node [" + nodeName + "] is unknown.");
|
|
168 |
| } |
|
169 |
| } |
|
170 |
| |
|
171 |
0
| public void setMessageRecorder(MessageRecorder messageRecorder) {
|
|
172 |
0
| messageRecorder.setVMCluster(this);
|
|
173 |
0
| this.messageRecorder = messageRecorder;
|
|
174 |
| } |
|
175 |
| |
|
176 |
44
| private Map snapshotDispatcherMap() {
|
|
177 |
44
| synchronized (nodeNameToDispatcher) {
|
|
178 |
44
| return new HashMap(nodeNameToDispatcher);
|
|
179 |
| } |
|
180 |
| } |
|
181 |
| |
|
182 |
3
| private void sendToAddress(Address to, Envelope message) {
|
|
183 |
3
| Map snapshotMap = snapshotDispatcherMap();
|
|
184 |
| |
|
185 |
4
| for (Iterator iter = snapshotMap.entrySet().iterator(); iter.hasNext();) {
|
|
186 |
4
| Map.Entry entry = (Map.Entry) iter.next();
|
|
187 |
4
| Dispatcher dispatcher = (Dispatcher) entry.getValue();
|
|
188 |
4
| if (dispatcher.getCluster().getLocalPeer().getAddress().equals(to)) {
|
|
189 |
3
| dispatcher.onEnvelope(message);
|
|
190 |
3
| return;
|
|
191 |
| } |
|
192 |
| } |
|
193 |
0
| throw new IllegalArgumentException("Destination " + to + " is unknown.");
|
|
194 |
| } |
|
195 |
| |
|
196 |
0
| private void sendToClusterDestination(Envelope message) {
|
|
197 |
0
| Map snapshotMap = snapshotDispatcherMap();
|
|
198 |
| |
|
199 |
0
| for (Iterator iter = snapshotMap.entrySet().iterator(); iter.hasNext();) {
|
|
200 |
0
| Map.Entry entry = (Map.Entry) iter.next();
|
|
201 |
0
| Dispatcher dispatcher = (Dispatcher) entry.getValue();
|
|
202 |
0
| dispatcher.onEnvelope(message);
|
|
203 |
| } |
|
204 |
| } |
|
205 |
| |
|
206 |
| } |