|
solarpowerlog trunk
|
00001 /* 00002 * CSharedConnection.cpp 00003 * 00004 * Created on: Sep 12, 2010 00005 * Author: tobi 00006 */ 00007 00008 #ifdef HAVE_CONFIG_H 00009 #include "config.h" 00010 #include "porting.h" 00011 #endif 00012 00013 #ifdef HAVE_COMMS_SHAREDCONNECTION 00014 00015 #include "CSharedConnection.h" 00016 #include "configuration/CConfigHelper.h" 00017 #include "CSharedConnectionMaster.h" 00018 #include "CSharedConnectionSlave.h" 00019 00020 using namespace libconfig; 00021 00022 CSharedConnection::CSharedConnection(const string & configurationname) : 00023 IConnect(configurationname) 00024 { 00025 concreteSharedConnection = NULL; 00026 } 00027 00028 CSharedConnection::~CSharedConnection() 00029 { 00030 if (concreteSharedConnection) 00031 delete concreteSharedConnection; 00032 } 00033 00034 bool CSharedConnection::CreateSharedConnectionObject() 00035 { 00036 if (concreteSharedConnection) return true; 00037 00038 CConfigHelper cfg(ConfigurationPath); 00039 std::string s; 00040 00041 if (!cfg.GetConfig("sharedconnection_type", s)) 00042 return false; 00043 00044 if (s == "master") { 00045 LOGDEBUG(this->logger,"Shared connection master requested."); 00046 concreteSharedConnection = new CSharedConnectionMaster( 00047 this->ConfigurationPath); 00048 } else if (s == "slave") { 00049 LOGDEBUG(this->logger,"Shared connection slave requested."); 00050 concreteSharedConnection = new CSharedConnectionSlave( 00051 this->ConfigurationPath); 00052 } else { 00053 LOGERROR(this->logger,"Shared connection; Slave or master?"); 00054 return false; 00055 } 00056 00057 concreteSharedConnection->SetupLogger(this->logger.getLoggername(), 00058 "SharedTarget"); 00059 00060 return true; 00061 00062 } 00063 00064 bool CSharedConnection::CheckConfig(void) 00065 { 00066 00067 bool fail = false; 00068 CConfigHelper cfg(ConfigurationPath); 00069 std::string s; 00070 00071 if (!cfg.GetConfig("sharedconnection_type", s)) { 00072 LOGERROR(logger,"Configuration Error: Sharedconnection_type not defined. Must be master or slave."); 00073 return false; 00074 } 00075 00076 if ( !CreateSharedConnectionObject() ) { 00077 LOGERROR(logger,"Configuration Error: Sharedconnection_type must be master or slave."); 00078 return false; 00079 } 00080 00081 00082 if (fail) 00083 return false; 00084 00085 if (!concreteSharedConnection->CheckConfig()) 00086 return false; 00087 00088 return true; 00089 00090 } 00091 00092 void CSharedConnection::SetupLogger(const string& parentlogger, const string &) 00093 { 00094 IConnect::SetupLogger(parentlogger, "Comms_SharedConnection"); 00095 } 00096 00097 #endif