From 3dcfcbc22881f69e6ce5cd3e92f7ccf8af7d58d1 Mon Sep 17 00:00:00 2001
From: Tristan Matthews <tristan.matthews@savoirfairelinux.com>
Date: Wed, 25 Jan 2012 16:52:08 -0500
Subject: [PATCH] * #8409: each account stores its own voiplink pointer

This avoids casts and is cleaner.
---
 daemon/src/account.cpp         |  1 -
 daemon/src/account.h           |  9 +--------
 daemon/src/iax/iaxaccount.cpp  | 14 +++++++++-----
 daemon/src/iax/iaxaccount.h    |  6 ++++++
 daemon/src/sip/sipaccount.cpp  | 32 +++++++++++++++++---------------
 daemon/src/sip/sipaccount.h    |  6 ++++++
 daemon/src/sip/sipvoiplink.cpp |  2 --
 7 files changed, 39 insertions(+), 31 deletions(-)

diff --git a/daemon/src/account.cpp b/daemon/src/account.cpp
index 5501e3aa94..af0b0689d7 100644
--- a/daemon/src/account.cpp
+++ b/daemon/src/account.cpp
@@ -38,7 +38,6 @@ Account::Account(const std::string& accountID, const std::string &type) :
     , username_()
     , hostname_()
     , alias_()
-    , link_(NULL)
     , enabled_(true)
     , type_(type)
     , registrationState_(Unregistered)
