reSIProcate/stack  9694
testTls.cxx
Go to the documentation of this file.
00001 #if defined(HAVE_CONFIG_H)
00002 #include "config.h"
00003 #endif
00004 
00005 #if defined (HAVE_POPT_H) 
00006 #include <popt.h>
00007 #else
00008 #ifndef WIN32
00009 #warning "will not work very well without libpopt"
00010 #endif
00011 #endif
00012 
00013 #include <sys/types.h>
00014 #include <unistd.h> // for sleep
00015 #include <iostream>
00016 #include <memory>
00017 
00018 #ifdef __MINGW32__
00019 #define sleep(x) Sleep((x)*1000)
00020 #endif
00021 
00022 #include "rutil/DnsUtil.hxx"
00023 #include "rutil/Inserter.hxx"
00024 #include "rutil/Logger.hxx"
00025 #include "resip/stack/Helper.hxx"
00026 #include "resip/stack/SipMessage.hxx"
00027 #include "resip/stack/SipStack.hxx"
00028 #include "resip/stack/Uri.hxx"
00029 
00030 using namespace resip;
00031 using namespace std;
00032 
00033 #define RESIPROCATE_SUBSYSTEM Subsystem::SIP
00034 
00035 int
00036 main(int argc, char* argv[])
00037 {
00038 #ifdef USE_SSL
00039    char logType[] = "cout";
00040    char logLevel[] = "DEBUG";
00041 
00042    int window = 5*20;
00043    int seltime = 100;
00044 
00045    const int MaxStacks=100;
00046    int numStacks=20;
00047 
00048    //logLevel = "ALERT";
00049    //logLevel = "INFO";
00050 
00051 #if defined(HAVE_POPT_H)
00052    struct poptOption table[] = {
00053       {"log-type",    'l', POPT_ARG_STRING, &logType,   0, "where to send logging messages", "syslog|cerr|cout"},
00054       {"log-level",   'v', POPT_ARG_STRING, &logLevel,  0, "specify the default log level", "DEBUG|INFO|WARNING|ALERT"},
00055       {"num-stacks",    'r', POPT_ARG_INT,  &numStacks,      0, "number of calls in test", 0},
00056       {"window-size", 'w', POPT_ARG_INT,    &window,    0, "number of concurrent transactions", 0},
00057       {"select-time", 's', POPT_ARG_INT,    &seltime,   0, "number of runs in test", 0},
00058       POPT_AUTOHELP
00059       { NULL, 0, 0, NULL, 0 }
00060    };
00061    
00062    poptContext context = poptGetContext(NULL, argc, const_cast<const char**>(argv), table, 0);
00063    poptGetNextOpt(context);
00064 #endif
00065 
00066    int runs = 3*numStacks*numStacks;
00067 
00068    runs = 1;
00069    
00070    Log::initialize(logType, logLevel, argv[0]);
00071    cout << "Performing " << runs << " runs." << endl;
00072 
00073    IpVersion version = V4;
00074    Data bindInterface;
00075    //bindInterface = Data( "127.0.0.1" );
00076          
00077    SipStack* stack[MaxStacks];
00078    for ( int s=0; s<numStacks; s++)
00079    {
00080       stack[s] = new SipStack;
00081 
00082       Data domain = Data("example") + Data(s) +".com";
00083       
00084 #ifdef USE_DTLS
00085       stack[s]->addTransport(DTLS, 25000+s,version, StunDisabled, bindInterface, domain);
00086 #else
00087       stack[s]->addTransport(TLS,  25000+s,version, StunDisabled, bindInterface, domain);
00088 #endif
00089    }
00090    
00091    NameAddr target;
00092    target.uri().scheme() = "sip";
00093    target.uri().user() = "fluffy";
00094    target.uri().host() = Data("127.0.0.1");
00095    target.uri().port() = 25000;
00096 #ifdef USE_DTLS
00097    target.uri().param(p_transport) = "dtls";
00098 #else
00099    target.uri().param(p_transport) = "tls";
00100 #endif  
00101 
00102    NameAddr contact;
00103    contact.uri().scheme() = "sip";
00104    contact.uri().user() = "fluffy";
00105    contact.uri().host() = Data("127.0.0.1");
00106    contact.uri().port() = 25000;
00107 
00108    NameAddr from = target;
00109    from.uri().port() = 25000;
00110    
00111    UInt64 startTime = Timer::getTimeMs();
00112    int outstanding=0;
00113    int count = 0;
00114    int sent = 0;
00115    int msgMod=0;
00116    
00117    while (count < runs)
00118    {
00119       //InfoLog (<< "count=" << count << " messages=" << messages.size());
00120       
00121       // load up the send window
00122       while (sent < runs && outstanding < window)
00123       {
00124          DebugLog (<< "Sending " << count << " / " << runs << " (" << outstanding << ")");
00125 
00126          // send from stack s to to stack r 
00127          int s = msgMod%numStacks;
00128          int r = (msgMod/numStacks)%numStacks;
00129          msgMod++;
00130          if ( s == r ) 
00131          {
00132             continue;
00133          }
00134          
00135          target.uri().port() = 25000+r;
00136          from.uri().port() = 25000+s;
00137          contact.uri().port() = 25000+s;
00138    
00139          SipMessage* msg = Helper::makeRegister( target, from, contact);
00140          msg->header(h_Vias).front().sentPort() = 25000+s;
00141          stack[s]->send(*msg);
00142          outstanding++;
00143          sent++;
00144          delete msg;
00145       }
00146       
00147       FdSet fdset; 
00148       for ( int s=0; s<numStacks; s++)
00149       {
00150          stack[s]->buildFdSet(fdset);
00151       }
00152       fdset.selectMilliSeconds(seltime); 
00153       for ( int s=0; s<numStacks; s++)
00154       {
00155          stack[s]->process(fdset);
00156       }
00157        
00158       for ( int s=0; s<numStacks; s++)
00159       { 
00160          SipMessage* msg = stack[s]->receive();
00161          
00162          if ( msg )
00163          {
00164             if ( msg->isRequest() )
00165             {  
00166                assert(msg->isRequest());
00167                assert(msg->header(h_RequestLine).getMethod() == REGISTER);
00168                
00169                SipMessage* response = Helper::makeResponse(*msg, 200);
00170                stack[s]->send(*response);
00171                delete response;
00172                delete msg;
00173             }
00174             else
00175             { 
00176                assert(msg->isResponse());
00177                assert(msg->header(h_CSeq).method() == REGISTER);
00178                assert(msg->header(h_StatusLine).statusCode() == 200);
00179                outstanding--;
00180                count++;
00181                delete msg;
00182             }
00183          }
00184       }
00185    }
00186    InfoLog (<< "Finished " << count << " runs");
00187    
00188    UInt64 elapsed = Timer::getTimeMs() - startTime;
00189    cout << runs << " registrations peformed in " << elapsed << " ms, a rate of " 
00190         << runs / ((float) elapsed / 1000.0) << " transactions per second.]" << endl;
00191 #if defined(HAVE_POPT_H)
00192    poptFreeContext(context);
00193 #endif
00194 
00195    while (true)
00196    {
00197       sleep(10);
00198    }
00199 #endif 
00200    
00201    return 0;
00202 }
00203 /* ====================================================================
00204  * The Vovida Software License, Version 1.0 
00205  * 
00206  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
00207  * 
00208  * Redistribution and use in source and binary forms, with or without
00209  * modification, are permitted provided that the following conditions
00210  * are met:
00211  * 
00212  * 1. Redistributions of source code must retain the above copyright
00213  *    notice, this list of conditions and the following disclaimer.
00214  * 
00215  * 2. Redistributions in binary form must reproduce the above copyright
00216  *    notice, this list of conditions and the following disclaimer in
00217  *    the documentation and/or other materials provided with the
00218  *    distribution.
00219  * 
00220  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00221  *    and "Vovida Open Communication Application Library (VOCAL)" must
00222  *    not be used to endorse or promote products derived from this
00223  *    software without prior written permission. For written
00224  *    permission, please contact vocal@vovida.org.
00225  *
00226  * 4. Products derived from this software may not be called "VOCAL", nor
00227  *    may "VOCAL" appear in their name, without prior written
00228  *    permission of Vovida Networks, Inc.
00229  * 
00230  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00231  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00232  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00233  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00234  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00235  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00236  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00237  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00238  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00239  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00240  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00241  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00242  * DAMAGE.
00243  * 
00244  * ====================================================================
00245  * 
00246  * This software consists of voluntary contributions made by Vovida
00247  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00248  * Inc.  For more information on Vovida Networks, Inc., please see
00249  * <http://www.vovida.org/>.
00250  *
00251  */