8de7176f9ee78fa6786cf986ff2b1d9c90aa16a7
1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 Ericsson
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
10 * Alexandre Montplaisir - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.core
.signal
;
15 import java
.util
.Timer
;
16 import java
.util
.TimerTask
;
18 import org
.eclipse
.tracecompass
.tmf
.core
.component
.ITmfComponent
;
19 import org
.eclipse
.tracecompass
.tmf
.core
.component
.TmfComponent
;
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.
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.
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
35 * @author Alexandre Montplaisir
37 public class TmfSignalThrottler
{
39 private final ITmfComponent fComponent
;
40 private final long fDelay
;
41 private final Timer fTimer
;
42 private TimerTask fCurrentTask
;
48 * The source component of the signals
50 * Time to wait before actually sending signals (in ms)
52 public TmfSignalThrottler(ITmfComponent component
, long delay
) {
53 this.fComponent
= component
;
55 this.fTimer
= new Timer();
58 * Initialize currentTask to something, so we don't have to do a null
61 fCurrentTask
= new TimerTask() { @Override public void run() {} };
65 * Queue a signal for sending. It will only be forward to the centralized
66 * signal handler if 'delay' elapses without another signal being sent
67 * through this method.
69 * You call this instead of calling {@link TmfComponent#broadcast}.
72 * The signal to queue for broadcasting
74 public synchronized void queue(TmfSignal signal
) {
75 fCurrentTask
.cancel();
76 fCurrentTask
= new BroadcastRequest(signal
);
77 fTimer
.schedule(fCurrentTask
, fDelay
);
81 * Dispose method. Will prevent any pending signal from being sent, and this
82 * throttler from be used again.
84 public synchronized void dispose() {
89 private class BroadcastRequest
extends TimerTask
{
91 private final TmfSignal signal
;
93 BroadcastRequest(TmfSignal signal
) {
99 fComponent
.broadcast(signal
);
This page took 0.03283 seconds and 4 git commands to generate.