diff --git a/daemon/src/account.h b/daemon/src/account.h
index 76007b95f0..97af525490 100644
--- a/daemon/src/account.h
+++ b/daemon/src/account.h
@@ -188,9 +188,7 @@ class Account : public Serializable {
          * Get the voiplink pointer
          * @return VoIPLink* the pointer or 0
          */
-        VoIPLink* getVoIPLink() {
-            return link_;
-        }
+        virtual VoIPLink* getVoIPLink() = 0;
 
         /**
          * Register the underlying VoIPLink. Launch the event listener.
@@ -324,11 +322,6 @@ class Account : public Serializable {
          */
         std::string alias_;
 
-        /**
-         * Voice over IP Link contains a listener thread and calls
-         */
-        VoIPLink* link_;
-
         /**
          * Tells if the link is enabled, active.
          * This implies the link will be initialized on startup.
diff --git a/daemon/src/iax/iaxaccount.cpp b/daemon/src/iax/iaxaccount.cpp
index eda848c4a8..5c653c037a 100644
--- a/daemon/src/iax/iaxaccount.cpp
+++ b/daemon/src/iax/iaxaccount.cpp
@@ -38,10 +38,9 @@
 #include "manager.h"
 
 IAXAccount::IAXAccount(const std::string& accountID)
-    : Account(accountID, "iax2"), password_()
-{
-    link_ = new IAXVoIPLink(accountID);
-}
+    : Account(accountID, "iax2"), password_(),
+    link_(new IAXVoIPLink(accountID))
+{}
 
 
 IAXAccount::~IAXAccount()
@@ -160,7 +159,7 @@ IAXAccount::unregisterVoIPLink()
 {
     try {
         link_->sendUnregister(this);
-        dynamic_cast<IAXVoIPLink*>(link_)->terminate();
+        link_->terminate();
     } catch (const VoipLinkException &e) {
         ERROR("IAXAccount: %s", e.what());
     }
@@ -174,3 +173,8 @@ IAXAccount::loadConfig()
     enabled_ = false;
 #endif
 }
+
+VoIPLink* IAXAccount::getVoIPLink()
+{
+    return link_;
+}
diff --git a/daemon/src/iax/iaxaccount.h b/daemon/src/iax/iaxaccount.h
index 60b9a1acee..7df121d90c 100644
--- a/daemon/src/iax/iaxaccount.h
+++ b/daemon/src/iax/iaxaccount.h
@@ -32,6 +32,9 @@
 #define IAXACCOUNT_H
 
 #include "account.h"
+#include "noncopyable.h"
+
+class IAXVoIPLink;
 
 /**
  * @file: iaxaccount.h
@@ -64,8 +67,11 @@ class IAXAccount : public Account {
         }
 
     private:
+        NON_COPYABLE(IAXAccount);
          // Account login information: password
         std::string password_;
+        IAXVoIPLink *link_;
+        virtual VoIPLink* getVoIPLink();
 };
 
 #endif
diff --git a/daemon/src/sip/sipaccount.cpp b/daemon/src/sip/sipaccount.cpp
index 28359cdb5c..fa252a5221 100644
--- a/daemon/src/sip/sipaccount.cpp
+++ b/daemon/src/sip/sipaccount.cpp
@@ -87,9 +87,8 @@ SIPAccount::SIPAccount(const std::string& accountID)
     , zrtpNotSuppWarning_(true)
     , registrationStateDetailed_()
     , keepAliveTimer_()
-{
-    link_ = SIPVoIPLink::instance();
-}
+    , link_(SIPVoIPLink::instance())
+{}
 
 SIPAccount::~SIPAccount()
 {
@@ -241,7 +240,7 @@ void SIPAccount::serialize(Conf::YamlEmitter *emitter)
         delete node;
     }
 
-    
+
 }
 
 void SIPAccount::unserialize(Conf::MappingNode *map)
@@ -575,28 +574,27 @@ void SIPAccount::unregisterVoIPLink()
 }
 
 void SIPAccount::startKeepAliveTimer() {
-    pj_time_val keepAliveDelay_;
 
     if (isTlsEnabled())
         return;
 
+    pj_time_val keepAliveDelay_;
     keepAliveTimer_.cb = &SIPAccount::keepAliveRegistrationCb;
-    keepAliveTimer_.user_data = (void *)this; 
+    keepAliveTimer_.user_data = this;
 
     // expiration may no be determined when during the first registration request
-    if(registrationExpire_ == 0) {
+    if(registrationExpire_ == 0)
         keepAliveDelay_.sec = 60;
-    }
-    else {
+    else
         keepAliveDelay_.sec = registrationExpire_;
-    }
+
     keepAliveDelay_.msec = 0;
  
-    reinterpret_cast<SIPVoIPLink *>(link_)->registerKeepAliveTimer(keepAliveTimer_, keepAliveDelay_); 
+    link_->registerKeepAliveTimer(keepAliveTimer_, keepAliveDelay_);
 }
 
 void SIPAccount::stopKeepAliveTimer() {
-     reinterpret_cast<SIPVoIPLink *>(link_)->cancelKeepAliveTimer(keepAliveTimer_); 
+     link_->cancelKeepAliveTimer(keepAliveTimer_);
 }
 
 pjsip_ssl_method SIPAccount::sslMethodStringToPjEnum(const std::string& method)
@@ -771,8 +769,7 @@ std::string SIPAccount::getContactHeader() const
 
     // Else we determine this infor based on transport information
     std::string address, port;
-    SIPVoIPLink *siplink = dynamic_cast<SIPVoIPLink *>(link_);
-    siplink->findLocalAddressFromTransport(transport_, transportType_, address, port);
+    link_->findLocalAddressFromTransport(transport_, transportType_, address, port);
 
     // UDP does not require the transport specification
     if (transportType_ == PJSIP_TRANSPORT_TLS) {
@@ -798,7 +795,7 @@ void SIPAccount::keepAliveRegistrationCb(UNUSED pj_timer_heap_t *th, pj_timer_en
        // send a new register request
        sipAccount->registerVoIPLink();
 
-       // make sure the current timer is deactivated   
+       // make sure the current timer is deactivated
        sipAccount->stopKeepAliveTimer(); 
 
        // register a new timer
@@ -1013,3 +1010,8 @@ void SIPAccount::setTlsSettings(const std::map<std::string, std::string>& detail
     set_opt(details, TLS_NEGOTIATION_TIMEOUT_SEC, tlsNegotiationTimeoutSec_);
     set_opt(details, TLS_NEGOTIATION_TIMEOUT_MSEC, tlsNegotiationTimeoutMsec_);
 }
+
+VoIPLink* SIPAccount::getVoIPLink()
+{
+    return link_;
+}
diff --git a/daemon/src/sip/sipaccount.h b/daemon/src/sip/sipaccount.h
index 1ba3aadfb9..fcfc3fbbc5 100644
--- a/daemon/src/sip/sipaccount.h
+++ b/daemon/src/sip/sipaccount.h
@@ -672,6 +672,12 @@ class SIPAccount : public Account {
          */ 
         pj_timer_entry keepAliveTimer_;
 
+
+        /**
+         * Voice over IP Link contains a listener thread and calls
+         */
+        SIPVoIPLink* link_;
+        virtual VoIPLink* getVoIPLink();
 };
 
 #endif
diff --git a/daemon/src/sip/sipvoiplink.cpp b/daemon/src/sip/sipvoiplink.cpp
index aac4a75a14..f085c486c5 100644
--- a/daemon/src/sip/sipvoiplink.cpp
+++ b/daemon/src/sip/sipvoiplink.cpp
@@ -1669,8 +1669,6 @@ void registration_cb(struct pjsip_regc_cbparam *param)
 {
     SIPAccount *account = static_cast<SIPAccount *>(param->token);
 
-    ERROR("SipVoipLink: REGISTRATION CALLBACK");
-
     if (account == NULL) {
         ERROR("SipVoipLink: account does'nt exist in registration callback");
         return;
-- 
GitLab