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