BandwidthMonitorPlugin.php 3.3KB

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