[complement-svn] SF.net SVN: complement: [1873] trunk/complement/explore/app/SMTP-tools/ smtp_serve
Status: Pre-Alpha
Brought to you by:
complement
From: <oke...@us...> - 2008-05-06 09:05:21
|
Revision: 1873 http://complement.svn.sourceforge.net/complement/?rev=1873&view=rev Author: okechina Date: 2008-05-06 02:05:14 -0700 (Tue, 06 May 2008) Log Message: ----------- Client requests and server answers are written and read using streams only. Client can get multi string answer from server. Server terminates after client's disconnection. Server does not return \n at the end of answer. Modified Paths: -------------- trunk/complement/explore/app/SMTP-tools/smtp_server_ut/SMTP_Server.cc trunk/complement/explore/app/SMTP-tools/smtp_server_ut/my_test.cc 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-05-05 14:18:45 UTC (rev 1872) +++ trunk/complement/explore/app/SMTP-tools/smtp_server_ut/SMTP_Server.cc 2008-05-06 09:05:14 UTC (rev 1873) @@ -69,10 +69,10 @@ switch ( com ) { case helo: if (st == connect) { - stout = "250 localhost Hello localhost, pleased to meet you\n"; + stout = "250 localhost Hello localhost, pleased to meet you"; st = hello; } else { - stout = "503 localhost Duplicate HELO/EHLO\n"; + stout = "503 localhost Duplicate HELO/EHLO"; } return; @@ -81,27 +81,27 @@ stout = "250-localhost Hello localhost, pleased to meet you\n"; stout += "250-8BITMIME\n"; stout += "250-SIZE 8000000\n"; - stout += "250 HELP\n"; + stout += "250 HELP"; st = hello; } else { - stout = "503 localhost Duplicate HELO/EHLO\n"; + stout = "503 localhost Duplicate HELO/EHLO"; } return; case mail: switch (st) { case connect: - stout = "503 Polite people say HELO first\n"; + stout = "503 Polite people say HELO first"; return; case hello: - stout = "250 " + param + "... Sender ok\n"; + stout = "250 " + param + "... Sender ok"; st = sender; return; case sender: - stout = "503 Sender already specified\n"; + stout = "503 Sender already specified"; return; case recipient: - stout = "503 Sender already specified\n"; + stout = "503 Sender already specified"; return; } break; @@ -109,17 +109,17 @@ case rcpt: switch (st) { case connect: - stout = "503 Need MAIL before RCPT\n"; + stout = "503 Need MAIL before RCPT"; return; case hello: - stout = "503 Need MAIL before RCPT\n"; + stout = "503 Need MAIL before RCPT"; return; case sender: - stout = "250 " + param + "... Recipient ok\n"; + stout = "250 " + param + "... Recipient ok"; st = recipient; return; case recipient: - stout = "250 " + param + "... Recipient ok\n"; + stout = "250 " + param + "... Recipient ok"; return; } break; @@ -127,53 +127,53 @@ case data: switch (st) { case connect: - stout = "503 Need MAIL command\n"; + stout = "503 Need MAIL command"; return; case hello: - stout = "503 Need MAIL command\n"; + stout = "503 Need MAIL command"; return; case sender: - stout = "503 Need RCPT (recipient)\n"; + stout = "503 Need RCPT (recipient)"; return; case recipient: - stout = "354 Enter mail, end with '.' on a line by itself\n"; + stout = "354 Enter mail, end with '.' on a line by itself"; st = letter; return; } break; case rset: - stout = "250 Reset state\n"; + stout = "250 Reset state"; if ( st != connect ) { st = hello; } return; case vrfy: - stout = "502 Command not implemented\n"; + stout = "502 Command not implemented"; return; case expn: - stout = "502 Command not implemented\n"; + stout = "502 Command not implemented"; return; case help: stout = "214-This is SMTP_Server\n"; - stout += "214 End of HELP info\n"; + stout += "214 End of HELP info"; return; case noop: - stout = "250 OK\n"; + stout = "250 OK"; return; case quit: - stout = "221 localhost closing connection\n"; - stout += "Connection closed by foreign host.\n"; + stout = "221 localhost closing connection; "; + stout += "Connection closed by foreign host."; st = disconnect; return; case none: - stout = "500 Command unrecognized\n"; + stout = "500 Command unrecognized"; return; } } 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-05-05 14:18:45 UTC (rev 1872) +++ trunk/complement/explore/app/SMTP-tools/smtp_server_ut/my_test.cc 2008-05-06 09:05:14 UTC (rev 1873) @@ -2,7 +2,6 @@ #include "my_test.h" - #include "SMTP_Server.h" #include <mt/thread> @@ -39,22 +38,7 @@ "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 -// ehlo -// mail -// rcpt -// data -// rset -// vrfy -// expn -// help -// noop -// quit -// none - - + const string set_command[] = { "helo mail.ru", "ehlo mail.ru", @@ -71,31 +55,19 @@ }; 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; -const int test_num = 0; - static int fd1[2], fd2[2]; typedef list<string> tests_container_type; static tests_container_type tests; -bool active; - void server_thread() { - char buffer[buf_size]; - state st = connect; - command com; #ifdef STLPORT ifstream in( fd2[0] ); @@ -107,100 +79,41 @@ __gnu_cxx::stdio_filebuf<char> _out_buf( fd1[1], std::ios_base::out ); basic_ostream<char> out( &_out_buf ); #endif - - + + state st = connect; + command com; string param, message, stout; + while ( st != disconnect ) { if ( st != letter ) { string str; in >> str; - getline(in, param); - com = setCom(str); - change(st, com, param, stout); - write( fd1[1], stout.data(), stout.length() ); -// out << stout; + if ( str.empty() ) + break; + getline( in, param ); + com = setCom( str ); + change( st, com, param, stout ); + out << stout << endl; } else { getline( in, param ); + if ( param.empty() ) + break; if ( param != "." ) { message += param + "\n"; } else { st = hello; -// out << message; +// cerr << message; message = ""; } } } - - -/* while ( st != disconnect ) { - if ( st != letter ) { - bool full = false; - stringstream st_stream; - while (!full) { - int n = read( fd2[0], buffer, sizeof(buffer) ); - if ( n < 0 ) { - cerr << "Reading error\n"; - break; - } else if ( n == 0 ) { - break; - } else { - buffer[n] = '\0'; - cerr << ">> " << buffer; - st_stream << buffer; - full = full || (buffer[n-1] == '\n'); - } - }; - if (full) { - while (st_stream.good()) { - string str; - string param, stout; - st_stream >> str; - if (str != "") { - getline (st_stream, param); -// 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 { //error appeared while reading a command - break; - }; - } else { - string message, s; - int n = read( fd2[0], buffer, sizeof(buffer) ); - if ( n < 0 ) { - cerr << "Reading error\n"; - break; - } else if ( n == 0 ) { - cerr << "Empty\n"; - break; - } else { - buffer[n] = '\0'; - s.assign (buffer, 0, n); - if ( s != "." ) { - message += s + "\n"; - } else { - st = hello; - // cout << message; - message = ""; - } - } - } - } -*/ close(fd2[0]); close(fd1[1]); - active = false; - // cerr << "Server's loop may be here" << endl; } int EXAM_IMPL(my_test::thread_call) { - active = false; - string s; string expected; string result; @@ -219,74 +132,34 @@ __gnu_cxx::stdio_filebuf<char> _out_buf( fd2[1], std::ios_base::out ); basic_ostream<char> out( &_out_buf ); #endif - char r_buffer[buf_size], w_buffer[buf_size]; std::tr2::basic_thread<0,0> t( server_thread ); - active = true; istringstream in_tst( *i ); - getline( in_tst, expected ); while ( in_tst.good() ){ getline( in_tst, s ); if ( !s.empty() ) { - out << s << endl; // write( fd2[1], s.data(), s.length() ); - // write( fd2[1], "\n", 1 ); -/* int n = read( fd1[0], r_buffer, sizeof(r_buffer) ); - if ( n < 0 ) { - cerr << "Reading error\n"; - break; - } else if ( n == 0 ) { - cerr << "Empty string\n"; - break; - } else { - r_buffer[n] = '\0'; - cerr << "<< " << r_buffer; - } - out << s << "\n"; -*/ cerr << s << "\n"; -// do { + out << s << endl; +// cerr << s << endl; + do { getline (in, result); - cerr << result; -// } while (!result.empty()); -// if (result.empty()) cerr << "Empty"; +// cerr << result << endl; + } while ( result[3] == '-' ); } } -// string str ("helo mail.ru\nmail from:se...@ma...\nrcpt to:cl...@ma...\nquit\n"); -// write( fd2[1], str.data(), str.length() ); - close(fd2[1]); close(fd1[0]); - 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 ); } -/* strcpy (w_buffer, "help"); - write (fd2[1], w_buffer, sizeof(w_buffer)); - read (fd1[0], r_buffer, sizeof(r_buffer)); - cerr << r_buffer; - - strcpy (w_buffer, "quit"); - write (fd2[1], w_buffer, sizeof(w_buffer)); - read (fd1[0], r_buffer, sizeof(r_buffer)); - cerr << r_buffer; - - 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; - return EXAM_RESULT; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |