reSIProcate/DialogUsageManager  9694
testIdentity.cxx
Go to the documentation of this file.
00001 #include "resip/dum/ClientAuthManager.hxx"
00002 #include "resip/dum/ClientRegistration.hxx"
00003 #include "resip/dum/DialogUsageManager.hxx"
00004 #include "resip/dum/MasterProfile.hxx"
00005 #include "resip/dum/Profile.hxx"
00006 #include "resip/dum/UserProfile.hxx"
00007 #include "resip/dum/RegistrationHandler.hxx"
00008 #include "resip/dum/ClientPagerMessage.hxx"
00009 #include "resip/dum/ServerPagerMessage.hxx"
00010 
00011 #include "resip/dum/DialogUsageManager.hxx"
00012 #include "resip/dum/AppDialogSet.hxx"
00013 #include "resip/dum/AppDialog.hxx"
00014 #include "resip/dum/RegistrationHandler.hxx"
00015 #include "resip/dum/PagerMessageHandler.hxx"
00016 #include "resip/stack/PlainContents.hxx"
00017 
00018 #include "resip/stack/external/HttpGetMessage.hxx"
00019 #include "curlHttp/CurlHttpProvider.hxx"
00020 
00021 #include "rutil/Log.hxx"
00022 #include "rutil/Logger.hxx"
00023 #include "rutil/Subsystem.hxx"
00024 
00025 #ifdef WIN32
00026 #include "resip/stack/ssl/WinSecurity.hxx"
00027 #endif
00028 
00029 #include <iostream>
00030 #include <string>
00031 #include <sstream>
00032 
00033 using namespace std;
00034 using namespace resip;
00035 
00036 #define RESIPROCATE_SUBSYSTEM Subsystem::TEST
00037 
00038 class TestIdentityHandler : public ClientPagerMessageHandler,
00039                             public ServerPagerMessageHandler,
00040                             public ClientRegistrationHandler
00041 {
00042    public:
00043       TestIdentityHandler() : _registered(false), _ended(false), _rcvd(false)
00044       {};
00045 
00046       bool isRegistered()
00047       {
00048          return _registered;
00049       };
00050 
00051       bool isEnded()
00052       {
00053          return _ended;
00054       };
00055 
00056       bool isRcvd()
00057       {
00058          return _rcvd;
00059       };
00060 
00061       virtual void onMessageArrived(ServerPagerMessageHandle handle,
00062                                     const SipMessage& message)
00063       {
00064          
00065          SipMessage ok = handle->accept();
00066          handle->send(ok);
00067 
00068          Contents *body = message.getContents();
00069 
00070          InfoLog( << "ServerPagerMessageHandler::onMessageArrived: "
00071                   << *body << "\n" );
00072 
00073          const SecurityAttributes *attr = message.getSecurityAttributes();
00074          InfoLog( << *attr );
00075          
00076          _rcvd = true;
00077       }
00078 
00079       virtual void onSuccess(ClientPagerMessageHandle,
00080                              const SipMessage& status)
00081       {
00082          InfoLog( << "ClientMessageHandler::onSuccess\n" );
00083          _ended = true;
00084       }
00085 
00086       virtual void onFailure(ClientPagerMessageHandle,
00087                              const SipMessage& status,
00088                              std::auto_ptr<Contents> contents)
00089       {
00090          InfoLog( << "ClientMessageHandler::onFailure\n" );
00091          _ended = true;
00092       }
00093 
00094       virtual void onSuccess(ClientRegistrationHandle,
00095                              const SipMessage& response)
00096       {
00097          InfoLog( << "ClientRegistrationHandler::onSuccess\n" );
00098          _registered = true;
00099       }
00100 
00101       virtual void onRemoved(ClientRegistrationHandle)
00102       {
00103          InfoLog( << "ClientRegistrationHander::onRemoved\n" );
00104          exit(-1);
00105       }
00106       
00107       virtual void onFailure(ClientRegistrationHandle,
00108                              const SipMessage& response)
00109       {
00110          InfoLog( << "ClientRegistrationHandler::onFailure\n" );
00111          exit(-1);
00112       }
00113 
00114       virtual int onRequestRetry(ClientRegistrationHandle,
00115                                  int retrySeconds, const SipMessage& response)
00116       {
00117          InfoLog( << "ClientRegistrationHandler::onRequestRetry\n" );
00118          exit(-1);
00119       }
00120 
00121    protected:
00122       bool _registered;
00123       bool _ended;
00124       bool _rcvd;
00125       
00126 };
00127 
00128 
00129 /*****************************************************************************/
00130 
00131 int main(int argc, char *argv[])
00132 {
00133 
00134    if ( argc < 3 ) {
00135       cout << "usage: " << argv[0] << " sip:user passwd\n";
00136       return 0;
00137    }
00138 
00139    Log::initialize(Log::Cout, Log::Debug, argv[0]);
00140 
00141    HttpProvider::setFactory(
00142       std::auto_ptr<HttpProviderFactory>(new CurlHttpProviderFactory()));
00143 
00144    bool first = true;
00145    NameAddr userAor(argv[1]);
00146    Data passwd(argv[2]);
00147 
00148    InfoLog(<< "user: " << userAor << ", passwd: " << passwd << "\n");
00149 
00150 #ifdef WIN32
00151    WinSecurity* security = new WinSecurity;
00152 #else
00153    Security* security = new Security;
00154 #endif
00155 
00156    assert( security );
00157 
00158    SipStack clientStack(security);
00159    DialogUsageManager clientDum(clientStack);
00160    clientDum.addTransport(UDP, 0, V4);
00161    clientDum.addTransport(TCP, 0, V4);
00162    clientDum.addTransport(TLS, 0, V4);
00163    // clientDum.addTransport(UDP, 0, V6);
00164    // clientDum.addTransport(TCP, 0, V6);
00165    // clientDum.addTransport(TLS, 0, V6);
00166 
00167    SharedPtr<MasterProfile> clientProfile(new MasterProfile);   
00168    auto_ptr<ClientAuthManager> clientAuth(new ClientAuthManager());   
00169    TestIdentityHandler clientHandler;
00170 
00171    clientDum.setMasterProfile(clientProfile);
00172    clientDum.setClientAuthManager(clientAuth);
00173    clientDum.setClientRegistrationHandler(&clientHandler);
00174    clientDum.setClientPagerMessageHandler(&clientHandler);
00175    clientDum.setServerPagerMessageHandler(&clientHandler);
00176    clientDum.getMasterProfile()->setDefaultRegistrationTime(70);                
00177    clientDum.getMasterProfile()->addSupportedMethod(MESSAGE);
00178    clientDum.getMasterProfile()->addSupportedMimeType(MESSAGE, Mime("text", "plain"));
00179 
00180    clientProfile->setDefaultFrom(userAor);
00181 
00182    InfoLog( << userAor.uri().host() << " " << userAor.uri().user() 
00183             << " " << passwd );
00184 
00185    clientProfile->setDigestCredential(userAor.uri().host(),
00186                                      userAor.uri().user(),
00187                                      passwd);
00188         
00189    SipMessage & regMessage = clientDum.makeRegistration(userAor);
00190         
00191    InfoLog( << regMessage << "Generated register: " << endl << regMessage );
00192    clientDum.send( regMessage );
00193 
00194    while (!clientHandler.isEnded() || !clientHandler.isRcvd() )
00195    {
00196       FdSet fdset;
00197 
00198       // Should these be buildFdSet on the DUM?
00199       clientStack.buildFdSet(fdset);
00200       int err = fdset.selectMilliSeconds(100);
00201       assert ( err != -1 );
00202       
00203       clientStack.process(fdset);
00204       while(clientDum.process());
00205       //if (!(n++ % 10)) cerr << "|/-\\"[(n/10)%4] << '\b';
00206                 
00207       if (first && clientHandler.isRegistered()) {
00208          first = false;
00209          InfoLog( << "client registered!!\n" );
00210          InfoLog( << "Sending MESSAGE\n" );
00211          ClientPagerMessageHandle cpmh = clientDum.makePagerMessage(userAor);                   
00212          auto_ptr<Contents> content(new PlainContents(Data("message")));
00213          cpmh.get()->page(content); 
00214       }
00215    } 
00216 
00217    return 0;
00218 }
00219 
00220 /* ====================================================================
00221  * The Vovida Software License, Version 1.0 
00222  * 
00223  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
00224  * 
00225  * Redistribution and use in source and binary forms, with or without
00226  * modification, are permitted provided that the following conditions
00227  * are met:
00228  * 
00229  * 1. Redistributions of source code must retain the above copyright
00230  *    notice, this list of conditions and the following disclaimer.
00231  * 
00232  * 2. Redistributions in binary form must reproduce the above copyright
00233  *    notice, this list of conditions and the following disclaimer in
00234  *    the documentation and/or other materials provided with the
00235  *    distribution.
00236  * 
00237  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00238  *    and "Vovida Open Communication Application Library (VOCAL)" must
00239  *    not be used to endorse or promote products derived from this
00240  *    software without prior written permission. For written
00241  *    permission, please contact vocal@vovida.org.
00242  *
00243  * 4. Products derived from this software may not be called "VOCAL", nor
00244  *    may "VOCAL" appear in their name, without prior written
00245  *    permission of Vovida Networks, Inc.
00246  * 
00247  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00248  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00249  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00250  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00251  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00252  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00253  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00254  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00255  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00256  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00257  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00258  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00259  * DAMAGE.
00260  * 
00261  * ====================================================================
00262  * 
00263  * This software consists of voluntary contributions made by Vovida
00264  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00265  * Inc.  For more information on Vovida Networks, Inc., please see
00266  * <http://www.vovida.org/>.
00267  *
00268  */