Thread: [complement-svn] SF.net SVN: complement: [1855] trunk/complement/explore/app/SMTP-tools/ smtp_serve
Status: Pre-Alpha
Brought to you by:
complement
From: <oke...@us...> - 2008-04-24 11:58:28
|
Revision: 1855 http://complement.svn.sourceforge.net/complement/?rev=1855&view=rev Author: okechina Date: 2008-04-24 04:58:15 -0700 (Thu, 24 Apr 2008) Log Message: ----------- Modified Paths: -------------- trunk/complement/explore/app/SMTP-tools/smtp_server_ut/my_test.cc trunk/complement/explore/app/SMTP-tools/smtp_server_ut/my_test.h trunk/complement/explore/app/SMTP-tools/smtp_server_ut/my_test_suite.cc 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-23 19:22:40 UTC (rev 1854) +++ trunk/complement/explore/app/SMTP-tools/smtp_server_ut/my_test.cc 2008-04-24 11:58:15 UTC (rev 1855) @@ -1,6 +1,8 @@ // -*- C++ -*- Time-stamp: <08/03/26 01:53:46 ptr> #include "my_test.h" + + #include "SMTP_Server.h" #include <mt/thread> @@ -10,6 +12,7 @@ #include <typeinfo> #include <iostream> +#include <fstream> #include <semaphore.h> #include <sys/wait.h> @@ -20,72 +23,156 @@ using namespace std; -static int fd1[2], fd2[2]; +//possible states +// disconnect +// connect +// hello +// sender +// 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" + }; + +//possible commands +// helo +// ehlo +// mail +// rcpt +// data +// rset +// vrfy +// expn +// help +// noop +// quit +// 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 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 buf_size = 1024; const int com_length = 4; +const int test_num = 0; +static int fd1[2], fd2[2]; + +bool active; + 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 fprintf (stderr,"%s\n",buffer); - - 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); + 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)); + strcpy (buffer, stout.c_str()); + write (fd1[1], buffer, sizeof(buffer)); + } } - else { + 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 = ""; - } + message = ""; + } }; }; - cerr << "Server's loop may be here" << endl; + active = false; +// cerr << "Server's loop may be here" << endl; } int EXAM_IMPL(my_test::thread_call) { - char r_buffer[buf_size], w_buffer[buf_size]; pipe (fd1); pipe (fd2); - std::tr2::basic_thread<0,0> t( server_thread ); + 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; - strcpy (w_buffer, "ehlo"); - write (fd2[1], w_buffer, sizeof(w_buffer)); - read (fd1[0], r_buffer, sizeof(r_buffer)); - cerr << r_buffer; + 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(); - strcpy (w_buffer, "help"); - write (fd2[1], w_buffer, sizeof(w_buffer)); - read (fd1[0], r_buffer, sizeof(r_buffer)); - cerr << r_buffer; + 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; + 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; - t.join(); - + 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(); @@ -95,3 +182,23 @@ 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++; + } + } + return EXAM_RESULT; +} + + Modified: trunk/complement/explore/app/SMTP-tools/smtp_server_ut/my_test.h =================================================================== --- trunk/complement/explore/app/SMTP-tools/smtp_server_ut/my_test.h 2008-04-23 19:22:40 UTC (rev 1854) +++ trunk/complement/explore/app/SMTP-tools/smtp_server_ut/my_test.h 2008-04-24 11:58:15 UTC (rev 1855) @@ -13,6 +13,7 @@ { public: int EXAM_DECL(thread_call); + int EXAM_DECL(test_gen); }; #endif // __MY_TEST_H Modified: trunk/complement/explore/app/SMTP-tools/smtp_server_ut/my_test_suite.cc =================================================================== --- trunk/complement/explore/app/SMTP-tools/smtp_server_ut/my_test_suite.cc 2008-04-23 19:22:40 UTC (rev 1854) +++ trunk/complement/explore/app/SMTP-tools/smtp_server_ut/my_test_suite.cc 2008-04-24 11:58:15 UTC (rev 1855) @@ -10,6 +10,7 @@ exam::test_suite t( "my test" ); my_test test; + t.add( &my_test::test_gen, test, "my_test::test_gen" ); t.add( &my_test::thread_call, test, "my_test::thread_call" ); return t.girdle(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <com...@us...> - 2008-04-25 08:55:17
|
Revision: 1858 http://complement.svn.sourceforge.net/complement/?rev=1858&view=rev Author: complement Date: 2008-04-25 01:55:14 -0700 (Fri, 25 Apr 2008) Log Message: ----------- change EOL style to native 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 Property Changed: ---------------- trunk/complement/explore/app/SMTP-tools/smtp_server_ut/ trunk/complement/explore/app/SMTP-tools/smtp_server_ut/Makefile trunk/complement/explore/app/SMTP-tools/smtp_server_ut/Makefile.inc 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 trunk/complement/explore/app/SMTP-tools/smtp_server_ut/my_test.h trunk/complement/explore/app/SMTP-tools/smtp_server_ut/my_test_suite.h Property changes on: trunk/complement/explore/app/SMTP-tools/smtp_server_ut ___________________________________________________________________ Name: svn:ignore - obj + obj test_* Property changes on: trunk/complement/explore/app/SMTP-tools/smtp_server_ut/Makefile ___________________________________________________________________ Name: svn:eol-style + native Property changes on: trunk/complement/explore/app/SMTP-tools/smtp_server_ut/Makefile.inc ___________________________________________________________________ Name: svn:eol-style + native 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 08:53:58 UTC (rev 1857) +++ trunk/complement/explore/app/SMTP-tools/smtp_server_ut/SMTP_Server.cc 2008-04-25 08:55:14 UTC (rev 1858) @@ -1,151 +1,151 @@ -#include <iostream> -#include <string> +#include <iostream> +#include <string> #include "SMTP_Server.h" - -using namespace std; - + +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; -} - -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"; + 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; +} + +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; - } -} + 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; + } +} - + int ServerWork() { - state st = connect; + state st = connect; command com; - string param, message, stout; - while (st != disconnect) { - if (st != letter) { - string str; + string param, message, stout; + while (st != disconnect) { + if (st != letter) { + string str; cin >> str; getline(cin, param); - com = setCom(str); + 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; -} + cout << stout; + } + else { + getline(cin, param); + if (param != ".") message = message + param + "\n"; + else { + st = hello; + cout << message; + message = ""; + } + }; + }; + return 0; +} Property changes on: trunk/complement/explore/app/SMTP-tools/smtp_server_ut/SMTP_Server.cc ___________________________________________________________________ Name: svn:eol-style + native 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 08:53:58 UTC (rev 1857) +++ trunk/complement/explore/app/SMTP-tools/smtp_server_ut/SMTP_Server.h 2008-04-25 08:55:14 UTC (rev 1858) @@ -7,7 +7,7 @@ sender, recipient, letter - }; + }; enum command { helo, ehlo, @@ -21,7 +21,7 @@ noop, quit, none - }; + }; int ServerWork(); Property changes on: trunk/complement/explore/app/SMTP-tools/smtp_server_ut/SMTP_Server.h ___________________________________________________________________ Name: svn:eol-style + native 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 08:53:58 UTC (rev 1857) +++ trunk/complement/explore/app/SMTP-tools/smtp_server_ut/my_test.cc 2008-04-25 08:55:14 UTC (rev 1858) @@ -1,4 +1,4 @@ -// -*- C++ -*- Time-stamp: <08/03/26 01:53:46 ptr> +// -*- C++ -*- Time-stamp: <08/04/25 12:51:09 yeti> #include "my_test.h" @@ -91,10 +91,10 @@ void server_thread() { char buffer[buf_size]; - state st = connect; + state st = connect; command com; string param, message, stout; - while (st != disconnect) { + while (st != disconnect) { if (st != letter) { if (read (fd2[0], buffer, sizeof(buffer)) < 1) fprintf(stderr,"Reading error\n"); else { @@ -102,23 +102,23 @@ string str (buffer); param.assign (str, com_length, str.size()); str.erase (com_length, str.size() - com_length + 1); - com = setCom (str); + 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; + if (param != ".") message = message + param + "\n"; + else { + st = hello; +// cout << message; message = ""; } - }; + }; }; active = false; // cerr << "Server's loop may be here" << endl; Property changes on: trunk/complement/explore/app/SMTP-tools/smtp_server_ut/my_test.cc ___________________________________________________________________ Name: svn:eol-style + native Property changes on: trunk/complement/explore/app/SMTP-tools/smtp_server_ut/my_test.h ___________________________________________________________________ Name: svn:eol-style + native Property changes on: trunk/complement/explore/app/SMTP-tools/smtp_server_ut/my_test_suite.h ___________________________________________________________________ Name: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <com...@us...> - 2008-04-25 09:17:07
|
Revision: 1859 http://complement.svn.sourceforge.net/complement/?rev=1859&view=rev Author: complement Date: 2008-04-25 02:17:02 -0700 (Fri, 25 Apr 2008) Log Message: ----------- include guard added; server functions/data structs moved to smtp namespace 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 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 08:55:14 UTC (rev 1858) +++ trunk/complement/explore/app/SMTP-tools/smtp_server_ut/SMTP_Server.cc 2008-04-25 09:17:02 UTC (rev 1859) @@ -1,7 +1,12 @@ +// -*- C++ -*- Time-stamp: <08/04/25 13:13:10 yeti> + #include <iostream> #include <string> + #include "SMTP_Server.h" +namespace smtp { + using namespace std; command setCom(const string& str) { @@ -149,3 +154,5 @@ }; 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 08:55:14 UTC (rev 1858) +++ trunk/complement/explore/app/SMTP-tools/smtp_server_ut/SMTP_Server.h 2008-04-25 09:17:02 UTC (rev 1859) @@ -1,32 +1,44 @@ -using namespace std; +// -*- C++ -*- Time-stamp: <08/04/25 13:11:53 yeti> -enum state { - disconnect, - connect, - hello, - sender, - recipient, - letter - }; -enum command { - helo, - ehlo, - mail, - rcpt, - data, - rset, - vrfy, - expn, - help, - noop, - quit, - none - }; +#ifndef __SMPT_SERVER_H +#define __SMPT_SERVER_H +#include <string> +namespace smtp { + +enum state +{ + disconnect, + connect, + hello, + sender, + recipient, + letter +}; + +enum command +{ + helo, + ehlo, + mail, + rcpt, + data, + rset, + vrfy, + expn, + help, + noop, + quit, + none +}; + int ServerWork(); -command setCom(const string& str); +command setCom( const std::string& str ); -void change(state& st, command& com, string& param, string& stout); +void change( state& st, command& com, std::string& param, std::string& stout ); +} // namespace smtp + +#endif // __SMPT_SERVER_H 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 08:55:14 UTC (rev 1858) +++ trunk/complement/explore/app/SMTP-tools/smtp_server_ut/my_test.cc 2008-04-25 09:17:02 UTC (rev 1859) @@ -1,4 +1,4 @@ -// -*- C++ -*- Time-stamp: <08/04/25 12:51:09 yeti> +// -*- C++ -*- Time-stamp: <08/04/25 13:14:01 yeti> #include "my_test.h" @@ -22,6 +22,7 @@ #include <unistd.h> using namespace std; +using namespace smtp; //possible states // disconnect This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
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. |
From: <com...@us...> - 2008-05-03 05:39:02
|
Revision: 1870 http://complement.svn.sourceforge.net/complement/?rev=1870&view=rev Author: complement Date: 2008-05-02 22:38:59 -0700 (Fri, 02 May 2008) Log Message: ----------- Use container with strings for tests instead of files; Sample: how to use istream instead of raw io; not finished, just suggestion. Modified Paths: -------------- trunk/complement/explore/app/SMTP-tools/smtp_server_ut/my_test.cc Removed Paths: ------------- trunk/complement/explore/app/SMTP-tools/smtp_server_ut/aux/ 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-01 11:24:15 UTC (rev 1869) +++ trunk/complement/explore/app/SMTP-tools/smtp_server_ut/my_test.cc 2008-05-03 05:38:59 UTC (rev 1870) @@ -1,4 +1,4 @@ -// -*- C++ -*- Time-stamp: <08/04/25 15:23:35 yeti> +// -*- C++ -*- Time-stamp: <08/05/03 09:31:01 ptr> #include "my_test.h" @@ -13,6 +13,9 @@ #include <iostream> #include <fstream> +#ifndef STLPORT +# include <ext/stdio_filebuf.h> +#endif #include <sstream> #include <unistd.h> @@ -83,6 +86,9 @@ static int fd1[2], fd2[2]; +typedef list<string> tests_container_type; +static tests_container_type tests; + bool active; void server_thread() @@ -90,6 +96,18 @@ char buffer[buf_size]; state st = connect; command com; + +#ifdef STLPORT + ifstream in( fd2[0] ); + ofstream out( fd1[1] ); +#elif defined( __GNUC__ ) + __gnu_cxx::stdio_filebuf _in_buf( fd2[0], std::ios_base::in ); + basic_istream<char> in( &_in_buf ); + + __gnu_cxx::stdio_filebuf _out_buf( fd1[1], std::ios_base::out ); + basic_istream<char> in( &_out_buf ); +#endif + while ( st != disconnect ) { if ( st != letter ) { bool full = false; @@ -157,29 +175,37 @@ } int EXAM_IMPL(my_test::thread_call) -{ - pipe (fd1); - pipe (fd2); - +{ active = false; string s; string expected; - for ( int i = 0; i <= test_num; ++i ) { + + for ( tests_container_type::const_iterator i = tests.begin(); i != tests.end(); ++i ) { + pipe( fd1 ); + pipe( fd2 ); + +#ifdef STLPORT + ifstream in( fd1[0] ); + ofstream out( fd2[1] ); +#elif defined( __GNUC__ ) + __gnu_cxx::stdio_filebuf _in_buf( fd1[0], std::ios_base::in ); + basic_istream<char> in( &_in_buf ); + + __gnu_cxx::stdio_filebuf _out_buf( fd2[1], std::ios_base::out ); + basic_istream<char> in( &_out_buf ); +#endif 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; + istringstream in_tst( *i ); - ifstream in( st.str().c_str() ); + getline( in_tst, expected ); - getline( in, expected ); - while ( in.good() ){ - getline( in, s ); + getline( in_tst, s ); if ( !s.empty() ) { write( fd2[1], s.data(), s.length() ); write( fd2[1], "\n", 1 ); @@ -197,17 +223,17 @@ } } -// string str ("helo mail.ru\nmail from:se...@ma...\nrcpt to:cl...@ma...\nquit\n"); + 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]); - in.close(); + t.join(); string result(r_buffer); if ( expected.compare(0, 3, result, 0, 3) != 0 ) { - cerr << expected << "!=" << result << " at " << i << endl; + cerr << expected << "!=" << result << " at " << *i << endl; } EXAM_CHECK( expected.compare(0, 3, result, 0, 3) == 0 ); @@ -237,17 +263,15 @@ int EXAM_IMPL(my_test::test_gen) { - int num = 1; + tests.push_back( string( "221\nhelo ya.ru\nhelp\nquit\n" ) ); // aka test_0 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++; + stringstream os; - ofstream os( st.str().c_str() ); + os << set_result[i][j] << "\n" << set_state[i] << set_command[j]; - os << set_result[i][j] << "\n" - << set_state[i] << set_command[j]; + tests.push_back( os.str() ); } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
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. |
From: <oke...@us...> - 2008-05-15 08:36:12
|
Revision: 1875 http://complement.svn.sourceforge.net/complement/?rev=1875&view=rev Author: okechina Date: 2008-05-15 01:35:35 -0700 (Thu, 15 May 2008) Log Message: ----------- A new class "session" is created. All the server methods are collected in the class. Server checks if there is data available. 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 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-14 08:53:23 UTC (rev 1874) +++ trunk/complement/explore/app/SMTP-tools/smtp_server_ut/SMTP_Server.cc 2008-05-15 08:35:35 UTC (rev 1875) @@ -9,8 +9,15 @@ using namespace std; -command setCom( const string& str ) +session::session ( std::basic_istream<char>& is, std::basic_ostream<char>& os ) : + in(is), + out(os) { + st = connect; +}; + +command session::setCom( const string& str ) +{ string Str; for ( string::const_iterator i = str.begin(); i != str.end(); ++i ) { @@ -64,8 +71,9 @@ return none; } -void change( state& st, const command& com, const string& param, string& stout ) +void session::changeSt( const string& str, const string& param, std::string& stout ) { + com = session::setCom( str ); switch ( com ) { case helo: if (st == connect) { @@ -178,34 +186,35 @@ } } - -int ServerWork() +int session::checkData () { - 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; + string str, param, stout; + if ( st != letter ) { + in >> str; + if ( str.empty() ) + return -1; + getline( in, param ); + changeSt( str, param, stout ); + out << stout << endl; + } else { + getline( in, param ); + if ( param.empty() ) + return -1; + if ( param != "." ) { + message += param + "\n"; } else { - getline( cin, param ); - if ( param != "." ) { - message += param + "\n"; - } else { - st = hello; - cout << message; - message = ""; - } + st = hello; +// cerr << message; + message = ""; } } - return 0; } + +state session::getState () +{ + return st; +} + } // 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-05-14 08:53:23 UTC (rev 1874) +++ trunk/complement/explore/app/SMTP-tools/smtp_server_ut/SMTP_Server.h 2008-05-15 08:35:35 UTC (rev 1875) @@ -4,6 +4,7 @@ #define __SMPT_SERVER_H #include <string> +#include <iostream> namespace smtp { @@ -33,12 +34,28 @@ none }; -int ServerWork(); +class session +{ + private: + state st; + command com; + std::string message; -command setCom( const std::string& str ); + std::basic_istream<char>& in; + std::basic_ostream<char>& out; -void change( state& st, const command& com, const std::string& param, std::string& stout ); + command setCom ( const std::string& str ); + void changeSt ( const std::string& str, const std::string& param, std::string& stout ); + public: + session ( std::basic_istream<char>& is, std::basic_ostream<char>& os ); + ~session () + { }; + int checkData (); + + state getState (); +}; + } // namespace smtp #endif // __SMPT_SERVER_H 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-14 08:53:23 UTC (rev 1874) +++ trunk/complement/explore/app/SMTP-tools/smtp_server_ut/my_test.cc 2008-05-15 08:35:35 UTC (rev 1875) @@ -80,32 +80,11 @@ basic_ostream<char> out( &_out_buf ); #endif - state st = connect; - command com; - string param, message, stout; + smtp::session ss (in, out); - while ( st != disconnect ) { - if ( st != letter ) { - string str; - in >> str; - 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; -// cerr << message; - message = ""; - } - } + while ( ss.getState() != disconnect ) { + int t = ss.checkData(); + if ( t < 0 ) break; } close(fd2[0]); @@ -142,14 +121,14 @@ getline( in_tst, s ); if ( !s.empty() ) { out << s << endl; -// cerr << s << endl; + cerr << s << endl; do { getline (in, result); -// cerr << result << endl; + cerr << result << endl; } while ( result[3] == '-' ); } } - + close(fd2[1]); close(fd1[0]); t.join(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <oke...@us...> - 2008-05-22 14:27:05
|
Revision: 1879 http://complement.svn.sourceforge.net/complement/?rev=1879&view=rev Author: okechina Date: 2008-05-22 07:26:19 -0700 (Thu, 22 May 2008) Log Message: ----------- Socket class is created, ECHO server and client use the class. Only one client can connect at once (server does not support parallel work) Added Paths: ----------- trunk/complement/explore/app/SMTP-tools/smtp_server_ut/socket/ trunk/complement/explore/app/SMTP-tools/smtp_server_ut/socket/ECHO_server.cc trunk/complement/explore/app/SMTP-tools/smtp_server_ut/socket/Makefile trunk/complement/explore/app/SMTP-tools/smtp_server_ut/socket/client.cc trunk/complement/explore/app/SMTP-tools/smtp_server_ut/socket/socket.cc trunk/complement/explore/app/SMTP-tools/smtp_server_ut/socket/socket.h Added: trunk/complement/explore/app/SMTP-tools/smtp_server_ut/socket/ECHO_server.cc =================================================================== --- trunk/complement/explore/app/SMTP-tools/smtp_server_ut/socket/ECHO_server.cc (rev 0) +++ trunk/complement/explore/app/SMTP-tools/smtp_server_ut/socket/ECHO_server.cc 2008-05-22 14:26:19 UTC (rev 1879) @@ -0,0 +1,44 @@ +#include <sys/types.h> +#include <sys/socket.h> + +#include <netinet/in.h> + +#include <stdlib.h> +#include <string.h> +#include <stdio.h> +#include <iostream> + +#include "socket.h" + +const int bufsize = 4096; + +int main() +{ + struct sockaddr_in fsin; + + char *service = "3000"; +// char *transport = "udp"; + char *transport = "tcp"; + int qlen = 0; + char buf[bufsize]; + + sct::sock s ( service, transport, qlen ); + unsigned int alen; + if (transport == "tcp") + { + alen = sizeof( fsin ); + int sc = accept( s.getDescr(), (struct sockaddr *)&fsin, &alen); + + while (1){ + int cc = read( sc, buf, sizeof( buf ) ); + write( sc, buf, cc ); + } + } else { + while (1){ + alen = sizeof( fsin ); + recvfrom( s.getDescr(), buf, sizeof( buf ), 0, (struct sockaddr *)&fsin, &alen ); + sendto( s.getDescr(), buf, sizeof( buf ), 0, (struct sockaddr *)&fsin, sizeof( fsin ) ); + } + } + return 0; +} Added: trunk/complement/explore/app/SMTP-tools/smtp_server_ut/socket/Makefile =================================================================== --- trunk/complement/explore/app/SMTP-tools/smtp_server_ut/socket/Makefile (rev 0) +++ trunk/complement/explore/app/SMTP-tools/smtp_server_ut/socket/Makefile 2008-05-22 14:26:19 UTC (rev 1879) @@ -0,0 +1,12 @@ +all: server client + +server: server.o socket.o + gcc -g -o server server.o socket.o -lstdc++ +server.o: ECHO_server.cc socket.h + gcc -g -c ECHO_server.cc -o server.o +client: client.o socket.o + gcc -g -o client client.o socket.o -lstdc++ +client.o: client.cc socket.h + gcc -g -c client.cc -o client.o +socket.o: socket.cc socket.h + gcc -g -c socket.cc -o socket.o Added: trunk/complement/explore/app/SMTP-tools/smtp_server_ut/socket/client.cc =================================================================== --- trunk/complement/explore/app/SMTP-tools/smtp_server_ut/socket/client.cc (rev 0) +++ trunk/complement/explore/app/SMTP-tools/smtp_server_ut/socket/client.cc 2008-05-22 14:26:19 UTC (rev 1879) @@ -0,0 +1,40 @@ +#include <unistd.h> +#include <stdio.h> +#include <iostream> + +#include <sys/types.h> +#include <sys/socket.h> + +#include <netinet/in.h> +#include <arpa/inet.h> + +#include <stdlib.h> +#include <string.h> +#include <netdb.h> + +#include "socket.h" + +int main(){ + const int linelen = 1000; + char *host = "localhost"; + char *service = "3000"; +// char *transport = "udp"; + char *transport = "tcp"; + char buf[linelen+1]; + int nchars; + + + sct::sock s( service, transport, host ); + + while ( fgets( buf, sizeof( buf ), stdin) ) { + buf[linelen] = '\0'; + + nchars = strlen( buf ); + write( s.getDescr(), buf, nchars ); + buf[0] = '\0'; + int n = read( s.getDescr(), buf, nchars ); + fputs( buf, stdout ); + } + + return 0; +} Added: trunk/complement/explore/app/SMTP-tools/smtp_server_ut/socket/socket.cc =================================================================== --- trunk/complement/explore/app/SMTP-tools/smtp_server_ut/socket/socket.cc (rev 0) +++ trunk/complement/explore/app/SMTP-tools/smtp_server_ut/socket/socket.cc 2008-05-22 14:26:19 UTC (rev 1879) @@ -0,0 +1,75 @@ +#include <sys/types.h> +#include <sys/socket.h> + +#include <netinet/in.h> +#include <arpa/inet.h> + +#include <stdlib.h> +#include <string.h> +#include <netdb.h> +#include <unistd.h> +#include <stdio.h> +#include <iostream> + +#include "socket.h" + +namespace sct +{ + +int sock::connectSock( const char *service, const char *transport, int qlen, char *host) +{ + int s; + memset (&sin, 0, sizeof(sin)); + sin.sin_family = AF_INET; + + if ( pse = getservbyname( service, transport ) ) + sin.sin_port = pse->s_port; + else if ( ( sin.sin_port = htons( (unsigned short)atoi( service ) ) ) == 0 ) + std::cerr << "error: can't get service entry"; + + ppe = getprotobyname(transport); + if (ppe == 0) + std::cerr << "error: can't get protocol entry"; + + if ( strcmp( transport, "udp" ) == 0 ) + type = SOCK_DGRAM; + else { + type = SOCK_STREAM; + } + + s = socket( PF_INET, type, ppe->p_proto ); + + if (s < 0) + std::cerr << "error: can't create socket"; + + if ( host == "" ) { //server socket is created + sin.sin_addr.s_addr = INADDR_ANY; + int t = bind( s, (struct sockaddr *)&sin, sizeof( sin ) ); +// std::cerr << t << std::endl; + if ( type == SOCK_STREAM ) { + listen(s,qlen); + } + } else { //client socket is created + if ( phe = gethostbyname( host ) ) + memcpy( &sin.sin_addr, phe->h_addr, phe->h_length ); + else sin.sin_addr.s_addr = inet_addr( host ); + int t = connect( s, (struct sockaddr *)&sin, sizeof( sin ) ); +// std::cerr << t << std::endl; + } + + return s; +} + +sock::sock( const char *service, const char *transport, int qlen) { //constructor for server socket + s = connectSock( service, transport, qlen, ""); +} + +sock::sock( const char *service, const char *transport, char *host) { //constructor for client socket + s = connectSock( service, transport, 0, host); +} + +int sock::getDescr() { + return s; +} + +} //namespace sct Added: trunk/complement/explore/app/SMTP-tools/smtp_server_ut/socket/socket.h =================================================================== --- trunk/complement/explore/app/SMTP-tools/smtp_server_ut/socket/socket.h (rev 0) +++ trunk/complement/explore/app/SMTP-tools/smtp_server_ut/socket/socket.h 2008-05-22 14:26:19 UTC (rev 1879) @@ -0,0 +1,35 @@ +#include <sys/types.h> +#include <sys/socket.h> + +#include <netinet/in.h> +#include <arpa/inet.h> + +#include <stdlib.h> +#include <string.h> +#include <netdb.h> +#include <unistd.h> +#include <stdio.h> + +namespace sct +{ + +class sock +{ + private: + struct hostent *phe; + struct servent *pse; + struct protoent *ppe; + struct sockaddr_in sin; + int s, type; + + int connectSock( const char *service, const char *transport, int qlen, char *host); + + public: + sock( const char *service, const char *transport, int qlen); //constructor for server socket + sock( const char *service, const char *transport, char *host); //constructor for client socket + ~sock() + {}; + int getDescr(); +}; + +} //namespace sct This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |