Revision: 40263 http://brlcad.svn.sourceforge.net/brlcad/?rev=40263&view=rev Author: davidloman Date: 2010-08-19 19:54:20 +0000 (Thu, 19 Aug 2010)
Log Message: ----------- WS, Formatting. Used Eclipse formatter so svn diff is likely useless. Modified Paths: -------------- rt^3/trunk/include/PkgClient.h rt^3/trunk/include/PkgServer.h rt^3/trunk/include/PkgTcpServer.h rt^3/trunk/include/PkgUdpClient.h rt^3/trunk/include/PkgUdpServer.h rt^3/trunk/include/pkgcppcommon.h rt^3/trunk/src/libPkgCpp/PkgClient.cxx rt^3/trunk/src/libPkgCpp/PkgServer.cxx rt^3/trunk/src/libPkgCpp/PkgTcpClient.cxx rt^3/trunk/src/libPkgCpp/PkgTcpServer.cxx rt^3/trunk/src/libPkgCpp/PkgUdpClient.cxx rt^3/trunk/src/libPkgCpp/PkgUdpServer.cxx rt^3/trunk/tests/libpkgcpp/pkgcppTest.cxx Modified: rt^3/trunk/include/PkgClient.h =================================================================== --- rt^3/trunk/include/PkgClient.h 2010-08-19 19:52:52 UTC (rev 40262) +++ rt^3/trunk/include/PkgClient.h 2010-08-19 19:54:20 UTC (rev 40263) @@ -33,50 +33,55 @@ class PkgClient { public: - PkgClient(std::string proto, std::string ipOrHostname, int port); - PkgClient(std::string proto, pkg_conn* conn); - virtual ~PkgClient(); + PkgClient(std::string proto, std::string ipOrHostname, int port); + PkgClient(std::string proto, pkg_conn* conn); + virtual + ~PkgClient(); - bool hasGoodConnection(); + bool + hasGoodConnection(); - /* - * Blocks until a specific opcode has been received on the connection - */ - char* waitForMsg(int opcode); + /* + * Blocks until a specific opcode has been received on the connection + */ + char* + waitForMsg(int opcode); - /* - * Attempts to route data in buffer to the appropriate call back. - * Returns: - * <0 on error - * 0 on EOF - * >0 on success - */ - int processData(); + /* + * Attempts to route data in buffer to the appropriate call back. + * Returns: + * <0 on error + * 0 on EOF + * >0 on success + */ + int + processData(); - /* - * Attempts to pull data off of the associated - * socket and place it in the internal buffer - * Returns: - * <0 on error - * 0 on EOF - * >0 on success - */ - int pullDataFromSocket(); + /* + * Attempts to pull data off of the associated + * socket and place it in the internal buffer + * Returns: + * <0 on error + * 0 on EOF + * >0 on success + */ + int + pullDataFromSocket(); - void close(); + void + close(); - int send(int opcode, const char* buffer, size_t bufferLen); + int + send(int opcode, const char* buffer, size_t bufferLen); private: - std::string proto; - pkg_conn* conn; + std::string proto; + pkg_conn* conn; - }; #endif /* __PKGCLIENT_H__ */ - /* * Local Variables: * mode: C Modified: rt^3/trunk/include/PkgServer.h =================================================================== --- rt^3/trunk/include/PkgServer.h 2010-08-19 19:52:52 UTC (rev 40262) +++ rt^3/trunk/include/PkgServer.h 2010-08-19 19:54:20 UTC (rev 40263) @@ -32,29 +32,31 @@ class PkgServer { public: - PkgServer(std::string proto, pkg_switch callbacks[]); - virtual ~PkgServer(); + PkgServer(std::string proto, pkg_switch callbacks[]); + virtual + ~PkgServer(); - bool listen(unsigned short port); + bool + listen(unsigned short port); - /* - * This function blocks until a new connection - */ - PkgClient* waitForClient(); + /* + * This function blocks until a new connection + */ + PkgClient* + waitForClient(); - private: - std::string proto; - int listenFD; - pkg_switch* callBackTable; + std::string proto; + int listenFD; + pkg_switch* callBackTable; - virtual PkgClient* getNewClient(pkg_conn* conn) = 0; + virtual PkgClient* + getNewClient(pkg_conn* conn) = 0; }; #endif /* __PKGSERVER_H__ */ - /* * Local Variables: * mode: C Modified: rt^3/trunk/include/PkgTcpServer.h =================================================================== --- rt^3/trunk/include/PkgTcpServer.h 2010-08-19 19:52:52 UTC (rev 40262) +++ rt^3/trunk/include/PkgTcpServer.h 2010-08-19 19:54:20 UTC (rev 40263) @@ -34,10 +34,12 @@ { public: PkgTcpServer(pkg_switch callbacks[]); - virtual ~PkgTcpServer(); + virtual + ~PkgTcpServer(); private: - PkgClient* getNewClient(pkg_conn* conn); + PkgClient* + getNewClient(pkg_conn* conn); }; Modified: rt^3/trunk/include/PkgUdpClient.h =================================================================== --- rt^3/trunk/include/PkgUdpClient.h 2010-08-19 19:52:52 UTC (rev 40262) +++ rt^3/trunk/include/PkgUdpClient.h 2010-08-19 19:54:20 UTC (rev 40263) @@ -34,7 +34,8 @@ public: PkgUdpClient(std::string ipOrHostname, int port); PkgUdpClient(pkg_conn* conn); - virtual ~PkgUdpClient(); + virtual + ~PkgUdpClient(); }; #endif /* __PKGUDPCLIENT_H__ */ Modified: rt^3/trunk/include/PkgUdpServer.h =================================================================== --- rt^3/trunk/include/PkgUdpServer.h 2010-08-19 19:52:52 UTC (rev 40262) +++ rt^3/trunk/include/PkgUdpServer.h 2010-08-19 19:54:20 UTC (rev 40263) @@ -34,10 +34,12 @@ { public: PkgUdpServer(pkg_switch callbacks[]); - virtual ~PkgUdpServer(); + virtual + ~PkgUdpServer(); private: - PkgClient* getNewClient(pkg_conn* conn); + PkgClient* + getNewClient(pkg_conn* conn); }; Modified: rt^3/trunk/include/pkgcppcommon.h =================================================================== --- rt^3/trunk/include/pkgcppcommon.h 2010-08-19 19:52:52 UTC (rev 40262) +++ rt^3/trunk/include/pkgcppcommon.h 2010-08-19 19:54:20 UTC (rev 40263) @@ -18,7 +18,6 @@ * information. */ - #ifndef __PKGCPPCOMMON_H__ #define __PKGCPPCOMMON_H__ Modified: rt^3/trunk/src/libPkgCpp/PkgClient.cxx =================================================================== --- rt^3/trunk/src/libPkgCpp/PkgClient.cxx 2010-08-19 19:52:52 UTC (rev 40262) +++ rt^3/trunk/src/libPkgCpp/PkgClient.cxx 2010-08-19 19:54:20 UTC (rev 40263) @@ -29,12 +29,13 @@ PkgClient::PkgClient(std::string proto, std::string ipOrHostname, int port) { this->proto = proto; - char portCString[7] = { 0 }; + char portCString[7] = + { 0 }; snprintf(portCString, 6, "%d", port); //TODO Make this more robust. TCP being hardcoded is bad. - this->conn = pkg_open(ipOrHostname.c_str(), portCString, proto.c_str(), NULL, NULL, - NULL, NULL); + this->conn = pkg_open(ipOrHostname.c_str(), portCString, proto.c_str(), NULL, + NULL, NULL, NULL); } PkgClient::PkgClient(std::string proto, pkg_conn* conn) Modified: rt^3/trunk/src/libPkgCpp/PkgServer.cxx =================================================================== --- rt^3/trunk/src/libPkgCpp/PkgServer.cxx 2010-08-19 19:52:52 UTC (rev 40262) +++ rt^3/trunk/src/libPkgCpp/PkgServer.cxx 2010-08-19 19:54:20 UTC (rev 40263) @@ -42,7 +42,8 @@ PkgServer::listen(unsigned short port) { //Convert port -> char* to make libpkg happy. - char portCString[7] = { 0 }; + char portCString[7] = + { 0 }; int fd; snprintf(portCString, 6, "%d", port); @@ -81,7 +82,6 @@ return pkgClientObj; } - /* * Local Variables: * mode: C Modified: rt^3/trunk/src/libPkgCpp/PkgTcpClient.cxx =================================================================== --- rt^3/trunk/src/libPkgCpp/PkgTcpClient.cxx 2010-08-19 19:52:52 UTC (rev 40262) +++ rt^3/trunk/src/libPkgCpp/PkgTcpClient.cxx 2010-08-19 19:54:20 UTC (rev 40263) @@ -27,12 +27,12 @@ #include "PkgTcpClient.h" PkgTcpClient::PkgTcpClient(std::string ipOrHostname, int port) : -PkgClient(PKGCPP_PROTO_TCP, ipOrHostname, port) + PkgClient(PKGCPP_PROTO_TCP, ipOrHostname, port) { } PkgTcpClient::PkgTcpClient(pkg_conn* conn) : -PkgClient(PKGCPP_PROTO_TCP, conn) + PkgClient(PKGCPP_PROTO_TCP, conn) { } Modified: rt^3/trunk/src/libPkgCpp/PkgTcpServer.cxx =================================================================== --- rt^3/trunk/src/libPkgCpp/PkgTcpServer.cxx 2010-08-19 19:52:52 UTC (rev 40262) +++ rt^3/trunk/src/libPkgCpp/PkgTcpServer.cxx 2010-08-19 19:54:20 UTC (rev 40263) @@ -38,9 +38,9 @@ // TODO Auto-generated destructor stub } - PkgClient* -PkgTcpServer::getNewClient(pkg_conn* conn) { +PkgTcpServer::getNewClient(pkg_conn* conn) +{ return new PkgTcpClient(conn); } // Local Variables: Modified: rt^3/trunk/src/libPkgCpp/PkgUdpClient.cxx =================================================================== --- rt^3/trunk/src/libPkgCpp/PkgUdpClient.cxx 2010-08-19 19:52:52 UTC (rev 40262) +++ rt^3/trunk/src/libPkgCpp/PkgUdpClient.cxx 2010-08-19 19:54:20 UTC (rev 40263) @@ -27,16 +27,15 @@ #include "PkgUdpClient.h" PkgUdpClient::PkgUdpClient(std::string ipOrHostname, int port) : -PkgClient(PKGCPP_PROTO_UDP, ipOrHostname, port) + PkgClient(PKGCPP_PROTO_UDP, ipOrHostname, port) { } PkgUdpClient::PkgUdpClient(pkg_conn* conn) : -PkgClient(PKGCPP_PROTO_UDP, conn) + PkgClient(PKGCPP_PROTO_UDP, conn) { } - PkgUdpClient::~PkgUdpClient() { // TODO Auto-generated destructor stub Modified: rt^3/trunk/src/libPkgCpp/PkgUdpServer.cxx =================================================================== --- rt^3/trunk/src/libPkgCpp/PkgUdpServer.cxx 2010-08-19 19:52:52 UTC (rev 40262) +++ rt^3/trunk/src/libPkgCpp/PkgUdpServer.cxx 2010-08-19 19:54:20 UTC (rev 40263) @@ -39,7 +39,8 @@ } PkgClient* -PkgUdpServer::getNewClient(pkg_conn* conn) { +PkgUdpServer::getNewClient(pkg_conn* conn) +{ return new PkgUdpClient(conn); } Modified: rt^3/trunk/tests/libpkgcpp/pkgcppTest.cxx =================================================================== --- rt^3/trunk/tests/libpkgcpp/pkgcppTest.cxx 2010-08-19 19:52:52 UTC (rev 40262) +++ rt^3/trunk/tests/libpkgcpp/pkgcppTest.cxx 2010-08-19 19:54:20 UTC (rev 40263) @@ -42,18 +42,26 @@ #define PKGCPP_DATA 2 #define PKGCPP_CIAO 3 - //Declares -void printUsage(std::string customMsg); -std::string getValidIpOrHostname(char* data); -int getValidPort(char* data); -int tryProcess(PkgClient* pkgClient); -int runServer(int port); -int runClient(std::string ipOrHostname, int port); +void +printUsage(std::string customMsg); +std::string +getValidIpOrHostname(char* data); +int +getValidPort(char* data); +int +tryProcess(PkgClient* pkgClient); +int +runServer(int port); +int +runClient(std::string ipOrHostname, int port); -void server_helo(struct pkg_conn* c, char* buf); -void server_data(struct pkg_conn* c, char* buf); -void server_ciao(struct pkg_conn* c, char* buf); +void +server_helo(struct pkg_conn* c, char* buf); +void +server_data(struct pkg_conn* c, char* buf); +void +server_ciao(struct pkg_conn* c, char* buf); //global bool listenLoop = true; @@ -74,25 +82,33 @@ std::string cliServ(argv[1]); std::transform(cliServ.begin(), cliServ.end(), cliServ.begin(), tolower); - if (cliServ == "client" ) { - isServer = false; - } else if (cliServ == "server") { - isServer = true; - } else { - printUsage("Unknown mode: '" + cliServ + "'"); - bu_exit(1, ""); - } + if (cliServ == "client") + { + isServer = false; + } + else if (cliServ == "server") + { + isServer = true; + } + else + { + printUsage("Unknown mode: '" + cliServ + "'"); + bu_exit(1, ""); + } int exitVal = 0; - if (isServer) { - int port = getValidPort(argv[2]); - exitVal = runServer(port); + if (isServer) + { + int port = getValidPort(argv[2]); + exitVal = runServer(port); - } else { - std::string ipOrHostname = getValidIpOrHostname(argv[2]); - int port = getValidPort(argv[3]); - exitVal = runClient(ipOrHostname, port); - } + } + else + { + std::string ipOrHostname = getValidIpOrHostname(argv[2]); + int port = getValidPort(argv[3]); + exitVal = runClient(ipOrHostname, port); + } return exitVal; } @@ -104,16 +120,19 @@ getValidIpOrHostname(char* data) { //Check IP/Host - std::string ipOrHostname(data); + std::string ipOrHostname(data); - if (ipOrHostname.length() > 0 ) { - //More validation goes here, if needed. - } else { - printUsage("Supplied IP/Host: '" + ipOrHostname+ "' is invalid"); - bu_exit(1, ""); - } + if (ipOrHostname.length() > 0) + { + //More validation goes here, if needed. + } + else + { + printUsage("Supplied IP/Host: '" + ipOrHostname + "' is invalid"); + bu_exit(1, ""); + } - return ipOrHostname; + return ipOrHostname; } /** @@ -126,12 +145,15 @@ int port = atoi(data); //Hardcode prolly not best for OS determined port range.... - if (port > 0x0000 && port < 0xFFFF ) { - //More validation goes here, if needed. - } else { - printUsage("Supplied Port '" + portStr+ "' is invalid."); - bu_exit(1, ""); - } + if (port > 0x0000 && port < 0xFFFF) + { + //More validation goes here, if needed. + } + else + { + printUsage("Supplied Port '" + portStr + "' is invalid."); + bu_exit(1, ""); + } return port; } @@ -139,18 +161,16 @@ runServer(int port) { - struct pkg_switch callbacks[] = { - {PKGCPP_HELO, server_helo, "HELO"}, - {PKGCPP_DATA, server_data, "DATA"}, - {PKGCPP_CIAO, server_ciao, "CIAO"}, - {0, 0, (char* )0} - }; + struct pkg_switch callbacks[] = + { + { PKGCPP_HELO, server_helo, "HELO" }, + { PKGCPP_DATA, server_data, "DATA" }, + { PKGCPP_CIAO, server_ciao, "CIAO" }, + { 0, 0, (char*) 0 } }; - PkgTcpServer pkgServer(callbacks); pkgServer.listen(port); - //Setup vars char* buffer; @@ -162,27 +182,27 @@ */ do { - //Blocks + //Blocks pkgClient = pkgServer.waitForClient(); //TODO probably should failsafe this loop if (pkgClient == NULL) continue; -/* //Attempt to get a HELO msg... and do nothing with it - buffer = pkgClient->waitForMsg(PKGCPP_HELO); + /* //Attempt to get a HELO msg... and do nothing with it + buffer = pkgClient->waitForMsg(PKGCPP_HELO); validate magic header that client should have sent - if (strcmp(buffer, CLIENT_NAME) != 0) - { - bu_log("Data Error, received a PKGCPP_HELO without proper data!\n"); - pkgClient->close(); - } + if (strcmp(buffer, CLIENT_NAME) != 0) + { + bu_log("Data Error, received a PKGCPP_HELO without proper data!\n"); + pkgClient->close(); + } - //TODO probably should failsafe this loop also - if (buffer == NULL) - continue; - */ + //TODO probably should failsafe this loop also + if (buffer == NULL) + continue; + */ } while (pkgClient == NULL); @@ -192,22 +212,26 @@ * connection. boilerplate triple-call loop. */ bu_log("Processing data from client\n"); - do { + do + { bu_log("Loop Pass #%d\n", counter); - /* process packets potentially received in a processing callback */ - itemsRemain = tryProcess(pkgClient); + /* process packets potentially received in a processing callback */ + itemsRemain = tryProcess(pkgClient); /* suck in data from the network */ int pkg_result = pkgClient->pullDataFromSocket(); - if (pkg_result < 0) { + if (pkg_result < 0) + { bu_log("Seemed to have trouble sucking in packets.\n"); break; - } else if (pkg_result == 0) { + } + else if (pkg_result == 0) + { bu_log("Client closed the connection.\n"); //usleep(100); //break; - } + } bu_log("Last pass: %d bytes.\n", pkg_result); @@ -215,7 +239,8 @@ itemsRemain = tryProcess(pkgClient); counter++; - } while (pkgClient != NULL && itemsRemain!=0 && listenLoop); + } + while (pkgClient != NULL && itemsRemain != 0 && listenLoop); bu_log("Done with client. Closing connection.\n"); @@ -227,16 +252,16 @@ return 0; } - int runClient(std::string ipOrHostname, int port) { //Generate random data block. - char data[RAND_DATA_SIZE] = {0}; + char data[RAND_DATA_SIZE] = { 0 }; - for (int i = 0; i<RAND_DATA_SIZE; ++i) { - data[i] = rand() % 0xFF; - } + for (int i = 0; i < RAND_DATA_SIZE; ++i) + { + data[i] = rand() % 0xFF; + } //Create PkgClient obj and open new connection PkgTcpClient* connToServer = new PkgTcpClient(ipOrHostname, port); @@ -253,36 +278,42 @@ //Send a HELO bytes = connToServer->send(PKGCPP_HELO, CLIENT_NAME, strlen(CLIENT_NAME) + 1); - if (bytes < 0) { + if (bytes < 0) + { connToServer->close(); - bu_log("Connection to %s, port %d, seems faulty.\n", ipOrHostname.c_str(), port); + bu_log("Connection to %s, port %d, seems faulty.\n", + ipOrHostname.c_str(), port); bu_bomb("ERROR: Unable to communicate with the server\n"); return 1; - } + } + for (int i = 0; i < 2; ++i) + { + bu_log("Sending %d bytes to server.\n", RAND_DATA_SIZE); - for (int i = 0; i < 2; ++i) { - bu_log("Sending %d bytes to server.\n", RAND_DATA_SIZE); + //Send Data + bytes = connToServer->send(PKGCPP_DATA, data, RAND_DATA_SIZE); + if (bytes < 0) + { + connToServer->close(); + bu_log("Connection to %s, port %d, seems faulty.\n", + ipOrHostname.c_str(), port); + bu_bomb("ERROR: Unable to send data to the server\n"); + } - //Send Data - bytes = connToServer->send(PKGCPP_DATA, data, RAND_DATA_SIZE); - if (bytes < 0) { - connToServer->close(); - bu_log("Connection to %s, port %d, seems faulty.\n", ipOrHostname.c_str(), port); - bu_bomb("ERROR: Unable to send data to the server\n"); + bu_log("Sent %d bytes to server.\n", bytes); + + sleep(1); } - bu_log("Sent %d bytes to server.\n", bytes); - - sleep(1); - } - bu_log("Sending CIAO.\n"); /* let the server know we're done. not necessary, but polite. */ bytes = connToServer->send(PKGCPP_CIAO, "CIAO", 4); - if (bytes < 0) { - bu_log("Unable to cleanly disconnect from %s, port %d.\n", ipOrHostname.c_str(), port); - } + if (bytes < 0) + { + bu_log("Unable to cleanly disconnect from %s, port %d.\n", + ipOrHostname.c_str(), port); + } sleep(5); bu_log("Disconnecting.\n"); @@ -295,9 +326,10 @@ tryProcess(PkgClient* pkgClient) { int pkg_result = pkgClient->processData(); - if (pkg_result < 0) { + if (pkg_result < 0) + { bu_log("Unable to process packets? Weird.\n"); - } + } return pkg_result; } @@ -313,12 +345,11 @@ { c = c; /* quell */ int lenDataRecved = c->pkc_inend - sizeof(pkg_header); - std::string name (buf); + std::string name(buf); bu_log("HELO recv-ed from '%s': %d bytes.\n", name.c_str(), lenDataRecved); free(buf); } - /** * callback when a DATA message packet is received */ @@ -331,7 +362,6 @@ free(buf); } - /** * callback when a CIAO message packet is received */ @@ -352,17 +382,17 @@ printUsage(std::string customMsg) { - if (customMsg.length() > 0) { - std::cout << customMsg << std::endl; - } + if (customMsg.length() > 0) + { + std::cout << customMsg << std::endl; + } - std::cout << "Usage for Client: pkgcppTest client ipAddress port." << std::endl; + std::cout << "Usage for Client: pkgcppTest client ipAddress port." + << std::endl; std::cout << "Usage for Server: pkgcppTest server port." << std::endl; return; } - - // Local Variables: // tab-width: 8 // mode: C++ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ This SF.net email is sponsored by Make an app they can't live without Enter the BlackBerry Developer Challenge http://p.sf.net/sfu/RIM-dev2dev _______________________________________________ BRL-CAD Source Commits mailing list brlcad-commits@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/brlcad-commits