litesoft
@ HEAD
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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
// This Source Code is in the Public Domain per: http://unlicense.org package org.litesoft.GWT.client.nonpublic; import org.litesoft.GWT.client.*; import org.litesoft.commonfoundation.base.*; import org.litesoft.core.delayed.*; import org.litesoft.logger.*; import com.google.gwt.event.logical.shared.*; import com.google.gwt.user.client.*; import java.util.*; public class CookieNet { public static final String NO_GRAND_POOBAH_PREFIX = "noGP_"; public interface Listener { public void changes( CookieNetDelta pDelta ); } public static final Logger LOGGER = LoggerFactory.getLogger( CookieNet.class ); public static final Long NORMAL_POLL_TIME = 1000L; private static final String PREFIX_MEMBER = "CN:"; private static final String GRAND_POOBAH = "CN-GP"; public static final String MSG_NEW = "+"; public static final String MSG_ATTN = "!"; public static final String MSG_DEAD = "-"; private static boolean mAllowUsToBeGrandPoobah = false; public static void initialize( boolean pAllowUsToBeGrandPoobah ) { mAllowUsToBeGrandPoobah = pAllowUsToBeGrandPoobah; String zName = WindowName.get(); if ( zName == null ) { WindowName.add( new MyListener() ); } else { Window.addCloseHandler( new MyListener() ); windowOpenedWithName(); } } public static void add( Listener pListener ) { if ( (pListener != null) && (-1 == indexOf( pListener )) ) { sListeners = append( sListeners, pListener ); } } public static void remove( Listener pListener ) { if ( pListener != null ) { int zAt = indexOf( pListener ); if ( zAt != -1 ) { sListeners = removeAt( sListeners, zAt ); } } } public static Map<String, String> dumpCookies() { Collection<String> zNames = Cookies.getCookieNames(); Map<String, String> zCookies = new HashMap<String, String>(); for ( String zName : zNames ) { zCookies.put( zName, Cookies.getCookie( zName ) ); } return zCookies; } private static int sLastNumber = 0; public static int getNextNumber() { int zNextNumber = --sLastNumber; return zNextNumber < -99999 ? (sLastNumber = -1) : zNextNumber; } private static void windowClosedWithName() { sClosing = true; setOurCookie( MSG_DEAD ); } private static void windowOpenedWithName() { setOurCookie( MSG_NEW ); Long zPollAgainIn = checkForCookieChanges(); if ( zPollAgainIn != null ) { TimedRunnableManager.INSTANCE.runIn( new TimedRunnable() { @Override public Again runOnce() { Long zPollAgainIn = checkForCookieChanges(); return (zPollAgainIn != null) ? new RunAgainIn( zPollAgainIn ) : null; } }, zPollAgainIn ); } } private static void setOurCookie( String pMsg ) { String zName = WindowName.get(); setCookie( PREFIX_MEMBER + zName, sMyLastValue = pMsg ); } private static void setCookie( String pCookieName, String pValue ) { Cookies.setCookie( pCookieName, pValue ); } private static void removeCookie( String pCookieName ) { Cookies.removeCookie( pCookieName ); } private static boolean sClosing = false; private static boolean sIamGrandPoobah = false; private static String sMyLastValue = "?"; private static Set<String> sMembers = new HashSet<String>(); private static CookieNetDelta createDelta( CurrentMemberState pNewState, String pWindowName ) { CookieNetDelta zDelta = new CookieNetDelta(); sIamGrandPoobah = zDelta.setNowGrandPoobah( sIamGrandPoobah, pNewState.isGrandPoobah( pWindowName ) ); sMyLastValue = zDelta.setWasPoked( sMyLastValue, pNewState.getMemberValue( pWindowName ) ); sMembers = zDelta.adjustMembers( sMembers, pNewState.getMembers() ); return zDelta; } private static String sLastRawDocCookies = null; /** * @return RunAgainIn */ private static Long checkForCookieChanges() { CurrentMemberState zNewState = null; for ( CookieSnapshot zSnapshot = new CookieSnapshot( GRAND_POOBAH ); // !sClosing && !zSnapshot.getRawDocCookies().equals( sLastRawDocCookies ); // zSnapshot = new CookieSnapshot( GRAND_POOBAH ) ) { sLastRawDocCookies = zSnapshot.getRawDocCookies(); zNewState = createNewState( zSnapshot ); } if ( sClosing ) { return null; } if ( zNewState != null ) { String zName = WindowName.get(); if ( mAllowUsToBeGrandPoobah && !zNewState.hasGrandPoobah() ) { String zCandidate = zNewState.getGrandPoobahCandidate(); if ( zName.equals( zCandidate ) ) { setCookie( GRAND_POOBAH, zName ); return checkForCookieChanges(); } if ( zCandidate == null ) { String zError = "No Members? from: " + zName; LOGGER.error.log( zError ); } return 1000L; // Everyone else BACK-OFF! } CookieNetDelta zDelta = createDelta( zNewState, zName ); Listener[] zListeners = sListeners; // Snag the List for ( Listener zListener : zListeners ) { zListener.changes( zDelta ); } } return NORMAL_POLL_TIME; } private static CurrentMemberState createNewState( CookieSnapshot pSnapshot ) { String zGrandPoobah = pSnapshot.getPrimaryCookieValue(); CurrentMemberState rv = new CurrentMemberState(); for ( String zName : Cookies.getCookieNames() ) { if ( zName.startsWith( PREFIX_MEMBER ) ) { String zWindowName = zName.substring( PREFIX_MEMBER.length() ); String zValue = ConstrainTo.notNull( Cookies.getCookie( zName ) ); if ( (zValue.length() == 0) || MSG_DEAD.equals( zValue ) ) { removeCookie( zName ); if ( zWindowName.equals( zGrandPoobah ) ) { removeCookie( GRAND_POOBAH ); zGrandPoobah = null; } } else { rv.addMember( zWindowName, zValue ); } } } rv.setGrandPoobah( zGrandPoobah ); return rv; } private static Listener[] sListeners = new Listener[0]; private static int indexOf( Listener pListener ) { for ( int i = 0; i < sListeners.length; i++ ) { Listener zListener = sListeners[i]; if ( zListener == pListener ) // Identity { return i; } } return -1; } private static Listener[] append( Listener[] pArray, Listener pListener ) { int zLength = pArray.length; if ( zLength == 0 ) { return new Listener[]{pListener}; } Listener[] zNew = new Listener[zLength + 1]; System.arraycopy( pArray, 0, zNew, 1, zLength ); zNew[0] = pListener; return zNew; } private static Listener[] removeAt( Listener[] pArray, int pIndexToRemove ) { int zNewLength = pArray.length - 1; Listener[] zNew = new Listener[zNewLength]; if ( pIndexToRemove != 0 ) { System.arraycopy( pArray, 0, zNew, 0, pIndexToRemove ); } if ( pIndexToRemove != zNewLength ) { System.arraycopy( pArray, pIndexToRemove + 1, zNew, pIndexToRemove, zNewLength - pIndexToRemove ); } return zNew; } private static class MyListener implements CloseHandler<Window>, WindowName.Listener { @Override public void windowNameChanged() { WindowName.remove( this ); Window.addCloseHandler( this ); windowOpenedWithName(); } @Override public void onClose( CloseEvent<Window> event ) { windowClosedWithName(); } } private static class CurrentMemberState { private String mGrandPoobah = null; private Map<String, String> mMembers = new HashMap<String, String>(); public boolean hasGrandPoobah() { return mGrandPoobah != null; } public String getGrandPoobah() { return mGrandPoobah; } public void setGrandPoobah( String pGrandPoobah ) { mGrandPoobah = pGrandPoobah; } public void addMember( String pWindowName, String pValue ) { mMembers.put( pWindowName, pValue ); } public boolean isGrandPoobah( String pWindowName ) { return hasGrandPoobah() && mGrandPoobah.equals( pWindowName ); } public String getMemberValue( String pWindowName ) { return mMembers.get( pWindowName ); } public Set<String> getMembers() { return mMembers.keySet(); } public String getGrandPoobahCandidate() { if ( !hasGrandPoobah() && !mMembers.isEmpty() ) { ArrayList<String> zMemberWinNames = new ArrayList<String>( mMembers.keySet() ); Collections.sort( zMemberWinNames ); for ( String zWinName : zMemberWinNames ) { if ( !zWinName.startsWith( NO_GRAND_POOBAH_PREFIX ) ) { return zWinName; } } } return null; } } } |
Commits for litesoft/trunk/Java/GWT/Client/src/org/litesoft/GWT/client/nonpublic/CookieNet.java
Revision | Author | Commited | Message |
---|---|---|---|
950 Diff | GeorgeS | Thu 19 Jun, 2014 17:57:04 +0000 | New Lines |
948 Diff | GeorgeS | Sat 07 Jun, 2014 23:42:39 +0000 | Jusefuls Formatter Updated to New Code Format |
947 Diff | GeorgeS | Fri 06 Jun, 2014 23:36:56 +0000 | Correct Spelling of package! |
939 Diff | GeorgeS | Mon 02 Jun, 2014 21:30:31 +0000 | Extracting commonfoundation |
802 Diff | GeorgeS | Wed 15 Aug, 2012 04:04:47 +0000 | |
49 Diff | GeorgeS | Mon 12 Apr, 2010 02:59:10 +0000 | License Text |
23 Diff | GeorgeS | Wed 24 Feb, 2010 00:34:32 +0000 | |
2 | GeorgeS | Sun 07 Feb, 2010 12:50:58 +0000 |