reSIProcate/stack  9694
testServer.cxx
Go to the documentation of this file.
00001 #ifndef WIN32
00002 #include <sys/time.h>
00003 #include <unistd.h>
00004 #include <sys/socket.h>
00005 #include <netinet/in.h>
00006 #include <arpa/inet.h>
00007 #endif
00008 
00009 #include <sys/types.h>
00010 #include <iostream>
00011 #include <memory>
00012 
00013 #include "resip/stack/Helper.hxx"
00014 #include "resip/stack/SipMessage.hxx"
00015 #include "resip/stack/Uri.hxx"
00016 #include "resip/stack/SipStack.hxx"
00017 #include "rutil/Logger.hxx"
00018 #include "rutil/ThreadIf.hxx"
00019 
00020 using namespace resip;
00021 using namespace std;
00022 
00023 #define RESIPROCATE_SUBSYSTEM Subsystem::TEST
00024 
00025 class Server : public ThreadIf
00026 {
00027     public:
00028 
00029       Server(SipStack& stack, int numCalls, TransportType transport) 
00030          : mStack(stack), 
00031            mNumCalls(numCalls),
00032            mTransport(transport)
00033       {}
00034       
00035       void thread()
00036       {
00037          InfoLog(<<"This is the Server");
00038 
00039          UInt64 startTime = Timer::getTimeMs();
00040 
00041          NameAddr contact;
00042          contact.uri().scheme() = "sip";
00043          contact.uri().user() = "fluffy";
00044          
00045          contact.uri().host() = SipStack::getHostname();
00046          contact.uri().port() = 5070;
00047          contact.uri().param(p_transport) = Tuple::toData(mTransport);
00048          
00049          int calls = mNumCalls;
00050          while(calls > 0)
00051          {
00052             FdSet fdset;
00053             mStack.buildFdSet(fdset);
00054             int err = fdset.selectMilliSeconds(0);
00055             assert (err != -1);
00056             mStack.process(fdset);
00057             
00058             SipMessage* received = mStack.receive();
00059             if (received)
00060             {
00061                auto_ptr<SipMessage> forDel(received);
00062                MethodTypes meth = received->header(h_RequestLine).getMethod();
00063                ErrLog ( << "Server received: " << getMethodName(meth));
00064                if ( meth == INVITE )
00065                {
00066                   Data localTag = Helper::computeTag(4);
00067                   auto_ptr<SipMessage> msg180(Helper::makeResponse(*received, 180, contact));
00068                   msg180->header(h_To).param(p_tag) = localTag;
00069                   ErrLog( << "Sent 180");
00070                   mStack.send( *msg180);
00071 
00072                   auto_ptr<SipMessage> msg200(Helper::makeResponse(*received, 200, contact));
00073                   msg200->header(h_To).param(p_tag) = localTag;
00074                   ErrLog( << "Sent 200");
00075                   mStack.send(*msg200);
00076                }
00077                if ( meth == BYE)
00078                {
00079                   auto_ptr<SipMessage> msg200(Helper::makeResponse(*received, 200, contact));
00080                   calls--;
00081                   ErrLog( << "Sent 200 to BYE");
00082                   mStack.send(*msg200);
00083                }
00084             }
00085          }
00086          UInt64 endTime = Timer::getTimeMs();
00087 
00088          CritLog(<< "Completed: " << mNumCalls << " calls in " << endTime - startTime << "ms, " 
00089                  << mNumCalls*1000 / (float)(endTime - startTime) << " CPS");
00090       }
00091    private:
00092       SipStack& mStack;
00093       int mNumCalls;
00094       TransportType mTransport;
00095 };
00096 
00097 int
00098 main(int argc, char* argv[])
00099 {
00100    if (argc != 4)
00101    {
00102       cerr << argv[0] << " LOG_LEVEL NUM_CALLS PROTOCOL" << endl;
00103       exit(-1);
00104    } 
00105    Log::initialize(Log::Cout, Log::toLevel(argv[1]), argv[0]);
00106    SipStack stack;
00107 
00108 
00109    TransportType protocol = UDP;
00110    if (strcasecmp(argv[3], "UDP") == 0)
00111    {
00112       protocol = UDP;
00113    }
00114    else if (strcasecmp(argv[3], "TCP") == 0)
00115    {
00116       protocol = TCP;
00117    }
00118    else
00119    {
00120       cerr << argv[0] << " LOG_LEVEL TARGET_URI PROTOCOL" << endl;
00121    }
00122 
00123    stack.addTransport(protocol, 5070);
00124 
00125 
00126    int numCalls = atoi(argv[2]);
00127 
00128    if (numCalls == 0)
00129    {
00130       cerr << argv[0] << " LOG_LEVEL NUM_CALLS" << endl;
00131       exit(-1);
00132    } 
00133 
00134    ::Server server(stack, numCalls, protocol);
00135    
00136    server.run();
00137    server.join();
00138    return 0;
00139 }
00140 /* ====================================================================
00141  * The Vovida Software License, Version 1.0 
00142  * 
00143  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
00144  * 
00145  * Redistribution and use in source and binary forms, with or without
00146  * modification, are permitted provided that the following conditions
00147  * are met:
00148  * 
00149  * 1. Redistributions of source code must retain the above copyright
00150  *    notice, this list of conditions and the following disclaimer.
00151  * 
00152  * 2. Redistributions in binary form must reproduce the above copyright
00153  *    notice, this list of conditions and the following disclaimer in
00154  *    the documentation and/or other materials provided with the
00155  *    distribution.
00156  * 
00157  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00158  *    and "Vovida Open Communication Application Library (VOCAL)" must
00159  *    not be used to endorse or promote products derived from this
00160  *    software without prior written permission. For written
00161  *    permission, please contact vocal@vovida.org.
00162  *
00163  * 4. Products derived from this software may not be called "VOCAL", nor
00164  *    may "VOCAL" appear in their name, without prior written
00165  *    permission of Vovida Networks, Inc.
00166  * 
00167  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00168  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00169  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00170  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00171  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00172  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00173  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00174  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00175  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00176  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00177  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00178  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00179  * DAMAGE.
00180  * 
00181  * ====================================================================
00182  * 
00183  * This software consists of voluntary contributions made by Vovida
00184  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00185  * Inc.  For more information on Vovida Networks, Inc., please see
00186  * <http://www.vovida.org/>.
00187  *
00188  */