reSIProcate/DialogUsageManager  9694
treg.cxx
Go to the documentation of this file.
00001 #include "rutil/Logger.hxx"
00002 #include "resip/dum/MasterProfile.hxx"
00003 #include "resip/dum/DumFeature.hxx"
00004 #include "resip/dum/OutgoingEvent.hxx"
00005 
00006 #include "CommandLineParser.hxx"
00007 #include "RegEventClient.hxx"
00008 
00009 using namespace resip;
00010 
00011 #define RESIPROCATE_SUBSYSTEM Subsystem::TEST
00012    
00013 // This feature will add a missing Contact header to incoming SIP messages on
00014 // 2xx responses to SUBSCRIBE or on NOTIFY requests. 
00015 class FixMissingContact : public DumFeature 
00016 {
00017    public:
00018       FixMissingContact(DialogUsageManager& dum, TargetCommand::Target& target) : 
00019          DumFeature(dum, target)
00020       {
00021       }
00022       
00023       virtual ProcessingResult process(Message* msg)
00024       {
00025          SipMessage* sip = dynamic_cast<SipMessage*>(msg);
00026 
00027          if (sip)
00028          {
00029             switch (sip->header(h_CSeq).method())
00030             {
00031                case NOTIFY:
00032                   if (sip->isRequest() && !sip->exists(h_Contacts) && sip->header(h_Contacts).empty())
00033                   {
00034                      sip->header(h_Contacts).push_back(sip->header(h_From));
00035                   }
00036                   break;
00037 
00038                case SUBSCRIBE:
00039                   if (sip->isResponse() && 
00040                       sip->header(h_StatusLine).statusCode() / 200 == 1 &&
00041                       !sip->exists(h_Contacts) && 
00042                       sip->header(h_Contacts).empty())
00043                   {
00044                      sip->header(h_Contacts).push_back(sip->header(h_To));                  
00045                   }
00046                   break;
00047 
00048                default:
00049                   break;
00050             }
00051          }
00052          return DumFeature::FeatureDone;
00053       }
00054 };
00055 
00056 
00057 // This feature is used to hardcode the host and port of the Contact header and
00058 // will apply to any outgoing SIP requests. 
00059 class OverrideContact : public DumFeature 
00060 {
00061    public:
00062       OverrideContact(const Uri& contact, 
00063                       DialogUsageManager& dum, 
00064                       TargetCommand::Target& target) : 
00065          DumFeature(dum, target),
00066          mContact(contact)
00067       {
00068       }
00069       
00070       virtual ProcessingResult process(Message* msg)
00071       {
00072          OutgoingEvent* og = dynamic_cast<OutgoingEvent*>(msg);
00073          if (og)
00074          {
00075             SharedPtr<SipMessage> sip = og->message();
00076             if (sip->isRequest() && 
00077                 sip->exists(h_Contacts) && 
00078                 sip->header(h_Contacts).size() == 1)
00079             {
00080                sip->header(h_Contacts).front().uri().host() = mContact.host();
00081                sip->header(h_Contacts).front().uri().port() = mContact.port();
00082             }
00083          }
00084          
00085          return DumFeature::FeatureDone;
00086       }
00087 
00088    private:
00089       Uri mContact;
00090 };
00091 
00092 
00093 class MyApp : public RegEventClient
00094 {
00095    public:
00096       MyApp(resip::SharedPtr<resip::MasterProfile> profile) : RegEventClient(profile)
00097       {
00098          SharedPtr<DumFeature> f1(new FixMissingContact(mDum, mDum.dumIncomingTarget()));
00099          mDum.addIncomingFeature(f1);
00100       }
00101 
00102       // optionally call this method once to override the host/port of contact
00103       // on outbound requests
00104       void overrideContact(const resip::Uri& contact)
00105       {
00106          // This is only necessary if the user agent is running behind a NAT. 
00107          SharedPtr<DumFeature> f2(new OverrideContact(contact, mDum, mDum.dumOutgoingTarget()));
00108          mDum.addOutgoingFeature(f2);
00109       }
00110       
00111       virtual void onRegEvent(const resip::Data& aor, const resip::Data& reg)
00112       {
00113          WarningLog (<< "Got result from " << aor << " --> " << reg);
00114       }
00115       
00116       virtual void onRegEventError(const resip::Data& aor, const resip::Data& reg)
00117       {
00118          WarningLog (<< "Got error for " << aor);
00119       }
00120 };
00121    
00122 int
00123 main(int argc, char** argv)
00124 {
00125    CommandLineParser clp(argc, argv);
00126    Log::initialize(clp.mLogType, clp.mLogLevel, argv[0]);
00127    
00128    SharedPtr<MasterProfile> profile(new MasterProfile);
00129 
00130    NameAddr from(clp.mAor);
00131    profile->setDefaultFrom(from);
00132    
00133    MyApp client(profile);
00134 
00135    // only necessary if behind a NAT
00136    client.overrideContact(clp.mContact);
00137    
00138    client.run();
00139    
00140    Uri a1(clp.mAor);
00141    client.watchAor(a1);
00142    
00143    sleep(3600);
00144    return 0;
00145 }
00146