⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 plan.cxx

📁 vovida的软交换
💻 CXX
字号:
/* ==================================================================== * The Vovida Software License, Version 1.0  *  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved. *  * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: *  * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. *  * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in *    the documentation and/or other materials provided with the *    distribution. *  * 3. The names "VOCAL", "Vovida Open Communication Application Library", *    and "Vovida Open Communication Application Library (VOCAL)" must *    not be used to endorse or promote products derived from this *    software without prior written permission. For written *    permission, please contact vocal@vovida.org. * * 4. Products derived from this software may not be called "VOCAL", nor *    may "VOCAL" appear in their name, without prior written *    permission of Vovida Networks, Inc. *  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * DAMAGE. *  * ==================================================================== *  * This software consists of voluntary contributions made by Vovida * Networks, Inc. and many individuals on behalf of Vovida Networks, * Inc.  For more information on Vovida Networks, Inc., please see * <http://www.vovida.org/>. * */static const char* const Plan_cxx_Version =    "$Id: Plan.cxx,v 1.1 2003/05/07 09:28:26 suhac Exp $";#include "DBResultset.hxx"#include "Plan.hxx"#include "cpLog.h"Plan::Plan(){#ifndef OLD_PROV  setRecordType(PLAN_RECORD);#endif};Plan::~Plan(){    clear();};/** * Create a new entry by adding it to the bottom of the list * Order is important when it comes time to search through the list. * @param  key  Regular expression * @param  contacts  Contact list  */voidPlan::addEntry( const Data& key, const  vector < Data > & contacts ){    // cpLog( LOG_DEBUG,"Plan::addEntry, adding: %s", key.logData());    /// Create the element    PlanElement element(key, contacts);    PlanMutex.WriteLock();    /// add it to the end of the vector    itsPlan.push_back(element);    PlanMutex.Unlock();};/** * Delete the specified key and its related contacts from the container. * @param  key  Regular expression to be deleted. */voidPlan::deleteEntry( const Data& key ){    PlanMutex.WriteLock();    for (vector < PlanElement > ::iterator i = itsPlan.begin();            i != itsPlan.end();            i++ )    {        if (key == i->getTextKey())        {            itsPlan.erase(i);        }    }    PlanMutex.Unlock();};/** * Remove the whole  plan - will need to * be done when a change is made */voidPlan::clear( ){    PlanMutex.WriteLock();    itsPlan.clear();    PlanMutex.Unlock();};/** * Search table using regexp matching. Retrieve and return a contact list * @param  key  String to match against the regular expressions in the table. * @param  elementFound List of contacts corresponding to expression matched. * @return True if found, false if not. * NOTE: If a key is found, and regular expression using grouping and  *       the contacts use $1, $2(replacement notations), then the *       contacts would be changed accordingly. */boolPlan::search( const Data& key , vector < Data > & elementFound){    //  cpLog( LOG_DEBUG,"Plan::search");    PlanMutex.ReadLock();    LocalScopeAllocator lo;    for (vector < PlanElement > ::iterator i = itsPlan.begin();            i != itsPlan.end();            ++i )    {        cpLog( LOG_DEBUG, "regexp in dialplan: %s", i->getTextKey().logData());        /// attempt to match the passed in key with the expression and		/// fill in the registers.        if ( REG_NOMATCH != regexec( &(i->getKey()), key.getData(lo), 							MAX_NUM_REGMATCH, 							(regmatch_t *)i->getMatch(), 0) )        {            cpLog( LOG_DEBUG, "Key: %s matched RegExp: %s",                   key.logData(), i->getTextKey().logData());            PlanMutex.Unlock();            /// Return the found contact list            elementFound = i->getContacts();			/// Check to see if the contacts needs to be replaced.(Regexec			/// replacement may have been requested)			if( i->isToBeReplaced() ) 	{				cpLog( LOG_DEBUG, "Invoking regular expression replacement" );				i->replace( key, elementFound );			}             return true;        }    }    PlanMutex.Unlock();    cpLog(LOG_DEBUG, "Failed to find the dial element");    return false;};/** * Display all the stored expressions and their related contacts. */voidPlan::display(){    if (cpLogGetPriority() >= LOG_DEBUG)    {        PlanMutex.ReadLock();        cpLog( LOG_DEBUG, "-----------------------------");        cpLog( LOG_DEBUG, " Plan ");        for (vector < PlanElement > ::iterator dpIter = itsPlan.begin(); dpIter != itsPlan.end(); ++dpIter )        {            cpLog( LOG_DEBUG, " Expression: %s", dpIter->getTextKey().logData());            /// print the data contained in the vector            const vector < Data > & currentContacts = dpIter->getContacts();            for (vector < Data > ::const_iterator i = currentContacts.begin(); i != currentContacts.end(); ++i)            {                cpLog( LOG_DEBUG, "    Contact: %s", i->logData() );            }        }        cpLog( LOG_DEBUG, "-----------------------------");        PlanMutex.Unlock();    }}#ifndef OLD_PROV/*  * The following functions implement the code to access the database directly */Record* Plan::clone(void) {   Plan* newPlan = new Plan();  PlanMutex.ReadLock();  // And copy the features vector;  vector<PlanElement>::iterator i = itsPlan.begin();  while (i != itsPlan.end()) {     newPlan->addEntry(i->getTextKey(), i->getContacts()); i++;  }  PlanMutex.Unlock();  return newPlan;}bool Plan::populateRecord(KeyType key, DBConnection* dbconn) {    DBResultset* results;    string centrex, type;    string::size_type pos;    // Split the key to get the centrex id / type string    if ((pos = key.rfind(':')) == string::npos) {       cpLog(LOG_NOTICE, "Key passed to plan didn't indicate type of Plan to fetch, key was %s, assuming Dial Plan", key.c_str());      type = "phone";    } else {       centrex = key.substr(0, pos);      type = key.substr(pos+1, key.size() - pos);    }    if ((type != "phone") && (type != "ip" )) {       cpLog(LOG_ERR, "Requested Plan of type %s, unknown assuming Dial Plan", type.c_str());      type = "phone";    }    // Now use the centrex and type information to pull the correct plan     // entries from the database    try {       results = dbconn->DBSQLSelect( (string) " " +  // (string allows '+' op)        "SELECT key, value "                                 +         "  FROM dial_plan_entries AS dpe, dial_plans AS dp" +   	    "  WHERE dpe.p_id = dp.id AND dp.c_id = " + centrex  +         "  AND type ='" + type + "'"                         +         "  ORDER BY dpe.priority DESC "                      + 	";"      );    }    catch ( VException &e ) {       cpLog(LOG_ERR, "DBSQLSelect failed due to %s",e.getDescription().c_str());      return false;    }    ResultTabletype recs = results->GetRecords();    delete results;  // Delete the results object in case this fails    ResultTabletype::iterator i = recs.begin();    while (i != recs.end()) {       ResultRecordtype::iterator j = (*i).begin();      Data dKey = *j;      j++;      vector <Data> contacts;      contacts.push_back(*j);      j++;      addEntry(dKey, contacts);           i++;    }    setKey(key);        return true;}KeyList Plan::getAllKeys(DBConnection* dbconn) {  KeyList keys;   DBResultset* results;  // TODO: This is really naive, we could (should?) form a list of CENTREX ids   // and then form two lists, one with valid IP plans, one with valid Dial   // this method assumes ALL centrexes have valid IP and Dial plans (which   // should be the case).  // This should be fine as long as any init procedure doesn't blindly trust   // that the keys returned by getAllKeys() can be trusted to deliver a   // real Plan object rather than NULL.  try {     results = dbconn->DBSQLSelect(      "SELECT id FROM CENTREXES;"    );  }  catch ( VException &e ) {     cpLog(LOG_ERR, "DBSQLSelect (for getAllKeys) failed due to %s",e.getDescription().c_str());    return keys;  }  ResultTabletype recs = results->GetRecords();  delete results;  // Delete the results object in case this fails  for (ResultTabletype::iterator i = recs.begin();i < recs.end();i++) {     // i should point to a single string in a list, check this    if ((*i).begin() < (*i).end()) {       // Add the first string to the key list twice, once for IP lists and       // once for phone lists.      keys.push_back("ip"+(*((*i).begin())));       keys.push_back("phone"+(*((*i).begin())));     }  }  return keys;}void Plan::displayRecord() {  Record::displayRecord("Plan.cxx");  PlanMutex.ReadLock();  for (vector < PlanElement > ::iterator dpIter = itsPlan.begin(); dpIter != itsPlan.end(); ++dpIter )        {	  cout << " Expression: :" << dpIter->getTextKey().logData() << ":";	  /// print the data contained in the vector            const vector < Data > & currentContacts = dpIter->getContacts();            for (vector < Data > ::const_iterator i = currentContacts.begin(); i != currentContacts.end(); ++i)            {	      cout << " -> :" << i->logData() << ":\n";            }	}  PlanMutex.Unlock();}#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -