udp_client.cpp

Go to the documentation of this file.
00001 /*
00002  * test_udp.cpp
00003  *
00004  * Copyright (C) 2008  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  * Testing of UDP send/receive
00008  */
00009 
00010 // includes --------------------------------------------------------------------
00011 #include "netlib/netlib.h"
00012 
00013 #include "common/wave_ex.h"
00014 #include "perf/perf.h"
00015 #include "util/parsing.h"
00016 
00017 #include <iostream>
00018 
00019 
00020 ////////////////////////////////////////////////////////////////////////////////
00021 //
00022 //      static helper methods
00023 //
00024 ////////////////////////////////////////////////////////////////////////////////
00025 
00026 static void
00027 sendMessage
00028 (
00029 IN netlib::conn_id_t conn_id,
00030 IN const std::string& msg
00031 )
00032 {
00033         ASSERT(conn_id, "null");
00034         const char * data = msg.c_str();
00035         if (!*data) {
00036                 return;         // empty message...
00037         }
00038 
00039         smart_ptr<netlib::MessageBuffer> buffer =
00040             netlib::MessageBuffer::create();
00041         ASSERT(buffer, "null");
00042 
00043         buffer->appendToken(data);
00044         buffer->close();
00045 
00046         if (!netlib::enqueueMessage(conn_id, buffer)) {
00047                 WAVE_EX(wex);
00048                 wex << "Failed to enqueue message";
00049         }
00050 }
00051 
00052 
00053 
00054 ////////////////////////////////////////////////////////////////////////////////
00055 //
00056 //      entry point
00057 //
00058 ////////////////////////////////////////////////////////////////////////////////
00059 
00060 int
00061 main
00062 (
00063 IN int argc,
00064 IN const char  * argv[]
00065 )
00066 {
00067         ASSERT(4 == argc,
00068             "Usage: test_udp <local_port> <remote_server> <remote_port>");
00069 
00070         int local_port = atoi(argv[1]);
00071         const char * server = argv[2];
00072         int remote_port = atoi(argv[3]);
00073 
00074         netlib::address_t addressLocal;
00075         addressLocal.setlocal(local_port);
00076         addressLocal.dump("local address");
00077         ASSERT(addressLocal.isValid(), "Invalid local address");
00078 
00079         netlib::conn_id_t local = netlib::createUdpLocal(addressLocal);
00080         ASSERT(local, "null");
00081         DPRINTF("Local UDP connection: 0x%04lx", (long) local);
00082 
00083         netlib::address_t address(server, remote_port);
00084         address.dump("server address");
00085         ASSERT(address.isValid(), "Invalid server address");
00086 
00087         netlib::conn_id_t remote = netlib::createUdpRemote(local, address);
00088         ASSERT(remote, "null");
00089         DPRINTF("Remote UDP connection: 0x%04lx", (long) remote);
00090 
00091         std::istream& stream = std::cin;
00092 
00093         while (stream.good()) {
00094                 std::string line = getNextLineFromStream(stream, eParse_None);
00095 
00096                 DPRINTF("Line = %s", line.c_str());
00097 
00098                 sendMessage(remote, line);
00099                 for (int i = 0; i < 5; ++i) {
00100                         netlib::envelope_t envelope;
00101                         smart_ptr<netlib::MessageBuffer> msgbuf;
00102                         if (netlib::getNextMessage(100, envelope, msgbuf)) {
00103                                 netlib::dumpMessage(std::cerr,
00104                                     "Received message from UDP listener",
00105                                     envelope, msgbuf);
00106                         }
00107                 }
00108         }
00109 
00110         std::string summary;
00111         perf::getTimingSummary(summary);
00112         DPRINTF("Timers:\n%s", summary.c_str());
00113 
00114         return 0;
00115 }
00116