Rename dummy filter to basic filter.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / data / filter / RateLimitingFilter.java
1 /*
2  * Sonitus - DelayFilter.java - Copyright © 2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sonitus.data.filter;
19
20 import java.io.IOException;
21 import java.util.logging.Logger;
22
23 import net.pterodactylus.sonitus.data.Metadata;
24
25 /**
26  * Rate limiting filter that only passes a specified amount of data per second
27  * from its {@link net.pterodactylus.sonitus.data.Source} to its {@link
28  * net.pterodactylus.sonitus.data.Sink}.
29  *
30  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
31  */
32 public class RateLimitingFilter extends BasicFilter {
33
34         /** The logger. */
35         private static final Logger logger = Logger.getLogger(RateLimitingFilter.class.getName());
36
37         /** The limiting rate in bytes/second. */
38         private final int rate;
39
40         /** The start time. */
41         private long startTime;
42
43         /** The number of bytes. */
44         private long counter;
45
46         /**
47          * Creates a new rate limiting filter.
48          *
49          * @param name
50          *              The name of the filter
51          * @param rate
52          */
53         public RateLimitingFilter(String name, int rate) {
54                 this(name, rate, 0);
55         }
56
57         /**
58          * Creates a new rate limiting filter.
59          *
60          * @param name
61          *              The name of the filter
62          * @param rate
63          *              The limiting rate (in bytes/second)
64          * @param fastStartTime
65          *              The amount of time at the start of the filtering during which no delay
66          */
67         public RateLimitingFilter(String name, int rate, long fastStartTime) {
68                 super(name);
69                 this.rate = rate;
70                 this.counter = (long) (-rate * (fastStartTime / 1000.0));
71         }
72
73         //
74         // FILTER METHODS
75         //
76
77         @Override
78         public void open(Metadata metadata) throws IOException {
79                 super.open(metadata);
80                 startTime = System.currentTimeMillis();
81         }
82
83         @Override
84         public void process(byte[] buffer) throws IOException {
85                 super.process(buffer);
86                 /* delay. */
87                 counter += buffer.length;
88                 long waitTime = (long) (counter / (rate / 1000.0));
89                 while ((System.currentTimeMillis() - startTime) < waitTime) {
90                         try {
91                                 long limitDelay = waitTime - (System.currentTimeMillis() - startTime);
92                                 logger.finest(String.format("Waiting %d ms...", limitDelay));
93                                 Thread.sleep(limitDelay);
94                         } catch (InterruptedException ie1) {
95                                 /* ignore, keep looping. */
96                         }
97                 }
98                 logger.finest(String.format("Processed %d Bytes during %d ms, that’s %.1f bytes/s.", counter, System.currentTimeMillis() - startTime, counter / ((System.currentTimeMillis() - startTime) / 1000.0)));
99         }
100
101 }