[complement-svn] SF.net SVN: complement: [1860] trunk/complement/explore/app/SMTP-tools/ smtp_serve
Status: Pre-Alpha
Brought to you by:
complement
From: <com...@us...> - 2008-04-25 11:26:38
|
Revision: 1860 http://complement.svn.sourceforge.net/complement/?rev=1860&view=rev Author: complement Date: 2008-04-25 04:26:22 -0700 (Fri, 25 Apr 2008) Log Message: ----------- clean code, no principal changes, but ones should be... Modified Paths: -------------- trunk/complement/explore/app/SMTP-tools/smtp_server_ut/SMTP_Server.cc trunk/complement/explore/app/SMTP-tools/smtp_server_ut/SMTP_Server.h trunk/complement/explore/app/SMTP-tools/smtp_server_ut/my_test.cc Added Paths: ----------- trunk/complement/explore/app/SMTP-tools/smtp_server_ut/aux/ trunk/complement/explore/app/SMTP-tools/smtp_server_ut/aux/test_0 Removed Paths: ------------- trunk/complement/explore/app/SMTP-tools/smtp_server_ut/test_0 Property Changed: ---------------- trunk/complement/explore/app/SMTP-tools/smtp_server_ut/ Property changes on: trunk/complement/explore/app/SMTP-tools/smtp_server_ut ___________________________________________________________________ Name: svn:ignore - obj test_* + obj Modified: trunk/complement/explore/app/SMTP-tools/smtp_server_ut/SMTP_Server.cc =================================================================== --- trunk/complement/explore/app/SMTP-tools/smtp_server_ut/SMTP_Server.cc 2008-04-25 09:17:02 UTC (rev 1859) +++ trunk/complement/explore/app/SMTP-tools/smtp_server_ut/SMTP_Server.cc 2008-04-25 11:26:22 UTC (rev 1860) @@ -1,4 +1,4 @@ -// -*- C++ -*- Time-stamp: <08/04/25 13:13:10 yeti> +// -*- C++ -*- Time-stamp: <08/04/25 14:45:59 yeti> #include <iostream> #include <string> @@ -9,150 +9,203 @@ using namespace std; -command setCom(const string& str) { - string Str(str); - for(int i = 0; Str[i] != '\0'; i++) - Str[i] = tolower(Str[i]); - if (Str == "helo") return helo; - else if (Str == "ehlo") return ehlo; - else if (Str == "mail") return mail; - else if (Str == "rcpt") return rcpt; - else if (Str == "data") return data; - else if (Str == "rset") return rset; - else if (Str == "vrfy") return vrfy; - else if (Str == "expn") return expn; - else if (Str == "help") return help; - else if (Str == "noop") return noop; - else if (Str == "quit") return quit; - else return none; +command setCom( const string& str ) +{ + string Str; + + for ( string::const_iterator i = str.begin(); i != str.end(); ++i ) { + Str += tolower( *i ); + } + + if ( Str == "helo" ) { + return helo; + } + + if ( Str == "ehlo" ) { + return ehlo; + } + + if ( Str == "mail" ) { + return mail; + } + + if ( Str == "rcpt" ) { + return rcpt; + } + + if ( Str == "data" ) { + return data; + } + + if (Str == "rset") { + return rset; + } + + if (Str == "vrfy") { + return vrfy; + } + + if (Str == "expn") { + return expn; + } + + if (Str == "help") { + return help; + } + + if (Str == "noop") { + return noop; + } + + if (Str == "quit") { + return quit; + } + + return none; } -void change(state& st, command& com, string& param, string& stout) { - switch (com) { - case helo: - if (st == connect) { - stout = "250 localhost Hello localhost, pleased to meet you\n"; - st = hello; - return; - } - else { - stout = "503 localhost Duplicate HELO/EHLO\n"; - return; - } - case ehlo: - if (st == connect) { - stout = "250-localhost Hello localhost, pleased to meet you\n"; - stout += "250-8BITMIME\n"; - stout += "250-SIZE 8000000\n"; - stout += "250 HELP\n"; - st = hello; - return; - } - else { - stout = "503 localhost Duplicate HELO/EHLO\n"; - return; - } - case mail: - switch (st) { - case connect: - stout = "503 Polite people say HELO first\n"; - return; - case hello: - stout = "250 " + param + "... Sender ok\n"; - st = sender; - return; - case sender: - stout = "503 Sender already specified\n"; - return; - case recipient: - stout = "503 Sender already specified\n"; - return; - } - case rcpt: - switch (st) { - case connect: - stout = "503 Need MAIL before RCPT\n"; - return; - case hello: - stout = "503 Need MAIL before RCPT\n"; - return; - case sender: - stout = "250 " + param + "... Recipient ok\n"; - st = recipient; - return; - case recipient: - stout = "250 " + param + "... Recipient ok\n"; - return; - } - case data: - switch (st) { - case connect: - stout = "503 Need MAIL command\n"; - return; - case hello: - stout = "503 Need MAIL command\n"; - return; - case sender: - stout = "503 Need RCPT (recipient)\n"; - return; - case recipient: - stout = "354 Enter mail, end with '.' on a line by itself\n"; - st = letter; - return; - } - case rset: - stout = "250 Reset state\n"; - if (st!=connect) st = hello; - return; - case vrfy: - stout = "502 Command not implemented\n"; - return; - case expn: - stout = "502 Command not implemented\n"; - return; - case help: - stout = "214-This is SMTP_Server\n"; - stout += "214 End of HELP info\n"; - return; - case noop: - stout = "250 OK\n"; - return; - case quit: - stout = "221 localhost closing connection\n"; - stout += "Connection closed by foreign host.\n"; - st = disconnect; - return; - case none: - stout = "500 Command unrecognized\n"; - return; - } +void change( state& st, const command& com, const string& param, string& stout ) +{ + switch ( com ) { + case helo: + if (st == connect) { + stout = "250 localhost Hello localhost, pleased to meet you\n"; + st = hello; + } else { + stout = "503 localhost Duplicate HELO/EHLO\n"; + } + return; + + case ehlo: + if (st == connect) { + stout = "250-localhost Hello localhost, pleased to meet you\n"; + stout += "250-8BITMIME\n"; + stout += "250-SIZE 8000000\n"; + stout += "250 HELP\n"; + st = hello; + } else { + stout = "503 localhost Duplicate HELO/EHLO\n"; + } + return; + + case mail: + switch (st) { + case connect: + stout = "503 Polite people say HELO first\n"; + return; + case hello: + stout = "250 " + param + "... Sender ok\n"; + st = sender; + return; + case sender: + stout = "503 Sender already specified\n"; + return; + case recipient: + stout = "503 Sender already specified\n"; + return; + } + break; + + case rcpt: + switch (st) { + case connect: + stout = "503 Need MAIL before RCPT\n"; + return; + case hello: + stout = "503 Need MAIL before RCPT\n"; + return; + case sender: + stout = "250 " + param + "... Recipient ok\n"; + st = recipient; + return; + case recipient: + stout = "250 " + param + "... Recipient ok\n"; + return; + } + break; + + case data: + switch (st) { + case connect: + stout = "503 Need MAIL command\n"; + return; + case hello: + stout = "503 Need MAIL command\n"; + return; + case sender: + stout = "503 Need RCPT (recipient)\n"; + return; + case recipient: + stout = "354 Enter mail, end with '.' on a line by itself\n"; + st = letter; + return; + } + break; + + case rset: + stout = "250 Reset state\n"; + if ( st != connect ) { + st = hello; + } + return; + + case vrfy: + stout = "502 Command not implemented\n"; + return; + + case expn: + stout = "502 Command not implemented\n"; + return; + + case help: + stout = "214-This is SMTP_Server\n"; + stout += "214 End of HELP info\n"; + return; + + case noop: + stout = "250 OK\n"; + return; + + case quit: + stout = "221 localhost closing connection\n"; + stout += "Connection closed by foreign host.\n"; + st = disconnect; + return; + + case none: + stout = "500 Command unrecognized\n"; + return; + } } -int ServerWork() { - state st = connect; - command com; - string param, message, stout; - while (st != disconnect) { - if (st != letter) { - string str; - cin >> str; - getline(cin, param); - com = setCom(str); - change(st, com, param, stout); - cout << stout; - } - else { - getline(cin, param); - if (param != ".") message = message + param + "\n"; - else { - st = hello; - cout << message; - message = ""; - } - }; - }; - return 0; +int ServerWork() +{ + state st = connect; + command com; + string param, message, stout; + + while ( st != disconnect ) { + if ( st != letter ) { + string str; + cin >> str; + getline(cin, param); + com = setCom(str); + change(st, com, param, stout); + cout << stout; + } else { + getline( cin, param ); + if ( param != "." ) { + message += param + "\n"; + } else { + st = hello; + cout << message; + message = ""; + } + } + } + + return 0; } } // namespace smtp Modified: trunk/complement/explore/app/SMTP-tools/smtp_server_ut/SMTP_Server.h =================================================================== --- trunk/complement/explore/app/SMTP-tools/smtp_server_ut/SMTP_Server.h 2008-04-25 09:17:02 UTC (rev 1859) +++ trunk/complement/explore/app/SMTP-tools/smtp_server_ut/SMTP_Server.h 2008-04-25 11:26:22 UTC (rev 1860) @@ -1,4 +1,4 @@ -// -*- C++ -*- Time-stamp: <08/04/25 13:11:53 yeti> +// -*- C++ -*- Time-stamp: <08/04/25 14:35:17 yeti> #ifndef __SMPT_SERVER_H #define __SMPT_SERVER_H @@ -37,7 +37,7 @@ command setCom( const std::string& str ); -void change( state& st, command& com, std::string& param, std::string& stout ); +void change( state& st, const command& com, const std::string& param, std::string& stout ); } // namespace smtp Copied: trunk/complement/explore/app/SMTP-tools/smtp_server_ut/aux/test_0 (from rev 1857, trunk/complement/explore/app/SMTP-tools/smtp_server_ut/test_0) =================================================================== --- trunk/complement/explore/app/SMTP-tools/smtp_server_ut/aux/test_0 (rev 0) +++ trunk/complement/explore/app/SMTP-tools/smtp_server_ut/aux/test_0 2008-04-25 11:26:22 UTC (rev 1860) @@ -0,0 +1,4 @@ +221 +helo ya.ru +help +quit Modified: trunk/complement/explore/app/SMTP-tools/smtp_server_ut/my_test.cc =================================================================== --- trunk/complement/explore/app/SMTP-tools/smtp_server_ut/my_test.cc 2008-04-25 09:17:02 UTC (rev 1859) +++ trunk/complement/explore/app/SMTP-tools/smtp_server_ut/my_test.cc 2008-04-25 11:26:22 UTC (rev 1860) @@ -1,4 +1,4 @@ -// -*- C++ -*- Time-stamp: <08/04/25 13:14:01 yeti> +// -*- C++ -*- Time-stamp: <08/04/25 15:23:35 yeti> #include "my_test.h" @@ -13,12 +13,8 @@ #include <iostream> #include <fstream> -#include <semaphore.h> +#include <sstream> -#include <sys/wait.h> -#include <sys/ipc.h> -#include <sys/shm.h> - #include <unistd.h> using namespace std; @@ -32,14 +28,14 @@ // recipient // letter -const string set_state [6] = { - "quit\n", - "", - "helo mail.ru\n", - "helo mail.ru\nmail from:se...@ma...\n", - "helo mail.ru\nmail from:se...@ma...\nrcpt to:cl...@ma...\n", - "helo mail.ru\nmail from:se...@ma...\nrcpt to:cl...@ma...\ndata\n" - }; +const string set_state[] = { + "quit\n", + "", + "helo mail.ru\n", + "helo mail.ru\nmail from:se...@ma...\n", + "helo mail.ru\nmail from:se...@ma...\nrcpt to:cl...@ma...\n", + "helo mail.ru\nmail from:se...@ma...\nrcpt to:cl...@ma...\ndata\n" +}; //possible commands // helo @@ -56,30 +52,30 @@ // none -const string set_command [12] = { - "helo mail.ru", - "ehlo mail.ru", - "mail from:se...@ma...", - "rcpt to:rec...@ma...", - "data", - "rset", - "vrfy se...@ma...", - "expn se...@ma...", - "help", - "noop", - "quit", - "none" - }; +const string set_command[] = { + "helo mail.ru", + "ehlo mail.ru", + "mail from:se...@ma...", + "rcpt to:rec...@ma...", + "data", + "rset", + "vrfy se...@ma...", + "expn se...@ma...", + "help", + "noop", + "quit", + "none" +}; -const int set_result [6][12] = { - {999, 999, 999, 999, 999, 999, 999, 999, 999, 999, 999, 999}, -// {221, 221, 221, 221, 221, 221, 221, 221, 221, 221, 221, 221}, - {250, 250, 503, 503, 503, 250, 502, 502, 214, 250, 221, 500}, - {503, 503, 250, 503, 503, 250, 502, 502, 214, 250, 221, 500}, - {503, 503, 503, 250, 503, 250, 502, 502, 214, 250, 221, 500}, - {503, 503, 503, 250, 354, 250, 502, 502, 214, 250, 221, 500}, - {999, 999, 999, 999, 999, 999, 999, 999, 999, 999, 999, 999} - }; +const int set_result[][sizeof(set_command)/sizeof(set_command[0])] = { + {999, 999, 999, 999, 999, 999, 999, 999, 999, 999, 999, 999}, + // {221, 221, 221, 221, 221, 221, 221, 221, 221, 221, 221, 221}, + {250, 250, 503, 503, 503, 250, 502, 502, 214, 250, 221, 500}, + {503, 503, 250, 503, 503, 250, 502, 502, 214, 250, 221, 500}, + {503, 503, 503, 250, 503, 250, 502, 502, 214, 250, 221, 500}, + {503, 503, 503, 250, 354, 250, 502, 502, 214, 250, 221, 500}, + {999, 999, 999, 999, 999, 999, 999, 999, 999, 999, 999, 999} +}; const int buf_size = 1024; const int com_length = 4; @@ -91,75 +87,83 @@ void server_thread() { - char buffer[buf_size]; - state st = connect; - command com; - string param, message, stout; - while (st != disconnect) { - if (st != letter) { - if (read (fd2[0], buffer, sizeof(buffer)) < 1) fprintf(stderr,"Reading error\n"); - else { - cerr << ">> "<< buffer << endl; - string str (buffer); - param.assign (str, com_length, str.size()); - str.erase (com_length, str.size() - com_length + 1); - com = setCom (str); - change (st, com, param, stout); - - strcpy (buffer, stout.c_str()); - write (fd1[1], buffer, sizeof(buffer)); - } - } - else { - read (fd2[0], buffer, sizeof(buffer)); - param.assign (buffer, 0, sizeof(buffer)); - if (param != ".") message = message + param + "\n"; - else { - st = hello; -// cout << message; - message = ""; - } - }; - }; - active = false; -// cerr << "Server's loop may be here" << endl; + char buffer[buf_size]; + state st = connect; + command com; + string param, message, stout; + + while ( st != disconnect ) { + if ( st != letter ) { + if ( read( fd2[0], buffer, sizeof(buffer) ) < 1 ) { // <-- error (1) + cerr << "Reading error\n"; + } else { + cerr << ">> " << buffer << endl; + string str( buffer ); // <-- error (1) + param.assign( str, com_length, str.size() ); + str.erase( com_length, str.size() - com_length + 1 ); + com = setCom( str ); + change( st, com, param, stout ); + write( fd1[1], stout.data(), stout.length() ); + } + } else { + read( fd2[0], buffer, sizeof(buffer) ); // <-- error (2) + param.assign (buffer, 0, sizeof(buffer) ); // <-- error (2) + if ( param != "." ) { + message += param + "\n"; + } else { + st = hello; + // cout << message; + message = ""; + } + } + } + + active = false; + // cerr << "Server's loop may be here" << endl; } int EXAM_IMPL(my_test::thread_call) { - pipe (fd1); - pipe (fd2); - active = false; - for (int i = 0; i <= test_num; i++) { - char r_buffer[buf_size], w_buffer[buf_size]; - std::tr2::basic_thread<0,0> t( server_thread ); - active = true; + pipe (fd1); + pipe (fd2); + + active = false; - ostringstream st; - st << "test_" << i; - ifstream in (st.str().c_str()); - - string expected; - getline(in, expected); - - while (!in.eof()){ - string s; - getline(in, s); - if (s != "") { - strcpy (w_buffer, s.c_str()); - write (fd2[1], w_buffer, sizeof(w_buffer)); - read (fd1[0], r_buffer, sizeof(r_buffer)); - cerr << "<< " << r_buffer; - } - } - in.close(); - t.join(); + string s; + string expected; - string result (r_buffer); - if (expected.compare (0, 3, result, 0, 3) != 0) - cerr << expected << "!=" << result << " at " << i << endl; + for ( int i = 0; i <= test_num; ++i ) { + char r_buffer[buf_size], w_buffer[buf_size]; + + std::tr2::basic_thread<0,0> t( server_thread ); + active = true; + + ostringstream st; + st << "aux/test_" << i; + + ifstream in( st.str().c_str() ); + + getline( in, expected ); + + while ( in.good() ){ + getline( in, s ); + if ( !s.empty() ) { + write( fd2[1], s.data(), s.length() ); + read( fd1[0], r_buffer, sizeof(r_buffer) ); // <-- error (3) + cerr << "<< " << r_buffer; // <-- error (3) + } + } + + in.close(); + + t.join(); + + string result(r_buffer); + if ( expected.compare(0, 3, result, 0, 3) != 0 ) { + cerr << expected << "!=" << result << " at " << i << endl; + } - EXAM_CHECK ((expected.compare (0, 3, result, 0, 3) == 0)); + EXAM_CHECK( expected.compare(0, 3, result, 0, 3) == 0 ); /* strcpy (w_buffer, "help"); write (fd2[1], w_buffer, sizeof(w_buffer)); @@ -173,33 +177,32 @@ cerr << "Client's text may be here" << endl; */ - } - // EXAM_CHECK( val == 1 ); - // std::tr2::basic_thread<0,0> t2( thread_func_int, 2 ); - // t2.join(); - // EXAM_CHECK( val == 2 ); - // val = 0; + } + // EXAM_CHECK( val == 1 ); + // std::tr2::basic_thread<0,0> t2( thread_func_int, 2 ); + // t2.join(); + // EXAM_CHECK( val == 2 ); + // val = 0; - return EXAM_RESULT; + return EXAM_RESULT; } int EXAM_IMPL(my_test::test_gen) { - int num = 1; - for (int i = 0; i < 6; i++) { - for (int j = 0; j < 12; j++) { - ostringstream st; - st << "test_" << num; -// st << "test_" << i << "_" << j; - ofstream os (st.str().c_str()); - os << set_result [i][j] << endl; - os << set_state [i] << set_command [j]; - os.close(); - num++; - } + int num = 1; + + for ( int i = 0; i < sizeof(set_state)/sizeof(set_state[0]); ++i ) { + for ( int j = 0; j < sizeof(set_command)/sizeof(set_command[0]); ++j ) { + ostringstream st; + st << "aux/test_" << num++; + + ofstream os( st.str().c_str() ); + + os << set_result[i][j] << "\n" + << set_state[i] << set_command[j]; } - return EXAM_RESULT; + } + + return EXAM_RESULT; } - - Deleted: trunk/complement/explore/app/SMTP-tools/smtp_server_ut/test_0 =================================================================== --- trunk/complement/explore/app/SMTP-tools/smtp_server_ut/test_0 2008-04-25 09:17:02 UTC (rev 1859) +++ trunk/complement/explore/app/SMTP-tools/smtp_server_ut/test_0 2008-04-25 11:26:22 UTC (rev 1860) @@ -1,4 +0,0 @@ -221 -helo ya.ru -help -quit This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |