|
@@ -1,15 +1,14 @@ |
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 java.util.*; |
5 |
|
- |
|
6 |
4 |
|
import org.litesoft.GWT.eventbus.client.nonpublic.*; |
7 |
5 |
|
import org.litesoft.GWT.eventbus.client.rpc.*; |
8 |
6 |
|
import org.litesoft.commonfoundation.base.*; |
9 |
7 |
|
import org.litesoft.logger.*; |
10 |
8 |
|
|
11 |
|
- |
public class ServerSideRemotePeerService implements RemotePeerService |
12 |
|
- |
{ |
|
9 |
+ |
import java.util.*; |
|
10 |
+ |
|
|
11 |
+ |
public class ServerSideRemotePeerService implements RemotePeerService { |
13 |
12 |
|
private final MillisecTimeSource mMillisecTimeSource; |
14 |
13 |
|
private final Set<String> mAddPeerInterestsForClient = new HashSet<String>(); |
15 |
14 |
|
private final Set<String> mRemovePeerInterestsForClient = new HashSet<String>(); |
|
@@ -21,8 +20,7 @@ |
21 |
20 |
|
private Long mGiveUpAfter = null; |
22 |
21 |
|
private PeerSupportingEventBus mServerEventBus; |
23 |
22 |
|
|
24 |
|
- |
public ServerSideRemotePeerService( MillisecTimeSource pMillisecTimeSource, Logger pLogger, PeerSupportingEventBus pServerEventBus ) |
25 |
|
- |
{ |
|
23 |
+ |
public ServerSideRemotePeerService( MillisecTimeSource pMillisecTimeSource, Logger pLogger, PeerSupportingEventBus pServerEventBus ) { |
26 |
24 |
|
mMillisecTimeSource = MillisecTimeSource.deNull( pMillisecTimeSource ); |
27 |
25 |
|
mLogger = pLogger; |
28 |
26 |
|
mServerEventBus = (pServerEventBus != null) ? // |
|
@@ -30,15 +28,12 @@ |
30 |
28 |
|
PeerSupportingEventBus.Null.INSTANCE; |
31 |
29 |
|
} |
32 |
30 |
|
|
33 |
|
- |
public ServerSideRemotePeerService( Logger pLogger, PeerSupportingEventBus pServerEventBus ) |
34 |
|
- |
{ |
|
31 |
+ |
public ServerSideRemotePeerService( Logger pLogger, PeerSupportingEventBus pServerEventBus ) { |
35 |
32 |
|
this( null, pLogger, pServerEventBus ); |
36 |
33 |
|
} |
37 |
34 |
|
|
38 |
|
- |
private synchronized void setGiveUpAfter( int pPollMillisecs ) |
39 |
|
- |
{ |
40 |
|
- |
if ( pPollMillisecs > 0 ) |
41 |
|
- |
{ |
|
35 |
+ |
private synchronized void setGiveUpAfter( int pPollMillisecs ) { |
|
36 |
+ |
if ( pPollMillisecs > 0 ) { |
42 |
37 |
|
mGiveUpAfter = mMillisecTimeSource.now() + pPollMillisecs + 60000L * 5; // 5 minutes |
43 |
38 |
|
} |
44 |
39 |
|
} |
|
@@ -48,22 +43,18 @@ |
48 |
43 |
|
return (mGiveUpAfter == null) || (mMillisecTimeSource.now() < mGiveUpAfter); |
49 |
44 |
|
} |
50 |
45 |
|
|
51 |
|
- |
private synchronized PeerSupportingEventBus getServerEventBus() |
52 |
|
- |
{ |
|
46 |
+ |
private synchronized PeerSupportingEventBus getServerEventBus() { |
53 |
47 |
|
return mServerEventBus; |
54 |
48 |
|
} |
55 |
49 |
|
|
56 |
|
- |
private synchronized void clrServerEventBus() |
57 |
|
- |
{ |
|
50 |
+ |
private synchronized void clrServerEventBus() { |
58 |
51 |
|
mServerEventBus = PeerSupportingEventBus.Null.INSTANCE; |
59 |
52 |
|
} |
60 |
53 |
|
|
61 |
|
- |
public void stopUnrequestedMessages() |
62 |
|
- |
{ |
|
54 |
+ |
public void stopUnrequestedMessages() { |
63 |
55 |
|
} |
64 |
56 |
|
|
65 |
|
- |
public synchronized RemotePeerService dispose() |
66 |
|
- |
{ |
|
57 |
+ |
public synchronized RemotePeerService dispose() { |
67 |
58 |
|
mLogger.trace.log( "Dispose: ServerSideRemotePeerService" ); |
68 |
59 |
|
mAddPeerInterestsForClient.clear(); |
69 |
60 |
|
mRemovePeerInterestsForClient.clear(); |
|
@@ -72,35 +63,25 @@ |
72 |
63 |
|
return RemotePeerService.Null.INSTANCE; |
73 |
64 |
|
} |
74 |
65 |
|
|
75 |
|
- |
public synchronized boolean forwardTo( ChannelServicePackage pToClient ) |
76 |
|
- |
{ |
77 |
|
- |
if ( pToClient != null ) |
78 |
|
- |
{ |
79 |
|
- |
if ( pToClient.anyAddPeerInterests() ) |
80 |
|
- |
{ |
81 |
|
- |
for ( String peerInterest : pToClient.getAddPeerInterests() ) |
82 |
|
- |
{ |
|
66 |
+ |
public synchronized boolean forwardTo( ChannelServicePackage pToClient ) { |
|
67 |
+ |
if ( pToClient != null ) { |
|
68 |
+ |
if ( pToClient.anyAddPeerInterests() ) { |
|
69 |
+ |
for ( String peerInterest : pToClient.getAddPeerInterests() ) { |
83 |
70 |
|
mAddPeerInterestsForClient.add( peerInterest ); |
84 |
71 |
|
} |
85 |
72 |
|
} |
86 |
|
- |
if ( pToClient.anyRemovePeerInterests() ) |
87 |
|
- |
{ |
88 |
|
- |
for ( String peerInterest : pToClient.getRemovePeerInterests() ) |
89 |
|
- |
{ |
|
73 |
+ |
if ( pToClient.anyRemovePeerInterests() ) { |
|
74 |
+ |
for ( String peerInterest : pToClient.getRemovePeerInterests() ) { |
90 |
75 |
|
mRemovePeerInterestsForClient.add( peerInterest ); |
91 |
76 |
|
} |
92 |
77 |
|
} |
93 |
|
- |
if ( pToClient.anyEvents() ) |
94 |
|
- |
{ |
95 |
|
- |
for ( ChannelEventPackage event : pToClient.getEvents() ) |
96 |
|
- |
{ |
|
78 |
+ |
if ( pToClient.anyEvents() ) { |
|
79 |
+ |
for ( ChannelEventPackage event : pToClient.getEvents() ) { |
97 |
80 |
|
mChannelEventsForClient.add( event ); |
98 |
81 |
|
} |
99 |
82 |
|
} |
100 |
|
- |
if ( pToClient.anyAckPeerInterests() ) |
101 |
|
- |
{ |
102 |
|
- |
for ( String ackInterest : pToClient.getAckPeerInterests() ) |
103 |
|
- |
{ |
|
83 |
+ |
if ( pToClient.anyAckPeerInterests() ) { |
|
84 |
+ |
for ( String ackInterest : pToClient.getAckPeerInterests() ) { |
104 |
85 |
|
mAckPeerInterestsForClient.add( ackInterest ); |
105 |
86 |
|
} |
106 |
87 |
|
} |
|
@@ -108,25 +89,19 @@ |
108 |
89 |
|
return okToCollect(); |
109 |
90 |
|
} |
110 |
91 |
|
|
111 |
|
- |
public ChannelServicePackage processClientRequest( int pPollMillisecs, ChannelServicePackage pFromClient ) |
112 |
|
- |
{ |
|
92 |
+ |
public ChannelServicePackage processClientRequest( int pPollMillisecs, ChannelServicePackage pFromClient ) { |
113 |
93 |
|
setGiveUpAfter( pPollMillisecs ); |
114 |
94 |
|
PeerSupportingEventBus eventBus = getServerEventBus(); |
115 |
95 |
|
boolean peerOK; |
116 |
|
- |
if ( (pFromClient != null) && pFromClient.isNotEmpty() ) |
117 |
|
- |
{ |
|
96 |
+ |
if ( (pFromClient != null) && pFromClient.isNotEmpty() ) { |
118 |
97 |
|
peerOK = eventBus.propagateFromRemotePeerService( pFromClient ); |
119 |
|
- |
} |
120 |
|
- |
else |
121 |
|
- |
{ |
|
98 |
+ |
} else { |
122 |
99 |
|
peerOK = eventBus.ping(); |
123 |
100 |
|
} |
124 |
|
- |
if ( !peerOK ) |
125 |
|
- |
{ |
|
101 |
+ |
if ( !peerOK ) { |
126 |
102 |
|
clrServerEventBus(); |
127 |
103 |
|
} |
128 |
|
- |
synchronized ( this ) |
129 |
|
- |
{ |
|
104 |
+ |
synchronized ( this ) { |
130 |
105 |
|
return new ChannelServicePackage( extractAddPeerInterestsForClient(), // |
131 |
106 |
|
extractRemovePeerInterestsForClient(), // |
132 |
107 |
|
extractChannelEventsForClient(), // |
|
@@ -134,8 +109,7 @@ |
134 |
109 |
|
} |
135 |
110 |
|
} |
136 |
111 |
|
|
137 |
|
- |
public String toString() |
138 |
|
- |
{ |
|
112 |
+ |
public String toString() { |
139 |
113 |
|
return "ServerSideRemotePeerService"; |
140 |
114 |
|
} |
141 |
115 |
|
|