litesoft
@ 943
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt package org.litesoft.GWT.eventbus.client.rpc; import org.litesoft.GWT.client.*; import org.litesoft.GWT.eventbus.client.nonpublic.*; import org.litesoft.commonfoundation.issues.*; import org.litesoft.core.simpletypes.temporal.*; import org.litesoft.logger.nonpublic.*; import com.google.gwt.user.client.*; import com.google.gwt.user.client.Timer; import com.google.gwt.user.client.rpc.*; public class ClientSideRemotePeerService implements RemotePeerService { private static final String COMMUNICATION_FAILURE = "TheServerCommunication"; private static PeerSupportingEventBus sClientEventBus; private static int sClientIdNumber; private static ChannelServiceAsync sChannelServiceAsync; private static int sPollMillisecs; private static PollTimer sPollTimer; public ClientSideRemotePeerService( PeerSupportingEventBus pClientEventBus, // ChannelServiceAsync pChannelServiceAsync, // int pPollSecs ) { sClientEventBus = pClientEventBus; sClientIdNumber = pClientEventBus.getClientIdNumber(); sChannelServiceAsync = pChannelServiceAsync; sPollMillisecs = (pPollSecs < 1) ? 0 : pPollSecs * 1000; sPollTimer = (sPollMillisecs <= 0) ? NoPollTimer.INSTANCE : new RealPollTimer(); } public String toString() { return "ClientSideRemotePeerService"; } public boolean forwardTo( ChannelServicePackage pFromPeer ) { return (pFromPeer == null) || pFromPeer.isEmpty() || // sendEm( ForwardToAsyncCallback.INSTANCE, pFromPeer ); } public void stopUnrequestedMessages() { sPollTimer.cancel(); } public RemotePeerService dispose() { return RemotePeerService.Null.INSTANCE; } private static int sMessageSequenceNumber = 0; private static boolean sendEm( AsyncCallback pAsyncCallback, ChannelServicePackage pChannelServicePackage ) { boolean sent = false; sPollTimer.cancel(); sMessageSequenceNumber++; try { trace( ">", pAsyncCallback, sMessageSequenceNumber ); sChannelServiceAsync.propagate( sClientIdNumber, sMessageSequenceNumber, // sPollMillisecs, pChannelServicePackage, // pAsyncCallback ); sent = true; } catch ( RuntimeException e ) { sClientEventBus.handleUnexpectedProblem( COMMUNICATION_FAILURE, // new Problem( e, "SendingToServer" ) ); } sPollTimer.schedule(); return sent; } private interface PollTimer { public void cancel(); public void schedule(); } private static class RealPollTimer extends Timer implements PollTimer { public void run() { if ( !AbstractClientMainAppEntryPoint.sWindowClosing ) { sendEm( PollAsyncCallback.INSTANCE, null ); } } public void schedule() { schedule( sPollMillisecs ); } } private static class NoPollTimer implements PollTimer { public static final PollTimer INSTANCE = new NoPollTimer(); public void cancel() { } public void schedule() { } } private static class AbstractAsyncCallback implements AsyncCallback { private String mTypeString; protected AbstractAsyncCallback( String pTypeString ) { mTypeString = pTypeString; } public void onSuccess( Object pObject ) { if ( !AbstractClientMainAppEntryPoint.sWindowClosing ) { ChannelServiceResult result = (ChannelServiceResult) pObject; trace( "<", this, result.getMessageSequenceNumber() ); if ( result.isForceClose() || result.isServerLost() ) { sPollTimer.cancel(); sPollTimer = NoPollTimer.INSTANCE; if ( result.isForceClose() ) { UtilsGwt.closeWindowNoConfirm( 5 ); } else { sClientEventBus.problemFromRemotePeerService( new NoServerEventBusEventPackage( "ChannelClient" ) ); } return; } DeferredCommand.addCommand( new DeferredServerMessageHandler( this, result ) ); } } private static class DeferredServerMessageHandler implements Command { private AsyncCallback mAsyncCallback; private ChannelServiceResult mResult; public DeferredServerMessageHandler( AsyncCallback pAsyncCallback, ChannelServiceResult pResult ) { mAsyncCallback = pAsyncCallback; mResult = pResult; } public void execute() { trace( "#", mAsyncCallback, mResult.getMessageSequenceNumber() ); ChannelServicePackage zPackage = mResult.getPackage(); if ( !sClientEventBus.propagateFromRemotePeerService( zPackage ) ) { sClientEventBus = PeerSupportingEventBus.Null.INSTANCE; } trace( "$", mAsyncCallback, mResult.getMessageSequenceNumber() ); } } public void onFailure( Throwable pThrowable ) { if ( !AbstractClientMainAppEntryPoint.sWindowClosing ) { sClientEventBus.handleUnexpectedProblem( COMMUNICATION_FAILURE, new Problem( pThrowable, "Failed to contact the server", toString() ) ); } } public String toString() { return mTypeString; } } private static class PollAsyncCallback extends AbstractAsyncCallback { public static final AsyncCallback INSTANCE = new PollAsyncCallback(); private PollAsyncCallback() { super( "POLL" ); } } private static class ForwardToAsyncCallback extends AbstractAsyncCallback { public static final AsyncCallback INSTANCE = new ForwardToAsyncCallback(); private ForwardToAsyncCallback() { super( "FRWD" ); } } /** * @noinspection UnusedDeclaration */ private static void trace( String pDirection, AsyncCallback pAsyncCallback, int pMessageSequenceNumber ) { AbstractLogger zLogger = sClientEventBus.getLogger().trace(); if ( zLogger.isEnabled() ) { zLogger.log( "CC-" + pAsyncCallback + " " + +sClientIdNumber + "|" + pMessageSequenceNumber + " " + pDirection + " " + SimpleTimestamp.now() ); } } } |
Commits for litesoft/trunk/Java/GWT/OldClient/src/org/litesoft/GWT/eventbus/client/rpc/ClientSideRemotePeerService.java
Revision | Author | Commited | Message |
---|---|---|---|
943 Diff | GeorgeS | Tue 03 Jun, 2014 04:25:50 +0000 | Extracting commonfoundation |
864 Diff | GeorgeS | Mon 19 Nov, 2012 01:48:25 +0000 | |
858 Diff | GeorgeS | Sun 04 Nov, 2012 18:40:40 +0000 | |
851 Diff | GeorgeS | Mon 08 Oct, 2012 00:05:32 +0000 | Breaking the code as Temporal changes are implemented... |
819 Diff | GeorgeS | Sat 18 Aug, 2012 18:09:40 +0000 | |
60 Diff | GeorgeS | Tue 24 Aug, 2010 00:37:36 +0000 | |
49 Diff | GeorgeS | Mon 12 Apr, 2010 02:59:10 +0000 | License Text |
2 | GeorgeS | Sun 07 Feb, 2010 12:50:58 +0000 |