|
@@ -1,57 +1,50 @@ |
1 |
1 |
|
// This Source Code is in the Public Domain per: http://unlicense.org |
2 |
2 |
|
package org.litesoft.GWT.eventbus.server.nonpublic; |
3 |
3 |
|
|
4 |
|
- |
import org.litesoft.commonfoundation.typeutils.Objects; |
5 |
|
- |
import java.util.*; |
6 |
|
- |
import javax.servlet.http.*; |
7 |
|
- |
|
8 |
4 |
|
import org.litesoft.GWT.eventbus.client.*; |
9 |
5 |
|
import org.litesoft.GWT.eventbus.client.nonpublic.*; |
10 |
6 |
|
import org.litesoft.GWT.eventbus.server.*; |
|
7 |
+ |
import org.litesoft.commonfoundation.typeutils.Objects; |
11 |
8 |
|
import org.litesoft.logger.*; |
12 |
9 |
|
import org.litesoft.sequences.*; |
13 |
10 |
|
|
14 |
|
- |
public class ServerEventBusManager implements ServerEventBusBroadcaster |
15 |
|
- |
{ |
|
11 |
+ |
import javax.servlet.http.*; |
|
12 |
+ |
import java.util.*; |
|
13 |
+ |
|
|
14 |
+ |
public class ServerEventBusManager implements ServerEventBusBroadcaster { |
16 |
15 |
|
private static final Logger LOGGER = LoggerFactory.getLogger( ServerEventBusManager.class ); |
17 |
16 |
|
|
18 |
17 |
|
public static final ServerEventBusManager INSTANCE = new ServerEventBusManager(); |
19 |
18 |
|
|
20 |
|
- |
private ServerEventBusManager() |
21 |
|
- |
{ |
|
19 |
+ |
private ServerEventBusManager() { |
22 |
20 |
|
} |
23 |
21 |
|
|
24 |
22 |
|
private static final String SERVER_EVENT_BUSES = "ServerEventBuses"; |
25 |
23 |
|
private static final Long MAX_CLIENT_ID = (long) (Integer.MAX_VALUE - 2000); |
26 |
24 |
|
private static SequenceSource sClientIdNumberSequenceSource = SequenceSource.NULL; |
27 |
25 |
|
|
28 |
|
- |
public static synchronized void setClientIdNumberSequenceSource( SequenceSource pSequenceSource ) |
29 |
|
- |
{ |
|
26 |
+ |
public static synchronized void setClientIdNumberSequenceSource( SequenceSource pSequenceSource ) { |
30 |
27 |
|
sClientIdNumberSequenceSource = (pSequenceSource != null) ? pSequenceSource : SequenceSource.NULL; |
31 |
28 |
|
} |
32 |
29 |
|
|
33 |
|
- |
public static synchronized int getNextClientIdNumber() |
34 |
|
- |
{ |
|
30 |
+ |
public static synchronized int getNextClientIdNumber() { |
35 |
31 |
|
long cid = sClientIdNumberSequenceSource.getNextSequenceNumber( SERVER_EVENT_BUSES, MAX_CLIENT_ID ); |
36 |
32 |
|
return (int) cid; |
37 |
33 |
|
} |
38 |
34 |
|
|
39 |
35 |
|
private final NodeMap mNodes = new NodeMap(); |
40 |
36 |
|
|
41 |
|
- |
public EventPackage getPackageForNewServerEventBus( int pClientIdNumber ) |
42 |
|
- |
{ |
|
37 |
+ |
public EventPackage getPackageForNewServerEventBus( int pClientIdNumber ) { |
43 |
38 |
|
return null; |
44 |
39 |
|
} |
45 |
40 |
|
|
46 |
41 |
|
public TemporaryServerEventBus createTemporaryServerEventBus( String pWhy, ServerEventBus pParent, |
47 |
|
- |
ClientWindowInstanceDataManager pInitialClientWindowInstanceData ) |
48 |
|
- |
{ |
|
42 |
+ |
ClientWindowInstanceDataManager pInitialClientWindowInstanceData ) { |
49 |
43 |
|
int zClientIdNumber = getNextClientIdNumber(); |
50 |
44 |
|
|
51 |
45 |
|
TemporaryServerEventBus zTempSEB = new TemporaryServerEventBus( zClientIdNumber ); |
52 |
46 |
|
|
53 |
|
- |
synchronized ( mNodes ) |
54 |
|
- |
{ |
|
47 |
+ |
synchronized ( mNodes ) { |
55 |
48 |
|
zTempSEB.initialize( pWhy, pParent.getClientIdNumber(), pInitialClientWindowInstanceData ); |
56 |
49 |
|
} |
57 |
50 |
|
|
|
@@ -63,14 +56,12 @@ |
63 |
56 |
|
|
64 |
57 |
|
public TemporaryServerEventBus createTemporaryServerEventBus( String pWhy, HttpServletRequest pRequest, |
65 |
58 |
|
ThreadLocalContextFactory pThreadLocalContextFactory, |
66 |
|
- |
ClientWindowInstanceDataManager pInitialClientWindowInstanceData ) |
67 |
|
- |
{ |
|
59 |
+ |
ClientWindowInstanceDataManager pInitialClientWindowInstanceData ) { |
68 |
60 |
|
int zClientIdNumber = getNextClientIdNumber(); |
69 |
61 |
|
|
70 |
62 |
|
TemporaryServerEventBus zTempSEB = new TemporaryServerEventBus( zClientIdNumber ); |
71 |
63 |
|
|
72 |
|
- |
synchronized ( mNodes ) |
73 |
|
- |
{ |
|
64 |
+ |
synchronized ( mNodes ) { |
74 |
65 |
|
zTempSEB.initialize( pWhy, null, pInitialClientWindowInstanceData ); |
75 |
66 |
|
} |
76 |
67 |
|
|
|
@@ -82,73 +73,61 @@ |
82 |
73 |
|
/** |
83 |
74 |
|
* @return may return null |
84 |
75 |
|
*/ |
85 |
|
- |
public Integer getParentClientID( int pClientIdNumber ) |
86 |
|
- |
{ |
|
76 |
+ |
public Integer getParentClientID( int pClientIdNumber ) { |
87 |
77 |
|
return mNodes.getNode( pClientIdNumber ).getParentClientID(); |
88 |
78 |
|
} |
89 |
79 |
|
|
90 |
80 |
|
/** |
91 |
81 |
|
* @return may return null |
92 |
82 |
|
*/ |
93 |
|
- |
public ServerEventBus getServerEventBus( int pClientIdNumber ) |
94 |
|
- |
{ |
|
83 |
+ |
public ServerEventBus getServerEventBus( int pClientIdNumber ) { |
95 |
84 |
|
return mNodes.getServerEventBusOrDisposedZombie( pClientIdNumber ); |
96 |
85 |
|
} |
97 |
86 |
|
|
98 |
87 |
|
/** |
99 |
88 |
|
* @return may return null |
100 |
89 |
|
*/ |
101 |
|
- |
public ServerEventBus getRootServerEventBus( int pClientIdNumber ) |
102 |
|
- |
{ |
|
90 |
+ |
public ServerEventBus getRootServerEventBus( int pClientIdNumber ) { |
103 |
91 |
|
return mNodes.getRootNode( pClientIdNumber ).getServerEventBus(); |
104 |
92 |
|
} |
105 |
93 |
|
|
106 |
94 |
|
/** |
107 |
95 |
|
* @return may return null |
108 |
96 |
|
*/ |
109 |
|
- |
public ServerEventBus getParentServerEventBus( int pClientIdNumber ) |
110 |
|
- |
{ |
|
97 |
+ |
public ServerEventBus getParentServerEventBus( int pClientIdNumber ) { |
111 |
98 |
|
return mNodes.getParentNode( pClientIdNumber ).getServerEventBus(); |
112 |
99 |
|
} |
113 |
100 |
|
|
114 |
101 |
|
/** |
115 |
102 |
|
* @return may return an empty array, but never null |
116 |
103 |
|
*/ |
117 |
|
- |
public ServerEventBus[] getAllServerEventBuses() |
118 |
|
- |
{ |
|
104 |
+ |
public ServerEventBus[] getAllServerEventBuses() { |
119 |
105 |
|
return SelectorAll.INSTANCE.select( mNodes.getNodes() ); |
120 |
106 |
|
} |
121 |
107 |
|
|
122 |
108 |
|
/** |
123 |
109 |
|
* @return may return an empty array, but never null |
124 |
110 |
|
*/ |
125 |
|
- |
public ServerEventBus[] getChildrenServerEventBuses( int pClientIdNumber ) |
126 |
|
- |
{ |
|
111 |
+ |
public ServerEventBus[] getChildrenServerEventBuses( int pClientIdNumber ) { |
127 |
112 |
|
return new SelectorChildren( pClientIdNumber ).select( mNodes.getNodes() ); |
128 |
113 |
|
} |
129 |
114 |
|
|
130 |
115 |
|
/** |
131 |
116 |
|
* @return may return an empty array, but never null |
132 |
117 |
|
*/ |
133 |
|
- |
public ServerEventBus[] getAllRelatedServerEventBuses( int pClientIdNumber ) |
134 |
|
- |
{ |
|
118 |
+ |
public ServerEventBus[] getAllRelatedServerEventBuses( int pClientIdNumber ) { |
135 |
119 |
|
Node rootNode = mNodes.getRootNode( pClientIdNumber ); |
136 |
120 |
|
return rootNode.isReal() ? // |
137 |
121 |
|
new SelectorAllRelated().select( rootNode, mNodes.getNodes() ) : // |
138 |
122 |
|
ServerEventBus.EMPTY_ARRAY; |
139 |
123 |
|
} |
140 |
124 |
|
|
141 |
|
- |
public void broadcast( EventPackage pEventPackage ) |
142 |
|
- |
{ |
143 |
|
- |
if ( pEventPackage != null ) |
144 |
|
- |
{ |
|
125 |
+ |
public void broadcast( EventPackage pEventPackage ) { |
|
126 |
+ |
if ( pEventPackage != null ) { |
145 |
127 |
|
ServerEventBus[] buses = getAllServerEventBuses(); |
146 |
|
- |
if ( buses != null ) |
147 |
|
- |
{ |
148 |
|
- |
for ( ServerEventBus bus : buses ) |
149 |
|
- |
{ |
150 |
|
- |
if ( bus != null ) |
151 |
|
- |
{ |
|
128 |
+ |
if ( buses != null ) { |
|
129 |
+ |
for ( ServerEventBus bus : buses ) { |
|
130 |
+ |
if ( bus != null ) { |
152 |
131 |
|
bus.publishIfNotDisposed( pEventPackage ); |
153 |
132 |
|
} |
154 |
133 |
|
} |
|
@@ -156,177 +135,141 @@ |
156 |
135 |
|
} |
157 |
136 |
|
} |
158 |
137 |
|
|
159 |
|
- |
public static class BackDoor |
160 |
|
- |
{ |
161 |
|
- |
public static ServerEventBus updateServerEventBus( ServerEventBus pNewSEB, Integer pParentClientID ) |
162 |
|
- |
{ |
|
138 |
+ |
public static class BackDoor { |
|
139 |
+ |
public static ServerEventBus updateServerEventBus( ServerEventBus pNewSEB, Integer pParentClientID ) { |
163 |
140 |
|
return ServerEventBusManager.INSTANCE.mNodes.updateServerEventBus( pNewSEB, pParentClientID ); |
164 |
141 |
|
} |
165 |
142 |
|
} |
166 |
143 |
|
|
167 |
|
- |
private static class Node |
168 |
|
- |
{ |
|
144 |
+ |
private static class Node { |
169 |
145 |
|
private int mClientID; |
170 |
146 |
|
private Integer mParentClientID; |
171 |
147 |
|
private ServerEventBus mServerEventBus; |
172 |
148 |
|
private int mChildCount = 0; |
173 |
149 |
|
|
174 |
|
- |
public Node( Integer pParentClientID, int pClientID, ServerEventBus pServerEventBus ) |
175 |
|
- |
{ |
|
150 |
+ |
public Node( Integer pParentClientID, int pClientID, ServerEventBus pServerEventBus ) { |
176 |
151 |
|
mParentClientID = pParentClientID; |
177 |
152 |
|
mClientID = pClientID; |
178 |
153 |
|
mServerEventBus = pServerEventBus; |
179 |
154 |
|
} |
180 |
155 |
|
|
181 |
|
- |
public String toString() |
182 |
|
- |
{ |
|
156 |
+ |
public String toString() { |
183 |
157 |
|
return "" + mClientID + ":" + ((getServerEventBus() != null) ? "SEB" : "---") + " ^" + |
184 |
158 |
|
mParentClientID + " c:" + mChildCount; |
185 |
159 |
|
} |
186 |
160 |
|
|
187 |
|
- |
public int getClientID() |
188 |
|
- |
{ |
|
161 |
+ |
public int getClientID() { |
189 |
162 |
|
return mClientID; |
190 |
163 |
|
} |
191 |
164 |
|
|
192 |
|
- |
public Integer getParentClientID() |
193 |
|
- |
{ |
|
165 |
+ |
public Integer getParentClientID() { |
194 |
166 |
|
return mParentClientID; |
195 |
167 |
|
} |
196 |
168 |
|
|
197 |
|
- |
public synchronized ServerEventBus getServerEventBus() |
198 |
|
- |
{ |
|
169 |
+ |
public synchronized ServerEventBus getServerEventBus() { |
199 |
170 |
|
return mServerEventBus; |
200 |
171 |
|
} |
201 |
172 |
|
|
202 |
|
- |
public synchronized void replaceServerEventBus( ServerEventBus pServerEventBus ) |
203 |
|
- |
{ |
|
173 |
+ |
public synchronized void replaceServerEventBus( ServerEventBus pServerEventBus ) { |
204 |
174 |
|
mServerEventBus = pServerEventBus; |
205 |
175 |
|
} |
206 |
176 |
|
|
207 |
|
- |
public synchronized ServerEventBus clearServerEventBus() |
208 |
|
- |
{ |
|
177 |
+ |
public synchronized ServerEventBus clearServerEventBus() { |
209 |
178 |
|
ServerEventBus rv = mServerEventBus; |
210 |
179 |
|
mServerEventBus = null; |
211 |
180 |
|
return rv; |
212 |
181 |
|
} |
213 |
182 |
|
|
214 |
|
- |
public synchronized boolean shouldRemove() |
215 |
|
- |
{ |
|
183 |
+ |
public synchronized boolean shouldRemove() { |
216 |
184 |
|
return isReal() && (mServerEventBus == null) && (mChildCount < 1); |
217 |
185 |
|
} |
218 |
186 |
|
|
219 |
|
- |
public synchronized void incChildCount() |
220 |
|
- |
{ |
|
187 |
+ |
public synchronized void incChildCount() { |
221 |
188 |
|
mChildCount++; |
222 |
189 |
|
} |
223 |
190 |
|
|
224 |
|
- |
public synchronized void decChildCount() |
225 |
|
- |
{ |
|
191 |
+ |
public synchronized void decChildCount() { |
226 |
192 |
|
mChildCount--; |
227 |
193 |
|
} |
228 |
194 |
|
|
229 |
|
- |
public boolean isReal() |
230 |
|
- |
{ |
|
195 |
+ |
public boolean isReal() { |
231 |
196 |
|
return true; |
232 |
197 |
|
} |
233 |
198 |
|
} |
234 |
199 |
|
|
235 |
|
- |
private static final Node NULL_NODE = new Node( null, 0, null ) |
236 |
|
- |
{ |
237 |
|
- |
public boolean isReal() |
238 |
|
- |
{ |
|
200 |
+ |
private static final Node NULL_NODE = new Node( null, 0, null ) { |
|
201 |
+ |
public boolean isReal() { |
239 |
202 |
|
return false; |
240 |
203 |
|
} |
241 |
204 |
|
|
242 |
|
- |
public void incChildCount() |
243 |
|
- |
{ |
|
205 |
+ |
public void incChildCount() { |
244 |
206 |
|
} |
245 |
207 |
|
|
246 |
|
- |
public void decChildCount() |
247 |
|
- |
{ |
|
208 |
+ |
public void decChildCount() { |
248 |
209 |
|
} |
249 |
210 |
|
}; |
250 |
211 |
|
|
251 |
|
- |
private abstract static class AbstractSelector |
252 |
|
- |
{ |
253 |
|
- |
public ServerEventBus[] select( Node[] pNodes ) |
254 |
|
- |
{ |
|
212 |
+ |
private abstract static class AbstractSelector { |
|
213 |
+ |
public ServerEventBus[] select( Node[] pNodes ) { |
255 |
214 |
|
List<ServerEventBus> buses = new ArrayList<ServerEventBus>(); |
256 |
215 |
|
addAppropriate( buses, pNodes ); |
257 |
216 |
|
return buses.toArray( new ServerEventBus[buses.size()] ); |
258 |
217 |
|
} |
259 |
218 |
|
|
260 |
|
- |
protected void addAppropriate( List<ServerEventBus> pBuses, Node[] pNodes ) |
261 |
|
- |
{ |
262 |
|
- |
for ( Node node : pNodes ) |
263 |
|
- |
{ |
|
219 |
+ |
protected void addAppropriate( List<ServerEventBus> pBuses, Node[] pNodes ) { |
|
220 |
+ |
for ( Node node : pNodes ) { |
264 |
221 |
|
ServerEventBus seb = node.getServerEventBus(); |
265 |
|
- |
if ( (seb != null) && acceptable( node, seb ) ) |
266 |
|
- |
{ |
|
222 |
+ |
if ( (seb != null) && acceptable( node, seb ) ) { |
267 |
223 |
|
pBuses.add( seb ); |
268 |
224 |
|
} |
269 |
225 |
|
} |
270 |
226 |
|
} |
271 |
227 |
|
|
272 |
|
- |
protected boolean acceptable( Node pNode, ServerEventBus pSEB ) |
273 |
|
- |
{ |
|
228 |
+ |
protected boolean acceptable( Node pNode, ServerEventBus pSEB ) { |
274 |
229 |
|
return true; |
275 |
230 |
|
} |
276 |
231 |
|
} |
277 |
232 |
|
|
278 |
|
- |
private static class SelectorAll extends AbstractSelector |
279 |
|
- |
{ |
|
233 |
+ |
private static class SelectorAll extends AbstractSelector { |
280 |
234 |
|
public static final AbstractSelector INSTANCE = new SelectorAll(); |
281 |
235 |
|
} |
282 |
236 |
|
|
283 |
|
- |
private static class SelectorChildren extends AbstractSelector |
284 |
|
- |
{ |
|
237 |
+ |
private static class SelectorChildren extends AbstractSelector { |
285 |
238 |
|
private Integer mParentID; |
286 |
239 |
|
|
287 |
|
- |
public SelectorChildren( int pParentID ) |
288 |
|
- |
{ |
|
240 |
+ |
public SelectorChildren( int pParentID ) { |
289 |
241 |
|
mParentID = pParentID; |
290 |
242 |
|
} |
291 |
243 |
|
|
292 |
|
- |
protected boolean acceptable( Node pNode, ServerEventBus pSEB ) |
293 |
|
- |
{ |
|
244 |
+ |
protected boolean acceptable( Node pNode, ServerEventBus pSEB ) { |
294 |
245 |
|
return mParentID.equals( pNode.getParentClientID() ); |
295 |
246 |
|
} |
296 |
247 |
|
} |
297 |
248 |
|
|
298 |
|
- |
private static class SelectorAllRelated |
299 |
|
- |
{ |
300 |
|
- |
public ServerEventBus[] select( Node pRoot, Node[] pNodes ) |
301 |
|
- |
{ |
|
249 |
+ |
private static class SelectorAllRelated { |
|
250 |
+ |
public ServerEventBus[] select( Node pRoot, Node[] pNodes ) { |
302 |
251 |
|
List<ServerEventBus> buses = new ArrayList<ServerEventBus>(); |
303 |
252 |
|
new Adder( buses, pNodes ).addAppropriate( pRoot ); |
304 |
253 |
|
return buses.toArray( new ServerEventBus[buses.size()] ); |
305 |
254 |
|
} |
306 |
255 |
|
|
307 |
|
- |
private static class Adder |
308 |
|
- |
{ |
|
256 |
+ |
private static class Adder { |
309 |
257 |
|
private List<ServerEventBus> mBuses; |
310 |
258 |
|
private Node[] mNodes; |
311 |
259 |
|
|
312 |
|
- |
public Adder( List<ServerEventBus> pBuses, Node[] pNodes ) |
313 |
|
- |
{ |
|
260 |
+ |
public Adder( List<ServerEventBus> pBuses, Node[] pNodes ) { |
314 |
261 |
|
mBuses = pBuses; |
315 |
262 |
|
mNodes = pNodes; |
316 |
263 |
|
} |
317 |
264 |
|
|
318 |
|
- |
public void addAppropriate( Node pCurrent ) |
319 |
|
- |
{ |
|
265 |
+ |
public void addAppropriate( Node pCurrent ) { |
320 |
266 |
|
ServerEventBus seb = pCurrent.getServerEventBus(); |
321 |
|
- |
if ( seb != null ) |
322 |
|
- |
{ |
|
267 |
+ |
if ( seb != null ) { |
323 |
268 |
|
mBuses.add( seb ); |
324 |
269 |
|
} |
325 |
270 |
|
Integer parentID = pCurrent.getClientID(); |
326 |
|
- |
for ( Node node : mNodes ) |
327 |
|
- |
{ |
328 |
|
- |
if ( parentID.equals( node.getParentClientID() ) ) |
329 |
|
- |
{ |
|
271 |
+ |
for ( Node node : mNodes ) { |
|
272 |
+ |
if ( parentID.equals( node.getParentClientID() ) ) { |
330 |
273 |
|
addAppropriate( node ); |
331 |
274 |
|
} |
332 |
275 |
|
} |
|
@@ -334,80 +277,67 @@ |
334 |
277 |
|
} |
335 |
278 |
|
} |
336 |
279 |
|
|
337 |
|
- |
private static class NodeMap implements ServerEventBusDisposeListener |
338 |
|
- |
{ |
|
280 |
+ |
private static class NodeMap implements ServerEventBusDisposeListener { |
339 |
281 |
|
private final Map<Integer, Node> mServerEventBusNodes = new HashMap<Integer, Node>(); |
340 |
282 |
|
private final Set<Integer> mDisposedServerEventBuses = new HashSet<Integer>(); |
341 |
283 |
|
|
342 |
|
- |
public Node getNode( Integer pClientIdNumberFromParentReference ) |
343 |
|
- |
{ |
|
284 |
+ |
public Node getNode( Integer pClientIdNumberFromParentReference ) { |
344 |
285 |
|
return (pClientIdNumberFromParentReference == null) ? // |
345 |
286 |
|
NULL_NODE : // |
346 |
287 |
|
getNode( pClientIdNumberFromParentReference.intValue() ); |
347 |
288 |
|
} |
348 |
289 |
|
|
349 |
|
- |
public Node getNode( int pClientIdNumber ) |
350 |
|
- |
{ |
|
290 |
+ |
public Node getNode( int pClientIdNumber ) { |
351 |
291 |
|
Node node = getRealNode( pClientIdNumber ); |
352 |
292 |
|
return (node != null) ? node : NULL_NODE; |
353 |
293 |
|
} |
354 |
294 |
|
|
355 |
|
- |
public Node getRequiredNode( int pClientIdNumber ) |
356 |
|
- |
{ |
|
295 |
+ |
public Node getRequiredNode( int pClientIdNumber ) { |
357 |
296 |
|
Node node = getRealNode( pClientIdNumber ); |
358 |
|
- |
if ( node == null ) |
359 |
|
- |
{ |
|
297 |
+ |
if ( node == null ) { |
360 |
298 |
|
throw new IllegalStateException( "No ServerEventBus Node for ClientID: " + pClientIdNumber ); |
361 |
299 |
|
} |
362 |
300 |
|
return node; |
363 |
301 |
|
} |
364 |
302 |
|
|
365 |
303 |
|
// from ServerEventBusDisposeListener |
366 |
|
- |
public void init( IServerEventBus pServerEventBus ) |
367 |
|
- |
{ |
|
304 |
+ |
public void init( IServerEventBus pServerEventBus ) { |
368 |
305 |
|
} |
369 |
306 |
|
|
370 |
307 |
|
// from ServerEventBusDisposeListener |
371 |
|
- |
public void disposing( IServerEventBus pServerEventBus ) |
372 |
|
- |
{ |
|
308 |
+ |
public void disposing( IServerEventBus pServerEventBus ) { |
373 |
309 |
|
int clientId = pServerEventBus.getClientIdNumber(); |
374 |
310 |
|
|
375 |
311 |
|
List<ServerEventBus> mAncestors = Collections.emptyList(); |
376 |
312 |
|
|
377 |
313 |
|
ServerEventBus zExistingSEB; |
378 |
314 |
|
|
379 |
|
- |
synchronized ( this ) |
380 |
|
- |
{ |
|
315 |
+ |
synchronized ( this ) { |
381 |
316 |
|
Node node = getNode( clientId ); |
382 |
317 |
|
zExistingSEB = node.clearServerEventBus(); |
383 |
|
- |
if ( null != zExistingSEB ) |
384 |
|
- |
{ |
|
318 |
+ |
if ( null != zExistingSEB ) { |
385 |
319 |
|
mAncestors = getAncestors( node ); |
386 |
320 |
|
|
387 |
321 |
|
mDisposedServerEventBuses.add( clientId ); |
388 |
322 |
|
} |
389 |
323 |
|
|
390 |
|
- |
while ( node.shouldRemove() ) |
391 |
|
- |
{ |
|
324 |
+ |
while ( node.shouldRemove() ) { |
392 |
325 |
|
mServerEventBusNodes.remove( node.getClientID() ); |
393 |
326 |
|
node = getNode( node.getParentClientID() ); |
394 |
327 |
|
node.decChildCount(); |
395 |
328 |
|
} |
396 |
329 |
|
} |
397 |
330 |
|
|
398 |
|
- |
if ( zExistingSEB instanceof NonTemporaryServerEventBus ) |
399 |
|
- |
{ |
|
331 |
+ |
if ( zExistingSEB instanceof NonTemporaryServerEventBus ) { |
400 |
332 |
|
LOGGER.debug.log( "---Disposed: ", zExistingSEB ); |
401 |
333 |
|
} |
402 |
334 |
|
|
403 |
335 |
|
notifyAncestorsOfRemovedDecendant( mAncestors, clientId ); |
404 |
336 |
|
} |
405 |
337 |
|
|
406 |
|
- |
public synchronized ServerEventBus getServerEventBusOrDisposedZombie( int pClientIdNumber ) |
407 |
|
- |
{ |
|
338 |
+ |
public synchronized ServerEventBus getServerEventBusOrDisposedZombie( int pClientIdNumber ) { |
408 |
339 |
|
ServerEventBus zBus = getNode( pClientIdNumber ).getServerEventBus(); |
409 |
|
- |
if ( zBus != null ) |
410 |
|
- |
{ |
|
340 |
+ |
if ( zBus != null ) { |
411 |
341 |
|
return zBus; |
412 |
342 |
|
} |
413 |
343 |
|
return mDisposedServerEventBuses.contains( pClientIdNumber ) ? // |
|
@@ -415,36 +345,29 @@ |
415 |
345 |
|
null; |
416 |
346 |
|
} |
417 |
347 |
|
|
418 |
|
- |
public synchronized Node getRealNode( int pClientIdNumber ) |
419 |
|
- |
{ |
|
348 |
+ |
public synchronized Node getRealNode( int pClientIdNumber ) { |
420 |
349 |
|
return mServerEventBusNodes.get( pClientIdNumber ); |
421 |
350 |
|
} |
422 |
351 |
|
|
423 |
|
- |
public synchronized Node[] getNodes() |
424 |
|
- |
{ |
|
352 |
+ |
public synchronized Node[] getNodes() { |
425 |
353 |
|
return mServerEventBusNodes.values().toArray( new Node[mServerEventBusNodes.size()] ); |
426 |
354 |
|
} |
427 |
355 |
|
|
428 |
|
- |
public synchronized Node getParentNode( int pClientIdNumber ) |
429 |
|
- |
{ |
|
356 |
+ |
public synchronized Node getParentNode( int pClientIdNumber ) { |
430 |
357 |
|
Integer parentClientID = getNode( pClientIdNumber ).getParentClientID(); |
431 |
358 |
|
return getNode( parentClientID ); |
432 |
359 |
|
} |
433 |
360 |
|
|
434 |
|
- |
public synchronized Node getRootNode( int pClientIdNumber ) |
435 |
|
- |
{ |
|
361 |
+ |
public synchronized Node getRootNode( int pClientIdNumber ) { |
436 |
362 |
|
Node node = getNode( pClientIdNumber ); |
437 |
|
- |
for ( Integer parentClientID; null != (parentClientID = node.getParentClientID()); ) |
438 |
|
- |
{ |
|
363 |
+ |
for ( Integer parentClientID; null != (parentClientID = node.getParentClientID()); ) { |
439 |
364 |
|
node = getNode( parentClientID ); |
440 |
365 |
|
} |
441 |
366 |
|
return node; |
442 |
367 |
|
} |
443 |
368 |
|
|
444 |
|
- |
public ServerEventBus updateServerEventBus( ServerEventBus pNewSEB, Integer pParentClientID ) |
445 |
|
- |
{ |
446 |
|
- |
if ( !pNewSEB.getType().isManagable() ) |
447 |
|
- |
{ |
|
369 |
+ |
public ServerEventBus updateServerEventBus( ServerEventBus pNewSEB, Integer pParentClientID ) { |
|
370 |
+ |
if ( !pNewSEB.getType().isManagable() ) { |
448 |
371 |
|
throw badType( "Managable", pNewSEB ); |
449 |
372 |
|
} |
450 |
373 |
|
|
|
@@ -454,43 +377,34 @@ |
454 |
377 |
|
|
455 |
378 |
|
List<ServerEventBus> mAncestors = Collections.emptyList(); |
456 |
379 |
|
|
457 |
|
- |
synchronized ( this ) |
458 |
|
- |
{ |
|
380 |
+ |
synchronized ( this ) { |
459 |
381 |
|
Node node = getRealNode( clientId ); |
460 |
|
- |
if ( node != null ) |
461 |
|
- |
{ |
|
382 |
+ |
if ( node != null ) { |
462 |
383 |
|
zExistingSEB = node.getServerEventBus(); |
463 |
|
- |
if ( zExistingSEB != null ) |
464 |
|
- |
{ |
|
384 |
+ |
if ( zExistingSEB != null ) { |
465 |
385 |
|
mAncestors = getAncestors( node ); |
466 |
386 |
|
|
467 |
387 |
|
// Since we are replacing we do NOT want to get the dispose notice on the old! |
468 |
388 |
|
zExistingSEB.removeListener( this ); |
469 |
389 |
|
|
470 |
|
- |
if ( !pNewSEB.getType().isUpdateReplacable() ) |
471 |
|
- |
{ |
|
390 |
+ |
if ( !pNewSEB.getType().isUpdateReplacable() ) { |
472 |
391 |
|
throw badType( "UpdateReplacable", pNewSEB ); |
473 |
392 |
|
} |
474 |
393 |
|
} |
475 |
394 |
|
node.replaceServerEventBus( pNewSEB ); |
476 |
|
- |
if ( !Objects.areNonArraysEqual( pParentClientID, node.getParentClientID() ) ) |
477 |
|
- |
{ |
|
395 |
+ |
if ( !Objects.areNonArraysEqual( pParentClientID, node.getParentClientID() ) ) { |
478 |
396 |
|
throw new IllegalStateException( "Mismatched Parent Client IDs (" + |
479 |
397 |
|
node.getParentClientID() + ":Existing - New:" + |
480 |
398 |
|
pParentClientID + ") for Client ID: " + clientId ); |
481 |
399 |
|
} |
482 |
|
- |
} |
483 |
|
- |
else |
484 |
|
- |
{ |
|
400 |
+ |
} else { |
485 |
401 |
|
mServerEventBusNodes.put( clientId, new Node( pParentClientID, clientId, pNewSEB ) ); |
486 |
|
- |
if ( pParentClientID != null ) |
487 |
|
- |
{ |
|
402 |
+ |
if ( pParentClientID != null ) { |
488 |
403 |
|
getNode( pParentClientID ).incChildCount(); |
489 |
404 |
|
} |
490 |
405 |
|
} |
491 |
406 |
|
} |
492 |
|
- |
if ( pNewSEB instanceof NonTemporaryServerEventBus ) |
493 |
|
- |
{ |
|
407 |
+ |
if ( pNewSEB instanceof NonTemporaryServerEventBus ) { |
494 |
408 |
|
LOGGER.debug.log( "---Alive---: ", pNewSEB ); |
495 |
409 |
|
} |
496 |
410 |
|
pNewSEB.addListener( this ); |
|
@@ -498,23 +412,19 @@ |
498 |
412 |
|
return zExistingSEB; |
499 |
413 |
|
} |
500 |
414 |
|
|
501 |
|
- |
private IllegalStateException badType( String pWhy, ServerEventBus pNewSEB ) |
502 |
|
- |
{ |
|
415 |
+ |
private IllegalStateException badType( String pWhy, ServerEventBus pNewSEB ) { |
503 |
416 |
|
return new IllegalStateException( "Not " + pWhy + ": " + pNewSEB.getType() + " ServerEventBus!" ); |
504 |
417 |
|
} |
505 |
418 |
|
|
506 |
419 |
|
/** |
507 |
420 |
|
* called under lock |
508 |
421 |
|
*/ |
509 |
|
- |
private List<ServerEventBus> getAncestors( Node pNode ) |
510 |
|
- |
{ |
|
422 |
+ |
private List<ServerEventBus> getAncestors( Node pNode ) { |
511 |
423 |
|
List<ServerEventBus> zBuses = new ArrayList<ServerEventBus>(); |
512 |
|
- |
for ( Integer parentClientID; null != (parentClientID = pNode.getParentClientID()); ) |
513 |
|
- |
{ |
|
424 |
+ |
for ( Integer parentClientID; null != (parentClientID = pNode.getParentClientID()); ) { |
514 |
425 |
|
pNode = getNode( parentClientID ); |
515 |
426 |
|
ServerEventBus zBus = pNode.getServerEventBus(); |
516 |
|
- |
if ( zBus != null ) |
517 |
|
- |
{ |
|
427 |
+ |
if ( zBus != null ) { |
518 |
428 |
|
zBuses.add( zBus ); |
519 |
429 |
|
} |
520 |
430 |
|
} |
|
@@ -524,30 +434,25 @@ |
524 |
434 |
|
/** |
525 |
435 |
|
* called unsynchronized |
526 |
436 |
|
*/ |
527 |
|
- |
private void notifyAncestorsOfAddedDecendant( List<ServerEventBus> pAncestors, int pClientId ) |
528 |
|
- |
{ |
|
437 |
+ |
private void notifyAncestorsOfAddedDecendant( List<ServerEventBus> pAncestors, int pClientId ) { |
529 |
438 |
|
notifyAncestorsWith( pAncestors, pClientId, true ); |
530 |
439 |
|
} |
531 |
440 |
|
|
532 |
441 |
|
/** |
533 |
442 |
|
* called unsynchronized |
534 |
443 |
|
*/ |
535 |
|
- |
private void notifyAncestorsOfRemovedDecendant( List<ServerEventBus> pAncestors, int pClientId ) |
536 |
|
- |
{ |
|
444 |
+ |
private void notifyAncestorsOfRemovedDecendant( List<ServerEventBus> pAncestors, int pClientId ) { |
537 |
445 |
|
notifyAncestorsWith( pAncestors, pClientId, false ); |
538 |
446 |
|
} |
539 |
447 |
|
|
540 |
448 |
|
/** |
541 |
449 |
|
* called unsynchronized |
542 |
450 |
|
*/ |
543 |
|
- |
private void notifyAncestorsWith( List<ServerEventBus> pAncestors, int pClientId, boolean pAdd ) |
544 |
|
- |
{ |
545 |
|
- |
if ( !pAncestors.isEmpty() ) |
546 |
|
- |
{ |
|
451 |
+ |
private void notifyAncestorsWith( List<ServerEventBus> pAncestors, int pClientId, boolean pAdd ) { |
|
452 |
+ |
if ( !pAncestors.isEmpty() ) { |
547 |
453 |
|
EventPackage eventPackage = |
548 |
454 |
|
new NotificationReDecendantEventPackage( "ServerEventBusManager", pClientId, pAdd ); |
549 |
|
- |
for ( ServerEventBus zAncestor : pAncestors ) |
550 |
|
- |
{ |
|
455 |
+ |
for ( ServerEventBus zAncestor : pAncestors ) { |
551 |
456 |
|
zAncestor.publish( eventPackage ); |
552 |
457 |
|
} |
553 |
458 |
|
} |