|
@@ -1,183 +1,183 @@ |
1 |
|
- |
// This Source Code is in the Public Domain per: http://unlicense.org |
2 |
|
- |
package org.litesoft.GWT.eventbus.client.rpc; |
3 |
|
- |
|
4 |
|
- |
import org.litesoft.GWT.client.*; |
5 |
|
- |
import org.litesoft.GWT.eventbus.client.nonpublic.*; |
6 |
|
- |
import org.litesoft.commonfoundation.issues.*; |
7 |
|
- |
import org.litesoft.core.simpletypes.temporal.*; |
8 |
|
- |
import org.litesoft.logger.nonpublic.*; |
9 |
|
- |
|
10 |
|
- |
import com.google.gwt.user.client.*; |
11 |
|
- |
import com.google.gwt.user.client.rpc.*; |
12 |
|
- |
|
13 |
|
- |
public class ClientSideRemotePeerService implements RemotePeerService { |
14 |
|
- |
private static final String COMMUNICATION_FAILURE = "TheServerCommunication"; |
15 |
|
- |
|
16 |
|
- |
private static PeerSupportingEventBus sClientEventBus; |
17 |
|
- |
private static int sClientIdNumber; |
18 |
|
- |
private static ChannelServiceAsync sChannelServiceAsync; |
19 |
|
- |
private static int sPollMillisecs; |
20 |
|
- |
private static PollTimer sPollTimer; |
21 |
|
- |
|
22 |
|
- |
public ClientSideRemotePeerService( PeerSupportingEventBus pClientEventBus, // |
23 |
|
- |
ChannelServiceAsync pChannelServiceAsync, // |
24 |
|
- |
int pPollSecs ) { |
25 |
|
- |
sClientEventBus = pClientEventBus; |
26 |
|
- |
sClientIdNumber = pClientEventBus.getClientIdNumber(); |
27 |
|
- |
sChannelServiceAsync = pChannelServiceAsync; |
28 |
|
- |
sPollMillisecs = (pPollSecs < 1) ? 0 : pPollSecs * 1000; |
29 |
|
- |
sPollTimer = (sPollMillisecs <= 0) ? NoPollTimer.INSTANCE : new RealPollTimer(); |
30 |
|
- |
} |
31 |
|
- |
|
32 |
|
- |
public String toString() { |
33 |
|
- |
return "ClientSideRemotePeerService"; |
34 |
|
- |
} |
35 |
|
- |
|
36 |
|
- |
public boolean forwardTo( ChannelServicePackage pFromPeer ) { |
37 |
|
- |
return (pFromPeer == null) || pFromPeer.isEmpty() || // |
38 |
|
- |
sendEm( ForwardToAsyncCallback.INSTANCE, pFromPeer ); |
39 |
|
- |
} |
40 |
|
- |
|
41 |
|
- |
public void stopUnrequestedMessages() { |
42 |
|
- |
sPollTimer.cancel(); |
43 |
|
- |
} |
44 |
|
- |
|
45 |
|
- |
public RemotePeerService dispose() { |
46 |
|
- |
return RemotePeerService.Null.INSTANCE; |
47 |
|
- |
} |
48 |
|
- |
|
49 |
|
- |
private static int sMessageSequenceNumber = 0; |
50 |
|
- |
|
51 |
|
- |
private static boolean sendEm( AsyncCallback pAsyncCallback, |
52 |
|
- |
ChannelServicePackage pChannelServicePackage ) { |
53 |
|
- |
boolean sent = false; |
54 |
|
- |
sPollTimer.cancel(); |
55 |
|
- |
sMessageSequenceNumber++; |
56 |
|
- |
try { |
57 |
|
- |
trace( ">", pAsyncCallback, sMessageSequenceNumber ); |
58 |
|
- |
sChannelServiceAsync.propagate( sClientIdNumber, sMessageSequenceNumber, // |
59 |
|
- |
sPollMillisecs, pChannelServicePackage, // |
60 |
|
- |
pAsyncCallback ); |
61 |
|
- |
sent = true; |
62 |
|
- |
} |
63 |
|
- |
catch ( RuntimeException e ) { |
64 |
|
- |
sClientEventBus.handleUnexpectedProblem( COMMUNICATION_FAILURE, // |
65 |
|
- |
new Problem( e, "SendingToServer" ) ); |
66 |
|
- |
} |
67 |
|
- |
sPollTimer.schedule(); |
68 |
|
- |
return sent; |
69 |
|
- |
} |
70 |
|
- |
|
71 |
|
- |
private interface PollTimer { |
72 |
|
- |
public void cancel(); |
73 |
|
- |
|
74 |
|
- |
public void schedule(); |
75 |
|
- |
} |
76 |
|
- |
|
77 |
|
- |
private static class RealPollTimer extends Timer implements PollTimer { |
78 |
|
- |
public void run() { |
79 |
|
- |
if ( !AbstractClientMainAppEntryPoint.sWindowClosing ) { |
80 |
|
- |
sendEm( PollAsyncCallback.INSTANCE, null ); |
81 |
|
- |
} |
82 |
|
- |
} |
83 |
|
- |
|
84 |
|
- |
public void schedule() { |
85 |
|
- |
schedule( sPollMillisecs ); |
86 |
|
- |
} |
87 |
|
- |
} |
88 |
|
- |
|
89 |
|
- |
private static class NoPollTimer implements PollTimer { |
90 |
|
- |
public static final PollTimer INSTANCE = new NoPollTimer(); |
91 |
|
- |
|
92 |
|
- |
public void cancel() { |
93 |
|
- |
} |
94 |
|
- |
|
95 |
|
- |
public void schedule() { |
96 |
|
- |
} |
97 |
|
- |
} |
98 |
|
- |
|
99 |
|
- |
private static class AbstractAsyncCallback implements AsyncCallback { |
100 |
|
- |
private String mTypeString; |
101 |
|
- |
|
102 |
|
- |
protected AbstractAsyncCallback( String pTypeString ) { |
103 |
|
- |
mTypeString = pTypeString; |
104 |
|
- |
} |
105 |
|
- |
|
106 |
|
- |
public void onSuccess( Object pObject ) { |
107 |
|
- |
if ( !AbstractClientMainAppEntryPoint.sWindowClosing ) { |
108 |
|
- |
ChannelServiceResult result = (ChannelServiceResult) pObject; |
109 |
|
- |
trace( "<", this, result.getMessageSequenceNumber() ); |
110 |
|
- |
if ( result.isForceClose() || result.isServerLost() ) { |
111 |
|
- |
sPollTimer.cancel(); |
112 |
|
- |
sPollTimer = NoPollTimer.INSTANCE; |
113 |
|
- |
if ( result.isForceClose() ) { |
114 |
|
- |
UtilsGwt.closeWindowNoConfirm( 5 ); |
115 |
|
- |
} else { |
116 |
|
- |
sClientEventBus.problemFromRemotePeerService( |
117 |
|
- |
new NoServerEventBusEventPackage( "ChannelClient" ) ); |
118 |
|
- |
} |
119 |
|
- |
return; |
120 |
|
- |
} |
121 |
|
- |
DeferredCommand.addCommand( new DeferredServerMessageHandler( this, result ) ); |
122 |
|
- |
} |
123 |
|
- |
} |
124 |
|
- |
|
125 |
|
- |
private static class DeferredServerMessageHandler implements Command { |
126 |
|
- |
private AsyncCallback mAsyncCallback; |
127 |
|
- |
private ChannelServiceResult mResult; |
128 |
|
- |
|
129 |
|
- |
public DeferredServerMessageHandler( AsyncCallback pAsyncCallback, ChannelServiceResult pResult ) { |
130 |
|
- |
mAsyncCallback = pAsyncCallback; |
131 |
|
- |
mResult = pResult; |
132 |
|
- |
} |
133 |
|
- |
|
134 |
|
- |
public void execute() { |
135 |
|
- |
trace( "#", mAsyncCallback, mResult.getMessageSequenceNumber() ); |
136 |
|
- |
ChannelServicePackage zPackage = mResult.getPackage(); |
137 |
|
- |
if ( !sClientEventBus.propagateFromRemotePeerService( zPackage ) ) { |
138 |
|
- |
sClientEventBus = PeerSupportingEventBus.Null.INSTANCE; |
139 |
|
- |
} |
140 |
|
- |
trace( "$", mAsyncCallback, mResult.getMessageSequenceNumber() ); |
141 |
|
- |
} |
142 |
|
- |
} |
143 |
|
- |
|
144 |
|
- |
public void onFailure( Throwable pThrowable ) { |
145 |
|
- |
if ( !AbstractClientMainAppEntryPoint.sWindowClosing ) { |
146 |
|
- |
sClientEventBus.handleUnexpectedProblem( COMMUNICATION_FAILURE, new Problem( pThrowable, |
147 |
|
- |
"Failed to contact the server", |
148 |
|
- |
toString() ) ); |
149 |
|
- |
} |
150 |
|
- |
} |
151 |
|
- |
|
152 |
|
- |
public String toString() { |
153 |
|
- |
return mTypeString; |
154 |
|
- |
} |
155 |
|
- |
} |
156 |
|
- |
|
157 |
|
- |
private static class PollAsyncCallback extends AbstractAsyncCallback { |
158 |
|
- |
public static final AsyncCallback INSTANCE = new PollAsyncCallback(); |
159 |
|
- |
|
160 |
|
- |
private PollAsyncCallback() { |
161 |
|
- |
super( "POLL" ); |
162 |
|
- |
} |
163 |
|
- |
} |
164 |
|
- |
|
165 |
|
- |
private static class ForwardToAsyncCallback extends AbstractAsyncCallback { |
166 |
|
- |
public static final AsyncCallback INSTANCE = new ForwardToAsyncCallback(); |
167 |
|
- |
|
168 |
|
- |
private ForwardToAsyncCallback() { |
169 |
|
- |
super( "FRWD" ); |
170 |
|
- |
} |
171 |
|
- |
} |
172 |
|
- |
|
173 |
|
- |
/** |
174 |
|
- |
* @noinspection UnusedDeclaration |
175 |
|
- |
*/ |
176 |
|
- |
private static void trace( String pDirection, AsyncCallback pAsyncCallback, int pMessageSequenceNumber ) { |
177 |
|
- |
AbstractLogger zLogger = sClientEventBus.getLogger().trace(); |
178 |
|
- |
if ( zLogger.isEnabled() ) { |
179 |
|
- |
zLogger.log( "CC-" + pAsyncCallback + " " + +sClientIdNumber + "|" + pMessageSequenceNumber + |
180 |
|
- |
" " + pDirection + " " + SimpleTimestamp.now() ); |
181 |
|
- |
} |
182 |
|
- |
} |
183 |
|
- |
} |
|
1 |
+ |
// This Source Code is in the Public Domain per: http://unlicense.org |
|
2 |
+ |
package org.litesoft.GWT.eventbus.client.rpc; |
|
3 |
+ |
|
|
4 |
+ |
import org.litesoft.GWT.client.*; |
|
5 |
+ |
import org.litesoft.GWT.eventbus.client.nonpublic.*; |
|
6 |
+ |
import org.litesoft.commonfoundation.issues.*; |
|
7 |
+ |
import org.litesoft.core.simpletypes.temporal.*; |
|
8 |
+ |
import org.litesoft.logger.nonpublic.*; |
|
9 |
+ |
|
|
10 |
+ |
import com.google.gwt.user.client.*; |
|
11 |
+ |
import com.google.gwt.user.client.rpc.*; |
|
12 |
+ |
|
|
13 |
+ |
public class ClientSideRemotePeerService implements RemotePeerService { |
|
14 |
+ |
private static final String COMMUNICATION_FAILURE = "TheServerCommunication"; |
|
15 |
+ |
|
|
16 |
+ |
private static PeerSupportingEventBus sClientEventBus; |
|
17 |
+ |
private static int sClientIdNumber; |
|
18 |
+ |
private static ChannelServiceAsync sChannelServiceAsync; |
|
19 |
+ |
private static int sPollMillisecs; |
|
20 |
+ |
private static PollTimer sPollTimer; |
|
21 |
+ |
|
|
22 |
+ |
public ClientSideRemotePeerService( PeerSupportingEventBus pClientEventBus, // |
|
23 |
+ |
ChannelServiceAsync pChannelServiceAsync, // |
|
24 |
+ |
int pPollSecs ) { |
|
25 |
+ |
sClientEventBus = pClientEventBus; |
|
26 |
+ |
sClientIdNumber = pClientEventBus.getClientIdNumber(); |
|
27 |
+ |
sChannelServiceAsync = pChannelServiceAsync; |
|
28 |
+ |
sPollMillisecs = (pPollSecs < 1) ? 0 : pPollSecs * 1000; |
|
29 |
+ |
sPollTimer = (sPollMillisecs <= 0) ? NoPollTimer.INSTANCE : new RealPollTimer(); |
|
30 |
+ |
} |
|
31 |
+ |
|
|
32 |
+ |
public String toString() { |
|
33 |
+ |
return "ClientSideRemotePeerService"; |
|
34 |
+ |
} |
|
35 |
+ |
|
|
36 |
+ |
public boolean forwardTo( ChannelServicePackage pFromPeer ) { |
|
37 |
+ |
return (pFromPeer == null) || pFromPeer.isEmpty() || // |
|
38 |
+ |
sendEm( ForwardToAsyncCallback.INSTANCE, pFromPeer ); |
|
39 |
+ |
} |
|
40 |
+ |
|
|
41 |
+ |
public void stopUnrequestedMessages() { |
|
42 |
+ |
sPollTimer.cancel(); |
|
43 |
+ |
} |
|
44 |
+ |
|
|
45 |
+ |
public RemotePeerService dispose() { |
|
46 |
+ |
return RemotePeerService.Null.INSTANCE; |
|
47 |
+ |
} |
|
48 |
+ |
|
|
49 |
+ |
private static int sMessageSequenceNumber = 0; |
|
50 |
+ |
|
|
51 |
+ |
private static boolean sendEm( AsyncCallback pAsyncCallback, |
|
52 |
+ |
ChannelServicePackage pChannelServicePackage ) { |
|
53 |
+ |
boolean sent = false; |
|
54 |
+ |
sPollTimer.cancel(); |
|
55 |
+ |
sMessageSequenceNumber++; |
|
56 |
+ |
try { |
|
57 |
+ |
trace( ">", pAsyncCallback, sMessageSequenceNumber ); |
|
58 |
+ |
sChannelServiceAsync.propagate( sClientIdNumber, sMessageSequenceNumber, // |
|
59 |
+ |
sPollMillisecs, pChannelServicePackage, // |
|
60 |
+ |
pAsyncCallback ); |
|
61 |
+ |
sent = true; |
|
62 |
+ |
} |
|
63 |
+ |
catch ( RuntimeException e ) { |
|
64 |
+ |
sClientEventBus.handleUnexpectedProblem( COMMUNICATION_FAILURE, // |
|
65 |
+ |
new Problem( e, "SendingToServer" ) ); |
|
66 |
+ |
} |
|
67 |
+ |
sPollTimer.schedule(); |
|
68 |
+ |
return sent; |
|
69 |
+ |
} |
|
70 |
+ |
|
|
71 |
+ |
private interface PollTimer { |
|
72 |
+ |
public void cancel(); |
|
73 |
+ |
|
|
74 |
+ |
public void schedule(); |
|
75 |
+ |
} |
|
76 |
+ |
|
|
77 |
+ |
private static class RealPollTimer extends Timer implements PollTimer { |
|
78 |
+ |
public void run() { |
|
79 |
+ |
if ( !AbstractClientMainAppEntryPoint.sWindowClosing ) { |
|
80 |
+ |
sendEm( PollAsyncCallback.INSTANCE, null ); |
|
81 |
+ |
} |
|
82 |
+ |
} |
|
83 |
+ |
|
|
84 |
+ |
public void schedule() { |
|
85 |
+ |
schedule( sPollMillisecs ); |
|
86 |
+ |
} |
|
87 |
+ |
} |
|
88 |
+ |
|
|
89 |
+ |
private static class NoPollTimer implements PollTimer { |
|
90 |
+ |
public static final PollTimer INSTANCE = new NoPollTimer(); |
|
91 |
+ |
|
|
92 |
+ |
public void cancel() { |
|
93 |
+ |
} |
|
94 |
+ |
|
|
95 |
+ |
public void schedule() { |
|
96 |
+ |
} |
|
97 |
+ |
} |
|
98 |
+ |
|
|
99 |
+ |
private static class AbstractAsyncCallback implements AsyncCallback { |
|
100 |
+ |
private String mTypeString; |
|
101 |
+ |
|
|
102 |
+ |
protected AbstractAsyncCallback( String pTypeString ) { |
|
103 |
+ |
mTypeString = pTypeString; |
|
104 |
+ |
} |
|
105 |
+ |
|
|
106 |
+ |
public void onSuccess( Object pObject ) { |
|
107 |
+ |
if ( !AbstractClientMainAppEntryPoint.sWindowClosing ) { |
|
108 |
+ |
ChannelServiceResult result = (ChannelServiceResult) pObject; |
|
109 |
+ |
trace( "<", this, result.getMessageSequenceNumber() ); |
|
110 |
+ |
if ( result.isForceClose() || result.isServerLost() ) { |
|
111 |
+ |
sPollTimer.cancel(); |
|
112 |
+ |
sPollTimer = NoPollTimer.INSTANCE; |
|
113 |
+ |
if ( result.isForceClose() ) { |
|
114 |
+ |
UtilsGwt.closeWindowNoConfirm( 5 ); |
|
115 |
+ |
} else { |
|
116 |
+ |
sClientEventBus.problemFromRemotePeerService( |
|
117 |
+ |
new NoServerEventBusEventPackage( "ChannelClient" ) ); |
|
118 |
+ |
} |
|
119 |
+ |
return; |
|
120 |
+ |
} |
|
121 |
+ |
DeferredCommand.addCommand( new DeferredServerMessageHandler( this, result ) ); |
|
122 |
+ |
} |
|
123 |
+ |
} |
|
124 |
+ |
|
|
125 |
+ |
private static class DeferredServerMessageHandler implements Command { |
|
126 |
+ |
private AsyncCallback mAsyncCallback; |
|
127 |
+ |
private ChannelServiceResult mResult; |
|
128 |
+ |
|
|
129 |
+ |
public DeferredServerMessageHandler( AsyncCallback pAsyncCallback, ChannelServiceResult pResult ) { |
|
130 |
+ |
mAsyncCallback = pAsyncCallback; |
|
131 |
+ |
mResult = pResult; |
|
132 |
+ |
} |
|
133 |
+ |
|
|
134 |
+ |
public void execute() { |
|
135 |
+ |
trace( "#", mAsyncCallback, mResult.getMessageSequenceNumber() ); |
|
136 |
+ |
ChannelServicePackage zPackage = mResult.getPackage(); |
|
137 |
+ |
if ( !sClientEventBus.propagateFromRemotePeerService( zPackage ) ) { |
|
138 |
+ |
sClientEventBus = PeerSupportingEventBus.Null.INSTANCE; |
|
139 |
+ |
} |
|
140 |
+ |
trace( "$", mAsyncCallback, mResult.getMessageSequenceNumber() ); |
|
141 |
+ |
} |
|
142 |
+ |
} |
|
143 |
+ |
|
|
144 |
+ |
public void onFailure( Throwable pThrowable ) { |
|
145 |
+ |
if ( !AbstractClientMainAppEntryPoint.sWindowClosing ) { |
|
146 |
+ |
sClientEventBus.handleUnexpectedProblem( COMMUNICATION_FAILURE, new Problem( pThrowable, |
|
147 |
+ |
"Failed to contact the server", |
|
148 |
+ |
toString() ) ); |
|
149 |
+ |
} |
|
150 |
+ |
} |
|
151 |
+ |
|
|
152 |
+ |
public String toString() { |
|
153 |
+ |
return mTypeString; |
|
154 |
+ |
} |
|
155 |
+ |
} |
|
156 |
+ |
|
|
157 |
+ |
private static class PollAsyncCallback extends AbstractAsyncCallback { |
|
158 |
+ |
public static final AsyncCallback INSTANCE = new PollAsyncCallback(); |
|
159 |
+ |
|
|
160 |
+ |
private PollAsyncCallback() { |
|
161 |
+ |
super( "POLL" ); |
|
162 |
+ |
} |
|
163 |
+ |
} |
|
164 |
+ |
|
|
165 |
+ |
private static class ForwardToAsyncCallback extends AbstractAsyncCallback { |
|
166 |
+ |
public static final AsyncCallback INSTANCE = new ForwardToAsyncCallback(); |
|
167 |
+ |
|
|
168 |
+ |
private ForwardToAsyncCallback() { |
|
169 |
+ |
super( "FRWD" ); |
|
170 |
+ |
} |
|
171 |
+ |
} |
|
172 |
+ |
|
|
173 |
+ |
/** |
|
174 |
+ |
* @noinspection UnusedDeclaration |
|
175 |
+ |
*/ |
|
176 |
+ |
private static void trace( String pDirection, AsyncCallback pAsyncCallback, int pMessageSequenceNumber ) { |
|
177 |
+ |
AbstractLogger zLogger = sClientEventBus.getLogger().trace(); |
|
178 |
+ |
if ( zLogger.isEnabled() ) { |
|
179 |
+ |
zLogger.log( "CC-" + pAsyncCallback + " " + +sClientIdNumber + "|" + pMessageSequenceNumber + |
|
180 |
+ |
" " + pDirection + " " + SimpleTimestamp.now() ); |
|
181 |
+ |
} |
|
182 |
+ |
} |
|
183 |
+ |
} |