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