TYPO3 API  SVNRelease
BandwidthMonitorPlugin.php
Go to the documentation of this file.
00001 <?php
00002 
00003 /*
00004  * This file is part of SwiftMailer.
00005  * (c) 2004-2009 Chris Corbyn
00006  *
00007  * For the full copyright and license information, please view the LICENSE
00008  * file that was distributed with this source code.
00009  */
00010 
00011 //@require 'Swift/Events/SendListener.php';
00012 //@require 'Swift/Events/SendEvent.php';
00013 //@require 'Swift/Events/CommandListener.php';
00014 //@require 'Swift/Events/CommandEvent.php';
00015 //@require 'Swift/Events/ResponseListener.php';
00016 //@require 'Swift/Events/ResponseEvent.php';
00017 //@require 'Swift/InputByteStream.php';
00018 
00019 /**
00020  * Reduces network flooding when sending large amounts of mail.
00021  * @package Swift
00022  * @subpackage Plugins
00023  * @author Chris Corbyn
00024  */
00025 class Swift_Plugins_BandwidthMonitorPlugin
00026   implements Swift_Events_SendListener, Swift_Events_CommandListener,
00027   Swift_Events_ResponseListener, Swift_InputByteStream
00028 {
00029 
00030   /**
00031    * The outgoing traffic counter.
00032    * @var int
00033    * @access private
00034    */
00035   private $_out = 0;
00036 
00037   /**
00038    * The incoming traffic counter.
00039    * @var int
00040    * @access private
00041    */
00042   private $_in = 0;
00043 
00044   /** Bound byte streams */
00045   private $_mirrors = array();
00046 
00047   /**
00048    * Not used.
00049    */
00050   public function beforeSendPerformed(Swift_Events_SendEvent $evt)
00051   {
00052   }
00053 
00054   /**
00055    * Invoked immediately after the Message is sent.
00056    * @param Swift_Events_SendEvent $evt
00057    */
00058   public function sendPerformed(Swift_Events_SendEvent $evt)
00059   {
00060     $message = $evt->getMessage();
00061     $message->toByteStream($this);
00062   }
00063 
00064   /**
00065    * Invoked immediately following a command being sent.
00066    * @param Swift_Events_ResponseEvent $evt
00067    */
00068   public function commandSent(Swift_Events_CommandEvent $evt)
00069   {
00070     $command = $evt->getCommand();
00071     $this->_out += strlen($command);
00072   }
00073 
00074   /**
00075    * Invoked immediately following a response coming back.
00076    * @param Swift_Events_ResponseEvent $evt
00077    */
00078   public function responseReceived(Swift_Events_ResponseEvent $evt)
00079   {
00080     $response = $evt->getResponse();
00081     $this->_in += strlen($response);
00082   }
00083 
00084   /**
00085    * Called when a message is sent so that the outgoing counter can be increased.
00086    * @param string $bytes
00087    */
00088   public function write($bytes)
00089   {
00090     $this->_out += strlen($bytes);
00091     foreach ($this->_mirrors as $stream)
00092     {
00093       $stream->write($bytes);
00094     }
00095   }
00096 
00097   /**
00098    * Not used.
00099    */
00100   public function commit()
00101   {
00102   }
00103 
00104   /**
00105    * Attach $is to this stream.
00106    * The stream acts as an observer, receiving all data that is written.
00107    * All {@link write()} and {@link flushBuffers()} operations will be mirrored.
00108    *
00109    * @param Swift_InputByteStream $is
00110    */
00111   public function bind(Swift_InputByteStream $is)
00112   {
00113     $this->_mirrors[] = $is;
00114   }
00115 
00116   /**
00117    * Remove an already bound stream.
00118    * If $is is not bound, no errors will be raised.
00119    * If the stream currently has any buffered data it will be written to $is
00120    * before unbinding occurs.
00121    *
00122    * @param Swift_InputByteStream $is
00123    */
00124   public function unbind(Swift_InputByteStream $is)
00125   {
00126     foreach ($this->_mirrors as $k => $stream)
00127     {
00128       if ($is === $stream)
00129       {
00130         unset($this->_mirrors[$k]);
00131       }
00132     }
00133   }
00134 
00135   /**
00136    * Not used.
00137    */
00138   public function flushBuffers()
00139   {
00140     foreach ($this->_mirrors as $stream)
00141     {
00142       $stream->flushBuffers();
00143     }
00144   }
00145 
00146   /**
00147    * Get the total number of bytes sent to the server.
00148    * @return int
00149    */
00150   public function getBytesOut()
00151   {
00152     return $this->_out;
00153   }
00154 
00155   /**
00156    * Get the total number of bytes received from the server.
00157    * @return int
00158    */
00159   public function getBytesIn()
00160   {
00161     return $this->_in;
00162   }
00163 
00164   /**
00165    * Reset the internal counters to zero.
00166    */
00167   public function reset()
00168   {
00169     $this->_out = 0;
00170     $this->_in = 0;
00171   }
00172 
00173 }