|
1 <?php |
|
2 |
|
3 /* |
|
4 * This file is part of SwiftMailer. |
|
5 * (c) 2004-2009 Chris Corbyn |
|
6 * |
|
7 * For the full copyright and license information, please view the LICENSE |
|
8 * file that was distributed with this source code. |
|
9 */ |
|
10 |
|
11 |
|
12 /** |
|
13 * Reduces network flooding when sending large amounts of mail. |
|
14 * @package Swift |
|
15 * @subpackage Plugins |
|
16 * @author Chris Corbyn |
|
17 */ |
|
18 class Swift_Plugins_BandwidthMonitorPlugin |
|
19 implements Swift_Events_SendListener, Swift_Events_CommandListener, |
|
20 Swift_Events_ResponseListener, Swift_InputByteStream |
|
21 { |
|
22 |
|
23 /** |
|
24 * The outgoing traffic counter. |
|
25 * @var int |
|
26 * @access private |
|
27 */ |
|
28 private $_out = 0; |
|
29 |
|
30 /** |
|
31 * The incoming traffic counter. |
|
32 * @var int |
|
33 * @access private |
|
34 */ |
|
35 private $_in = 0; |
|
36 |
|
37 /** Bound byte streams */ |
|
38 private $_mirrors = array(); |
|
39 |
|
40 /** |
|
41 * Not used. |
|
42 */ |
|
43 public function beforeSendPerformed(Swift_Events_SendEvent $evt) |
|
44 { |
|
45 } |
|
46 |
|
47 /** |
|
48 * Invoked immediately after the Message is sent. |
|
49 * @param Swift_Events_SendEvent $evt |
|
50 */ |
|
51 public function sendPerformed(Swift_Events_SendEvent $evt) |
|
52 { |
|
53 $message = $evt->getMessage(); |
|
54 $message->toByteStream($this); |
|
55 } |
|
56 |
|
57 /** |
|
58 * Invoked immediately following a command being sent. |
|
59 * @param Swift_Events_ResponseEvent $evt |
|
60 */ |
|
61 public function commandSent(Swift_Events_CommandEvent $evt) |
|
62 { |
|
63 $command = $evt->getCommand(); |
|
64 $this->_out += strlen($command); |
|
65 } |
|
66 |
|
67 /** |
|
68 * Invoked immediately following a response coming back. |
|
69 * @param Swift_Events_ResponseEvent $evt |
|
70 */ |
|
71 public function responseReceived(Swift_Events_ResponseEvent $evt) |
|
72 { |
|
73 $response = $evt->getResponse(); |
|
74 $this->_in += strlen($response); |
|
75 } |
|
76 |
|
77 /** |
|
78 * Called when a message is sent so that the outgoing counter can be increased. |
|
79 * @param string $bytes |
|
80 */ |
|
81 public function write($bytes) |
|
82 { |
|
83 $this->_out += strlen($bytes); |
|
84 foreach ($this->_mirrors as $stream) |
|
85 { |
|
86 $stream->write($bytes); |
|
87 } |
|
88 } |
|
89 |
|
90 /** |
|
91 * Not used. |
|
92 */ |
|
93 public function commit() |
|
94 { |
|
95 } |
|
96 |
|
97 /** |
|
98 * Attach $is to this stream. |
|
99 * The stream acts as an observer, receiving all data that is written. |
|
100 * All {@link write()} and {@link flushBuffers()} operations will be mirrored. |
|
101 * |
|
102 * @param Swift_InputByteStream $is |
|
103 */ |
|
104 public function bind(Swift_InputByteStream $is) |
|
105 { |
|
106 $this->_mirrors[] = $is; |
|
107 } |
|
108 |
|
109 /** |
|
110 * Remove an already bound stream. |
|
111 * If $is is not bound, no errors will be raised. |
|
112 * If the stream currently has any buffered data it will be written to $is |
|
113 * before unbinding occurs. |
|
114 * |
|
115 * @param Swift_InputByteStream $is |
|
116 */ |
|
117 public function unbind(Swift_InputByteStream $is) |
|
118 { |
|
119 foreach ($this->_mirrors as $k => $stream) |
|
120 { |
|
121 if ($is === $stream) |
|
122 { |
|
123 unset($this->_mirrors[$k]); |
|
124 } |
|
125 } |
|
126 } |
|
127 |
|
128 /** |
|
129 * Not used. |
|
130 */ |
|
131 public function flushBuffers() |
|
132 { |
|
133 foreach ($this->_mirrors as $stream) |
|
134 { |
|
135 $stream->flushBuffers(); |
|
136 } |
|
137 } |
|
138 |
|
139 /** |
|
140 * Get the total number of bytes sent to the server. |
|
141 * @return int |
|
142 */ |
|
143 public function getBytesOut() |
|
144 { |
|
145 return $this->_out; |
|
146 } |
|
147 |
|
148 /** |
|
149 * Get the total number of bytes received from the server. |
|
150 * @return int |
|
151 */ |
|
152 public function getBytesIn() |
|
153 { |
|
154 return $this->_in; |
|
155 } |
|
156 |
|
157 /** |
|
158 * Reset the internal counters to zero. |
|
159 */ |
|
160 public function reset() |
|
161 { |
|
162 $this->_out = 0; |
|
163 $this->_in = 0; |
|
164 } |
|
165 |
|
166 } |