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
9
10
11
12
13
14
15
16
17
18
19 public class TaskStateMetricImpl<E> extends MetricImpl<E> {
20 private AbstractTaskImpl m_task;
21 private boolean m_isListening;
22
23
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
31 public TaskStateMetricImpl<E> clone() throws CloneNotSupportedException {
32
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 }