diff --git a/src/Makefile.am b/src/Makefile.am index 7d1605fbb96c8e6629e188f23333acc68eaa1651..b49de9425b7da16f7e877fdd11e68b0ba9f1f19e 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -15,7 +15,8 @@ SUBDIRS = audio config gui $(ZEROCONFDIR) sflphoned_SOURCES = call.cpp eventthread.cpp main.cpp sipvoiplink.cpp voIPLink.cpp \ sipcall.cpp managerimpl.cpp \ - observer.cpp account.cpp + observer.cpp \ + account.cpp sipaccount.cpp aixaccount.cpp accountcreator.cpp sflphoned_CXXFLAGS = -DPREFIX=\"$(prefix)\" -DPROGSHAREDIR=\"${datadir}/sflphone\" $(ZEROCONFFLAGS) @@ -38,4 +39,5 @@ libsflphone_la_SOURCES = noinst_LTLIBRARIES = libsflphone.la noinst_HEADERS = managerimpl.h manager.h global.h observer.h eventthread.h sipvoiplink.h user_cfg.h \ - call.h voIPLink.h sipcall.h account.h + call.h voIPLink.h sipcall.h \ + account.h sipaccount.h aixaccount.h accountcreator.h diff --git a/src/account.h b/src/account.h index 3d04b10ef8437f4206a469068789b35288f385de..6415bc230bbaf9c5005447beaa60c4b6d9950697 100644 --- a/src/account.h +++ b/src/account.h @@ -23,6 +23,7 @@ class VoIPLink; typedef std::string AccountID; +#define AccountNULL "" /** @author Yan Morin @@ -36,13 +37,6 @@ public: ~Account(); - /** - * Create a unique voIPLink() depending on the protocol - * Multiple call to this function do nothing (if the voiplink pointer is 0) - * @return false if an error occurs - */ - virtual bool createVoIPLink() = 0; - /** * Register the account * @return false is an error occurs @@ -68,6 +62,13 @@ public: virtual bool terminate() = 0; private: + /** + * Create a unique voIPLink() depending on the protocol + * Multiple call to this function do nothing (if the voiplink pointer is 0) + * @return false if an error occurs + */ + virtual bool createVoIPLink() = 0; + /** * Account ID are assign in constructor and shall not changed */ diff --git a/src/accountcreator.cpp b/src/accountcreator.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b013d09bfff229ebd3d793dbf8a3ad8d1d86399a --- /dev/null +++ b/src/accountcreator.cpp @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2006 Savoir-Faire Linux inc. + * Author: Yan Morin <yan.morin@savoirfairelinux.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +#include "accountcreator.h" +#include "sipaccount.h" +#include "aixaccount.h" + +AccountCreator::AccountCreator() +{ +} + + +AccountCreator::~AccountCreator() +{ +} + +Account* +AccountCreator::createAccount(AccountType type, AccountID accountID) +{ + switch(type) { + case SIP_ACCOUNT: + return new SIPAccount(accountID); + break; + + case AIX_ACCOUNT: + return new AIXAccount(accountID); + break; + } + return 0; +} + diff --git a/src/accountcreator.h b/src/accountcreator.h new file mode 100644 index 0000000000000000000000000000000000000000..b38d78b0263a3d397909f1e84b415c2ec61f72bc --- /dev/null +++ b/src/accountcreator.h @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2006 Savoir-Faire Linux inc. + * Author: Yan Morin <yan.morin@savoirfairelinux.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +#ifndef ACCOUNTCREATOR_H +#define ACCOUNTCREATOR_H + +#include "account.h" + +class Account; + +/** + @author Yan Morin <yan.morin@gmail.com> + AccountCreator create Protocol-specific Account +*/ +class AccountCreator{ +public: + ~AccountCreator(); + /** + * Public account type + */ + enum AccountType {SIP_ACCOUNT, AIX_ACCOUNT }; + + /** + * Create a new account or null + * @param type type of the account + * @param accountID accountID (must be unique for each account) + */ + static Account* createAccount(AccountType type, AccountID accountID); + +private: + /** Hidden creator */ + AccountCreator(); +}; + +#endif diff --git a/src/aixaccount.cpp b/src/aixaccount.cpp new file mode 100644 index 0000000000000000000000000000000000000000..408bb41091b4f418ddd930f5e83546ccde7fe195 --- /dev/null +++ b/src/aixaccount.cpp @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2006 Savoir-Faire Linux inc. + * Author: Yan Morin <yan.morin@savoirfairelinux.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +#include "aixaccount.h" + +AIXAccount::AIXAccount(const AccountID& accountID) + : Account(accountID) +{ +} + + +AIXAccount::~AIXAccount() +{ +} + +/* virtual Account function implementation */ +bool +AIXAccount::createVoIPLink() +{ + return false; +} + +bool +AIXAccount::registerAccount() +{ + return false; +} + +bool +AIXAccount::unregisterAccount() +{ + return false; +} + +bool +AIXAccount::init() +{ + return false; +} + +bool +AIXAccount::terminate() +{ + return false; +} + + diff --git a/src/aixaccount.h b/src/aixaccount.h new file mode 100644 index 0000000000000000000000000000000000000000..a90f1f67c0b357ca65eecd017c5531f4f245b23c --- /dev/null +++ b/src/aixaccount.h @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2006 Savoir-Faire Linux inc. + * Author: Yan Morin <yan.morin@savoirfairelinux.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +#ifndef AIXACCOUNT_H +#define AIXACCOUNT_H + +#include "account.h" + +/** + @author Yan Morin <yan.morin@gmail.com> + An AIX Account specify AIX specific functions and objects (AIXCall/AIXVoIPLink) +*/ +class AIXAccount : public Account +{ +public: + AIXAccount(const AccountID& accountID); + + ~AIXAccount(); + + /* virtual Account function implementation */ + bool registerAccount(); + bool unregisterAccount(); + bool init(); + bool terminate(); + +private: + /* virtual Account function implementation */ + bool createVoIPLink(); +}; + +#endif diff --git a/src/managerimpl.cpp b/src/managerimpl.cpp index cf1779d5f933cc02b1c397d546ecbf7596964d26..d737a7fce8f0a7331d955959bc024d78211e3e4f 100644 --- a/src/managerimpl.cpp +++ b/src/managerimpl.cpp @@ -37,6 +37,7 @@ #include "audio/audiocodec.h" #include "audio/tonelist.h" +#include "accountcreator.h" // create new account #include "sipvoiplink.h" #include "voIPLink.h" #include "call.h" @@ -106,9 +107,11 @@ void ManagerImpl::init() { #ifdef TEST + testAccountMap(); + loadAccountMap(); testCallAccountMap(); + unloadAccountMap(); #endif - initVolume(); if (_exist == 0) { @@ -154,6 +157,7 @@ ManagerImpl::init() // Set a sip voip link by default _voIPLinkVector.push_back(new SipVoIPLink()); + loadAccountMap(); // initRegisterVoIP was here, but we doing it after the gui loaded... // the stun detection is long, so it's a better idea to do it after getEvents initZeroconf(); @@ -182,6 +186,8 @@ void ManagerImpl::terminate() _callVector.clear(); _mutex.leaveMutex(); + unloadAccountMap(); + _debug("Unload DTMF Key\n"); delete _dtmfKey; @@ -1787,14 +1793,14 @@ ManagerImpl::setSwitch(const std::string& switchName) { bool ManagerImpl::associateCallToAccount(CALLID callID, const AccountID& accountID) { - if (getAccountFromCall(callID) == "") { // nothing with the same ID - //if ( accountExists(accountID) ) { // account id exist in AccountMap + if (getAccountFromCall(callID) == AccountNULL) { // nothing with the same ID + if ( accountExists(accountID) ) { // account id exist in AccountMap ost::MutexLock m(_callAccountMapMutex); _callAccountMap[callID] = accountID; return true; - //} else { - // return false; - //} + } else { + return false; + } } else { return false; } @@ -1806,7 +1812,7 @@ ManagerImpl::getAccountFromCall(const CALLID callID) ost::MutexLock m(_callAccountMapMutex); CallAccountMap::iterator iter = _callAccountMap.find(callID); if ( iter == 0 || iter == _callAccountMap.end()) { - return ""; + return AccountNULL; } else { return iter->second; } @@ -1827,19 +1833,71 @@ ManagerImpl::getNewCallID() { CALLID random_id = (unsigned)rand(); // when it's not found, it return "" - while (getAccountFromCall(random_id) != "") { + while (getAccountFromCall(random_id) != AccountNULL) { random_id = rand(); } return random_id; } + + +AccountMap _accountMap; + +short +ManagerImpl::loadAccountMap() +{ + short nbAccount = 0; + + + AccountID accID = "acc0"; + _accountMap[accID] = AccountCreator::createAccount(AccountCreator::SIP_ACCOUNT, accID); + nbAccount++; + + accID = "acc1"; + _accountMap[accID] = AccountCreator::createAccount(AccountCreator::AIX_ACCOUNT, accID); + nbAccount++; + + return nbAccount; +} + +void +ManagerImpl::unloadAccountMap() +{ + AccountMap::iterator iter = _accountMap.begin(); + while ( iter != _accountMap.end() ) { + delete iter->second; iter->second = 0; + iter++; + } + _accountMap.clear(); +} + +bool +ManagerImpl::accountExists(AccountID accountID) +{ + AccountMap::iterator iter = _accountMap.find(accountID); + if ( iter == 0 || iter == _accountMap.end() ) { + return false; + } + return true; +} + +Account* +ManagerImpl::getAccount(AccountID accountID) +{ + AccountMap::iterator iter = _accountMap.find(accountID); + if ( iter == 0 || iter == _accountMap.end() ) { + return 0; + } + return iter->second; +} + #ifdef TEST /** * Test accountMap */ bool ManagerImpl::testCallAccountMap() { - if ( getAccountFromCall(1) != "" ) { + if ( getAccountFromCall(1) != AccountNULL ) { _debug("TEST: getAccountFromCall with empty list failed\n"); } if ( removeCallAccount(1) != false ) { @@ -1865,4 +1923,31 @@ bool ManagerImpl::testCallAccountMap() return true; } + +/** + * Test AccountMap + */ +bool ManagerImpl::testAccountMap() +{ + if (loadAccountMap() != 2) { + _debug("TEST: loadAccountMap didn't load 2 account\n"); + } + if (accountExists("acc0") == false) { + _debug("TEST: accountExists didn't find acc0\n"); + } + if (accountExists("accZ") != false) { + _debug("TEST: accountExists found an unknown account\n"); + } + if (getAccount("acc0") == 0) { + _debug("TEST: getAccount didn't find acc0\n"); + } + if (getAccount("accZ") != 0) { + _debug("TEST: getAccount found an unknown account\n"); + } + unloadAccountMap(); + if ( accountExists("acc0") == true ) { + _debug("TEST: accountExists found an account after unloadAccount\n"); + } + return true; +} #endif diff --git a/src/managerimpl.h b/src/managerimpl.h index 7ac3f5d9351371f09b73677e372e10787df82076..433ec0e86b4b82ea14bc01834d6dd23191d46601 100644 --- a/src/managerimpl.h +++ b/src/managerimpl.h @@ -78,6 +78,11 @@ typedef std::vector< VoIPLink* > VoIPLinkVector; * Define a type for a CallID to AccountID Map inside ManagerImpl */ typedef std::map<CALLID, AccountID> CallAccountMap; + +/** + * Define a type for a AccountMap container + */ +typedef std::map<AccountID, Account*> AccountMap; /** * To send multiple string @@ -454,11 +459,38 @@ private: */ CALLID getNewCallID(); - // bool accountExists(AccountID accountID); + /** Contains a list of account (sip, aix, etc) and their respective voiplink/calls */ + AccountMap _accountMap; + + /** + * Load the account from configuration + * @return number of account + */ + short loadAccountMap(); + + /** + * Unload the account (delete them) + */ + void unloadAccountMap(); + + /** + * Tell if an account exists + * @param accountID account ID check + */ + bool accountExists(AccountID accountID); + + /** + * Get an account pointer + * @param accountID account ID to get + * @param the account pointer or 0 + */ + Account* getAccount(AccountID accountID); #ifdef TEST bool testCallAccountMap(); + bool testAccountMap(); #endif + }; #endif // __MANAGER_H__ diff --git a/src/sipaccount.cpp b/src/sipaccount.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ae7acca4888a55305fa91abe169e82739c5dbe38 --- /dev/null +++ b/src/sipaccount.cpp @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2006 Savoir-Faire Linux inc. + * Author: Yan Morin <yan.morin@savoirfairelinux.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +#include "sipaccount.h" + +SIPAccount::SIPAccount(const AccountID& accountID) + : Account(accountID) +{ +} + + +SIPAccount::~SIPAccount() +{ +} + +/* virtual Account function implementation */ +bool +SIPAccount::createVoIPLink() +{ + return false; +} + +bool +SIPAccount::registerAccount() +{ + return false; +} + +bool +SIPAccount::unregisterAccount() +{ + return false; +} + +bool +SIPAccount::init() +{ + return false; +} + +bool +SIPAccount::terminate() +{ + return false; +} + diff --git a/src/sipaccount.h b/src/sipaccount.h new file mode 100644 index 0000000000000000000000000000000000000000..1069c2b7e76a3b25a00b5ac5b4e4f46f02ede8c6 --- /dev/null +++ b/src/sipaccount.h @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2006 Savoir-Faire Linux inc. + * Author: Yan Morin <yan.morin@savoirfairelinux.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +#ifndef SIPACCOUNT_H +#define SIPACCOUNT_H + +#include "account.h" + +/** + @author Yan Morin <yan.morin@gmail.com> + A Sip Account specify SIP specific functions and object (SIPCall/SIPVoIPLink) +*/ +class SIPAccount : public Account +{ +public: + SIPAccount(const AccountID& accountID); + + ~SIPAccount(); + + /* virtual Account function implementation */ + bool registerAccount(); + bool unregisterAccount(); + bool init(); + bool terminate(); + +private: + /* virtual Account function implementation */ + bool createVoIPLink(); + +}; + +#endif