reSIProcate/stack  9694
testNonInviteServerTx.cxx
Go to the documentation of this file.
00001 #include "resip/stack/SipStack.hxx"
00002 #include "resip/stack/Transport.hxx"
00003 #include "resip/stack/Uri.hxx"
00004 #include "resip/stack/Helper.hxx"
00005 #include "resip/stack/UdpTransport.hxx"
00006 
00007 #include "rutil/Logger.hxx"
00008 #include "rutil/DataStream.hxx"
00009 
00010 #include <sys/socket.h>
00011 #include <netinet/in.h>
00012 #include <arpa/inet.h>
00013 
00014 
00015 using namespace resip;
00016 using namespace std;
00017 
00018 #define RESIPROCATE_SUBSYSTEM Subsystem::APP
00019 #define CRLF "\r\n"
00020 
00021 
00022 SipStack* client=0;
00023 Fifo<Message> received;
00024 UdpTransport* server=0;
00025 
00026 void
00027 doit(int serverResponse, int expectedRetrans, int expectedClientResponse);
00028 
00029 
00030 int
00031 main(int argc, char *argv[])
00032 {
00033     Log::initialize(Log::Cout, Log::Debug, argv[0]);
00034 
00035     InfoLog( << "Starting up, making stack");
00036 
00037     client = new SipStack();
00038     client->addTransport(UDP, 5060);
00039 
00040     server = new UdpTransport("localhost", 5070, "default", received);
00041 
00042     
00043     // Test 1: 
00044     // client sends a reg, server does nothing, client should retransmit 10
00045     // times, client should receive 408
00046 
00047     // Test 2: 
00048     // client sends a reg, server sends 100, client should retransmit 7 times
00049     // client should receive 408
00050     
00051     // Test 3:
00052     // client sends a reg, server sends 200, client shouldn't retransmit at all
00053     // client should receive 200
00054 
00055     // Test 4:
00056     // client sends a reg, server sends 400, client shouldn't retransmit at all
00057     // client should receive 400
00058 
00059     //doit(100, 7, 408);
00060     doit(200, 1, 200);
00061     doit(400, 1, 400);
00062     doit(0, 10, 408);
00063     
00064     return 0;
00065 }
00066 
00067 
00068 void
00069 doit(int serverResponse, int expectedRetrans, int expectedClientResponse)
00070 {
00071    InfoLog (<< "Running test: " << serverResponse << " " << expectedRetrans << " " << expectedClientResponse);
00072    
00073     NameAddr me;
00074     me.uri().host() = "localhost";
00075     me.uri().port() = 5070;
00076     SipMessage* reg = Helper::makeRegister(me, me);
00077     Data encoded(2048, true);
00078     DataStream strm(encoded);
00079     reg->encode(strm);
00080     strm.flush();
00081 
00082     client->send(*reg);
00083 
00084     Data encodedResponse(2048, true);
00085     
00086     int count=0;
00087     while (1)
00088     {
00089        struct timeval tv;
00090        fd_set fdReadSet;
00091        int fdSetSize = 0;
00092        
00093        // Init the fd_set for the select()
00094        FD_ZERO(&fdReadSet);
00095        
00096        fdSetSize = 0;
00097        client->buildFdSet(&fdReadSet, &fdSetSize);
00098        server->buildFdSet(&fdReadSet, &fdSetSize);
00099        
00100        // block on fdset
00101        tv.tv_sec = 0;
00102        tv.tv_usec = 1000 * client->getTimeTillNextProcess();
00103 
00104        // get the sip message that we just sent and process it
00105        int err = select(fdSetSize, &fdReadSet, 0, 0, &tv);
00106        assert (err != -1);
00107        
00108        client->process(&fdReadSet);
00109        server->process(&fdReadSet);
00110        
00111        SipMessage* sipMessage = client->receive();
00112             
00113        if (sipMessage) 
00114        {
00115           InfoLog( << "got message (client)" << *sipMessage);
00116           assert(sipMessage->isResponse());
00117           assert(sipMessage->header(h_StatusLine).responseCode() == expectedClientResponse);
00118           assert(count == expectedRetrans);
00119           return;
00120        }
00121 
00122        client->process(&fdReadSet);
00123        if (received.messageAvailable())
00124        {
00125           count++;
00126           
00127           SipMessage* sip = dynamic_cast<SipMessage*>(received.getNext());
00128           assert(sip);
00129           InfoLog( << "got message (server)" << *sip);
00130 
00131           if (serverResponse)
00132           {
00133              SipMessage* response = Helper::makeResponse(*sip, serverResponse);
00134              DebugLog (<< "server sending response = " << endl << *response);
00135             
00136              DataStream strm(encodedResponse);
00137              response->encode(strm);
00138              strm.flush();
00139 
00140              // create address to send to
00141              struct sockaddr_in sa;
00142             
00143              sa.sin_family = PF_INET;
00144              sa.sin_addr.s_addr = inet_addr("127.0.0.1");
00145              sa.sin_port = htons(5060);
00146             
00147              server->send(sa, encodedResponse);
00148           }
00149          
00150           delete sip;
00151             
00152        }
00153 
00154        usleep(20);
00155     }
00156 }
00157 /* ====================================================================
00158  * The Vovida Software License, Version 1.0 
00159  * 
00160  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
00161  * 
00162  * Redistribution and use in source and binary forms, with or without
00163  * modification, are permitted provided that the following conditions
00164  * are met:
00165  * 
00166  * 1. Redistributions of source code must retain the above copyright
00167  *    notice, this list of conditions and the following disclaimer.
00168  * 
00169  * 2. Redistributions in binary form must reproduce the above copyright
00170  *    notice, this list of conditions and the following disclaimer in
00171  *    the documentation and/or other materials provided with the
00172  *    distribution.
00173  * 
00174  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00175  *    and "Vovida Open Communication Application Library (VOCAL)" must
00176  *    not be used to endorse or promote products derived from this
00177  *    software without prior written permission. For written
00178  *    permission, please contact vocal@vovida.org.
00179  *
00180  * 4. Products derived from this software may not be called "VOCAL", nor
00181  *    may "VOCAL" appear in their name, without prior written
00182  *    permission of Vovida Networks, Inc.
00183  * 
00184  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00185  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00186  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00187  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00188  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00189  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00190  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00191  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00192  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00193  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00194  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00195  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00196  * DAMAGE.
00197  * 
00198  * ====================================================================
00199  * 
00200  * This software consists of voluntary contributions made by Vovida
00201  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00202  * Inc.  For more information on Vovida Networks, Inc., please see
00203  * <http://www.vovida.org/>.
00204  *
00205  */