Entity.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006-2010 Jacek Sieka, arnetheduck on gmail point com
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  */
00018 
00019 #include "adchpp.h"
00020 
00021 #include "Entity.h"
00022 
00023 #include "ClientManager.h"
00024 
00025 namespace adchpp {
00026 
00027 Entity::~Entity() {
00028     for(PluginDataMap::iterator i = pluginData.begin(), iend = pluginData.end(); i != iend; ++i) {
00029         (*i->first)(i->second);
00030     }
00031 }
00032 
00033 void Entity::inject(AdcCommand& cmd) {
00034     ClientManager::getInstance()->onReceive(*this, cmd);
00035 }
00036 
00037 const std::string& Entity::getField(const char* name) const {
00038     FieldMap::const_iterator i = fields.find(AdcCommand::toField(name));
00039     return i == fields.end() ? Util::emptyString : i->second;
00040 }
00041 
00042 bool Entity::hasField(const char* name) const {
00043     return fields.find(AdcCommand::toField(name)) != fields.end();
00044 }
00045 
00046 void Entity::setField(const char* name, const std::string& value) {
00047     uint16_t code = AdcCommand::toField(name);
00048 
00049     if(code == AdcCommand::toField("SU")) {
00050         filters.clear();
00051 
00052         if((value.size() + 1) % 5 == 0) {
00053             filters.reserve((value.size() + 1) / 5);
00054             for(size_t i = 0; i < value.size(); i += 5) {
00055                 filters.push_back(AdcCommand::toFourCC(value.data() + i));
00056             }
00057         }
00058     }
00059 
00060     if(value.empty()) {
00061         fields.erase(code);
00062     } else {
00063         fields[code] = value;
00064     }
00065 
00066     INF = BufferPtr();
00067 }
00068 
00069 bool Entity::getAllFields(AdcCommand& cmd) const throw() {
00070     for(FieldMap::const_iterator i = fields.begin(); i != fields.end(); ++i)
00071         cmd.addParam(AdcCommand::fromField(i->first), i->second);
00072     return !fields.empty();
00073 }
00074 
00075 void Entity::updateFields(const AdcCommand& cmd) {
00076     dcassert(cmd.getCommand() == AdcCommand::CMD_INF);
00077     for(StringIterC j = cmd.getParameters().begin(); j != cmd.getParameters().end(); ++j) {
00078         if(j->size() < 2)
00079             continue;
00080         setField(j->c_str(), j->substr(2));
00081     }
00082 }
00083 
00084 const BufferPtr& Entity::getINF() const {
00085     if(!INF) {
00086         AdcCommand cmd(AdcCommand::CMD_INF, getSID() == AdcCommand::HUB_SID ? AdcCommand::TYPE_INFO : AdcCommand::TYPE_BROADCAST, getSID());
00087         getAllFields(cmd);
00088         INF = cmd.getBuffer();
00089     }
00090     return INF;
00091 }
00092 
00093 bool Entity::addSupports(uint32_t feature) {
00094     if(std::find(supports.begin(), supports.end(), feature) != supports.end()) {
00095         return false;
00096     }
00097 
00098     supports.push_back(feature);
00099 
00100     SUP = BufferPtr();
00101 
00102     return true;
00103 }
00104 
00105 StringList Entity::getSupportList() const {
00106     StringList ret(supports.size());
00107     for(size_t i = 0; i < supports.size(); ++i) {
00108         ret[i] = AdcCommand::fromFourCC(supports[i]);
00109     }
00110 
00111     return ret;
00112 }
00113 
00114 bool Entity::removeSupports(uint32_t feature) {
00115     std::vector<uint32_t>::iterator i = std::find(supports.begin(), supports.end(), feature);
00116     if(i == supports.end()) {
00117         return false;
00118     }
00119 
00120     supports.erase(i);
00121 
00122     SUP = BufferPtr();
00123 
00124     return true;
00125 }
00126 
00127 const BufferPtr& Entity::getSUP() const {
00128     if(!SUP) {
00129         AdcCommand cmd(AdcCommand::CMD_SUP, getSID() == AdcCommand::HUB_SID ? AdcCommand::TYPE_INFO : AdcCommand::TYPE_BROADCAST, getSID());
00130         for(std::vector<uint32_t>::const_iterator i = supports.begin(), iend = supports.end(); i != iend; ++i) {
00131             cmd.addParam("AD", AdcCommand::fromFourCC(*i));
00132         }
00133         SUP = cmd.getBuffer();
00134     }
00135     return SUP;
00136 }
00137 
00138 bool Entity::hasSupport(uint32_t feature) const {
00139     return find(supports.begin(), supports.end(), feature) != supports.end();
00140 }
00141 
00142 void Entity::updateSupports(const AdcCommand& cmd) throw() {
00143     for(StringIterC i = cmd.getParameters().begin(); i != cmd.getParameters().end(); ++i) {
00144         const std::string& str = *i;
00145         if(str.size() != 6) {
00146             continue;
00147         }
00148 
00149         if(str[0] == 'A' && str[1] == 'D') {
00150             addSupports(AdcCommand::toFourCC(str.c_str() + 2));
00151         } else if(str[0] == 'R' && str[1] == 'M') {
00152             removeSupports(AdcCommand::toFourCC(str.c_str() + 2));
00153         }
00154     }
00155 }
00156 
00157 bool Entity::isFiltered(const std::string& features) const {
00158     if(filters.empty()) {
00159         return true;
00160     }
00161 
00162     for(size_t i = 0; i < features.size(); i += 5) {
00163         if(features[i] == '-') {
00164             if(std::find(filters.begin(), filters.end(), AdcCommand::toFourCC(features.data() + i + 1)) != filters.end()) {
00165                 return true;
00166             }
00167         } else if(features[i] == '+') {
00168             if(std::find(filters.begin(), filters.end(), AdcCommand::toFourCC(features.data() + i + 1)) == filters.end()) {
00169                 return true;
00170             }
00171         }
00172     }
00173     return false;
00174 }
00175 
00176 void Entity::setPluginData(const PluginDataHandle& handle, void* data) throw() {
00177     clearPluginData(handle);
00178     pluginData.insert(std::make_pair(handle, data));
00179 }
00180 
00181 void* Entity::getPluginData(const PluginDataHandle& handle) const throw() {
00182     PluginDataMap::const_iterator i = pluginData.find(handle);
00183     return i == pluginData.end() ? 0 : i->second;
00184 }
00185 
00186 void Entity::clearPluginData(const PluginDataHandle& handle) throw() {
00187     PluginDataMap::iterator i = pluginData.find(handle);
00188     if(i == pluginData.end()) {
00189         return;
00190     }
00191 
00192     (*i->first)(i->second);
00193     pluginData.erase(i);
00194 }
00195 
00196 void Entity::setFlag(size_t flag) {
00197     flags.setFlag(flag);
00198     if(flag & MASK_CLIENT_TYPE) {
00199         setField("CT", Util::toString(flags.getFlags() & MASK_CLIENT_TYPE));
00200     }
00201 }
00202 
00203 void Entity::unsetFlag(size_t flag) {
00204     flags.setFlag(flag);
00205     if(flag & MASK_CLIENT_TYPE) {
00206         setField("CT", Util::toString(flags.getFlags() & MASK_CLIENT_TYPE));
00207     }
00208 }
00209 
00210 size_t Entity::getQueuedBytes() const {
00211     return 0;
00212 }
00213 
00214 time_t Entity::getOverflow() const {
00215     return 0;
00216 }
00217 
00218 }
Generated on Sat Nov 27 23:37:53 2010 for adchpp by  doxygen 1.6.3