ClientManager.h

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 #ifndef ADCHPP_CLIENTMANAGER_H
00020 #define ADCHPP_CLIENTMANAGER_H
00021 
00022 #include "CID.h"
00023 #include "AdcCommand.h"
00024 #include "Signal.h"
00025 #include "Client.h"
00026 #include "Singleton.h"
00027 #include "Hub.h"
00028 #include "Bot.h"
00029 
00030 namespace adchpp {
00031 
00032 class ManagedSocket;
00033 
00038 class ClientManager : public Singleton<ClientManager>, public CommandHandler<ClientManager>
00039 {
00040 public:
00041     typedef std::unordered_map<uint32_t, Entity*> EntityMap;
00042     typedef EntityMap::iterator EntityIter;
00043 
00045     ADCHPP_DLL uint32_t getSID(const std::string& nick) const throw();
00047     ADCHPP_DLL uint32_t getSID(const CID& cid) const throw();
00048 
00050     ADCHPP_DLL Entity* getEntity(uint32_t aSid) throw();
00051 
00053     ADCHPP_DLL Bot* createBot(const Bot::SendHandler& handler);
00054     ADCHPP_DLL void regBot(Bot& bot);
00055 
00060     EntityMap& getEntities() throw() { return entities; }
00061 
00063     ADCHPP_DLL void send(const AdcCommand& cmd) throw();
00064 
00066     ADCHPP_DLL void sendToAll(const BufferPtr& buffer) throw();
00067 
00069     ADCHPP_DLL void sendTo(const BufferPtr& buffer, uint32_t to);
00070 
00077     ADCHPP_DLL void enterIdentify(Entity& c, bool sendData) throw();
00078 
00086     ADCHPP_DLL ByteVector enterVerify(Entity& c, bool sendData) throw();
00087 
00097     ADCHPP_DLL bool enterNormal(Entity& c, bool sendData, bool sendOwnInf) throw();
00098 
00102     ADCHPP_DLL bool verifySUP(Entity& c, AdcCommand& cmd) throw();
00103 
00107     ADCHPP_DLL bool verifyINF(Entity& c, AdcCommand& cmd) throw();
00108 
00113     ADCHPP_DLL bool verifyNick(Entity& c, const AdcCommand& cmd) throw();
00114 
00118     ADCHPP_DLL bool verifyPassword(Entity& c, const std::string& password, const ByteVector& salt, const std::string& suppliedHash);
00119 
00123     ADCHPP_DLL bool verifyIp(Client& c, AdcCommand& cmd) throw();
00124 
00128     ADCHPP_DLL bool verifyCID(Entity& c, AdcCommand& cmd) throw();
00129 
00133     ADCHPP_DLL bool verifyOverflow(Entity& c);
00134 
00136     ADCHPP_DLL void setState(Entity& c, Entity::State newState) throw();
00137 
00138     ADCHPP_DLL size_t getQueuedBytes() throw();
00139 
00140     typedef SignalTraits<void (Entity&)> SignalConnected;
00141     typedef SignalTraits<void (Entity&, AdcCommand&, bool&)> SignalReceive;
00142     typedef SignalTraits<void (Entity&, const std::string&)> SignalBadLine;
00143     typedef SignalTraits<void (Entity&, const AdcCommand&, bool&)> SignalSend;
00144     typedef SignalTraits<void (Entity&, int)> SignalState;
00145     typedef SignalTraits<void (Entity&)> SignalDisconnected;
00146 
00147     SignalConnected::Signal& signalConnected() { return signalConnected_; }
00148     SignalReceive::Signal& signalReceive() { return signalReceive_; }
00149     SignalBadLine::Signal& signalBadLine() { return signalBadLine_; }
00150     SignalSend::Signal& signalSend() { return signalSend_; }
00151     SignalState::Signal& signalState() { return signalState_; }
00152     SignalDisconnected::Signal& signalDisconnected() { return signalDisconnected_; }
00153 
00154     void setLoginTimeout(uint32_t millis) { loginTimeout = millis; }
00155     uint32_t getLoginTimeout() { return loginTimeout; }
00156 
00157 private:
00158     friend class Client;
00159     friend class Entity;
00160     friend class Bot;
00161 
00162     std::list<std::pair<Client*, uint32_t> > logins;
00163 
00164     EntityMap entities;
00165     typedef std::unordered_map<std::string, Entity*> NickMap;
00166     NickMap nicks;
00167     typedef std::unordered_map<CID, Entity*> CIDMap;
00168     CIDMap cids;
00169 
00170     Hub hub;
00171 
00172     size_t loginTimeout;
00173 
00174     // Temporary string to use whenever a temporary string is needed (to avoid (de)allocating memory all the time...)
00175     std::string strtmp;
00176 
00177     static const std::string className;
00178 
00179     friend class Singleton<ClientManager>;
00180     ADCHPP_DLL static ClientManager* instance;
00181 
00182     friend class CommandHandler<ClientManager>;
00183 
00184     uint32_t makeSID();
00185 
00186     void maybeSend(Entity& c, const AdcCommand& cmd);
00187 
00188     void removeLogins(Entity& c) throw();
00189     void removeEntity(Entity& c) throw();
00190 
00191     bool handle(AdcCommand::SUP, Entity& c, AdcCommand& cmd) throw();
00192     bool handle(AdcCommand::INF, Entity& c, AdcCommand& cmd) throw();
00193     bool handleDefault(Entity& c, AdcCommand& cmd) throw();
00194 
00195     template<typename T> bool handle(T, Entity& c, AdcCommand& cmd) throw() { return handleDefault(c, cmd); }
00196 
00197     void handleIncoming(const ManagedSocketPtr& sock) throw();
00198 
00199     void onConnected(Client&) throw();
00200     void onReceive(Entity&, AdcCommand&) throw();
00201     void onBadLine(Client&, const std::string&) throw();
00202     void onFailed(Client&, const boost::system::error_code& e) throw();
00203 
00204     void badState(Entity& c, const AdcCommand& cmd) throw();
00205 
00206     SignalConnected::Signal signalConnected_;
00207     SignalReceive::Signal signalReceive_;
00208     SignalBadLine::Signal signalBadLine_;
00209     SignalSend::Signal signalSend_;
00210     SignalState::Signal signalState_;
00211     SignalDisconnected::Signal signalDisconnected_;
00212 
00213     ClientManager() throw();
00214 
00215     virtual ~ClientManager() throw();
00216 
00217 };
00218 
00219 }
00220 
00221 #endif // CLIENTMANAGER_H
Generated on Sat Nov 27 23:37:53 2010 for adchpp by  doxygen 1.6.3