[ekiga] Made the SIP endpoint cache the default address of record (bug #565443)



commit 83b71c1b57b0cb1565694036e7dbac532dac4636
Author: Julien Puydt <jpuydt gnome org>
Date:   Mon Jun 22 20:45:38 2009 +0200

    Made the SIP endpoint cache the default address of record (bug #565443)
    
    It does it by watching the opal bank and scanning it for active
    accounts -- giving a preference to ekiga.net accounts.

 lib/engine/components/opal/sip-endpoint.cpp |   63 +++++++++++++++++++++------
 lib/engine/components/opal/sip-endpoint.h   |    7 +++-
 2 files changed, 55 insertions(+), 15 deletions(-)
---
diff --git a/lib/engine/components/opal/sip-endpoint.cpp b/lib/engine/components/opal/sip-endpoint.cpp
index a25d214..579e35a 100644
--- a/lib/engine/components/opal/sip-endpoint.cpp
+++ b/lib/engine/components/opal/sip-endpoint.cpp
@@ -108,12 +108,7 @@ Opal::Sip::EndPoint::EndPoint (Opal::CallManager & _manager,
 	core (_core)
 {
   gmref_ptr<Ekiga::ChatCore> chat_core = core.get ("chat-core");
-
-  {
-    gmref_ptr<Opal::Bank> smart = core.get ("opal-account-store");
-    smart->reference (); // take a reference in the main thread
-    bank = smart.get ();
-  }
+  gmref_ptr<Opal::Bank> bank = core.get ("opal-account-store");
 
   auto_answer_call = false;
   protocol_name = "sip";
@@ -123,6 +118,10 @@ Opal::Sip::EndPoint::EndPoint (Opal::CallManager & _manager,
   dialect = gmref_ptr<SIP::Dialect>(new SIP::Dialect (core, sigc::mem_fun (this, &Opal::Sip::EndPoint::send_message)));
   chat_core->add_dialect (dialect);
 
+  bank->account_added.connect (sigc::mem_fun (this, &Opal::Sip::EndPoint::on_bank_updated));
+  bank->account_removed.connect (sigc::mem_fun (this, &Opal::Sip::EndPoint::on_bank_updated));
+  bank->account_updated.connect (sigc::mem_fun (this, &Opal::Sip::EndPoint::on_bank_updated));
+
   /* Timeouts */
   SetAckTimeout (PTimeInterval (0, 32));
   SetPduCleanUpTimeout (PTimeInterval (0, 1));
@@ -148,7 +147,6 @@ Opal::Sip::EndPoint::EndPoint (Opal::CallManager & _manager,
 
 Opal::Sip::EndPoint::~EndPoint ()
 {
-  bank->unreference (); // leave a reference in the main thread
 }
 
 
@@ -177,6 +175,7 @@ Opal::Sip::EndPoint::menu_builder_add_actions (const std::string& fullname,
 {
   bool populated = false;
 
+  gmref_ptr<Opal::Bank> bank = core.get ("opal-account-store");
 
   std::list<std::string> uris;
   std::list<std::string> accounts;
@@ -1079,6 +1078,7 @@ Opal::Sip::EndPoint::GetRegisteredPartyName (const SIPURL & host,
   WORD port;
   PString url;
   SIPURL registration_address;
+  PWaitAndSignal mut(defaultAORMutex);
 
   /* If we are registered to an account corresponding to host, use it.
    */
@@ -1096,13 +1096,8 @@ Opal::Sip::EndPoint::GetRegisteredPartyName (const SIPURL & host,
      */
     if (host.GetHostAddress ().GetIpAndPort (address, port) && !manager.IsLocalAddress (address)) {
 
-      /* FIXME: this is the only place where we use the bank in a thread
-       * can't we just return GetDefaultDisplayName () ?
-       */
-      AccountPtr account = bank->find_account ("Ekiga.net");
-
-      if (account)
-        return SIPURL ("\"" + GetDefaultDisplayName () + "\" <" + PString(account->get_aor ()) + ">");
+      if ( !default_aor.empty ())
+        return SIPURL ("\"" + GetDefaultDisplayName () + "\" <" + PString(default_aor) + ">");
     }
   }
 
@@ -1241,6 +1236,7 @@ Opal::Sip::EndPoint::registration_event_in_main (const std::string aor,
 						 Opal::Account::RegistrationState state,
 						 const std::string msg)
 {
+  gmref_ptr<Opal::Bank> bank = core.get ("opal-account-store");
   AccountPtr account = bank->find_account (aor);
 
   if (account) {
@@ -1279,6 +1275,7 @@ void
 Opal::Sip::EndPoint::mwi_received_in_main (const std::string aor,
 					   const std::string info)
 {
+  gmref_ptr<Opal::Bank> bank = core.get ("opal-account-store");
   AccountPtr account = bank->find_account (aor);
 
   if (account) {
@@ -1286,3 +1283,41 @@ Opal::Sip::EndPoint::mwi_received_in_main (const std::string aor,
     account->handle_message_waiting_information (info);
   }
 }
+
+void
+Opal::Sip::EndPoint::on_bank_updated (Ekiga::ContactPtr /*contact*/)
+{
+  { // first we flush the existing value
+    PWaitAndSignal mut(defaultAORMutex);
+    default_aor = "";
+  }
+
+  { // and now we compute it again
+    gmref_ptr<Opal::Bank> bank = core.get ("opal-account-store");
+    bank->visit_accounts (sigc::mem_fun (this, &Opal::Sip::EndPoint::search_for_default_account));
+  }
+}
+
+bool
+Opal::Sip::EndPoint::search_for_default_account (Opal::AccountPtr account)
+{
+  PWaitAndSignal mut(defaultAORMutex);
+  bool result = true;
+
+  /* here is how result is computed here : first, remember it means to go on
+   * the search ; then we want the ekiga.net accounts to have some priority over
+   * others, so by default we want to go on. But if we find an account which is both
+   * suitable (active) and ekiga.net, then we want to stop.
+   */
+
+  if (account->is_active ()) {
+
+    default_aor = account->get_aor ();
+    if (account->get_type () == Opal::Account::Ekiga) {
+
+      result = false;
+    }
+  }
+
+  return result;
+}
diff --git a/lib/engine/components/opal/sip-endpoint.h b/lib/engine/components/opal/sip-endpoint.h
index ad0b08f..23b5419 100644
--- a/lib/engine/components/opal/sip-endpoint.h
+++ b/lib/engine/components/opal/sip-endpoint.h
@@ -230,8 +230,13 @@ namespace Opal {
       std::list<std::string> to_subscribe_uris;  // List of uris to subscribe
       std::list<std::string> subscribed_uris;    // List of subscribed uris
       std::list<std::string> aors;               // List of registered aor
+
+      PMutex defaultAORMutex;
+      std::string default_aor;
+      void on_bank_updated (Ekiga::ContactPtr contact);
+      bool search_for_default_account(Opal::AccountPtr account);
+
       Ekiga::ServiceCore & core;
-      Opal::Bank* bank;
 
       Ekiga::CallProtocolManager::Interface listen_iface;
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]