BandwidthMonitorPlugin.php 3.6KB

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