reSIProcate/stack  9694
InviteClient.cxx
Go to the documentation of this file.
00001 #include <memory>
00002 
00003 #include "rutil/Logger.hxx"
00004 #include "rutil/Timer.hxx"
00005 #include "resip/stack/DeprecatedDialog.hxx"
00006 #include "resip/stack/SipMessage.hxx"
00007 #include "resip/stack/Helper.hxx"
00008 #include "Resolver.hxx"
00009 
00010 
00011 #include "InviteClient.hxx"
00012 #include "Transceiver.hxx"
00013 
00014 using namespace resip;
00015 using namespace Loadgen;
00016 using namespace std;
00017 
00018 #define RESIPROCATE_SUBSYSTEM Subsystem::SIP
00019 
00020 InviteClient::InviteClient(Transceiver& transceiver, const resip::Uri& proxy, 
00021                            int firstExtension, int lastExtension, 
00022                            int numInvites)
00023    : mTransceiver(transceiver),
00024      mProxy(proxy),
00025      mFirstExtension(firstExtension),
00026      mLastExtension(lastExtension),
00027      mNumInvites(numInvites)
00028 {
00029    if (mNumInvites == 0)
00030    {
00031       mNumInvites = (mLastExtension - mFirstExtension) / 2;
00032    }
00033 }
00034 
00035 void
00036 InviteClient::go()
00037 {
00038    int numInvited = 0;
00039    
00040    Resolver target(mProxy);
00041 
00042    NameAddr to;
00043    to.uri() = mProxy;
00044    
00045    NameAddr from;
00046    from.uri() = mProxy;
00047    
00048    NameAddr contact;
00049    contact.uri() = mTransceiver.contactUri();
00050    
00051    UInt64 startTime = Timer::getTimeMs();
00052    InfoLog(<< "Invite client is attempting " << mNumInvites << " calls.");
00053    while (numInvited < mNumInvites)
00054    {
00055       for (int i=mFirstExtension; i < mLastExtension-1 && numInvited < mNumInvites; i+=2)
00056       {
00057          contact.uri().user() = Data(i);
00058          from.uri().user() = Data(i);
00059          to.uri().user() = Data(i+1);
00060          
00061          auto_ptr<SipMessage> invite(Helper::makeInvite(to, from, contact));
00062          
00063          mTransceiver.send(target, *invite);
00064          
00065          try
00066          {
00067             auto_ptr<SipMessage> i_100(waitForResponse(100, 1000));
00068             auto_ptr<SipMessage> i_180(waitForResponse(180, 1000));
00069             auto_ptr<SipMessage> i_200(waitForResponse(200, 1000));
00070 
00071             DebugLog(<< "Creating dialog.");
00072             
00073             DeprecatedDialog dlog(contact);
00074 
00075             DebugLog(<< "Creating dialog as UAC.");
00076             dlog.createDialogAsUAC(*i_200);
00077             
00078             DebugLog(<< "making ack.");
00079             auto_ptr<SipMessage> ack(dlog.makeAck(*invite));
00080             DebugLog(<< "making bye.");
00081             auto_ptr<SipMessage> bye(dlog.makeBye());
00082 
00083             DebugLog(<< "Sending ack: << *ack");
00084             
00085             mTransceiver.send(*ack);
00086             mTransceiver.send(*bye);
00087             auto_ptr<SipMessage> b_200(waitForResponse(200, 1000));
00088             numInvited++;
00089          }
00090          catch(Exception e)
00091          {
00092             ErrLog(<< "Proxy not responding.");
00093             exit(-1);
00094          }
00095       }
00096    }
00097    UInt64 elapsed = Timer::getTimeMs() - startTime;
00098    cout << mNumInvites << " peformed in " << elapsed << " ms, a rate of " 
00099         << mNumInvites / ((float) elapsed / 1000.0) << " calls per second." << endl;
00100 }
00101 
00102 SipMessage* 
00103 InviteClient::waitForResponse(int responseCode,
00104                               int waitMs)
00105 {
00106    DebugLog(<< "Waiting for a " << responseCode << " for " << waitMs  << "ms");
00107    SipMessage* reg = mTransceiver.receive(waitMs);
00108    DebugLog(<< "Finished waiting for " << responseCode);
00109    if(reg)
00110    {         
00111       if (reg->isResponse() &&
00112           reg->header(h_StatusLine).responseCode() == responseCode)
00113       {
00114          return reg;
00115       }
00116       else
00117       {
00118          throw Exception("Invalid response.", __FILE__, __LINE__);
00119       }
00120    }
00121    else
00122    {
00123       throw Exception("Timed out.", __FILE__, __LINE__);
00124    }
00125 }
00126 
00127 /* ====================================================================
00128  * The Vovida Software License, Version 1.0 
00129  * 
00130  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
00131  * 
00132  * Redistribution and use in source and binary forms, with or without
00133  * modification, are permitted provided that the following conditions
00134  * are met:
00135  * 
00136  * 1. Redistributions of source code must retain the above copyright
00137  *    notice, this list of conditions and the following disclaimer.
00138  * 
00139  * 2. Redistributions in binary form must reproduce the above copyright
00140  *    notice, this list of conditions and the following disclaimer in
00141  *    the documentation and/or other materials provided with the
00142  *    distribution.
00143  * 
00144  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00145  *    and "Vovida Open Communication Application Library (VOCAL)" must
00146  *    not be used to endorse or promote products derived from this
00147  *    software without prior written permission. For written
00148  *    permission, please contact vocal@vovida.org.
00149  *
00150  * 4. Products derived from this software may not be called "VOCAL", nor
00151  *    may "VOCAL" appear in their name, without prior written
00152  *    permission of Vovida Networks, Inc.
00153  * 
00154  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00155  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00156  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00157  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00158  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00159  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00160  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00161  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00162  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00163  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00164  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00165  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00166  * DAMAGE.
00167  * 
00168  * ====================================================================
00169  * 
00170  * This software consists of voluntary contributions made by Vovida
00171  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00172  * Inc.  For more information on Vovida Networks, Inc., please see
00173  * <http://www.vovida.org/>.
00174  *
00175  */