From f04f9cf9358d79b49eea94960ee891eb76a92b5e Mon Sep 17 00:00:00 2001 From: Carlos Daniel Ruvalcaba Valenzuela Date: Wed, 19 Mar 2008 20:02:53 -0700 Subject: [PATCH] Changed smtp for new worker classes --- backends/protocol/smtp.cpp | 72 ++++++++++++++++++++++++++++++---------------- 1 file changed, 47 insertions(+), 25 deletions(-) diff --git a/backends/protocol/smtp.cpp b/backends/protocol/smtp.cpp index 4939328..a3a5280 100644 --- a/backends/protocol/smtp.cpp +++ b/backends/protocol/smtp.cpp @@ -18,6 +18,8 @@ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ +#include + #include #include @@ -53,14 +55,35 @@ int knhash (char v) { return 255; } -class SMTPHandler : public ProtocolHandler { +int genPath(char *buffer){ + time_t ctime; + struct tm * gtime; + int r; + + /* Get current time*/ + time(&ctime); + gtime = gmtime(&ctime); + + /* Generate a Random number */ + r = rand(); + + /* Construct a unique filename for the slot */ + sprintf(buffer, "/tmp/fmail/queue/incoming/%i%i%i%i%i", gtime->tm_yday, gtime->tm_hour, gtime->tm_min, gtime->tm_sec, r); + return 1; +} + + +class SMTPHandler : public Job { SearchTree < int, 27, &knhash > cmdtree; std::string queue_ipc; + Socket *s; public: - SMTPHandler (std::string qipc) { + SMTPHandler (std::string qipc, Socket *sock) : Job() { /* Queue IPC string */ queue_ipc = qipc; + s = sock; + /* Insert the SMTP commands to search tree */ cmdtree.Insert ("EHLO", 1); cmdtree.Insert ("HELO", 2); @@ -70,7 +93,7 @@ public: cmdtree.Insert ("QUIT", 6); cmdtree.Insert ("RSET", 7); } - int Handle (Socket * s) { + int Run () { char buffer[255], * slotname; int r, l, ret; int onrun, cmd, state; @@ -89,8 +112,8 @@ public: s->Write (CRLF, 2); /* Connect to the Queue server */ - ipc = IPC::CreateIPC ((char *) queue_ipc.c_str ()); - ipc->RequestIPC (); + //ipc = IPC::CreateIPC ((char *) queue_ipc.c_str ()); + //ipc->RequestIPC (); while (onrun) { /* Wait for client input */ @@ -164,20 +187,23 @@ public: /* Give the client green light to send its data */ s->Write ("354 OK", 6); s->Write (CRLF, 2); + + genPath(buffer); + slotname = buffer; /* Request a slot */ - msg = new IPCMessage ("slot"); + /* msg = new IPCMessage ("slot"); msg->PushParam ("incoming"); ipc->SendMessage (msg); - delete msg; + delete msg; */ /* Retrieve Response */ - msg = ipc->ReciveMessage (); + /* msg = ipc->ReciveMessage (); if (!strcmp (msg->GetMessageName (), "ok")) { slotname = msg->PopParam (); } else { break; - } + } */ /* Open Queue Slot File */ fd = NULL; @@ -213,16 +239,16 @@ public: /* Close our slot file */ fclose (fd); - delete msg; + //delete msg; /* Push the message to the Queue */ - msg = new IPCMessage ("push"); + /* msg = new IPCMessage ("push"); msg->PushParam ("incoming"); msg->PushParam (slotname); - ipc->SendMessage (msg); + ipc->SendMessage (msg);*/ - delete msg; - free (slotname); + //delete msg; + //free (slotname); s->Write ("250 OK", 6); s->Write (CRLF, 2); @@ -254,9 +280,9 @@ public: } /* Close Connection */ - msg = new IPCMessage ("quit"); + /*msg = new IPCMessage ("quit"); ipc->SendMessage (msg); - ipc->CloseIPC (); + ipc->CloseIPC ();*/ s->Close (); @@ -274,9 +300,8 @@ public: }; int main () { - LoadHandler * lh; - SMTPHandler * handler; Socket * s, * cl; + Boss *boss; int ret, port, tcount; std::string conf_handler, qipc; Configuration conf ("smtp.conf"); @@ -300,16 +325,13 @@ int main () { /* Load queue connection string */ qipc = conf.getString ("queue", "socket://127.0.0.1:14003"); - /* Create a handler object */ - handler = new SMTPHandler (qipc); - - /* Create a LoadHandler Object based on configuration */ + /* Create a Boss for Thread Worker based on configuration */ if (conf_handler == "thread") { /* If we got threaded loadhandler check for max worker thread count */ tcount = conf.getInt ("thread.count", 10); - lh = new ThreadLoad (handler, tcount); + boss = new Boss(tcount); } else { - lh = new SimpleLoad (); + boss = new Boss(1); } /* TODO: Don't loop forever, have a condition. Options: @@ -323,7 +345,7 @@ int main () { if (ret & SOCKET_POLL_READ) { /* Accept client and dispatch */ cl = s->Accept (); - lh->Dispatch (cl, handler); + boss->Dispatch(new SMTPHandler (qipc, cl)); } else if (ret & SOCKET_POLL_ERROR) { //error return -1; } else { //timeout -- 2.11.4.GIT