tcp_client.cpp

Go to the documentation of this file.
00001 /*
00002  * test_tcp_client.cpp
00003  * 
00004  * Copyright (C) 2007  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  * Simple TCP client.
00008  */
00009 
00010 // includes --------------------------------------------------------------------
00011 #include "netlib/netlib.h"
00012 #include "common/wave_ex.h"
00013 
00014 #include "perf/perf.h"
00015 
00016 #include <iostream>
00017 
00018 
00019 ////////////////////////////////////////////////////////////////////////////////
00020 //
00021 //      static helper methods
00022 //
00023 ////////////////////////////////////////////////////////////////////////////////
00024 
00025 static void
00026 sendData
00027 (
00028 IN netlib::conn_id_t conn_id,
00029 IN const char * data,
00030 IN long bytes
00031 )
00032 {
00033         perf::Timer timer("sendData");
00034         ASSERT(conn_id, "null");
00035         ASSERT(data, "null");
00036         ASSERT(bytes > 0, "bad byte count: %ld", bytes);
00037 
00038         DPRINTF("Sending %ld bytes...", bytes);
00039 
00040         smart_ptr<netlib::MessageBuffer> mb = netlib::MessageBuffer::create();
00041         ASSERT(mb, "failed to create message buffer?");
00042 
00043         mb->appendData(data, bytes);
00044         mb->close();
00045 
00046         if (!netlib::enqueueMessage(conn_id, mb)) {
00047                 WAVE_EX(wex);
00048                 wex << "Failed to enqueue message";
00049         }
00050 }
00051 
00052 
00053 
00054 static void
00055 sendMessage
00056 (
00057 IN netlib::conn_id_t conn_id
00058 )
00059 {
00060         ASSERT(conn_id, "Bad connection ID");
00061 
00062         const int bufsize = 10000;
00063         char buffer[bufsize];
00064         for (int i = 0; i < 3; ++i) {
00065                 sprintf(buffer, "This is test message number %d", i);
00066                 int len = strlen(buffer);
00067 
00068                 sendData(conn_id, buffer, len);
00069         }
00070 
00071         for (int j = 0; j < 3; ++j) {
00072                 int bytes = rand() % (bufsize - 1);
00073                 bytes++;
00074                 for (int i = 0; i < bytes; ++i) {
00075                         buffer[i] = 'A' + (i % 26);
00076                         if (0 == ((i + 1) % 80)) {
00077                                 buffer[i] = '\n';
00078                         }
00079                 }
00080                 sendData(conn_id, buffer, bytes);
00081         }
00082 }
00083 
00084 
00085 
00086 ////////////////////////////////////////////////////////////////////////////////
00087 //
00088 //      entry point
00089 //
00090 ////////////////////////////////////////////////////////////////////////////////
00091 
00092 int
00093 main
00094 (
00095 IN int argc,
00096 IN const char * argv[]
00097 )
00098 {
00099         ASSERT(3 == argc, "Usage: netlib-tcp_client <server> <port>");
00100 
00101         const char * server = argv[1];
00102         int port = atoi(argv[2]);
00103         ASSERT(port > 0, "Bad port: %d", port);
00104 
00105         netlib::address_t address(server, port);
00106         ASSERT(address.isValid(), "invalid address?");
00107 
00108         netlib::conn_id_t conn_id = netlib::createTcpConnection(address);
00109         ASSERT(conn_id, "Failed to create connection to %s:%d", server, port);
00110 
00111         for (int i = 0; i < 3000; ++i) {
00112                 netlib::envelope_t envelope;
00113                 smart_ptr<netlib::MessageBuffer> msgbuf;
00114                 if (netlib::getNextMessage(1000, envelope, msgbuf)) {
00115                         netlib::dumpMessage(std::cerr,
00116                                 "Received message from server",
00117                                 envelope, msgbuf);
00118                 }
00119 
00120                 if (i < 5) {
00121                         sendMessage(conn_id);
00122                 }
00123         }
00124 
00125         std::string summary;
00126         perf::getTimingSummary(summary);
00127         DPRINTF("Timers:\n%s", summary.c_str());
00128 
00129         netlib::dumpStats();
00130 }
00131