609eb692b432a0a21d1879befa7370a2694a8e06
[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.AbstractFilter;
24 import net.pterodactylus.sonitus.data.DataPacket;
25 import net.pterodactylus.sonitus.data.Filter;
26 import net.pterodactylus.sonitus.data.Metadata;
27
28 /**
29  * Rate limiting filter that only passes a specified amount of data per second
30  * from its {@link net.pterodactylus.sonitus.data.Source} to its {@link
31  * net.pterodactylus.sonitus.data.Sink}.
32  *
33  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34  */
35 public class RateLimitingFilter extends AbstractFilter implements Filter {
36
37         /** The logger. */
38         private static final Logger logger = Logger.getLogger(RateLimitingFilter.class.getName());
39
40         /** The limiting rate in bytes/second. */
41         private final int rate;
42
43         /** The start time. */
44         private long startTime;
45
46         /** The number of bytes. */
47         private long counter;
48
49         /**
50          * Creates a new rate limiting filter.
51          *
52          * @param name
53          *              The name of the filter
54          * @param rate
55          */
56         public RateLimitingFilter(String name, int rate) {
57                 this(name, rate, 0);
58         }
59
60         /**
61          * Creates a new rate limiting filter.
62          *
63          * @param name
64          *              The name of the filter
65          * @param rate
66          *              The limiting rate (in bytes/second)
67          * @param fastStartTime
68          *              The amount of time at the start of the filtering during which no delay
69          */
70         public RateLimitingFilter(String name, int rate, long fastStartTime) {
71                 super(name);
72                 this.rate = rate;
73                 this.counter = (long) (-rate * (fastStartTime / 1000.0));
74         }
75
76         //
77         // FILTER METHODS
78         //
79
80         @Override
81         public void open(Metadata metadata) throws IOException {
82                 super.open(metadata);
83                 startTime = System.currentTimeMillis();
84         }
85
86         @Override
87         public void process(DataPacket dataPacket) throws IOException {
88                 super.process(dataPacket);
89                 /* delay. */
90                 counter += dataPacket.buffer().length;
91                 long waitTime = (long) (counter / (rate / 1000.0));
92                 while ((System.currentTimeMillis() - startTime) < waitTime) {
93                         try {
94                                 long limitDelay = waitTime - (System.currentTimeMillis() - startTime);
95                                 logger.finest(String.format("Waiting %d ms...", limitDelay));
96                                 Thread.sleep(limitDelay);
97                         } catch (InterruptedException ie1) {
98                                 /* ignore, keep looping. */
99                         }
100                 }
101                 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)));
102         }
103
104 }