BandwidthMonitorPlugin.php 3.6KB

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