01: /*
02: ItsNat Java Web Application Framework
03: Copyright (C) 2007 Innowhere Software Services S.L., Spanish Company
04: Author: Jose Maria Arranz Santamaria
05:
06: This program is free software: you can redistribute it and/or modify
07: it under the terms of the GNU Affero General Public License as published by
08: the Free Software Foundation, either version 3 of the License, or
09: (at your option) any later version. See the GNU Affero General Public
10: License for more details. See the copy of the GNU Affero General Public License
11: included in this program. If not, see <http://www.gnu.org/licenses/>.
12: */
13:
14: package org.itsnat.impl.core.listener;
15:
16: import org.itsnat.core.ItsNatException;
17: import org.itsnat.impl.core.comet.CometNotifierImpl;
18: import org.itsnat.impl.core.event.client2serv.*;
19:
20: /**
21: *
22: * @author jmarranz
23: */
24: public class CometTask extends GenericTask {
25: protected CometNotifierImpl notifier;
26: protected Object monitor; // apunta al mismo objeto que el CometNotifierImpl
27:
28: /**
29: * Creates a new instance of CometTask
30: */
31: public CometTask(CometNotifierImpl notifier) {
32: this .notifier = notifier;
33: this .monitor = notifier.getMonitor(); // Siempre no nulo
34: }
35:
36: public void dispose() {
37: this .monitor = null;
38: }
39:
40: public boolean isDisposed() {
41: return monitor == null;
42: }
43:
44: public CometNotifierImpl getCometNotifier() {
45: return notifier;
46: }
47:
48: public long getAJAXTimeout() {
49: return notifier.getAJAXTimeout();
50: }
51:
52: public boolean mustWait() {
53: return !isDisposed() && !notifier.isStopped()
54: && !notifier.hasPendingData();
55: }
56:
57: public void waitToFinish() {
58: // Para que espere tranquilito (parado) dando oportunidad a otros hilos
59: if (monitor == null)
60: throw new ItsNatException("INTERNAL ERROR"); // No debería ocurrir nunca pero por si acaso
61:
62: try {
63: long before = System.currentTimeMillis();
64: long delay = notifier.getExpirationDelay();
65: synchronized (monitor) {
66: if (mustWait())
67: monitor.wait(delay);
68: }
69: long after = System.currentTimeMillis();
70: if (after - before > delay) // Se ha despertado no porque le hayan notificado sino porque ha terminado el tiempo de espera
71: notifier.stop(); // Terminamos el CometNotifier porque parece que el agente notificador ha muerto, así evitamos un proceso Comet indefinido
72: } catch (InterruptedException ex) {
73: }
74:
75: dispose(); // para asegurar que no debemos esperar de nuevo con este CometTask, ha de procesarse
76: }
77: }
|