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