reSIProcate/stack  9694
testUdp.cxx
Go to the documentation of this file.
00001 #if defined(HAVE_CONFIG_H)
00002 #include "config.h"
00003 #endif
00004 
00005 #include <iostream>
00006 
00007 #if defined (HAVE_POPT_H) 
00008 #include <popt.h>
00009 #else
00010 #ifndef WIN32
00011 #warning "will not work very well without libpopt"
00012 #endif
00013 #endif
00014 
00015 #include "resip/stack/UdpTransport.hxx"
00016 #include "resip/stack/Helper.hxx"
00017 #include "resip/stack/SipMessage.hxx"
00018 #include "resip/stack/Uri.hxx"
00019 #include "rutil/Data.hxx"
00020 #include "rutil/DnsUtil.hxx"
00021 #include "rutil/Logger.hxx"
00022 #include "rutil/DataStream.hxx"
00023 
00024 using namespace resip;
00025 using namespace std;
00026 
00027 #define RESIPROCATE_SUBSYSTEM Subsystem::SIP
00028 
00029 int
00030 main(int argc, char* argv[])
00031 {
00032    char* logType = 0;
00033    char* logLevel = 0;
00034    int runs = 100;
00035    int window = 10;
00036    int seltime = 100;
00037 
00038 #if defined (HAVE_POPT_H) 
00039    struct poptOption table[] = {
00040       {"log-type",    'l', POPT_ARG_STRING, &logType,   0, "where to send logging messages", "syslog|cerr|cout"},
00041       {"log-level",   'v', POPT_ARG_STRING, &logLevel,  0, "specify the default log level", "DEBUG|INFO|WARNING|ALERT"},
00042       {"num-runs",    'r', POPT_ARG_INT,    &runs,      0, "number of calls in test", 0},
00043       {"window-size", 'w', POPT_ARG_INT,    &window,    0, "number of registrations in test", 0},
00044       {"select-time", 's', POPT_ARG_INT,    &seltime,   0, "number of runs in test", 0},
00045       POPT_AUTOHELP
00046       { NULL, 0, 0, NULL, 0 }
00047    };
00048    
00049    poptContext context = poptGetContext(NULL, argc, const_cast<const char**>(argv), table, 0);
00050    poptGetNextOpt(context);
00051 #endif
00052 
00053 #ifdef WIN32
00054    Log::initialize(Log::Cout, Log::Debug, argv[0]);
00055    initNetwork();
00056 #else
00057    Log::initialize(logType, logLevel, argv[0]);
00058 #endif
00059 
00060    cout << "Performing " << runs << " runs." << endl;
00061    
00062    Fifo<TransactionMessage> txFifo;
00063    UdpTransport* sender = new UdpTransport(txFifo, 5070, V4, StunDisabled, Data::Empty);
00064 
00065    Fifo<TransactionMessage> rxFifo;
00066    UdpTransport* receiver = new UdpTransport(rxFifo, 5080, V4, StunDisabled, Data::Empty);
00067 
00068    NameAddr target;
00069    target.uri().scheme() = "sip";
00070    target.uri().user() = "fluffy";
00071    target.uri().host() = "bremen.internal.xten.net";
00072    target.uri().port() = 5080;
00073    target.uri().param(p_transport) = "udp";
00074    
00075    NameAddr from = target;
00076    from.uri().port() = 5070;
00077 
00078    InfoLog (<< "Creating messages");
00079 
00080    list<SipMessage*> messages;
00081    {
00082       UInt64 startTime = Timer::getTimeMs();
00083       for (int i=0; i<runs; i++)
00084       {
00085          SipMessage* m = Helper::makeInvite( target, from, from);      
00086          //m->header(h_Vias).front().transport() = Tuple::toData(sender->transport());
00087          //m->header(h_Vias).front().sentHost() = "localhost";
00088          //m->header(h_Vias).front().sentPort() = sender->port();
00089          messages.push_back(m);
00090       }
00091 
00092       UInt64 elapsed = Timer::getTimeMs() - startTime;
00093       cout << runs << " calls peformed in " << elapsed << " ms, a rate of " 
00094            << runs / ((float) elapsed / 1000.0) << " calls per second.]" << endl;
00095       
00096       InfoLog (<< "Messages created");
00097    }
00098    
00099    
00100    in_addr in;
00101    DnsUtil::inet_pton("127.0.0.1", in);
00102    Tuple dest(in, target.uri().port(), UDP);
00103    InfoLog (<< "Sending to " << dest);
00104    
00105    UInt64 startTime = Timer::getTimeMs();
00106 
00107    int tid=1;
00108    int outstanding=0;
00109    int count=0;
00110    while (!messages.empty())
00111    {
00112       if (count > 500) exit(0);
00113       // load up the send window
00114       while (outstanding < window)
00115       {
00116          Data encoded;
00117          {
00118             DataStream strm(encoded);
00119             SipMessage* next = messages.front();
00120             messages.pop_front();
00121             next->encode(strm);
00122             outstanding++;
00123          }
00124          std::auto_ptr<SendData> toSend(sender->makeSendData(dest, encoded, Data(tid++), Data::Empty));
00125          sender->send(toSend);
00126       }
00127 
00128       FdSet fdset; 
00129       receiver->buildFdSet(fdset);
00130       //sender->buildFdSet(fdset);
00131       fdset.selectMilliSeconds(seltime);
00132 
00133       receiver->process(fdset);
00134       sender->process(fdset);
00135       
00136       Message* msg;
00137       if (rxFifo.messageAvailable())
00138       {
00139          msg = rxFifo.getNext();
00140          SipMessage* received = dynamic_cast<SipMessage*>(msg);
00141          if (received)
00142          {
00143            received->encode(resipCout);
00144             outstanding--;
00145          
00146             assert (received->header(h_RequestLine).uri().host() == "localhost");
00147             assert (received->header(h_To).uri().host() == "localhost");
00148             assert (received->header(h_From).uri().host() == "localhost");
00149             //assert (!received->header(h_Vias).begin()->sentHost().empty());
00150             assert(received->header(h_Vias).begin()->param(p_received) == "127.0.0.1");
00151             assert (received->header(h_Contacts).begin()->uri().host() == "localhost");
00152             assert (!received->header(h_CallId).value().empty());
00153             delete received;
00154          }
00155       }
00156    }
00157 
00158    UInt64 elapsed = Timer::getTimeMs() - startTime;
00159    cout << runs << " calls peformed in " << elapsed << " ms, a rate of " 
00160         << runs / ((float) elapsed / 1000.0) << " calls per second.]" << endl;
00161 
00162    return 0;
00163 }
00164 /* ====================================================================
00165  * The Vovida Software License, Version 1.0 
00166  * 
00167  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
00168  * 
00169  * Redistribution and use in source and binary forms, with or without
00170  * modification, are permitted provided that the following conditions
00171  * are met:
00172  * 
00173  * 1. Redistributions of source code must retain the above copyright
00174  *    notice, this list of conditions and the following disclaimer.
00175  * 
00176  * 2. Redistributions in binary form must reproduce the above copyright
00177  *    notice, this list of conditions and the following disclaimer in
00178  *    the documentation and/or other materials provided with the
00179  *    distribution.
00180  * 
00181  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00182  *    and "Vovida Open Communication Application Library (VOCAL)" must
00183  *    not be used to endorse or promote products derived from this
00184  *    software without prior written permission. For written
00185  *    permission, please contact vocal@vovida.org.
00186  *
00187  * 4. Products derived from this software may not be called "VOCAL", nor
00188  *    may "VOCAL" appear in their name, without prior written
00189  *    permission of Vovida Networks, Inc.
00190  * 
00191  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00192  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00193  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00194  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00195  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00196  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00197  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00198  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00199  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00200  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00201  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00202  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00203  * DAMAGE.
00204  * 
00205  * ====================================================================
00206  * 
00207  * This software consists of voluntary contributions made by Vovida
00208  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00209  * Inc.  For more information on Vovida Networks, Inc., please see
00210  * <http://www.vovida.org/>.
00211  *
00212  */