reSIProcate/stack  9694
testLockStep.cxx
Go to the documentation of this file.
00001 
00002 #ifndef WIN32
00003 #include <sys/time.h>
00004 #include <unistd.h>
00005 #include <sys/socket.h>
00006 #include <netinet/in.h>
00007 #include <arpa/inet.h>
00008 #endif
00009 
00010 #include <sys/types.h>
00011 #include <iostream>
00012 #include <memory>
00013 
00014 #include "resip/stack/Helper.hxx"
00015 #include "resip/stack/SipMessage.hxx"
00016 #include "resip/stack/Uri.hxx"
00017 #include "resip/stack/SipStack.hxx"
00018 #include "resip/stack/DeprecatedDialog.hxx"
00019 #include "rutil/Logger.hxx"
00020 #include "rutil/ThreadIf.hxx"
00021 
00022 
00023 using namespace resip;
00024 using namespace std;
00025 
00026 #ifdef WIN32
00027 #define usleep(x) Sleep(x/1000)
00028 #define sleep(x) Sleep(x*1000)
00029 #endif
00030 
00031 
00032 #define RESIPROCATE_SUBSYSTEM Subsystem::SIP
00033 
00034 
00035 class Client : public ThreadIf
00036 {
00037     public:
00038       Client(SipStack& stack) : mStack(stack)
00039       {}
00040       
00041       void thread()
00042       {
00043          InfoLog(<<"This is the Client");
00044 
00045          NameAddr dest;
00046          dest.uri().scheme() = "sip";
00047          dest.uri().user() = "fluffy";
00048          dest.uri().host() = "localhost";
00049          dest.uri().port() = 5080;
00050          dest.uri().param(p_transport) = "tcp";
00051          
00052          NameAddr from = dest;
00053          from.uri().port() = 5070;
00054          
00055          Data count(1);
00056    
00057          from.uri().user() = count;
00058          {
00059             auto_ptr<SipMessage> message(Helper::makeInvite( dest, from, from));
00060             mStack.send(*message);
00061          }
00062          
00063          bool done = false;
00064          //bool inviteState = true;
00065          
00066          
00067          while(true)
00068          {
00069             FdSet fdset;
00070             mStack.buildFdSet(fdset);
00071             int err = fdset.selectMilliSeconds(5);
00072             assert (err != -1);
00073             mStack.process(fdset);
00074             
00075             SipMessage* received = mStack.receive();
00076             if (received)
00077             {
00078                InfoLog (<< "Client received: " << received->brief());
00079                
00080                auto_ptr<SipMessage> forDel(received);
00081                if ( (received->isResponse()) )
00082                {
00083                   if ( received->header(h_StatusLine).responseCode() == 200 )
00084                   {
00085                      if (done)
00086                      {
00087                         break;
00088                      }
00089                      done = true;
00090                      
00091                      DebugLog(<< "Creating dialog.");
00092                      DeprecatedDialog dlog(from);
00093                         
00094                      DebugLog(<< "Creating dialog as UAC.");
00095                      dlog.createDialogAsUAC(*received);
00096                         
00097                      DebugLog(<< "making ack.");
00098                      auto_ptr<SipMessage> ack(dlog.makeAck(*received) );
00099                         
00100                      DebugLog(<< "making bye.");
00101                      auto_ptr<SipMessage> bye(dlog.makeBye());
00102                         
00103                      DebugLog(<< "Sending ack: << " << endl << *ack);
00104                      mStack.send(*ack);
00105                         
00106                      DebugLog(<< "Sending bye: << " << endl << *bye);
00107                      mStack.send(*bye);
00108                   }
00109                }
00110             }
00111             usleep(1000);
00112          }
00113       }
00114    private:
00115       SipStack& mStack;
00116 };
00117 
00118 class Server : public ThreadIf
00119 {
00120     public:
00121 
00122       Server(SipStack& stack) : mStack(stack)
00123       {}
00124       
00125       void thread()
00126       {
00127          InfoLog(<<"This is the Server");
00128 
00129          NameAddr dest;
00130          dest.uri().scheme() = "sip";
00131          dest.uri().user() = "fluffy";
00132          dest.uri().host() = "localhost";
00133          dest.uri().port() = 5080;
00134          dest.uri().param(p_transport) = "tcp";
00135 
00136          while(true)
00137          {
00138             FdSet fdset;
00139             mStack.buildFdSet(fdset);
00140             int err = fdset.selectMilliSeconds(5);
00141             assert (err != -1);
00142             mStack.process(fdset);
00143             
00144             SipMessage* received = mStack.receive();
00145             if (received)
00146             {
00147                auto_ptr<SipMessage> forDel(received);
00148                InfoLog ( << "Server recieved: " << received->brief());
00149                MethodTypes meth = received->header(h_RequestLine).getMethod();
00150                if ( meth == INVITE )
00151                {
00152                   Data localTag = Helper::computeTag(4);
00153                   auto_ptr<SipMessage> msg180(Helper::makeResponse(*received, 180, dest));
00154                   msg180->header(h_To).param(p_tag) = localTag;
00155                   mStack.send( *msg180);
00156                   
00157                   auto_ptr<SipMessage> msg200(Helper::makeResponse(*received, 200, dest));
00158                   msg200->header(h_To).param(p_tag) = localTag;
00159                   mStack.send(*msg200);
00160                }
00161                if ( meth == BYE)
00162                {
00163                   auto_ptr<SipMessage> msg200(Helper::makeResponse(*received, 200, dest));
00164                   InfoLog (<< "stack2 got bye - send 200 : " << *msg200 );   
00165                
00166                   mStack.send(*msg200);
00167                }
00168             }
00169          }
00170       }
00171    private:
00172       SipStack& mStack;
00173 };
00174 
00175 
00176 int
00177 main(int argc, char* argv[])
00178 {
00179    Log::initialize(Log::Cout, argc > 1 ? Log::toLevel(argv[1]) :  Log::Debug, argv[0]);
00180    Log::toLevel( Data("DEBUG") );
00181 
00182    SipStack stack1;
00183    stack1.addTransport(TCP, 5070);
00184 
00185    SipStack stack2;
00186    stack2.addTransport(TCP, 5080);
00187 
00188    Client client(stack1);
00189    ::Server server(stack2);
00190    
00191    client.run();
00192    server.run();
00193 
00194    client.join();
00195    server.join();
00196 
00197    InfoLog(<< "Test failed.");
00198 
00199    return 0;
00200 }
00201 /* ====================================================================
00202  * The Vovida Software License, Version 1.0 
00203  * 
00204  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
00205  * 
00206  * Redistribution and use in source and binary forms, with or without
00207  * modification, are permitted provided that the following conditions
00208  * are met:
00209  * 
00210  * 1. Redistributions of source code must retain the above copyright
00211  *    notice, this list of conditions and the following disclaimer.
00212  * 
00213  * 2. Redistributions in binary form must reproduce the above copyright
00214  *    notice, this list of conditions and the following disclaimer in
00215  *    the documentation and/or other materials provided with the
00216  *    distribution.
00217  * 
00218  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00219  *    and "Vovida Open Communication Application Library (VOCAL)" must
00220  *    not be used to endorse or promote products derived from this
00221  *    software without prior written permission. For written
00222  *    permission, please contact vocal@vovida.org.
00223  *
00224  * 4. Products derived from this software may not be called "VOCAL", nor
00225  *    may "VOCAL" appear in their name, without prior written
00226  *    permission of Vovida Networks, Inc.
00227  * 
00228  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00229  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00230  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00231  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00232  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00233  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00234  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00235  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00236  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00237  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00238  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00239  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00240  * DAMAGE.
00241  * 
00242  * ====================================================================
00243  * 
00244  * This software consists of voluntary contributions made by Vovida
00245  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00246  * Inc.  For more information on Vovida Networks, Inc., please see
00247  * <http://www.vovida.org/>.
00248  *
00249  */