![]() |
User Manual, Developers Guide and API Documentation |
![]() |
00001 /******************************************************************************* 00002 * This file is part of openWNS (open Wireless Network Simulator) 00003 * _____________________________________________________________________________ 00004 * 00005 * Copyright (C) 2004-2007 00006 * Chair of Communication Networks (ComNets) 00007 * Kopernikusstr. 16, D-52074 Aachen, Germany 00008 * phone: ++49-241-80-27910, 00009 * fax: ++49-241-80-22242 00010 * email: info@openwns.org 00011 * www: http://www.openwns.org 00012 * _____________________________________________________________________________ 00013 * 00014 * openWNS is free software; you can redistribute it and/or modify it under the 00015 * terms of the GNU Lesser General Public License version 2 as published by the 00016 * Free Software Foundation; 00017 * 00018 * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY 00019 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 00020 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 00021 * details. 00022 * 00023 * You should have received a copy of the GNU Lesser General Public License 00024 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00025 * 00026 ******************************************************************************/ 00027 00028 #include <WNS/Exception.hpp> 00029 #include <WNS/module/Base.hpp> 00030 00031 #include <sstream> 00032 #include <iostream> 00033 #include <dlfcn.h> 00034 00035 using namespace wns::module; 00036 00037 Base::Base(const pyconfig::View& pyco) : 00038 convertMap(), 00039 //version(Release(), DepList(), DateTime(), "", ""), 00040 global(false), 00041 logger(pyco.get<pyconfig::View>("logger")) 00042 { 00043 MESSAGE_BEGIN(NORMAL, logger, m, "Constructor called"); 00044 MESSAGE_END(); 00045 } 00046 00047 Base::~Base() 00048 { 00049 MESSAGE_SINGLE(NORMAL, logger, "Destructor called"); 00050 00051 ConvertMap::iterator itr; 00052 for(itr=convertMap.begin(); itr!=convertMap.end(); ++itr) 00053 { 00054 removeServiceFromGlobalMap(itr->first); 00055 } 00056 } 00057 00058 // wns::module::VersionInformation 00059 // Base::getVersionInformation() const 00060 // { 00061 // return version; 00062 // } 00063 00064 bool 00065 Base::existsService(const std::string& s) 00066 { 00067 return getGlobalMap().find(s) != getGlobalMap().end(); 00068 } 00069 00070 wns::module::Base::TypeMap& 00071 Base::getGlobalMap() 00072 { 00073 static TypeMap globalMap; 00074 return globalMap; 00075 } 00076 00077 void 00078 Base::removeServiceFromGlobalMap(const std::string& s) 00079 { 00080 MESSAGE_BEGIN(NORMAL, logger, m, "Removing service: "); 00081 m << s; 00082 MESSAGE_END(); 00083 00084 TypeMap::iterator itr = getGlobalMap().find(s); 00085 if(itr==getGlobalMap().end()) { 00086 std::stringstream err; 00087 err << "Service" << s << " not registered"; 00088 throw(Exception(err.str())); 00089 } else { 00090 getGlobalMap().erase(itr); 00091 } 00092 } 00093 00094 void 00095 Base::addServiceToGlobalMap(const std::string& s) 00096 { 00097 MESSAGE_BEGIN(NORMAL, logger, m, "Adding service: "); 00098 m << s; 00099 MESSAGE_END(); 00100 00101 TypeMap::iterator itr = getGlobalMap().find(s); 00102 if(itr==getGlobalMap().end()) { 00103 getGlobalMap()[s]=this; 00104 } else { 00105 std::stringstream err; 00106 err << "Service " << s << " already registered"; 00107 throw(Exception(err.str())); 00108 } 00109 } 00110 00111 wns::ChamaeleonBase* 00112 Base::create(const std::string& s) 00113 { 00114 ConvertMap::iterator itr = convertMap.find(s); 00115 if(itr==convertMap.end()) { 00116 return NULL; 00117 } else { 00118 return (*(itr->second))(this); 00119 } 00120 } 00121 00122 bool 00123 Base::getGlobalFlag() const 00124 { 00125 return global; 00126 } 00127 00128 bool 00129 Base::load(const std::string& name, bool absolutePath, bool beVerbose, bool lazyBinding) 00130 { 00131 std::string str; 00132 if (absolutePath) { 00133 str = name; 00134 } else { 00135 str = "lib" + name + ".so"; 00136 } 00137 00138 if(beVerbose) { 00139 std::cout << std::endl << "Loading " << str; 00140 } 00141 00142 void* handle = NULL; 00143 if (lazyBinding) { 00144 handle = dlopen(str.c_str(),RTLD_LAZY | RTLD_GLOBAL); 00145 if(beVerbose) { 00146 std::cout << " with lazy binding!" << std::endl; 00147 } 00148 } else { 00149 handle = dlopen(str.c_str(),RTLD_NOW | RTLD_GLOBAL); 00150 if(beVerbose) { 00151 std::cout << std::endl; 00152 } 00153 } 00154 00155 return handle != NULL; 00156 } 00157
1.5.5