smtpthread/ThreadedSMTPConnection.cc

00001 // DOCUMENT:  ThreadedSMTPConnection-Class
00002 // VERSION:   $Revision: 1.6 $
00003 // DATE:      $Date: 2007-10-26 23:09:19 $
00004 // AUTHOR:    M.Beranek
00005 // COPYRIGHT: M.Beranek
00006 
00007 // TODO: Handle changing connection-data after initConnection...
00008 
00009 #include <iostream>
00010 #include <stdio.h>
00011 #include <stdlib.h>
00012 #include <unistd.h>
00013 #include <string>
00014 #include <errno.h>
00015 #include <sys/socket.h>
00016 #include <sys/types.h>
00017 #include <netinet/in.h>
00018 #include <arpa/inet.h>
00019 #include <pthread.h>
00020 #include "Thread.h"
00021 #include "SMTPConnection.h"
00022 #include "ThreadedSMTPConnection.h"
00023 
00024 using namespace std;
00025 
00026 namespace SmtpThread
00027 {
00028 // --------------------------------------------------------------------------
00029     /**
00030      * Default-Constructor.
00031      * Initalizes all values to connect to "127.0.0.1", port 25 and
00032      * identifies ourself as "localhost".
00033      * The connection is not opened until a mail is sent.
00034      */
00035     ThreadedSMTPConnection::ThreadedSMTPConnection()
00036     {
00037         _isReady = false;
00038         _smtp = SMTPConnection::NOT_CONNECTED;
00039         _host = "127.0.0.1";
00040         _port = 25;
00041         _myname = "localhost";
00042         _hasMail = false;
00043     }
00044     
00045     
00046 // --------------------------------------------------------------------------
00047     /**
00048      * Starts execution of SMTP-connection-handler.
00049      */
00050     void ThreadedSMTPConnection::Execute( void * arg )
00051     {
00052         // cout << "Start threading... " << endl;
00053         
00054         _isReady = true;
00055         
00056         // endless loop while running:
00057         while( _isRunning )
00058         {
00059             // Assume that we have all connection-data provided,
00060             // so let's open a connection. If connection is opened
00061             // already, the connection won't change.
00062             // _smtpCon.openConnection();
00063             
00064             // If someone put a mail into our object:
00065             if( _hasMail )
00066             {
00067                 //cout << "Setting Lock" << endl;
00068                 
00069                 // Begin Critical section:
00070                 getLock();
00071                 
00072                 // cout << "Calling SMTPConnection.sendMail" << endl;
00073                 
00074                 // Set mail-parameters:
00075                 _smtpCon.sendMail( _from, _to, _body );
00076                 
00077                 // Status zurücksetzen:
00078                 _hasMail = false;
00079                 _isReady = true;
00080                 
00081                 // Send the message:
00082                 // _smtpCon.sendSMTPMessage();
00083                 
00084                 // End Critical section:
00085                 releaseLock();
00086                 
00087                 // cout << "Releasing Lock" << endl;
00088                 
00089             }
00090             
00091             // wait a few micro-seconds:
00092             // 1s    = 1000000
00093             // 0.5s  =  500000
00094             // 0.25s =  250000
00095             usleep( 250000 );
00096             
00097         }
00098         
00099         // if we are terminated before all mails were sent:
00100         if( (! _isRunning) && _hasMail )
00101         {
00102             
00103             // QUESTION: critical section or not?
00104             
00105             // Begin Critical section:
00106             getLock();
00107             
00108             // send the last mail:
00109             _smtpCon.sendMail( _from, _to, _body );
00110             // _smtpCon.sendSMTPMessage();
00111             
00112             _hasMail = false;
00113             
00114             // End critical section
00115             releaseLock();
00116             
00117         }
00118         
00119         // if we have stopped:
00120         if( ! _isRunning )
00121         {
00122             _smtpCon.disconnect();
00123         }
00124     }
00125     
00126     
00127 // --------------------------------------------------------------------------
00128     /**
00129      * Returns true, if the connection is ready to accept messages.
00130      * If the connection currently delivers mail, returns false.
00131      *
00132      * @return bool true / false = ready / not ready to accepting mail.
00133      */
00134     bool ThreadedSMTPConnection::isReady()
00135     {
00136         return _isReady;
00137     }
00138     
00139     
00140 // --------------------------------------------------------------------------
00141     /**
00142      * Gets the current SMTP-State.
00143      *
00144      * Possible values are:
00145      * STATE_NOT_CONNECTED, STATE_CONNECTED, STATE_HELO, STATE_MAILFROM,
00146      * STATE_RCPTTO, STATE_DATA, STATE_DATA_SENDING
00147      *
00148      * @return int SMTP-State.
00149      */
00150     SMTPConnection::SMTPState ThreadedSMTPConnection::getSMTPState()
00151     {
00152         return _smtpCon.getSMTPState();
00153     }
00154     
00155     
00156 // --------------------------------------------------------------------------
00157     /**
00158      * Accepts a new email to be send via SMTP.
00159      *
00160      * The email-body should contain additional headers like subject etc.
00161      *
00162      * @param string* from sender-email
00163      * @param string* to receiverr-email
00164      * @param string* msg email-body
00165      *
00166      */
00167     void ThreadedSMTPConnection::sendMail( string& from, string& to, string& msg )
00168     {
00169         if( _isReady )
00170         {
00171             // cout << "ThreadedSMTPConnection::sendMail" << endl;
00172             // Critical section begins:
00173             getLock();
00174             
00175             _isReady = false;
00176             _from = from;
00177             _to   = to;
00178             _body = msg;
00179             _hasMail = true;
00180             
00181             // End of critical section:
00182             releaseLock();
00183         }
00184     }
00185     
00186     
00187 // --------------------------------------------------------------------------
00188     /**
00189      * Sets the SMTP-Host (IP).
00190      *
00191      * @param string host IP-Address of the SMTP-host.
00192      */
00193     void ThreadedSMTPConnection::setSMTPHost( string host )
00194     {
00195         // cout << "ThreadedSMTPConnection::setSMTPHost" << endl;
00196         
00197         // Begin critical section:
00198         getLock();
00199         
00200         // Set SMTP-host:
00201         _host = host;
00202         _smtpCon.setSMTPHost( _host );
00203         
00204         // End critical section:
00205         releaseLock();
00206         
00207     }
00208     
00209     
00210 // --------------------------------------------------------------------------
00211     /**
00212      * Sets the Port to connect to, defaults to 25.
00213      *
00214      * @param int port Port
00215      *
00216      */
00217     void ThreadedSMTPConnection::setSMTPPort( int port )
00218     {
00219         // Begin critical section:
00220         getLock();
00221         // cout << "ThreadedSMTPConnection::setSMTPPort" << endl;
00222         
00223         // Set port:
00224         _port = port;
00225         _smtpCon.setSMTPPort( _port );
00226         
00227         // End critical section:
00228         releaseLock();
00229         
00230     }
00231     
00232     
00233 // --------------------------------------------------------------------------
00234     /**
00235      * Sets the name, wich will be send with the "HELO"-command.
00236      */
00237     void ThreadedSMTPConnection::setMyHostname( string myname )
00238     {
00239         // cout << "ThreadedSMTPConnection::setMyHostname" << endl;
00240         
00241         // Begin critical section:
00242         getLock();
00243         
00244         // Set the HELO-name:
00245         _myname = myname;
00246         _smtpCon.setMyHostname( _myname );
00247         
00248         // End critical section:
00249         releaseLock();
00250         
00251     }
00252     
00253 }

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