udp.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 <iostream>
00012 
00013 #include "netlib/netlib.h"
00014 #include "perf/perf.h"
00015 #include "util/parsing.h"
00016 
00017 
00018 ////////////////////////////////////////////////////////////////////////////////
00019 //
00020 //      static helper methods
00021 //
00022 ////////////////////////////////////////////////////////////////////////////////
00023 
00024 static void
00025 sendMessage
00026 (
00027 IN netlib::conn_id_t conn_id,
00028 IN const std::string& msg
00029 )
00030 {
00031         ASSERT(conn_id, "null");
00032         const char * data = msg.c_str();
00033         if (!*data) {
00034                 return;         // empty message...
00035         }
00036 
00037         smart_ptr<netlib::MessageBuffer> buffer =
00038             netlib::MessageBuffer::create();
00039         ASSERT(buffer, "null");
00040 
00041         buffer->appendToken(data);
00042         buffer->close();
00043 
00044         netlib::msg_t nmsg;
00045         nmsg.conn_id = conn_id;
00046         nmsg.msgbuf = buffer;
00047 
00048         ASSERT(netlib::sendMessage(nmsg),
00049             "Failed to send message!");
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::conn_id_t local = netlib::createUdpLocal(local_port);
00075         ASSERT(local, "null");
00076         DPRINTF("Local UDP connection: 0x%04lx", local);
00077 
00078         netlib::conn_id_t remote =
00079             netlib::createUdpRemote(local, server, remote_port);
00080         ASSERT(remote, "null");
00081         DPRINTF("Remote UDP connection: 0x%04lx", remote);
00082 
00083         std::istream& stream = std::cin;
00084 
00085         while (stream.good()) {
00086                 std::string line = getNextLineFromStream(stream, eParse_None);
00087 
00088                 DPRINTF("Line = %s", line.c_str());
00089 
00090                 sendMessage(remote, line);
00091                 for (int i = 0; i < 5; ++i) {
00092                         netlib::msg_t msg = netlib::getNextMessage(5000);
00093                         if (!msg.is_empty()) {
00094                                 DPRINTF("Got a message!");
00095                         }
00096                 }
00097         }
00098 
00099         std::string summary;
00100         perf::getTimingSummary(summary);
00101         DPRINTF("Timers:\n%s", summary.c_str());
00102 
00103         return 0;
00104 }
00105