1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.handler.traffic;
17
18 import org.jboss.netty.handler.traffic.GlobalChannelTrafficShapingHandler.PerChannel;
19 import org.jboss.netty.util.Timeout;
20 import org.jboss.netty.util.Timer;
21 import org.jboss.netty.util.TimerTask;
22
23 import java.util.concurrent.TimeUnit;
24
25
26
27
28
29
30
31 public class GlobalChannelTrafficCounter extends TrafficCounter {
32
33
34
35
36
37 public GlobalChannelTrafficCounter(GlobalChannelTrafficShapingHandler trafficShapingHandler,
38 Timer timer, String name, long checkInterval) {
39 super(trafficShapingHandler, timer, name, checkInterval);
40 if (timer == null) {
41 throw new IllegalArgumentException("Timer must not be null");
42 }
43 }
44
45
46
47
48
49 private static final class MixedTrafficMonitoringTask implements TimerTask {
50
51
52
53 private final GlobalChannelTrafficShapingHandler trafficShapingHandler1;
54
55
56
57
58 private final TrafficCounter counter;
59
60
61
62
63
64 MixedTrafficMonitoringTask(
65 GlobalChannelTrafficShapingHandler trafficShapingHandler,
66 TrafficCounter counter) {
67 trafficShapingHandler1 = trafficShapingHandler;
68 this.counter = counter;
69 }
70
71 public void run(Timeout timeout) throws Exception {
72 if (!counter.monitorActive) {
73 return;
74 }
75 long newLastTime = milliSecondFromNano();
76 counter.resetAccounting(newLastTime);
77 for (PerChannel perChannel : trafficShapingHandler1.channelQueues.values()) {
78 perChannel.channelTrafficCounter.resetAccounting(newLastTime);
79 }
80 trafficShapingHandler1.doAccounting(counter);
81 counter.timer.newTimeout(this, counter.checkInterval.get(), TimeUnit.MILLISECONDS);
82 }
83 }
84
85
86
87
88 @Override
89 public synchronized void start() {
90 if (monitorActive) {
91 return;
92 }
93 lastTime.set(milliSecondFromNano());
94 long localCheckInterval = checkInterval.get();
95 if (localCheckInterval > 0) {
96 monitorActive = true;
97 timerTask =
98 new MixedTrafficMonitoringTask(
99 (GlobalChannelTrafficShapingHandler) trafficShapingHandler, this);
100 timeout = timer.newTimeout(timerTask, checkInterval.get(), TimeUnit.MILLISECONDS);
101 }
102 }
103
104
105
106
107 @Override
108 public synchronized void stop() {
109 if (!monitorActive) {
110 return;
111 }
112 monitorActive = false;
113 resetAccounting(milliSecondFromNano());
114 trafficShapingHandler.doAccounting(this);
115 if (timeout != null) {
116 timeout.cancel();
117 }
118 }
119
120 @Override
121 public void resetCumulativeTime() {
122 for (PerChannel perChannel :
123 ((GlobalChannelTrafficShapingHandler) trafficShapingHandler).channelQueues.values()) {
124 perChannel.channelTrafficCounter.resetCumulativeTime();
125 }
126 super.resetCumulativeTime();
127 }
128
129 }