From: <mik...@us...> - 2003-12-26 10:04:55
|
Update of /cvsroot/sharedaemon/sharedaemon-ui-web/src/test In directory sc8-pr-cvs1:/tmp/cvs-serv24435/src/test Added Files: TestSocket.cpp TestSocket.h Log Message: 26/12/2003 Mikael Barbeaux * Implemented Socket, ServerSocket and SocketException classes. * Added simple test program for Sockets. --- NEW FILE: TestSocket.cpp --- /* * This file is part of webInterface. * Copyright (C) 2003 Mikael Barbeaux <mik...@us...> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "TestSocket.h" #include "../exceptions/Exception.h" #include <iostream> using namespace std; #include <stdio.h> /** * Constructor for TestSocket. */ TestSocket::TestSocket() : TestHandler() { } /** * Destructor for TestSocket. */ TestSocket::~TestSocket() { } /** * Runs the tester */ void TestSocket::test() { /** This test tries to launch a thread * and goes on working. */ cout << endl; cout << "ShareDaemon Web Interface - Test" << endl << endl; cout << "Test for sockets... " << endl; cout << "The tester will create a socket, listening for connections on " << endl; cout << "port 4589. Open your favourite browser and waits for the socket" << endl; cout << "to start. Then, try to open \"http://localhost:4589\" with your " << endl; cout << "browser to test the socket." << endl; cout << "Press enter to continue" << endl; getchar(); cout << endl << endl; try { cout << "Creates a server socket..." << endl; ServerSocket server; cout << "Validates the server socket..." << endl; server.validate(); cout << "Bind server socket on port 4589..." << endl; server.bind(4589); cout << "Listening for connections..." << endl; server.listen(5); cout << "Take your browser to test \"http://localhost:4589\"..." << endl; Socket *socket = server.accept(); cout << "Socket accepted !" << endl; char *recv; socket->receive(&recv); cout << "Reveiced mesage : " << endl; cout << recv << endl; cout << "Sending response..." << endl; delete[] recv; char *resp = "HTTP/1.1 200 OK\n\n<html><body>Request received !</body></html>\n"; socket->send(resp, strlen(resp)); cout << "Closing socket..." << endl; socket->close(); cout << "Closing server socket..." << endl << endl; server.close(); } catch(Exception& e) { // display the exception message cout << e.getMessage() << endl; } cout << "Could you connect to the socket and did you see the response ?" << endl; cout << " If no => VERY BAD :(" << endl; cout << " If yes => Test worked !! :D" << endl << endl; cout << "Press enter to continue" << endl; getchar(); } --- NEW FILE: TestSocket.h --- /* * This file is part of webInterface. * Copyright (C) 2003 Mikael Barbeaux <mik...@us...> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef _TEST_SOCKET_H_ #define _TEST_SOCKET_H_ #include "TestHandler.h" #include "../server/network/Socket.h" #include "../server/network/ServerSocket.h" /** * Defines a test class for sockets. */ class TestSocket : public TestHandler { public: /** * Creates a test for sockets. */ TestSocket(); /** * Destructor for TestSocket */ ~TestSocket(); /** * Runs the tester. */ virtual void test(); }; #endif |