diff options
author | 2013-01-12 00:10:03 -0800 | |
---|---|---|
committer | 2013-01-12 00:10:03 -0800 | |
commit | 7b04566d191b27d8018fd486570a12b6f1049a16 (patch) | |
tree | e84617c937960fedab80548e83f780824d4d3eb4 /src/async/smtp | |
parent | 739b68a69682d80d8247d4465eae7b182acc9da0 (diff) |
implemented asynchronous SMTP
Diffstat (limited to 'src/async/smtp')
-rw-r--r-- | src/async/smtp/MCAsyncSMTP.h | 16 | ||||
-rw-r--r-- | src/async/smtp/MCSMTPAsyncSession.cc | 43 | ||||
-rw-r--r-- | src/async/smtp/MCSMTPAsyncSession.h | 13 | ||||
-rw-r--r-- | src/async/smtp/MCSMTPOperation.cpp | 85 | ||||
-rw-r--r-- | src/async/smtp/MCSMTPOperation.h | 46 | ||||
-rw-r--r-- | src/async/smtp/MCSMTPOperationCallback.h | 22 | ||||
-rw-r--r-- | src/async/smtp/MCSMTPSendWithBuilderOperation.cc | 34 | ||||
-rw-r--r-- | src/async/smtp/MCSMTPSendWithBuilderOperation.h | 24 | ||||
-rw-r--r-- | src/async/smtp/MCSMTPSendWithDataOperation.cc | 33 | ||||
-rw-r--r-- | src/async/smtp/MCSMTPSendWithDataOperation.h | 21 | ||||
-rw-r--r-- | src/async/smtp/MCSMTPSendWithRecipientOperation.cc | 62 | ||||
-rw-r--r-- | src/async/smtp/MCSMTPSendWithRecipientOperation.h | 31 |
12 files changed, 405 insertions, 25 deletions
diff --git a/src/async/smtp/MCAsyncSMTP.h b/src/async/smtp/MCAsyncSMTP.h new file mode 100644 index 00000000..5d5e8282 --- /dev/null +++ b/src/async/smtp/MCAsyncSMTP.h @@ -0,0 +1,16 @@ +// +// MCAsyncSMTP.h +// mailcore2 +// +// Created by DINH Viêt Hoà on 1/11/13. +// Copyright (c) 2013 MailCore. All rights reserved. +// + +#ifndef mailcore2_MCAsyncSMTP_h +#define mailcore2_MCAsyncSMTP_h + +#include <mailcore/MCSMTPAsyncSession.h> +#include <mailcore/MCSMTPOperation.h> +#include <mailcore/MCSMTPOperationCallback.h> + +#endif diff --git a/src/async/smtp/MCSMTPAsyncSession.cc b/src/async/smtp/MCSMTPAsyncSession.cc index 9798fc0c..e7ddebb7 100644 --- a/src/async/smtp/MCSMTPAsyncSession.cc +++ b/src/async/smtp/MCSMTPAsyncSession.cc @@ -4,6 +4,7 @@ #include "MCSMTPSendWithRecipientOperation.h" #include "MCSMTPSendWithDataOperation.h" #include "MCSMTPSendWithBuilderOperation.h" +#include "MCSMTPOperation.h" using namespace mailcore; @@ -15,13 +16,12 @@ SMTPAsyncSession::SMTPAsyncSession() SMTPAsyncSession::~SMTPAsyncSession() { - mQueue->release(); - mSession->release(); -} - -String * SMTPAsyncSession::className() -{ - return MCSTR("SMTPAsyncSession"); + MC_SAFE_RELEASE(mHostname); + MC_SAFE_RELEASE(mUsername); + MC_SAFE_RELEASE(mPassword); + + MC_SAFE_RELEASE(mQueue); + MC_SAFE_RELEASE(mSession); } void SMTPAsyncSession::setHostname(String * hostname) @@ -114,32 +114,39 @@ bool SMTPAsyncSession::useHeloIPEnabled() return mSession->useHeloIPEnabled(); } -SMTPOperation * sendMessage(Address * from, Array * recipients, Data * messageData) +void SMTPAsyncSession::runOperation(SMTPOperation * operation) +{ + mQueue->addOperation(operation); +} + +SMTPSession * SMTPAsyncSession::SMTPAsyncSession::session() +{ + return mSession; +} + +SMTPOperation * SMTPAsyncSession::sendMessageOperationWithFromAndRecipient(Address * from, Array * recipients, Data * messageData) { -#if 0 SMTPSendWithRecipientOperation * op = new SMTPSendWithRecipientOperation(); + op->setSession(this); op->setFrom(from); op->setRecipients(recipients); - op->messageData(messageData); + op->setMessageData(messageData); return (SMTPOperation *) op->autorelease(); -#endif - return NULL; } -SMTPOperation * sendMessage(Data * messageData) +SMTPOperation * SMTPAsyncSession::sendMessageOperation(Data * messageData) { -#if 0 SMTPSendWithDataOperation * op = new SMTPSendWithDataOperation(); - op->setData(messageData); + op->setSession(this); + op->setMessageData(messageData); return (SMTPOperation *) op->autorelease(); -#endif - return NULL; } -SMTPOperation * sendMessage(MessageBuilder * msg) +SMTPOperation * SMTPAsyncSession::sendMessageOperation(MessageBuilder * msg) { #if 0 SMTPSendWithBuilderOperation * op = new SMTPSendWithBuilderOperation(); + op->setSession(this); op->setBuilder(msg); return (SMTPOperation *) op->autorelease(); #endif diff --git a/src/async/smtp/MCSMTPAsyncSession.h b/src/async/smtp/MCSMTPAsyncSession.h index 8e0e50b0..c4411a00 100644 --- a/src/async/smtp/MCSMTPAsyncSession.h +++ b/src/async/smtp/MCSMTPAsyncSession.h @@ -34,8 +34,6 @@ namespace mailcore { SMTPAsyncSession(); virtual ~SMTPAsyncSession(); - virtual String * className(); - virtual void setHostname(String * hostname); virtual String * hostname(); @@ -63,9 +61,14 @@ namespace mailcore { virtual void setUseHeloIPEnabled(bool enabled); virtual bool useHeloIPEnabled(); - virtual SMTPOperation * sendMessage(Address * from, Array * recipients, Data * messageData); - virtual SMTPOperation * sendMessage(Data * messageData); - virtual SMTPOperation * sendMessage(MessageBuilder * msg); + virtual SMTPOperation * sendMessageOperationWithFromAndRecipient(Address * from, Array * recipients, Data * messageData); + virtual SMTPOperation * sendMessageOperation(Data * messageData); + virtual SMTPOperation * sendMessageOperation(MessageBuilder * msg); + + + // private + virtual void runOperation(SMTPOperation * operation); + virtual SMTPSession * session(); }; } diff --git a/src/async/smtp/MCSMTPOperation.cpp b/src/async/smtp/MCSMTPOperation.cpp new file mode 100644 index 00000000..532c6ab4 --- /dev/null +++ b/src/async/smtp/MCSMTPOperation.cpp @@ -0,0 +1,85 @@ +// +// MCSMTPOperation.cpp +// mailcore2 +// +// Created by DINH Viêt Hoà on 1/11/13. +// Copyright (c) 2013 MailCore. All rights reserved. +// + +#include "MCSMTPOperation.h" + +#include <stdlib.h> + +#include "MCSMTPAsyncSession.h" +#include "MCSMTPOperationCallback.h" + +using namespace mailcore; + +SMTPOperation::SMTPOperation() +{ + mSession = NULL; + mError = ErrorNone; +} + +SMTPOperation::~SMTPOperation() +{ + MC_SAFE_RELEASE(mSession); +} + +void SMTPOperation::setSession(SMTPAsyncSession * session) +{ + MC_SAFE_REPLACE_RETAIN(SMTPAsyncSession, mSession, session); +} + +SMTPAsyncSession * SMTPOperation::session() +{ + return mSession; +} + +void SMTPOperation::start() +{ + mSession->runOperation(this); +} + +void SMTPOperation::setSmtpCallback(SMTPOperationCallback * callback) +{ + mSmtpCallback = callback; +} + +SMTPOperationCallback * SMTPOperation::smtpCallback() +{ + return mSmtpCallback; +} + +void SMTPOperation::setError(ErrorCode error) +{ + mError = error; +} + +ErrorCode SMTPOperation::error() +{ + return mError; +} + +struct progressContext { + unsigned int current; + unsigned int maximum; +}; + +void SMTPOperation::bodyProgress(SMTPSession * session, unsigned int current, unsigned int maximum) +{ + struct progressContext * context = (struct progressContext *) calloc(sizeof(* context), 1); + context->current = current; + context->maximum = maximum; + performMethodOnMainThread((Object::Method) &SMTPOperation::bodyProgressOnMainThread, context); +} + +void SMTPOperation::bodyProgressOnMainThread(void * ctx) +{ + struct progressContext * context = (struct progressContext *) ctx; + if (mSmtpCallback != NULL) { + mSmtpCallback->bodyProgress(this, context->current, context->maximum); + } + free(context); +} + diff --git a/src/async/smtp/MCSMTPOperation.h b/src/async/smtp/MCSMTPOperation.h new file mode 100644 index 00000000..841c845c --- /dev/null +++ b/src/async/smtp/MCSMTPOperation.h @@ -0,0 +1,46 @@ +// +// MCSMTPOperation.h +// mailcore2 +// +// Created by DINH Viêt Hoà on 1/11/13. +// Copyright (c) 2013 MailCore. All rights reserved. +// + +#ifndef __mailcore2__MCSMTPOperation__ +#define __mailcore2__MCSMTPOperation__ + +#include <mailcore/MCBaseTypes.h> +#include <mailcore/MCSMTPProgressCallback.h> + +namespace mailcore { + + class SMTPAsyncSession; + class SMTPOperationCallback; + + class SMTPOperation : public Operation, public SMTPProgressCallback { + SMTPAsyncSession * mSession; + SMTPOperationCallback * mSmtpCallback; + ErrorCode mError; + private: + virtual void bodyProgress(SMTPSession * session, unsigned int current, unsigned int maximum); + virtual void bodyProgressOnMainThread(void * context); + + public: + SMTPOperation(); + virtual ~SMTPOperation(); + + virtual void setSession(SMTPAsyncSession * session); + virtual SMTPAsyncSession * session(); + + virtual void setSmtpCallback(SMTPOperationCallback * callback); + virtual SMTPOperationCallback * smtpCallback(); + + virtual void setError(ErrorCode error); + virtual ErrorCode error(); + + virtual void start(); + }; + +} + +#endif /* defined(__mailcore2__MCSMTPOperation__) */ diff --git a/src/async/smtp/MCSMTPOperationCallback.h b/src/async/smtp/MCSMTPOperationCallback.h new file mode 100644 index 00000000..fec52767 --- /dev/null +++ b/src/async/smtp/MCSMTPOperationCallback.h @@ -0,0 +1,22 @@ +// +// MCSMTPOperationCallback.h +// mailcore2 +// +// Created by DINH Viêt Hoà on 1/11/13. +// Copyright (c) 2013 MailCore. All rights reserved. +// + +#ifndef __mailcore2__MCSMTPOperationCallback__ +#define __mailcore2__MCSMTPOperationCallback__ + +namespace mailcore { + + class SMTPOperation; + + class SMTPOperationCallback { + public: + virtual void bodyProgress(SMTPOperation * session, unsigned int current, unsigned int maximum) {}; + }; +} + +#endif /* defined(__mailcore2__MCSMTPOperationCallback__) */ diff --git a/src/async/smtp/MCSMTPSendWithBuilderOperation.cc b/src/async/smtp/MCSMTPSendWithBuilderOperation.cc index 560998ff..df19ee17 100644 --- a/src/async/smtp/MCSMTPSendWithBuilderOperation.cc +++ b/src/async/smtp/MCSMTPSendWithBuilderOperation.cc @@ -7,3 +7,37 @@ // #include "MCSMTPSendWithBuilderOperation.h" + +#include "MCSMTPAsyncSession.h" +#include "MCSMTPSession.h" +#include "MCMessageBuilder.h" + +using namespace mailcore; + +SMTPSendWithBuilderOperation::SMTPSendWithBuilderOperation() +{ + mBuilder = NULL; +} + +SMTPSendWithBuilderOperation::~SMTPSendWithBuilderOperation() +{ + MC_SAFE_RELEASE(mBuilder); +} + +void SMTPSendWithBuilderOperation::setBuilder(MessageBuilder * builder) +{ + MC_SAFE_REPLACE_RETAIN(MessageBuilder, mBuilder, builder); +} + +MessageBuilder * SMTPSendWithBuilderOperation::builder() +{ + return mBuilder; +} + +void SMTPSendWithBuilderOperation::main() +{ + ErrorCode error; + + session()->session()->sendMessage(mBuilder, this, &error); + setError(error); +} diff --git a/src/async/smtp/MCSMTPSendWithBuilderOperation.h b/src/async/smtp/MCSMTPSendWithBuilderOperation.h index d306e30c..827e34b5 100644 --- a/src/async/smtp/MCSMTPSendWithBuilderOperation.h +++ b/src/async/smtp/MCSMTPSendWithBuilderOperation.h @@ -9,6 +9,28 @@ #ifndef __mailcore2__MCSMTPSendWithBuilderOperation__ #define __mailcore2__MCSMTPSendWithBuilderOperation__ -#include <iostream> +#include <mailcore/MCBaseTypes.h> +#include <mailcore/MCAbstract.h> +#include <mailcore/MCSMTPOperation.h> + +namespace mailcore { + + class MessageBuilder; + + class SMTPSendWithBuilderOperation : public SMTPOperation { + MessageBuilder * mBuilder; + private: + + public: + SMTPSendWithBuilderOperation(); + virtual ~SMTPSendWithBuilderOperation(); + + virtual void setBuilder(MessageBuilder * builder); + virtual MessageBuilder * builder(); + + void main(); + }; + +} #endif /* defined(__mailcore2__SMTPSendWithBuilderOperation__) */ diff --git a/src/async/smtp/MCSMTPSendWithDataOperation.cc b/src/async/smtp/MCSMTPSendWithDataOperation.cc index ab42aa0e..5c43828a 100644 --- a/src/async/smtp/MCSMTPSendWithDataOperation.cc +++ b/src/async/smtp/MCSMTPSendWithDataOperation.cc @@ -7,3 +7,36 @@ // #include "MCSMTPSendWithDataOperation.h" + +#include "MCSMTPAsyncSession.h" +#include "MCSMTPSession.h" + +using namespace mailcore; + +SMTPSendWithDataOperation::SMTPSendWithDataOperation() +{ + mMessageData = NULL; +} + +SMTPSendWithDataOperation::~SMTPSendWithDataOperation() +{ + MC_SAFE_RELEASE(mMessageData); +} + +void SMTPSendWithDataOperation::setMessageData(Data * data) +{ + MC_SAFE_REPLACE_RETAIN(Data, mMessageData, data); +} + +Data * SMTPSendWithDataOperation::messageData() +{ + return mMessageData; +} + +void SMTPSendWithDataOperation::main() +{ + ErrorCode error; + + session()->session()->sendMessage(mMessageData, this, &error); + setError(error); +} diff --git a/src/async/smtp/MCSMTPSendWithDataOperation.h b/src/async/smtp/MCSMTPSendWithDataOperation.h index c13ee07d..4f71e233 100644 --- a/src/async/smtp/MCSMTPSendWithDataOperation.h +++ b/src/async/smtp/MCSMTPSendWithDataOperation.h @@ -9,6 +9,25 @@ #ifndef __mailcore2__MCSMTPSendWithDataOperation__ #define __mailcore2__MCSMTPSendWithDataOperation__ -#include <iostream> +#include <mailcore/MCBaseTypes.h> +#include <mailcore/MCAbstract.h> +#include <mailcore/MCSMTPOperation.h> + +namespace mailcore { + + class SMTPSendWithDataOperation : public SMTPOperation { + Data * mMessageData; + private: + + public: + SMTPSendWithDataOperation(); + virtual ~SMTPSendWithDataOperation(); + + virtual void setMessageData(Data * data); + virtual Data * messageData(); + + virtual void main(); + }; +} #endif /* defined(__mailcore2__SMTPSendWithDataOperation__) */ diff --git a/src/async/smtp/MCSMTPSendWithRecipientOperation.cc b/src/async/smtp/MCSMTPSendWithRecipientOperation.cc index 5fee7fa3..ca91ab63 100644 --- a/src/async/smtp/MCSMTPSendWithRecipientOperation.cc +++ b/src/async/smtp/MCSMTPSendWithRecipientOperation.cc @@ -7,3 +7,65 @@ // #include "MCSMTPSendWithRecipientOperation.h" + +#include "MCSMTPAsyncSession.h" +#include "MCSMTPSession.h" + +using namespace mailcore; + +void SMTPSendWithRecipientOperation::init() +{ + mRecipients = NULL; + mFrom = NULL; + mMessageData = NULL; +} + +SMTPSendWithRecipientOperation::SMTPSendWithRecipientOperation() +{ + init(); +} + +SMTPSendWithRecipientOperation::~SMTPSendWithRecipientOperation() +{ + MC_SAFE_RELEASE(mRecipients); + MC_SAFE_RELEASE(mFrom); + MC_SAFE_RELEASE(mMessageData); +} + +void SMTPSendWithRecipientOperation::setFrom(Address * from) +{ + MC_SAFE_REPLACE_RETAIN(Address, mFrom, from); +} + +Address * SMTPSendWithRecipientOperation::from() +{ + return mFrom; +} + +void SMTPSendWithRecipientOperation::setRecipients(Array * recipients) +{ + MC_SAFE_REPLACE_RETAIN(Array, mRecipients, recipients); +} + +Array * SMTPSendWithRecipientOperation::recipients() +{ + return mRecipients; +} + +void SMTPSendWithRecipientOperation::setMessageData(Data * data) +{ + MC_SAFE_REPLACE_RETAIN(Data, mMessageData, data); +} + +Data * SMTPSendWithRecipientOperation::messageData() +{ + return mMessageData; +} + +void SMTPSendWithRecipientOperation::main() +{ + ErrorCode error; + + session()->session()->sendMessage(mFrom, mRecipients, mMessageData, this, &error); + setError(error); +} diff --git a/src/async/smtp/MCSMTPSendWithRecipientOperation.h b/src/async/smtp/MCSMTPSendWithRecipientOperation.h index f4134688..936e8a51 100644 --- a/src/async/smtp/MCSMTPSendWithRecipientOperation.h +++ b/src/async/smtp/MCSMTPSendWithRecipientOperation.h @@ -9,5 +9,36 @@ #ifndef __mailcore2__MCSMTPSendWithRecipientOperation__ #define __mailcore2__MCSMTPSendWithRecipientOperation__ +#include <mailcore/MCBaseTypes.h> +#include <mailcore/MCAbstract.h> +#include <mailcore/MCSMTPOperation.h> + +namespace mailcore { + + class SMTPSendWithRecipientOperation : public SMTPOperation { + private: + Array * mRecipients; + Address * mFrom; + Data * mMessageData; + + void init(); + + public: + SMTPSendWithRecipientOperation(); + virtual ~SMTPSendWithRecipientOperation(); + + virtual void setFrom(Address * from); + virtual Address * from(); + + virtual void setRecipients(Array * recipients); + virtual Array * recipients(); + + virtual void setMessageData(Data * data); + virtual Data * messageData(); + + virtual void main(); + }; + +} #endif /* defined(__mailcore2__SMTPSendWithRecipientOperation__) */ |