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

📄 accountrecord.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/>. * *//*  This implements 'Account Record' subclass of the 'Record' *  superclass. * *  AccountRecord.cxx           version 1.0 -- 17 Mar 2003 *                              Suha Cubukcuoglu          */  #include "AccountRecord.hxx"#include "FeatureRecord.hxx"#include "DBResultset.hxx"#include "DBConnection.hxx"#include "PGSQLConnection.hxx"#include "VException.hxx"#include "cpLog.h"#include "Cache.hxx"#include "support.hxx"#include <string>#include <algorithm>AccountRecord::AccountRecord(){    setRecordType(ACCOUNT_RECORD);}AccountRecord::~AccountRecord() {}void AccountRecord::displayRecord(){    cpLog(LOG_DEBUG, "Displaying AccountRecord:\n");    cpLog(LOG_DEBUG, "-------------------------\n\n");    cpLog(LOG_DEBUG, "UserName  : [%s] (== Record key)\n", getUserName().c_str());    cpLog(LOG_DEBUG, "GroupName : [%s]\n", getGroupName().c_str());    cpLog(LOG_DEBUG, "CentrexID : [%d]\n", getCentrexID());    cpLog(LOG_DEBUG, "Auth_type : [%s]\n", getAuthType().c_str());    cpLog(LOG_DEBUG, "IP_Addr   : [%s]\n", getIP().c_str());    cpLog(LOG_DEBUG, "Password  : [%s]\n", getPassword().c_str());    cpLog(LOG_DEBUG, "StaticReg : [%d]\n", isStaticReg());        cpLog(LOG_DEBUG, "Displaying Feature Information:\n");    cpLog(LOG_DEBUG, "-------------------------------\n\n");        vector <string>::iterator i;   	i = feature_ids.begin();        FeatureRecord *feature;     Cache *cache = Cache::getInstance();    cpLog(LOG_DEBUG, "ID \tPriority\t Name\t URI (Called/Calling)");    for (; i != feature_ids.end(); i++) {       // Get feature from the cache      feature = dynamic_cast<FeatureRecord *>(cache->getRecord(FEATURE_RECORD, *i));      if (feature) { 	cpLog(LOG_DEBUG, " %s \t   %02d   \t %s\t %s (%s)",	      feature->getKey().c_str(),	      feature->getPriority(),	      feature->getName().c_str(),	      feature->getURI().c_str(),	      feature->isCallingFeature() ? "Calling" : "Called"	      );	delete feature;      } else {	cpLog(LOG_DEBUG, " %s \t  *** FEATURE NOT FOUND IN CACHE OR DATABASE ! *** ", (*i).c_str());      }    }}bool AccountRecord::populateRecord(KeyType key, DBConnection* dbconn) {     DBResultset* results;    try {    	       results = dbconn->DBSQLSelect((string)" " + "SELECT a.centrex_id, a.group_name, "     +                                                  "a.password, a.auth_type, a.static_reg, " +                                                  "a.ipaddr FROM ACCOUNTS a, ALIASES s "    +                                                  "WHERE a.username ='" + key + "'"         +                                                  "AND a.id = s.account_id"                 +                                                    ";" ) ;                                                                   }    catch ( VException &e ) {       cpLog(LOG_ERR, "DBSQLSelect failed due to %s", e.getDescription().c_str());      return false;    }    ResultTabletype recs = results->GetRecords();        // cout << "Record Table Size :" << recs.size() <<"\n";        if (recs.begin() == recs.end()){         return false; // No records    }        ResultRecordtype oneRec = *(recs.begin());        if (oneRec.begin() == oneRec.end()){         return false; // No data in record    }    // Populate the record;    setKey(key);        ResultRecordtype::iterator i;    i = oneRec.begin();        setCentrexID(atoi(i[0].c_str()));    setGroupName(i[1]);    setPassword(i[2]);    setAuthType(i[3]);    staticReg = (i[4].c_str() == (string) "t") ? true: false;     setIP(i[5]);        // Get the feature id's, and also force the feature objects to be cached        try {       results = dbconn->DBSQLSelect((string)" " + "SELECT afb.f_id FROM ACCOUNTS a, ACCOUNT_FEATURE_BINDING afb "  + 				    " WHERE a.username = '" + i[1] + "'"                           + 				    " AND a.id = afb.account_id "                                   +				    ";" );     }    catch ( VException &e ) {       cpLog(LOG_ERR, "Second DBSQLSelect failed due to %s", e.getDescription().c_str());      return false;    }    recs = results->GetRecords();    delete results;  // Delete the results object in case this fails          cpLog(LOG_DEBUG, "Feature List Table Size :[%d]\n", recs.size());           if(recs.size() != 0){          ResultTabletype::iterator j = recs.begin();              while(j != recs.end()){               i = (*j).begin();               	      cpLog(LOG_DEBUG, "Getting feature key = '%s'", i[0].c_str());	      // Store the feature id	      feature_ids.push_back(i[0]);	      	      // Now force this feature information to be cached (should be fast if it's already there)	      Record *tmp = (Cache::getInstance())->getRecord(FEATURE_RECORD, i[0]);	      if (tmp) {              delete tmp;	      }           else           { 		     cpLog(LOG_ERR, "Database problem, can't find feature %s in cache/db used by user %s", i[0].c_str(), getUserName().c_str());	      }	      j++;       }              sort_features(feature_ids, feature_ids.size());    }          return true;}vector <string>AccountRecord::getFeatures(bool calling){    vector <string> featureList(0);    FeatureRecord *feature;     Cache *cache = Cache::getInstance();        for (vector<string>::iterator i = feature_ids.begin(); i != feature_ids.end(); i++) {       // Get feature from the cache      feature = dynamic_cast<FeatureRecord *>(cache->getRecord(FEATURE_RECORD, *i));      if (feature) { 	  // Check if this feature agrees with the type we were looking for	      if (feature->isCallingFeature() == calling) { 	             featureList.push_back(feature->getURI());	      }	      delete feature;      }    }        return featureList;  }void AccountRecord::sort_features(vector <string> &f, int n){        int i, j;    string tmp;        FeatureRecord *feature1;    FeatureRecord *feature2;     Cache *cache = Cache::getInstance();        for(i = 0 ; i < n; i++){        feature1 = dynamic_cast<FeatureRecord *>(cache->getRecord(FEATURE_RECORD, f[i]));        if (feature1) { 	            for(j = i + 1; j < n; j++){	  	            feature2 = dynamic_cast<FeatureRecord *>(cache->getRecord(FEATURE_RECORD, f[j]));	  	            if (feature2) {                         if (feature1->getPriority() > feature2->getPriority()) { 	    	  	  	            tmp = f[i];	    	  	  	            f[i] = f[j];	    	  	  	            f[j] = tmp;	  	                } 	  	  	            delete feature2;	  	            } else {	  	            cpLog(LOG_ERR, "Database problem, can't find feature %s in cache/db used by user %s", 	  	                  f[j].c_str(), getUserName().c_str());                    }	            }      } else { 	           cpLog(LOG_ERR, "Database problem, can't find feature %s in cache/db used by user %s", 	                 f[i].c_str(), getUserName().c_str());      }      delete feature1;   }}   bool AccountRecord::amendRecord(DBConnection* dbconn){    cout << "Called Amend Record in Account Record\n";    // Would do a database AMEND here    return false;}bool AccountRecord::deleteRecord(DBConnection* dbconn){    cout << "Called Delete Record in Account Record\n";    // Would do a database DELETE here    return false;}bool AccountRecord::insertRecord(DBConnection* dbconn){    cout << "Called Insert Record in Account Record\n";        bool result;    string ipaddr = getIP();        if (ipaddr.compare("") == 0) {       ipaddr = "NULL";    } else {      ipaddr = "'"+ipaddr+"'";    }                   	      try {             string str = (string)" "                   +               "INSERT INTO accounts (centrex_id, username, "     +                "group_name, password, auth_type, static_reg, "    +               "ipaddr)"                                          +                " VALUES ('" + itos(getCentrexID()) + "', '"       +               getUserName() + "', '" + getGroupName() +  "', '"  +               getPassword() + "', '" + getAuthType() + "', '"    +	       // Note ipaddr already quoted (as it can be NULL or 'address')	       (staticReg ? "t" : "f") + "', " + ipaddr + ")"     + 	";";      // cout << str << "\n";	result = dbconn->DBSQLInsert(str);     }    catch ( VException &e ) {       cpLog(LOG_ERR, "DBSQLInsert failed due to %s", e.getDescription().c_str());      return false;    }    return result;}KeyList AccountRecord::getAllKeys(DBConnection* dbconn){   KeyList keys;   DBResultset* results;     // Would do a database SELECT to get all relavent keys and   // return them in key list  try {     results = dbconn->DBSQLSelect(      "SELECT username FROM ACCOUNTS;"    );  }  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    keys.push_back(*((*i).begin()));  }    return keys;}Record* AccountRecord::clone() {     AccountRecord* rec = new AccountRecord();  Record::copyData(rec);  //  rec->setUserName(userName);  rec->setGroupName(groupName);  rec->setCentrexID(centrexID);  rec->setAuthType(authType);  rec->setIP(IPAddr);  rec->setPassword(password);  if (staticReg) { rec->setStaticReg(); } else { rec->unsetStaticReg(); }    // And copy the feature_ids vector;  vector<string>::iterator i = feature_ids.begin();  while (i != feature_ids.end()) {     rec->feature_ids.push_back(*i); i++;  }  return rec;}

⌨️ 快捷键说明

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