View Javadoc

1   package fr.in2p3.jsaga.impl.task;
2   
3   import fr.in2p3.jsaga.impl.monitoring.*;
4   import org.ogf.saga.error.*;
5   import org.ogf.saga.monitoring.Callback;
6   
7   /* ***************************************************
8   * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
9   * ***             http://cc.in2p3.fr/             ***
10  * ***************************************************
11  * File:   TaskStateMetricImpl
12  * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
13  * Date:   5 mai 2008
14  * ***************************************************
15  * Description:                                      */
16  /**
17   *
18   */
19  public class TaskStateMetricImpl<E> extends MetricImpl<E> {
20      private AbstractTaskImpl m_task;
21      private boolean m_isListening;
22  
23      /** constructor */
24      public TaskStateMetricImpl(AbstractTaskImpl task, String name, String desc, MetricMode mode, String unit, MetricType type, E initialValue) {
25          super(task, name, desc, mode, unit, type, initialValue);
26          m_task = task;
27          m_isListening = false;
28      }
29  
30      /** clone */
31      public TaskStateMetricImpl<E> clone() throws CloneNotSupportedException {
32          // clone attributes
33          TaskStateMetricImpl<E> clone = (TaskStateMetricImpl<E>) super.clone();
34          clone.m_task = m_task;
35          clone.m_isListening = m_isListening;
36          return clone;
37      }
38  
39      public synchronized int addCallback(Callback cb) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, IncorrectStateException, TimeoutException, NoSuccessException {
40          int nbBefore = super.getNumberOfCallbacks();
41          int cookie = super.addCallback(cb);
42          int nbAfter = super.getNumberOfCallbacks();
43          if (nbBefore==0 && nbAfter==1) {
44              m_isListening = m_task.startListening();
45          }
46          return cookie;
47      }
48  
49      public synchronized void removeCallback(int cookie) throws NotImplementedException, BadParameterException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, TimeoutException, NoSuccessException {
50          int nbBefore = super.getNumberOfCallbacks();
51          super.removeCallback(cookie);
52          int nbAfter = super.getNumberOfCallbacks();
53          if (nbBefore==1 && nbAfter==0) {
54              m_task.stopListening();
55              m_isListening = false;
56          }
57      }
58  
59      public boolean isListening() {
60          return m_isListening;
61      }
62  }