udp_broadcast.cpp

Go to the documentation of this file.
00001 /*
00002  * udp_broadcast.cpp
00003  *
00004  * Copyright (C) 2009  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  * Testing of UDP broadcast (based on netlib, not raw)
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 char * msg
00031 )
00032 {
00033         ASSERT(conn_id, "null");
00034         ASSERT(msg, "null");
00035 
00036         smart_ptr<netlib::MessageBuffer> buffer =
00037             netlib::MessageBuffer::create();
00038         ASSERT(buffer, "null");
00039 
00040         buffer->appendToken(msg);
00041         buffer->close();
00042 
00043         if (!netlib::enqueueMessage(conn_id, buffer)) {
00044                 WAVE_EX(wex);
00045                 wex << "Failed to enqueue message";
00046         }
00047 }
00048 
00049 
00050 
00051 static void
00052 doBroadcast
00053 (
00054 IN int port,
00055 IN const char * msg
00056 )
00057 {
00058         perf::Timer timer("doBroadcast");
00059 
00060         netlib::address_t address;
00061         address.setlocal(port);
00062         address.ip.setBroadcast();
00063         address.dump("Broadcast address");
00064 
00065         netlib::conn_id_t bcast = netlib::createUdpBroadcast(address);
00066         ASSERT2(bcast, "null");
00067         DPRINTF("broadcast UDP connection: 0x%04lx", (long) bcast);
00068 
00069         int nSends = 100;
00070         long nMicrosecondsPerIteration = 1000 * 1000;   // 1 second
00071         long nCallsPerIteration = 20;
00072         long nWait = nMicrosecondsPerIteration / nCallsPerIteration;
00073         for (int i = 0; i < nSends; ++i) {
00074                 int percent = int((100.0 * i / nSends) + 0.5);
00075                 DPRINTF("  Broadcasting (%2d%%)...", percent);
00076 
00077                 sendMessage(bcast, msg);
00078                 for (int i = 0; i < nCallsPerIteration; ++i) {
00079                         netlib::envelope_t envelope;
00080                         smart_ptr<netlib::MessageBuffer> msgbuf;
00081                         netlib::getNextMessage(nWait, envelope, msgbuf);
00082                 }
00083         }
00084 }
00085 
00086 
00087 
00088 ////////////////////////////////////////////////////////////////////////////////
00089 //
00090 //      entry point
00091 //
00092 ////////////////////////////////////////////////////////////////////////////////
00093 
00094 int
00095 main
00096 (
00097 IN int argc,
00098 IN const char  * argv[]
00099 )
00100 {
00101         ASSERT(3 == argc,
00102             "Usage: test_udp <remote_port> <message>");
00103 
00104         int retval = 0;
00105         int remote_port = atoi(argv[1]);
00106         const char * msg = argv[2];
00107 
00108         try {
00109                 perf::Timer timer("overall timer");
00110 
00111                 doBroadcast(remote_port, msg);
00112 
00113         } catch (std::exception& e) {
00114                 DPRINTF("EXCEPTION: %s", e.what());
00115                 retval = 1;
00116         }
00117 
00118         perf::dumpTimingSummary(std::cerr);
00119 
00120         return retval;
00121 }
00122