Commit | Line | Data |
---|---|---|
7f82e3c1 AM |
1 | /******************************************************************************* |
2 | * Copyright (c) 2013 Ericsson | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Alexandre Montplaisir - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.tmf.core.signal; | |
14 | ||
15 | import java.util.Timer; | |
16 | import java.util.TimerTask; | |
17 | ||
18 | import org.eclipse.linuxtools.tmf.core.component.ITmfComponent; | |
19 | import org.eclipse.linuxtools.tmf.core.component.TmfComponent; | |
20 | ||
21 | /** | |
22 | * "Buffer" between a TmfComponent and the signal manager. You can use this if | |
23 | * you want to throttle the amount of signals your component will send. | |
24 | * | |
25 | * It works by specifying a delay, then calling {@link #queue}. The signals will | |
26 | * only be really sent if no other call to {@link #queue} happens within $delay | |
27 | * milliseconds afterwards. This guarantees that only the *last* signal is | |
28 | * actually broadcasted. | |
29 | * | |
30 | * Note that this class does not discriminate for signal types, sources, or | |
31 | * whatever. If you want to throttle different signals in different ways, you | |
32 | * can use multiple signal throttlers in your component and call them | |
33 | * accordingly. | |
34 | * | |
35 | * @author Alexandre Montplaisir | |
36 | * @since 2.0 | |
37 | */ | |
38 | public class TmfSignalThrottler { | |
39 | ||
40 | private final ITmfComponent fComponent; | |
41 | private final long fDelay; | |
42 | private final Timer fTimer; | |
43 | private TimerTask fCurrentTask; | |
44 | ||
45 | /** | |
46 | * Constructor | |
47 | * | |
48 | * @param component | |
49 | * The source component of the signals | |
50 | * @param delay | |
51 | * Time to wait before actually sending signals (in ms) | |
52 | */ | |
53 | public TmfSignalThrottler(ITmfComponent component, long delay) { | |
54 | this.fComponent = component; | |
55 | this.fDelay = delay; | |
56 | this.fTimer = new Timer(); | |
57 | ||
58 | /* | |
59 | * Initialize currentTask to something, so we don't have to do a null | |
60 | * check every time | |
61 | */ | |
62 | fCurrentTask = new TimerTask() { @Override public void run() {} }; | |
63 | } | |
64 | ||
65 | /** | |
66 | * Queue a signal for sending. It will only be forward to the centralized | |
67 | * signal handler if 'delay' elapses without another signal being sent | |
68 | * through this method. | |
69 | * | |
70 | * You call this instead of calling {@link TmfComponent#broadcast}. | |
71 | * | |
72 | * @param signal | |
73 | * The signal to queue for broadcasting | |
74 | */ | |
75 | public synchronized void queue(TmfSignal signal) { | |
76 | fCurrentTask.cancel(); | |
77 | fCurrentTask = new BroadcastRequest(signal); | |
78 | fTimer.schedule(fCurrentTask, fDelay); | |
79 | } | |
80 | ||
81 | /** | |
82 | * Dispose method. Will prevent any pending signal from being sent, and this | |
83 | * throttler from be used again. | |
84 | */ | |
85 | public synchronized void dispose() { | |
86 | fTimer.cancel(); | |
87 | fTimer.purge(); | |
88 | } | |
89 | ||
90 | private class BroadcastRequest extends TimerTask { | |
91 | ||
92 | private final TmfSignal signal; | |
93 | ||
94 | BroadcastRequest(TmfSignal signal) { | |
95 | this.signal = signal; | |
96 | } | |
97 | ||
98 | @Override | |
99 | public void run() { | |
100 | fComponent.broadcast(signal); | |
101 | } | |
102 | } | |
103 | } |