///////////////////////////////////////////////////////////////////////////// // ServerTest.cpp - Test passing strings, blocks, and buffers // // Ver 1.1 // // Jim Fawcett, CSE#687 - Object Oriented Design, Spring 2015 // // CST 4-187, Syracuse University, 315 443-3948, jfawcett@twcny.rr.com // //-------------------------------------------------------------------------// // Jim Fawcett (c) copyright 2015 // // All rights granted provided this copyright notice is retained // //-------------------------------------------------------------------------// // Application: Demo for Project #3, Communication Channel // // Platform: Dell 2720, Win Pro 8.1, Visual Studio 2013 // ///////////////////////////////////////////////////////////////////////////// /* * Package Operations: * ------------------- * This package provides tests for Sockets-4.5: * - passing byte buffers * - passing strings * * Required Files: * --------------- * ClientTest.cpp, ServerTest.cpp * Sockets.h, Sockets.cpp, * Logger.h, Logger.cpp, * Utilities.h, Utilities.cpp, * * Build Command: * -------------- * devenv Server.sln /rebuild debug * * Maintenance History: * -------------------- * ver 1.2 : 22 Feb 16 * - Replaced verbose I/O with Logger I/O * - Replaced ApplicationHelpers package with Utilities package * ver 1.1 : 30 Mar 15 * - minor modification to comments, above * ver 1.0 : 30 Mar 15 * - first release */ #include "../sockets/sockets.h" #include "../Logger/Logger.h" #include "../Utilities/Utilities.h" #include #include #include using Util = Utilities::StringHelper; template using Conv = Utilities::Converter; ///////////////////////////////////////////////////////////////////////////// // Server's client handler class // - must be callable object so we've built as a functor // - passed to SocketListener::start(CallObject& co) // - Client handling thread starts by calling operator() class ClientHandler { public: void operator()(Socket& socket_); bool testStringHandling(Socket& socket_); bool testBufferHandling(Socket& socket_); }; //----< Client Handler thread starts running this function >----------------- void ClientHandler::operator()(Socket& socket_) { while (true) { // interpret test command std::string command = socket_.recvString(); if (command.size() == 0) break; StaticLogger<1>::write("\n command: " + command); if (command == "TEST_STRING_HANDLING") { if (testStringHandling(socket_)) StaticLogger<1>::write("\n ----String Handling test passed"); else StaticLogger<1>::write("\n ----String Handling test failed"); continue; // go back and get another command } if (command == "TEST_BUFFER_HANDLING") { if (testBufferHandling(socket_)) StaticLogger<1>::write("\n ----Buffer Handling test passed"); else StaticLogger<1>::write("\n ----Buffer Handling test failed"); continue; // get another command } } // we get here if command isn't requesting a test, e.g., "TEST_STOP" StaticLogger<1>::write("\n ClientHandler socket connection closing"); socket_.shutDown(); socket_.close(); StaticLogger<1>::write("\n ClientHandler thread terminating"); } //----< test string handling >----------------------------------------------- /* * Creates strings, sends to server, then reads strings server echos back. */ bool ClientHandler::testStringHandling(Socket& socket_) { StaticLogger<1>::title("String handling test on server"); while (true) { if (socket_.bytesWaiting() == 0) break; std::string str = socket_.recvString(); if (socket_ == INVALID_SOCKET) return false; if (str.size() > 0) { //StaticLogger<1>::write("\n bytes recvd at server: " + toString(str.size() + 1)); StaticLogger<1>::write("\n server recvd : " + str); if (socket_.sendString(str)) { //StaticLogger<1>::write("\n bytes sent by server: " + Conv::toString(str.size() + 1)); } else { return false; } } else { break; } } socket_.sendString("TEST_END"); return true; } //----< test buffer handling >----------------------------------------------- /* * Creates buffers, sends to server, then reads buffers server echos back. */ bool ClientHandler::testBufferHandling(Socket& socket_) { StaticLogger<1>::title("Buffer handling test on server"); const int BufLen = 20; Socket::byte buffer[BufLen]; bool ok; while (true) { if (socket_.bytesWaiting() == 0) break; ok = socket_.recv(BufLen, buffer); if (socket_ == INVALID_SOCKET || !ok) return false; std::string temp; for (size_t i = 0; i < BufLen; ++i) temp += buffer[i]; //StaticLogger<1>::write("\n bytes recvd at server: " + toString(BufLen)); StaticLogger<1>::write("\n server recvd : " + temp); if (socket_.send(BufLen, buffer)) { //StaticLogger<1>::write("\n bytes sent by server: " + toString(BufLen)); } else { return false; } } std::string temp = "TEST_END"; for (size_t i = 0; i < BufLen; ++i) { if (i < temp.size()) buffer[i] = temp[i]; else buffer[i] = '#'; } socket_.send(BufLen, buffer); return true; } //----< test stub starts here >---------------------------------------------- struct Cosmetic { ~Cosmetic() { std::cout << "\n press key to exit: "; std::cin.get(); std::cout << "\n\n"; } } aGlobalForCosmeticAction; //----< demonstration >------------------------------------------------------ int main() { StaticLogger<1>::attach(&std::cout); StaticLogger<1>::start(); StaticLogger<1>::title("Testing Socket Server", '='); try { SocketSystem ss; SocketListener sl(9080, Socket::IP6); ClientHandler cp; sl.start(cp); StaticLogger<1>::write("\n press key to exit: "); std::cout.flush(); std::cin.get(); } catch (std::exception& ex) { StaticLogger<1>::write("\n Exception caught:"); StaticLogger<1>::write("\n " + std::string("\n ") + ex.what() + "\n\n"); } }