001: /*
002: * Copyright (c) 1999-2001 Lutris Technologies, Inc. All Rights
003: * Reserved.
004: *
005: * This source code file is distributed by Lutris Technologies, Inc. for
006: * use only by licensed users of product(s) that include this source
007: * file. Use of this source file or the software that uses it is covered
008: * by the terms and conditions of the Lutris Enhydra Development License
009: * Agreement included with this product.
010: *
011: * This Software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
012: * ANY KIND, either express or implied. See the License for the specific terms
013: * governing rights and limitations under the License.
014: *
015: * Contributor(s):
016: *
017: * $Id: UpdateImpl.java,v 1.1 2006-09-11 12:27:25 sinisa Exp $
018: */
019:
020: package com.lutris.airsent.business.delivery;
021:
022: import com.lutris.airsent.spec.delivery.Update;
023: import com.lutris.airsent.business.AirSentBusinessException;
024:
025: /**
026: * Update handles the backend of the ServerPush technique
027: * by keeping tabs on the delivery changes.
028: *
029: * @author
030: * @version %I%, %G%
031: */
032: public class UpdateImpl implements Update {
033: private long updateState;
034:
035: /**
036: * Plain Constructor
037: *
038: *
039: *
040: */
041: public UpdateImpl() {
042: }
043:
044: /**
045: * Constructor with initial time state.
046: *
047: *
048: * @param initialState
049: *
050: *
051: */
052: public UpdateImpl(long initialState) {
053: updateState = initialState;
054: }
055:
056: /**
057: * Gets the update time.
058: *
059: *
060: * @param browserState
061: * @param wait
062: *
063: * @return
064: *
065: *
066: */
067: public long needUpdate(long browserState, long wait)
068: throws AirSentBusinessException {
069: waitForChange(browserState, wait);
070:
071: return getUpdateState();
072: }
073:
074: /**
075: * Sets the update state.
076: *
077: *
078: * @param updateState
079: *
080: *
081: */
082: public void setUpdateState(long updateState) {
083: this .updateState = updateState;
084: }
085:
086: /**
087: * Gets the update time.
088: *
089: *
090: * @return
091: *
092: *
093: */
094: public long getUpdateState() {
095: return updateState;
096: }
097:
098: /**
099: * Helper function. Only does the waiting. Waits till browserState
100: * is not the current state, or for wait seconds, whichever comes
101: * first. This function will return immediatly if no waiting is
102: * needed. While waiting the thread is sleeping, not kept running
103: * in a loop (which would waste processor time).
104: *
105: * Written by Andy John
106: */
107: private void waitForChange(long browserState, long wait)
108: throws AirSentBusinessException {
109:
110: if ((browserState != updateState) || (wait <= 0)) {
111:
112: // Already new data to report.
113:
114: return;
115: }
116:
117: long now = System.currentTimeMillis();
118:
119: long giveUpTime = now + (wait * 1000);
120:
121: // We loop here because the wait might return prematurely.
122: while (browserState == updateState) {
123: long leftToGo = giveUpTime - now;
124:
125: if (leftToGo <= 0) {
126:
127: // Either the wait time was 0 or we ran out of time.
128:
129: break;
130: }
131:
132: try {
133: Thread.sleep(leftToGo);
134: } catch (InterruptedException e) {
135: throw new AirSentBusinessException(
136: "Exception in waitForChange: " + e);
137: }
138:
139: now = System.currentTimeMillis();
140: }
141: }
142:
143: }
|