reSIProcate/DialogUsageManager  9694
basicPublication.cxx
Go to the documentation of this file.
00001 // made by Aron Rosenberg
00002 // Will do an initial PUBLICATION of pidf presence
00003 // then do 2 in-dialog updates, then quit
00004 // If you define PUB_REALLY_FAST then overlapping
00005 // or queued PUBLISH'es will be created.
00006 
00007 #include "rutil/Log.hxx"
00008 #include "rutil/Logger.hxx"
00009 #include "rutil/Subsystem.hxx"
00010 #include "resip/dum/ClientAuthManager.hxx"
00011 #include "resip/dum/DialogUsageManager.hxx"
00012 #include "resip/dum/InviteSessionHandler.hxx"
00013 #include "resip/dum/MasterProfile.hxx"
00014 #include "resip/dum/Profile.hxx"
00015 #include "resip/dum/UserProfile.hxx"
00016 #include "resip/dum/PublicationHandler.hxx"
00017 #include "resip/dum/ClientPublication.hxx"
00018 
00019 #include "resip/dum/AppDialogSet.hxx"
00020 #include "resip/dum/AppDialog.hxx"
00021 #include "resip/stack/PlainContents.hxx"
00022 #include "resip/stack/Pidf.hxx"
00023 #include "rutil/Random.hxx"
00024 
00025 #include <iostream>
00026 #include <string>
00027 #include <sstream>
00028 
00029 using namespace std;
00030 using namespace resip;
00031 
00032 #define RESIPROCATE_SUBSYSTEM Subsystem::TEST
00033 
00034 //#define PUB_REALLY_FAST 1// Will create overlapping PUBLISH'es
00035 
00036 int transCount = 0;
00037 
00038 class ClientPubHandler : public ClientPublicationHandler {
00039 public:
00040    ClientPubHandler() {}
00041    virtual void onSuccess(ClientPublicationHandle cph, const SipMessage& status)
00042    {
00043       handle = cph;
00044       InfoLog(<<"ClientPubHandler::onSuccess\n");
00045       transCount--;
00046    }
00047    virtual void onRemove(ClientPublicationHandle cph, const SipMessage& status)
00048    {
00049           InfoLog(<<"ClientPubHandler::onRemove\n");
00050       handle = ClientPublicationHandle();
00051       transCount--;
00052    }
00053    virtual int onRequestRetry(ClientPublicationHandle cph, int retrySeconds, const SipMessage& status)
00054    {
00055       handle = cph;
00056       InfoLog(<<"ClientPubHandler::onRequestRetry\n");
00057       return 30;
00058    }
00059    virtual void onFailure(ClientPublicationHandle cph, const SipMessage& status)
00060    {
00061       InfoLog(<<"ClientPubHandler::onFailure\n");
00062       handle = ClientPublicationHandle();
00063       transCount--;
00064    }
00065    ClientPublicationHandle handle;
00066 };
00067 
00068 
00069 /*****************************************************************************/
00070 
00071 int main(int argc, char *argv[])
00072 {
00073    if( (argc < 5) || (argc > 6) ) {
00074       cout << "usage: " << argv[0] << " sip:aor user passwd realm [port]\n";
00075       return 0;
00076    }
00077 
00078    Log::initialize(Log::VSDebugWindow, Log::Debug, argv[0]);
00079 
00080    bool first = true;
00081    string aor(argv[1]);
00082    string user(argv[2]);
00083    string passwd(argv[3]);
00084    string realm(argv[4]);
00085    int port = 5060;
00086    if(argc == 6) {
00087       string temp(argv[5]);
00088       istringstream src(temp);
00089       src >> port;
00090    }
00091    Data eventName("presence");
00092 
00093    InfoLog(<< "log: aor: " << aor << ", port: " << port << "\n");
00094    InfoLog(<< "user: " << user << ", passwd: " << passwd << ", realm: " << realm << "\n");
00095    
00096    // sip logic
00097    SharedPtr<MasterProfile> profile(new MasterProfile);   
00098    auto_ptr<ClientAuthManager> clientAuth(new ClientAuthManager());   
00099 
00100    SipStack clientStack;
00101    DialogUsageManager clientDum(clientStack);
00102    clientDum.addTransport(UDP, port);
00103    clientDum.setMasterProfile(profile);
00104 
00105    clientDum.setClientAuthManager(clientAuth);
00106    clientDum.getMasterProfile()->addSupportedMethod(PUBLISH);
00107    clientDum.getMasterProfile()->addSupportedMimeType(PUBLISH,Pidf::getStaticType());
00108 
00109    ClientPubHandler cph;
00110    clientDum.addClientPublicationHandler(eventName,&cph);
00111 
00113    NameAddr naAor(aor.c_str());
00114    profile->setDefaultFrom(naAor);
00115    profile->setDigestCredential(realm.c_str(), user.c_str(), passwd.c_str());
00116    
00117    Pidf pidf;
00118    pidf.setSimpleStatus(true);
00119    pidf.setEntity(naAor.uri());
00120    pidf.setSimpleId(Random::getRandomHex(3));
00121 
00122    {
00123       SharedPtr<SipMessage> pubMessage = clientDum.makePublication(naAor, profile,pidf,eventName,120);
00124       InfoLog( << "Generated publish: " << endl << *pubMessage );
00125       transCount++;
00126       clientDum.send( pubMessage );
00127    }
00128 
00129    int nAttempts = 0;
00130    bool bKeepGoing = true;
00131    while(bKeepGoing)
00132    {
00133       FdSet fdset;
00134 
00135       // Should these be buildFdSet on the DUM?
00136       clientStack.buildFdSet(fdset);
00137       int err = fdset.selectMilliSeconds(100);
00138       assert ( err != -1 );
00139       
00140       clientStack.process(fdset);
00141         while(clientDum.process());
00142       //if (!(n++ % 10)) cerr << "|/-\\"[(n/10)%4] << '\b';
00143 
00144       if(transCount == 0) // No pending transactions.
00145       {
00146          nAttempts++;
00147          if(nAttempts > 2)
00148             bKeepGoing = false;
00149          else
00150          {
00151             int nLoops = 1;
00152 #ifdef PUB_REALLY_FAST
00153             nLoops = 2;
00154 #endif
00155             for(int i = 0; i < nLoops; i++)
00156             {
00157                pidf.setSimpleStatus(!pidf.getSimpleStatus()); //toggle status
00158                transCount++;
00159                cph.handle->update(&pidf);
00160             }
00161          }
00162       }
00163    }   
00164 
00165    return 0;
00166 }
00167 
00168 /* ====================================================================
00169  * The Vovida Software License, Version 1.0 
00170  * 
00171  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
00172  * 
00173  * Redistribution and use in source and binary forms, with or without
00174  * modification, are permitted provided that the following conditions
00175  * are met:
00176  * 
00177  * 1. Redistributions of source code must retain the above copyright
00178  *    notice, this list of conditions and the following disclaimer.
00179  * 
00180  * 2. Redistributions in binary form must reproduce the above copyright
00181  *    notice, this list of conditions and the following disclaimer in
00182  *    the documentation and/or other materials provided with the
00183  *    distribution.
00184  * 
00185  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00186  *    and "Vovida Open Communication Application Library (VOCAL)" must
00187  *    not be used to endorse or promote products derived from this
00188  *    software without prior written permission. For written
00189  *    permission, please contact vocal@vovida.org.
00190  *
00191  * 4. Products derived from this software may not be called "VOCAL", nor
00192  *    may "VOCAL" appear in their name, without prior written
00193  *    permission of Vovida Networks, Inc.
00194  * 
00195  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00196  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00197  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00198  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00199  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00200  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00201  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00202  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00203  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00204  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00205  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00206  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00207  * DAMAGE.
00208  * 
00209  * ====================================================================
00210  * 
00211  * This software consists of voluntary contributions made by Vovida
00212  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00213  * Inc.  For more information on Vovida Networks, Inc., please see
00214  * <http://www.vovida.org/>.
00215  *
00216  */