Commit | Line | Data |
---|---|---|
7f82e3c1 | 1 | /******************************************************************************* |
ed902a2b | 2 | * Copyright (c) 2013, 2014 Ericsson |
7f82e3c1 AM |
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 | ||
2bdf0193 | 13 | package org.eclipse.tracecompass.tmf.core.signal; |
7f82e3c1 AM |
14 | |
15 | import java.util.Timer; | |
16 | import java.util.TimerTask; | |
17 | ||
e14d610d AM |
18 | import org.eclipse.jdt.annotation.NonNullByDefault; |
19 | import org.eclipse.jdt.annotation.Nullable; | |
2bdf0193 | 20 | import org.eclipse.tracecompass.tmf.core.component.ITmfComponent; |
7f82e3c1 AM |
21 | |
22 | /** | |
23 | * "Buffer" between a TmfComponent and the signal manager. You can use this if | |
24 | * you want to throttle the amount of signals your component will send. | |
25 | * | |
26 | * It works by specifying a delay, then calling {@link #queue}. The signals will | |
27 | * only be really sent if no other call to {@link #queue} happens within $delay | |
28 | * milliseconds afterwards. This guarantees that only the *last* signal is | |
29 | * actually broadcasted. | |
30 | * | |
31 | * Note that this class does not discriminate for signal types, sources, or | |
32 | * whatever. If you want to throttle different signals in different ways, you | |
33 | * can use multiple signal throttlers in your component and call them | |
34 | * accordingly. | |
35 | * | |
36 | * @author Alexandre Montplaisir | |
7f82e3c1 | 37 | */ |
e14d610d | 38 | @NonNullByDefault |
7f82e3c1 AM |
39 | public class TmfSignalThrottler { |
40 | ||
e14d610d | 41 | private final @Nullable ITmfComponent fComponent; |
7f82e3c1 AM |
42 | private final long fDelay; |
43 | private final Timer fTimer; | |
e14d610d | 44 | |
7f82e3c1 AM |
45 | private TimerTask fCurrentTask; |
46 | ||
47 | /** | |
48 | * Constructor | |
49 | * | |
50 | * @param component | |
e14d610d AM |
51 | * The optional source component of the signals. If non-null, its |
52 | * {@link ITmfComponent#broadcast} method will be used to finally | |
53 | * send the signal. If null, the generic | |
54 | * {@link TmfSignalManager#dispatchSignal} is used. | |
7f82e3c1 AM |
55 | * @param delay |
56 | * Time to wait before actually sending signals (in ms) | |
57 | */ | |
e14d610d | 58 | public TmfSignalThrottler(@Nullable ITmfComponent component, long delay) { |
7f82e3c1 AM |
59 | this.fComponent = component; |
60 | this.fDelay = delay; | |
61 | this.fTimer = new Timer(); | |
62 | ||
63 | /* | |
64 | * Initialize currentTask to something, so we don't have to do a null | |
e14d610d | 65 | * check every time. |
7f82e3c1 | 66 | */ |
e14d610d AM |
67 | fCurrentTask = new TimerTask() { |
68 | @Override | |
69 | public void run() { | |
70 | } | |
71 | }; | |
7f82e3c1 AM |
72 | } |
73 | ||
74 | /** | |
75 | * Queue a signal for sending. It will only be forward to the centralized | |
76 | * signal handler if 'delay' elapses without another signal being sent | |
77 | * through this method. | |
78 | * | |
e14d610d AM |
79 | * You call this instead of {@link ITmfComponent#broadcast} or |
80 | * {@link TmfSignalManager#dispatchSignal}. | |
7f82e3c1 AM |
81 | * |
82 | * @param signal | |
83 | * The signal to queue for broadcasting | |
84 | */ | |
85 | public synchronized void queue(TmfSignal signal) { | |
86 | fCurrentTask.cancel(); | |
87 | fCurrentTask = new BroadcastRequest(signal); | |
88 | fTimer.schedule(fCurrentTask, fDelay); | |
89 | } | |
90 | ||
91 | /** | |
92 | * Dispose method. Will prevent any pending signal from being sent, and this | |
93 | * throttler from be used again. | |
94 | */ | |
95 | public synchronized void dispose() { | |
96 | fTimer.cancel(); | |
97 | fTimer.purge(); | |
98 | } | |
99 | ||
100 | private class BroadcastRequest extends TimerTask { | |
101 | ||
102 | private final TmfSignal signal; | |
103 | ||
104 | BroadcastRequest(TmfSignal signal) { | |
105 | this.signal = signal; | |
106 | } | |
107 | ||
108 | @Override | |
109 | public void run() { | |
e14d610d AM |
110 | if (fComponent != null) { |
111 | fComponent.broadcast(signal); | |
112 | } else { | |
113 | TmfSignalManager.dispatchSignal(signal); | |
114 | } | |
7f82e3c1 AM |
115 | } |
116 | } | |
e14d610d | 117 | |
7f82e3c1 | 118 | } |