Subversion Repository Public Repository

litesoft

Diff Revisions 948 vs 950 for /trunk/Java/GWT/Client/src/org/litesoft/GWT/client/nonpublic/CookieNet.java

Diff revisions: vs.
  @@ -1,281 +1,281 @@
1 - // This Source Code is in the Public Domain per: http://unlicense.org
2 - package org.litesoft.GWT.client.nonpublic;
3 -
4 - import org.litesoft.GWT.client.*;
5 - import org.litesoft.commonfoundation.typeutils.*;
6 - import org.litesoft.core.delayed.*;
7 - import org.litesoft.logger.*;
8 -
9 - import com.google.gwt.event.logical.shared.*;
10 - import com.google.gwt.user.client.*;
11 -
12 - import java.util.*;
13 -
14 - public class CookieNet {
15 - public static final String NO_GRAND_POOBAH_PREFIX = "noGP_";
16 -
17 - public interface Listener {
18 - public void changes( CookieNetDelta pDelta );
19 - }
20 -
21 - public static final Logger LOGGER = LoggerFactory.getLogger( CookieNet.class );
22 -
23 - public static final Long NORMAL_POLL_TIME = 1000L;
24 -
25 - private static final String PREFIX_MEMBER = "CN:";
26 - private static final String GRAND_POOBAH = "CN-GP";
27 -
28 - public static final String MSG_NEW = "+";
29 - public static final String MSG_ATTN = "!";
30 - public static final String MSG_DEAD = "-";
31 -
32 - private static boolean mAllowUsToBeGrandPoobah = false;
33 -
34 - public static void initialize( boolean pAllowUsToBeGrandPoobah ) {
35 - mAllowUsToBeGrandPoobah = pAllowUsToBeGrandPoobah;
36 - String zName = WindowName.get();
37 - if ( zName == null ) {
38 - WindowName.add( new MyListener() );
39 - } else {
40 - Window.addCloseHandler( new MyListener() );
41 - windowOpenedWithName();
42 - }
43 - }
44 -
45 - public static void add( Listener pListener ) {
46 - if ( (pListener != null) && (-1 == indexOf( pListener )) ) {
47 - sListeners = append( sListeners, pListener );
48 - }
49 - }
50 -
51 - public static void remove( Listener pListener ) {
52 - if ( pListener != null ) {
53 - int zAt = indexOf( pListener );
54 - if ( zAt != -1 ) {
55 - sListeners = removeAt( sListeners, zAt );
56 - }
57 - }
58 - }
59 -
60 - public static Map<String, String> dumpCookies() {
61 - Collection<String> zNames = Cookies.getCookieNames();
62 - Map<String, String> zCookies = new HashMap<String, String>();
63 - for ( String zName : zNames ) {
64 - zCookies.put( zName, Cookies.getCookie( zName ) );
65 - }
66 - return zCookies;
67 - }
68 -
69 - private static int sLastNumber = 0;
70 -
71 - public static int getNextNumber() {
72 - int zNextNumber = --sLastNumber;
73 - return zNextNumber < -99999 ? (sLastNumber = -1) : zNextNumber;
74 - }
75 -
76 - private static void windowClosedWithName() {
77 - sClosing = true;
78 - setOurCookie( MSG_DEAD );
79 - }
80 -
81 - private static void windowOpenedWithName() {
82 - setOurCookie( MSG_NEW );
83 - Long zPollAgainIn = checkForCookieChanges();
84 -
85 - if ( zPollAgainIn != null ) {
86 - TimedRunnableManager.INSTANCE.runIn( new TimedRunnable() {
87 - @Override
88 - public Again runOnce() {
89 - Long zPollAgainIn = checkForCookieChanges();
90 - return (zPollAgainIn != null) ? new RunAgainIn( zPollAgainIn ) : null;
91 - }
92 - }, zPollAgainIn );
93 - }
94 - }
95 -
96 - private static void setOurCookie( String pMsg ) {
97 - String zName = WindowName.get();
98 - setCookie( PREFIX_MEMBER + zName, sMyLastValue = pMsg );
99 - }
100 -
101 - private static void setCookie( String pCookieName, String pValue ) {
102 - Cookies.setCookie( pCookieName, pValue );
103 - }
104 -
105 - private static void removeCookie( String pCookieName ) {
106 - Cookies.removeCookie( pCookieName );
107 - }
108 -
109 - private static boolean sClosing = false;
110 -
111 - private static boolean sIamGrandPoobah = false;
112 -
113 - private static String sMyLastValue = "?";
114 - private static Set<String> sMembers = new HashSet<String>();
115 -
116 - private static CookieNetDelta createDelta( CurrentMemberState pNewState, String pWindowName ) {
117 - CookieNetDelta zDelta = new CookieNetDelta();
118 - sIamGrandPoobah = zDelta.setNowGrandPoobah( sIamGrandPoobah, pNewState.isGrandPoobah( pWindowName ) );
119 - sMyLastValue = zDelta.setWasPoked( sMyLastValue, pNewState.getMemberValue( pWindowName ) );
120 - sMembers = zDelta.adjustMembers( sMembers, pNewState.getMembers() );
121 - return zDelta;
122 - }
123 -
124 - private static String sLastRawDocCookies = null;
125 -
126 - /**
127 - * @return RunAgainIn
128 - */
129 - private static Long checkForCookieChanges() {
130 - CurrentMemberState zNewState = null;
131 - for ( CookieSnapshot zSnapshot = new CookieSnapshot( GRAND_POOBAH ); //
132 - !sClosing && !zSnapshot.getRawDocCookies().equals( sLastRawDocCookies ); //
133 - zSnapshot = new CookieSnapshot( GRAND_POOBAH ) ) {
134 - sLastRawDocCookies = zSnapshot.getRawDocCookies();
135 - zNewState = createNewState( zSnapshot );
136 - }
137 - if ( sClosing ) {
138 - return null;
139 - }
140 - if ( zNewState != null ) {
141 - String zName = WindowName.get();
142 - if ( mAllowUsToBeGrandPoobah && !zNewState.hasGrandPoobah() ) {
143 - String zCandidate = zNewState.getGrandPoobahCandidate();
144 - if ( zName.equals( zCandidate ) ) {
145 - setCookie( GRAND_POOBAH, zName );
146 - return checkForCookieChanges();
147 - }
148 - if ( zCandidate == null ) {
149 - String zError = "No Members? from: " + zName;
150 - LOGGER.error.log( zError );
151 - }
152 - return 1000L; // Everyone else BACK-OFF!
153 - }
154 - CookieNetDelta zDelta = createDelta( zNewState, zName );
155 - Listener[] zListeners = sListeners; // Snag the List
156 - for ( Listener zListener : zListeners ) {
157 - zListener.changes( zDelta );
158 - }
159 - }
160 - return NORMAL_POLL_TIME;
161 - }
162 -
163 - private static CurrentMemberState createNewState( CookieSnapshot pSnapshot ) {
164 - String zGrandPoobah = pSnapshot.getPrimaryCookieValue();
165 - CurrentMemberState rv = new CurrentMemberState();
166 - for ( String zName : Cookies.getCookieNames() ) {
167 - if ( zName.startsWith( PREFIX_MEMBER ) ) {
168 - String zWindowName = zName.substring( PREFIX_MEMBER.length() );
169 - String zValue = Strings.deNull( Cookies.getCookie( zName ) );
170 - if ( (zValue.length() == 0) || MSG_DEAD.equals( zValue ) ) {
171 - removeCookie( zName );
172 - if ( zWindowName.equals( zGrandPoobah ) ) {
173 - removeCookie( GRAND_POOBAH );
174 - zGrandPoobah = null;
175 - }
176 - } else {
177 - rv.addMember( zWindowName, zValue );
178 - }
179 - }
180 - }
181 - rv.setGrandPoobah( zGrandPoobah );
182 - return rv;
183 - }
184 -
185 - private static Listener[] sListeners = new Listener[0];
186 -
187 - private static int indexOf( Listener pListener ) {
188 - for ( int i = 0; i < sListeners.length; i++ ) {
189 - Listener zListener = sListeners[i];
190 - if ( zListener == pListener ) // Identity
191 - {
192 - return i;
193 - }
194 - }
195 - return -1;
196 - }
197 -
198 - private static Listener[] append( Listener[] pArray, Listener pListener ) {
199 - int zLength = pArray.length;
200 - if ( zLength == 0 ) {
201 - return new Listener[]{pListener};
202 - }
203 - Listener[] zNew = new Listener[zLength + 1];
204 - System.arraycopy( pArray, 0, zNew, 1, zLength );
205 - zNew[0] = pListener;
206 - return zNew;
207 - }
208 -
209 - private static Listener[] removeAt( Listener[] pArray, int pIndexToRemove ) {
210 - int zNewLength = pArray.length - 1;
211 - Listener[] zNew = new Listener[zNewLength];
212 - if ( pIndexToRemove != 0 ) {
213 - System.arraycopy( pArray, 0, zNew, 0, pIndexToRemove );
214 - }
215 - if ( pIndexToRemove != zNewLength ) {
216 - System.arraycopy( pArray, pIndexToRemove + 1, zNew, pIndexToRemove, zNewLength - pIndexToRemove );
217 - }
218 - return zNew;
219 - }
220 -
221 - private static class MyListener implements CloseHandler<Window>,
222 - WindowName.Listener {
223 - @Override
224 - public void windowNameChanged() {
225 - WindowName.remove( this );
226 - Window.addCloseHandler( this );
227 - windowOpenedWithName();
228 - }
229 -
230 - @Override
231 - public void onClose( CloseEvent<Window> event ) {
232 - windowClosedWithName();
233 - }
234 - }
235 -
236 - private static class CurrentMemberState {
237 - private String mGrandPoobah = null;
238 - private Map<String, String> mMembers = new HashMap<String, String>();
239 -
240 - public boolean hasGrandPoobah() {
241 - return mGrandPoobah != null;
242 - }
243 -
244 - public String getGrandPoobah() {
245 - return mGrandPoobah;
246 - }
247 -
248 - public void setGrandPoobah( String pGrandPoobah ) {
249 - mGrandPoobah = pGrandPoobah;
250 - }
251 -
252 - public void addMember( String pWindowName, String pValue ) {
253 - mMembers.put( pWindowName, pValue );
254 - }
255 -
256 - public boolean isGrandPoobah( String pWindowName ) {
257 - return hasGrandPoobah() && mGrandPoobah.equals( pWindowName );
258 - }
259 -
260 - public String getMemberValue( String pWindowName ) {
261 - return mMembers.get( pWindowName );
262 - }
263 -
264 - public Set<String> getMembers() {
265 - return mMembers.keySet();
266 - }
267 -
268 - public String getGrandPoobahCandidate() {
269 - if ( !hasGrandPoobah() && !mMembers.isEmpty() ) {
270 - ArrayList<String> zMemberWinNames = new ArrayList<String>( mMembers.keySet() );
271 - Collections.sort( zMemberWinNames );
272 - for ( String zWinName : zMemberWinNames ) {
273 - if ( !zWinName.startsWith( NO_GRAND_POOBAH_PREFIX ) ) {
274 - return zWinName;
275 - }
276 - }
277 - }
278 - return null;
279 - }
280 - }
281 - }
1 + // This Source Code is in the Public Domain per: http://unlicense.org
2 + package org.litesoft.GWT.client.nonpublic;
3 +
4 + import org.litesoft.GWT.client.*;
5 + import org.litesoft.commonfoundation.base.*;
6 + import org.litesoft.core.delayed.*;
7 + import org.litesoft.logger.*;
8 +
9 + import com.google.gwt.event.logical.shared.*;
10 + import com.google.gwt.user.client.*;
11 +
12 + import java.util.*;
13 +
14 + public class CookieNet {
15 + public static final String NO_GRAND_POOBAH_PREFIX = "noGP_";
16 +
17 + public interface Listener {
18 + public void changes( CookieNetDelta pDelta );
19 + }
20 +
21 + public static final Logger LOGGER = LoggerFactory.getLogger( CookieNet.class );
22 +
23 + public static final Long NORMAL_POLL_TIME = 1000L;
24 +
25 + private static final String PREFIX_MEMBER = "CN:";
26 + private static final String GRAND_POOBAH = "CN-GP";
27 +
28 + public static final String MSG_NEW = "+";
29 + public static final String MSG_ATTN = "!";
30 + public static final String MSG_DEAD = "-";
31 +
32 + private static boolean mAllowUsToBeGrandPoobah = false;
33 +
34 + public static void initialize( boolean pAllowUsToBeGrandPoobah ) {
35 + mAllowUsToBeGrandPoobah = pAllowUsToBeGrandPoobah;
36 + String zName = WindowName.get();
37 + if ( zName == null ) {
38 + WindowName.add( new MyListener() );
39 + } else {
40 + Window.addCloseHandler( new MyListener() );
41 + windowOpenedWithName();
42 + }
43 + }
44 +
45 + public static void add( Listener pListener ) {
46 + if ( (pListener != null) && (-1 == indexOf( pListener )) ) {
47 + sListeners = append( sListeners, pListener );
48 + }
49 + }
50 +
51 + public static void remove( Listener pListener ) {
52 + if ( pListener != null ) {
53 + int zAt = indexOf( pListener );
54 + if ( zAt != -1 ) {
55 + sListeners = removeAt( sListeners, zAt );
56 + }
57 + }
58 + }
59 +
60 + public static Map<String, String> dumpCookies() {
61 + Collection<String> zNames = Cookies.getCookieNames();
62 + Map<String, String> zCookies = new HashMap<String, String>();
63 + for ( String zName : zNames ) {
64 + zCookies.put( zName, Cookies.getCookie( zName ) );
65 + }
66 + return zCookies;
67 + }
68 +
69 + private static int sLastNumber = 0;
70 +
71 + public static int getNextNumber() {
72 + int zNextNumber = --sLastNumber;
73 + return zNextNumber < -99999 ? (sLastNumber = -1) : zNextNumber;
74 + }
75 +
76 + private static void windowClosedWithName() {
77 + sClosing = true;
78 + setOurCookie( MSG_DEAD );
79 + }
80 +
81 + private static void windowOpenedWithName() {
82 + setOurCookie( MSG_NEW );
83 + Long zPollAgainIn = checkForCookieChanges();
84 +
85 + if ( zPollAgainIn != null ) {
86 + TimedRunnableManager.INSTANCE.runIn( new TimedRunnable() {
87 + @Override
88 + public Again runOnce() {
89 + Long zPollAgainIn = checkForCookieChanges();
90 + return (zPollAgainIn != null) ? new RunAgainIn( zPollAgainIn ) : null;
91 + }
92 + }, zPollAgainIn );
93 + }
94 + }
95 +
96 + private static void setOurCookie( String pMsg ) {
97 + String zName = WindowName.get();
98 + setCookie( PREFIX_MEMBER + zName, sMyLastValue = pMsg );
99 + }
100 +
101 + private static void setCookie( String pCookieName, String pValue ) {
102 + Cookies.setCookie( pCookieName, pValue );
103 + }
104 +
105 + private static void removeCookie( String pCookieName ) {
106 + Cookies.removeCookie( pCookieName );
107 + }
108 +
109 + private static boolean sClosing = false;
110 +
111 + private static boolean sIamGrandPoobah = false;
112 +
113 + private static String sMyLastValue = "?";
114 + private static Set<String> sMembers = new HashSet<String>();
115 +
116 + private static CookieNetDelta createDelta( CurrentMemberState pNewState, String pWindowName ) {
117 + CookieNetDelta zDelta = new CookieNetDelta();
118 + sIamGrandPoobah = zDelta.setNowGrandPoobah( sIamGrandPoobah, pNewState.isGrandPoobah( pWindowName ) );
119 + sMyLastValue = zDelta.setWasPoked( sMyLastValue, pNewState.getMemberValue( pWindowName ) );
120 + sMembers = zDelta.adjustMembers( sMembers, pNewState.getMembers() );
121 + return zDelta;
122 + }
123 +
124 + private static String sLastRawDocCookies = null;
125 +
126 + /**
127 + * @return RunAgainIn
128 + */
129 + private static Long checkForCookieChanges() {
130 + CurrentMemberState zNewState = null;
131 + for ( CookieSnapshot zSnapshot = new CookieSnapshot( GRAND_POOBAH ); //
132 + !sClosing && !zSnapshot.getRawDocCookies().equals( sLastRawDocCookies ); //
133 + zSnapshot = new CookieSnapshot( GRAND_POOBAH ) ) {
134 + sLastRawDocCookies = zSnapshot.getRawDocCookies();
135 + zNewState = createNewState( zSnapshot );
136 + }
137 + if ( sClosing ) {
138 + return null;
139 + }
140 + if ( zNewState != null ) {
141 + String zName = WindowName.get();
142 + if ( mAllowUsToBeGrandPoobah && !zNewState.hasGrandPoobah() ) {
143 + String zCandidate = zNewState.getGrandPoobahCandidate();
144 + if ( zName.equals( zCandidate ) ) {
145 + setCookie( GRAND_POOBAH, zName );
146 + return checkForCookieChanges();
147 + }
148 + if ( zCandidate == null ) {
149 + String zError = "No Members? from: " + zName;
150 + LOGGER.error.log( zError );
151 + }
152 + return 1000L; // Everyone else BACK-OFF!
153 + }
154 + CookieNetDelta zDelta = createDelta( zNewState, zName );
155 + Listener[] zListeners = sListeners; // Snag the List
156 + for ( Listener zListener : zListeners ) {
157 + zListener.changes( zDelta );
158 + }
159 + }
160 + return NORMAL_POLL_TIME;
161 + }
162 +
163 + private static CurrentMemberState createNewState( CookieSnapshot pSnapshot ) {
164 + String zGrandPoobah = pSnapshot.getPrimaryCookieValue();
165 + CurrentMemberState rv = new CurrentMemberState();
166 + for ( String zName : Cookies.getCookieNames() ) {
167 + if ( zName.startsWith( PREFIX_MEMBER ) ) {
168 + String zWindowName = zName.substring( PREFIX_MEMBER.length() );
169 + String zValue = ConstrainTo.notNull( Cookies.getCookie( zName ) );
170 + if ( (zValue.length() == 0) || MSG_DEAD.equals( zValue ) ) {
171 + removeCookie( zName );
172 + if ( zWindowName.equals( zGrandPoobah ) ) {
173 + removeCookie( GRAND_POOBAH );
174 + zGrandPoobah = null;
175 + }
176 + } else {
177 + rv.addMember( zWindowName, zValue );
178 + }
179 + }
180 + }
181 + rv.setGrandPoobah( zGrandPoobah );
182 + return rv;
183 + }
184 +
185 + private static Listener[] sListeners = new Listener[0];
186 +
187 + private static int indexOf( Listener pListener ) {
188 + for ( int i = 0; i < sListeners.length; i++ ) {
189 + Listener zListener = sListeners[i];
190 + if ( zListener == pListener ) // Identity
191 + {
192 + return i;
193 + }
194 + }
195 + return -1;
196 + }
197 +
198 + private static Listener[] append( Listener[] pArray, Listener pListener ) {
199 + int zLength = pArray.length;
200 + if ( zLength == 0 ) {
201 + return new Listener[]{pListener};
202 + }
203 + Listener[] zNew = new Listener[zLength + 1];
204 + System.arraycopy( pArray, 0, zNew, 1, zLength );
205 + zNew[0] = pListener;
206 + return zNew;
207 + }
208 +
209 + private static Listener[] removeAt( Listener[] pArray, int pIndexToRemove ) {
210 + int zNewLength = pArray.length - 1;
211 + Listener[] zNew = new Listener[zNewLength];
212 + if ( pIndexToRemove != 0 ) {
213 + System.arraycopy( pArray, 0, zNew, 0, pIndexToRemove );
214 + }
215 + if ( pIndexToRemove != zNewLength ) {
216 + System.arraycopy( pArray, pIndexToRemove + 1, zNew, pIndexToRemove, zNewLength - pIndexToRemove );
217 + }
218 + return zNew;
219 + }
220 +
221 + private static class MyListener implements CloseHandler<Window>,
222 + WindowName.Listener {
223 + @Override
224 + public void windowNameChanged() {
225 + WindowName.remove( this );
226 + Window.addCloseHandler( this );
227 + windowOpenedWithName();
228 + }
229 +
230 + @Override
231 + public void onClose( CloseEvent<Window> event ) {
232 + windowClosedWithName();
233 + }
234 + }
235 +
236 + private static class CurrentMemberState {
237 + private String mGrandPoobah = null;
238 + private Map<String, String> mMembers = new HashMap<String, String>();
239 +
240 + public boolean hasGrandPoobah() {
241 + return mGrandPoobah != null;
242 + }
243 +
244 + public String getGrandPoobah() {
245 + return mGrandPoobah;
246 + }
247 +
248 + public void setGrandPoobah( String pGrandPoobah ) {
249 + mGrandPoobah = pGrandPoobah;
250 + }
251 +
252 + public void addMember( String pWindowName, String pValue ) {
253 + mMembers.put( pWindowName, pValue );
254 + }
255 +
256 + public boolean isGrandPoobah( String pWindowName ) {
257 + return hasGrandPoobah() && mGrandPoobah.equals( pWindowName );
258 + }
259 +
260 + public String getMemberValue( String pWindowName ) {
261 + return mMembers.get( pWindowName );
262 + }
263 +
264 + public Set<String> getMembers() {
265 + return mMembers.keySet();
266 + }
267 +
268 + public String getGrandPoobahCandidate() {
269 + if ( !hasGrandPoobah() && !mMembers.isEmpty() ) {
270 + ArrayList<String> zMemberWinNames = new ArrayList<String>( mMembers.keySet() );
271 + Collections.sort( zMemberWinNames );
272 + for ( String zWinName : zMemberWinNames ) {
273 + if ( !zWinName.startsWith( NO_GRAND_POOBAH_PREFIX ) ) {
274 + return zWinName;
275 + }
276 + }
277 + }
278 + return null;
279 + }
280 + }
281 + }