smtpthread/SMTPConnectionPool.h

00001 // DOCUMENT:  SMTPConnectionPool-Class
00002 // VERSION:   $Revision: 1.7 $
00003 // DATE:      $Date: 2007-11-01 08:44:12 $
00004 // AUTHOR:    M.Beranek
00005 // COPYRIGHT: M.Beranek
00006 
00007 #ifndef _SMTPCONNECTIONPOOL_H
00008 #define _SMTPCONNECTIONPOOL_H
00009 
00010 #include <string>
00011 #include <vector>
00012 #include "SMTPConnection.h"
00013 #include "ThreadedSMTPConnection.h"
00014 
00015 namespace SmtpThread
00016 {
00017     /**
00018      * SMTPConnectionPool-Class.
00019      *
00020      * The SMTPConnectionPool sets up an amount of SMTPConnections.
00021      * These connections are threaded objects and run each in a seperate
00022      * thread. To send a message, just utilize the sendMail-method.
00023      * The SMTPConnectionPool then tries to find a ready connection
00024      * and delegates the message to this connection.
00025      * 
00026      * Typical usage:
00027      * <code><pre>
00028      * \#include <iostream>
00029      * \#include <sstream>
00030      * \#include <stdlib.h>
00031      * \#include <string>
00032      * \#include <stdio.h>
00033      * \#include <stdlib.h>
00034      * \#include <smtpthread/SMTPConnectionPool.h>
00035      * 
00036      * using namespace std;
00037      * using namespace SmtpThread;
00038      * 
00039      * int main(int argc, char** argv) {
00040      * 
00041      *     string ip = "127.0.0.1";      // SMTP-Server
00042      *     string host = "localhost";    // for HELO-command
00043      *     string to = "you@localhost";  // sender-email
00044      *     string from = "me@localhost"; // recipient-email
00045      *     string port = "25";           // SMTP-port
00046      * 
00047      *     using namespace std;
00048      * 
00049      *     mailbody  = "From: \"me@localhost\"\n";
00050      *     mailbody += "To: \"you@remotehost\"\n";
00051      *     mailbody += "Subject: This is a test\n";
00052      *     mailbody += "\n";
00053      *     mailbody += "This is the first line of the body.\n";
00054      *     mailbody += "This is the second line of the body.\n";
00055      * 
00056      *     SMTPConnectionPool pool;
00057      * 
00058      *     pool.setMyHostname( host );
00059      *     pool.setSMTPHost( ip );
00060      *     pool.setSMTPPort( portNumber );
00061      *     pool.setMaxPoolSize( 100 );
00062      *     pool.setMinPoolSize( 1 );
00063      *     pool.setPoolSize( 10 );
00064      *     pool.setOptimizePool( true );
00065      * 
00066      *     int success = 0;
00067      *     int error   = 0;
00068      *     int result  = 0;
00069      * 
00070      *     for( int idx = 0; idx < 1000; idx++ )
00071      *     {
00072      *         result = pool.sendMail( from, to, mailbody  );
00073      *         if( result == 0 ) ++success;
00074      *         else ++error;
00075      *     }
00076      * 
00077      *     cout << "Success: " << success << endl;
00078      *     cout << "Errors:  " << error   << endl;
00079      * 
00080      * }
00081      * </pre></code>
00082      */
00083     class SMTPConnectionPool
00084     {
00085     public:
00086         /**
00087          * Default Constructor.
00088          */
00089         SMTPConnectionPool();
00090         
00091         /**
00092          * Default Destructor.
00093          */
00094         ~SMTPConnectionPool();
00095         
00096         /**
00097          * Send an email to the connection-pool.
00098          * @param string from sender-email, used in the SMTP-dialog.
00099          * @param string to receiver-email, used in the SMTP-dialog.
00100          * @param string msg email-body to send, including additional headers.
00101          */
00102         int sendMail( string& from, string& to, string& msg );
00103         
00104         /**
00105          * Sets the pool-size.
00106          * The pool-size is the number of SMTP-connectons, which are
00107          * connected to the SMTP-Server.
00108          * @param unsigned long poolSize number of SMTP-connections.
00109          */
00110         void setPoolSize( unsigned long );
00111         
00112         /**
00113          * Returns the pool-size.
00114          * @return unsigned long pool-size.
00115          */
00116         unsigned long getPoolSize();
00117         
00118         /**
00119          * Sets the IP-address of the SMTP-host.
00120          *
00121          * NOTE: Must be an IP-address and no domain-name.
00122          *
00123          * @param string host IP-address of the SMTP-server.
00124          */
00125         void setSMTPHost( string host );
00126         
00127         /**
00128          * Sets the port-number to connect to. Defaults to 25.
00129          * @param int port port-number of the remote SMTP-server.
00130          */
00131         void setSMTPPort( int port );
00132         
00133         /**
00134          * Sets the hostname, which is send in the HELO-commend.
00135          * @param string myname hostname of the mail-sending-host.
00136          */
00137         void setMyHostname( string myname );
00138         
00139         /**
00140          * Tells the pool wether or not to optimize the pool-size.
00141          *
00142          * Optimizing tries to find a pool-size, which has enough
00143          * connections so that a new sendmail-command does not have to
00144          * wait for previous connection to finish.
00145          *
00146          * The optimal pool-size is calculated and set, if within the
00147          * upper and lower limits set with "setMinPoolSize" and
00148          * "setMaxPoolSize".
00149          *
00150          * Please remember, that changing the pool-size also affects
00151          * allocating or deallocating of new/existing objects. This
00152          * results in small performanc-impacts. The bast way is to
00153          * already know the optimal pool-size, initialize the pool
00154          * with that size and disable optimizing the pool.
00155          *
00156          * NOTE: Optimizing the pool-size is not a trivial task.
00157          * The algorithm used in this implementation, is a rather
00158          * simple approach. Works for me, but might not be smart
00159          * enough for you.
00160          *
00161          * @param bool opt true / false = optimize / do not optimize the pool-size.
00162          * @see SMTPConnectionPool::setMinPoolSize
00163          * @see SMTPConnectionPool::setMaxPoolSize
00164          */
00165         void setOptimizePool( bool );
00166         
00167         /**
00168          * Sets the minimum size of the connection-pool.
00169          * Takes effect only, when pool-optimization is active.
00170          * @param long minimum minimal pool-size.
00171          */
00172         void setMinPoolSize( long );
00173         
00174         /**
00175          * Sets the maximum size of the connection-pool.
00176          * Takes effect only, when pool-optimization is active.
00177          * @param long maximum maximal pool-size.
00178          */
00179         void setMaxPoolSize( long );
00180         
00181     private:
00182         
00183         /**
00184          * Initializes the connection-pool.
00185          */
00186         void init();
00187         
00188         /**
00189          * Closes the connection-pool by stopping all threads.
00190          */
00191         void close();
00192         
00193         /** Container for the SMTP-connections. */
00194         vector< ThreadedSMTPConnection* > pool;
00195         
00196         /** Minimal pool-size. */
00197         unsigned long _minPoolSize;
00198         
00199         /** Maximal pool-size. */
00200         unsigned long _maxPoolSize;
00201         
00202         /** Status of pool-optimization. */
00203         bool _optimzePoolSize;
00204         
00205         /** Counter of all processed mails. */
00206         unsigned long _statsTotalMails;
00207         
00208         /** Counter of all mails with errors. */
00209         unsigned long _statsTotalErrors;
00210         
00211         /** Counter for pool-optimization. */
00212         unsigned long _statsMailsAfterOpt;
00213         
00214         /** Average number of tries to get an open connection. */
00215         double _avgNumOfTries;
00216         
00217         /** Root-Mean-Square of _avgNumOfTries. */
00218         double _rms;
00219         
00220         /** Hostname to send in HELO-commend. */
00221         string _myHostname;
00222         
00223         /** IP-address of SMTP-server. */
00224         string _smtpHost;
00225         
00226         /** Port of SMTP-server. */
00227         int _smtpPort;
00228         
00229         
00230     };
00231     
00232 }
00233 
00234 #endif  /* _SMTPCONNECTIONPOOL_H */
00235 

Generated on Thu Nov 1 09:51:22 2007 for libSmtpThread by  doxygen 1.5.1