client/protocol/socket/SocketDriverFactory.cpp

Go to the documentation of this file.
00001 /*------------------------------------------------------------------------------
00002 Name:      SocketDriverFactory.cpp
00003 Project:   xmlBlaster.org
00004 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
00005 Comment:   The client driver for the socket protocol
00006 ------------------------------------------------------------------------------*/
00007 #include <util/ErrorCode.h>
00008 #include <util/XmlBlasterException.h>
00009 #include <util/Global.h>
00010 #include <util/lexical_cast.h>
00011 #include <client/protocol/socket/SocketDriverFactory.h>
00012 #include <client/protocol/socket/SocketDriver.h>
00013 
00014 namespace org {
00015  namespace xmlBlaster {
00016   namespace client {
00017    namespace protocol {
00018     namespace socket {
00019 
00020 using namespace std;
00021 using namespace org::xmlBlaster::util;
00022 
00023 SocketDriverFactory::SocketDriverFactory(Global& global)
00024    : ME("SocketDriverFactory"), 
00025      drivers_(),
00026      getterMutex_()
00027 {
00028    I_Log& log = global.getLog("org.xmlBlaster.client.protocol.socket");
00029    if (log.call()) log.call(ME, string("Constructor"));
00030 
00031    //int args                 = global_.getArgs();
00032    //const char * const* argc = global_.getArgc();
00033 }
00034 
00035 SocketDriverFactory::SocketDriverFactory(const SocketDriverFactory& factory)
00036 : ME(factory.ME), 
00037   drivers_(),
00038   getterMutex_()
00039 {
00040    throw util::XmlBlasterException(INTERNAL_NOTIMPLEMENTED, ME, "private copy constructor");
00041 }
00042 
00043 SocketDriverFactory& SocketDriverFactory::operator =(const SocketDriverFactory&)
00044 {
00045    throw util::XmlBlasterException(INTERNAL_NOTIMPLEMENTED, ME, "private assignement operator");
00046 }
00047 
00048 SocketDriverFactory::~SocketDriverFactory()
00049 {
00050    //if (log_.call()) log_.call(ME, "Destructor start");
00051    thread::Lock lock(getterMutex_);
00052    DriversMap::iterator iter = drivers_.begin();
00053    while (iter != drivers_.end()) {
00054       delete ((*iter).second).first;
00055       iter++;
00056    }
00057    drivers_.erase(drivers_.begin(), drivers_.end());
00058    //if (log_.trace()) log_.trace(ME, "erased all drivers");
00059 }
00060 
00061 SocketDriverFactory* SocketDriverFactory::factory_ = NULL;
00062 
00063 SocketDriverFactory& SocketDriverFactory::getFactory(Global& global)
00064 {
00065    if(factory_ == NULL)
00066    {
00067      factory_ = new SocketDriverFactory(global);
00068      org::xmlBlaster::util::Object_Lifetime_Manager::instance()->manage_object("XB_SocketDriverFactory", factory_);  // if not pre-allocated.
00069    }
00070    return *factory_;
00071 }
00072 
00073 SocketDriver& SocketDriverFactory::getDriverInstance(Global* global)
00074 {
00075    string instanceName = lexical_cast<string>(global);
00076    I_Log& log = global->getLog("org.xmlBlaster.client.protocol.socket");
00077 
00078    if (log.call()) log.call(ME, string("getInstance for ") + instanceName);
00079    SocketDriver*  driver = NULL;
00080    int count = 1;
00081    {
00082       thread::Lock lock(getterMutex_);
00083       DriversMap::iterator iter = drivers_.find(global);
00084       if (iter == drivers_.end()) {
00085          if (log.trace()) log.trace(ME, string("created a new instance for ") + instanceName);
00086          driver = new SocketDriver(*global, instanceName);
00087          // initially the counter is set to 1
00088          drivers_.insert(DriversMap::value_type(global, pair<SocketDriver*, int>(driver, 1)));
00089       }
00090       else {
00091          driver = ((*iter).second).first;
00092          count = ((*iter).second).second++; // this is the counter ...
00093       }
00094    }
00095    if (log.trace()) 
00096       log.trace(ME, string("number of instances for '") + instanceName + "' are " + lexical_cast<std::string>(count));
00097    return *driver;
00098 }
00099 
00100 
00101 int SocketDriverFactory::killDriverInstance(Global* global)
00102 {
00103    string instanceName = lexical_cast<string>(global);
00104    I_Log& log = global->getLog("org.xmlBlaster.client.protocol.socket");
00105 
00106    log.call(ME, string("killDriverInstance with a total of ") + lexical_cast<string>(drivers_.size()) + " instances, looking for global " + global->getId() + " instanceName=" + instanceName);
00107    thread::Lock lock(getterMutex_);
00108    DriversMap::iterator iter = drivers_.find(global);
00109    if (iter == drivers_.end()) return -1;
00110    int ret = --(*iter).second.second;
00111    if (log.trace()) log.trace(ME, string("instances before deleting ") + lexical_cast<std::string>(ret+1));
00112    if (ret <= 0) {
00113       if (log.trace()) log.trace(ME, string("kill instance '") + instanceName + "' will be deleted now");
00114       // do remove it since the counter is zero
00115       SocketDriver* driver = (*iter).second.first;
00116       drivers_.erase(iter);
00117       delete driver;
00118    }
00119    if (log.trace()) 
00120       log.trace(ME, string("kill instance '") + instanceName + "' the number of references is " + lexical_cast<std::string>(ret));
00121    return ret;
00122 }
00123 
00124 void SocketDriverFactory::run()
00125 {
00126    // if (log_.trace()) log_.trace(ME, "the socket loop starts now");
00127 }
00128 
00129 
00130 }}}}} // namespaces
00131