summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrandomdan <randomdan@localhost>2012-04-23 22:32:56 +0000
committerrandomdan <randomdan@localhost>2012-04-23 22:32:56 +0000
commit729a8ba1de5969c416b7c0895b37ad516f0b64dc (patch)
tree91ba47995fb9434efacfbf8a9cd1e048d5c430a1
parentPlugable loggers (diff)
downloadproject2-729a8ba1de5969c416b7c0895b37ad516f0b64dc.tar.bz2
project2-729a8ba1de5969c416b7c0895b37ad516f0b64dc.tar.xz
project2-729a8ba1de5969c416b7c0895b37ad516f0b64dc.zip
Tidy up some error handling and mutables
-rw-r--r--project2/mail/sendmailTask.cpp20
-rw-r--r--project2/mail/sendmailTask.h7
-rw-r--r--project2/processes/processStream.cpp9
3 files changed, 15 insertions, 21 deletions
diff --git a/project2/mail/sendmailTask.cpp b/project2/mail/sendmailTask.cpp
index f70fe64..3cc3a92 100644
--- a/project2/mail/sendmailTask.cpp
+++ b/project2/mail/sendmailTask.cpp
@@ -61,12 +61,6 @@ SendMailTask::~SendMailTask()
{
}
-const char *
-SendMailTask::writeMailWrapper(void ** buf, int * len, void * arg)
-{
- return static_cast<const SendMailTask*>(arg)->writeMail(buf, len);
-}
-
SendMailTask::MailPart::MailPart(uint8_t s, uint8_t i, uint8_t p) :
sec(s),
ind(i),
@@ -205,7 +199,7 @@ class EmailViewHost : public ViewHost {
void
SendMailTask::execute() const
{
- parts = new Parts();
+ boost::intrusive_ptr<Parts> parts = new Parts();
MailPart::mimeIdx = 0;
parts->parts.insert(new Header("To", to));
parts->parts.insert(new Header("From", from));
@@ -218,7 +212,7 @@ SendMailTask::execute() const
ViewHostPtr vsp = new EmailViewHost(parts, Environment::getCurrent()->resolveScript("emails", present(), false)->root());
vsp->executeViews();
vsp->doTransforms();
- part = parts->parts.begin();
+ parts->part = parts->parts.begin();
// Write email
smtp_session_t session = smtp_create_session();
@@ -226,21 +220,21 @@ SendMailTask::execute() const
smtp_set_server(session, server());
smtp_set_header(message, "To", NULL, NULL);
smtp_add_recipient(message, to());
- smtp_set_messagecb(message, writeMailWrapper, (SendMailTask*)this);
+ smtp_set_messagecb(message, writeMail, parts.get());
if (!smtp_start_session(session)) {
char buf[BUFSIZ];
smtp_strerror(smtp_errno(), buf, sizeof buf);
throw SendEmailFailed(buf);
}
- parts.reset();
}
const char *
-SendMailTask::writeMail(void ** buf, int * len) const
+SendMailTask::writeMail(void ** buf, int * len, void * arg)
{
- if (len == NULL || part == parts->parts.end()) {
+ Parts * parts = static_cast<Parts *>(arg);
+ if (len == NULL || parts->part == parts->parts.end()) {
return NULL;
}
- return (*part++)->write((char**)buf, len);
+ return (*parts->part++)->write((char**)buf, len);
}
diff --git a/project2/mail/sendmailTask.h b/project2/mail/sendmailTask.h
index 3703203..75d98f6 100644
--- a/project2/mail/sendmailTask.h
+++ b/project2/mail/sendmailTask.h
@@ -28,6 +28,7 @@ class SendMailTask : public Task {
class Parts : public TransformChainLink {
public:
PartList parts;
+ PartList::iterator part;
};
SendMailTask(ScriptNodePtr p);
@@ -42,13 +43,9 @@ class SendMailTask : public Task {
const Variable present;
private:
- static const char * writeMailWrapper(void ** buf, int * len, void * arg);
- const char * writeMail(void ** buf, int * len) const;
+ static const char * writeMail(void ** buf, int * len, void * arg);
PresenterPtr createDefaultPresenter(ScriptNodePtr n) const;
- mutable boost::intrusive_ptr<Parts> parts;
- mutable PartList::iterator part;
-
// Configurables
friend class CustomSendMailTaskLoader;
friend class TransformWritableContentToEmail;
diff --git a/project2/processes/processStream.cpp b/project2/processes/processStream.cpp
index 13c2555..1a853e4 100644
--- a/project2/processes/processStream.cpp
+++ b/project2/processes/processStream.cpp
@@ -1,5 +1,6 @@
#include "iHaveParameters.h"
#include "scriptLoader.h"
+#include "scopeObject.h"
#include "scripts.h"
#include "stream.h"
#include <exception>
@@ -29,8 +30,13 @@ class ProcessStream : public Stream, IHaveParameters {
callProc[pidx++] = p.second();
}
callProc[pidx] = NULL;
+ int fds[2];
popenrw(callProc, fds);
+ ScopeObject doClose([&] {
+ close(fds[0]);
+ close(fds[1]);
+ });
char buf[BUFSIZ];
while (ssize_t r = read(fds[1], buf, BUFSIZ) != 0) {
if (r < 0) {
@@ -39,8 +45,6 @@ class ProcessStream : public Stream, IHaveParameters {
sink(buf, r);
}
- close(fds[0]);
- close(fds[1]);
int status;
wait(&status);
// ignore any error if the application is still running,
@@ -51,7 +55,6 @@ class ProcessStream : public Stream, IHaveParameters {
}
}
protected:
- mutable int fds[2];
const Variable path;
};