Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

AgentPack.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004 Ivo Danihelka (ivo@danihelka.net)
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 #include "AgentPack.h"
00010 
00011 #include "BaseAgent.h"
00012 #include "LogicException.h"
00013 #include "NameException.h"
00014 #include "MessagerAgent.h"
00015 
00016 AgentPack *AgentPack::ms_singleton = NULL;
00017 //-----------------------------------------------------------------
00018 AgentPack::AgentPack()
00019 {
00020     //NOTE: this is not thread safe
00021     if (ms_singleton) {
00022         throw LogicException(ExInfo("AgentPack is singleton"));
00023     }
00024 
00025     ms_singleton = this;
00026     //NOTE: MessagerAgent must be the first
00027     MessagerAgent *messager = new MessagerAgent();
00028     messager->init();
00029     addAgent(messager);
00030 }
00031 //-----------------------------------------------------------------
00032 AgentPack::~AgentPack()
00033 {
00034     t_agents::iterator end = m_agents.end();
00035     for (t_agents::iterator i = m_agents.begin(); i != end; ++i) {
00036         delete i->second;
00037     }
00038     ms_singleton = NULL;
00039 }
00040 //-----------------------------------------------------------------
00041 /**
00042  * Insert agent to pack
00043  * and insert his to listeners too.
00044  * NOTE: agent->init() is not called yet
00045  *
00046  * @param agent agent
00047  * @throws NameEception if agent with this name already exists.
00048  */
00049     void
00050 AgentPack::addAgent(BaseAgent *agent)
00051 {
00052     std::pair<t_agents::iterator,bool> status =
00053         m_agents.insert(
00054                 std::pair<std::string,BaseAgent*>(agent->getName(), agent));
00055     if (!status.second) {
00056         throw NameException(ExInfo("agent already exists")
00057                 .addInfo("name", agent->getName()));
00058     }
00059 
00060     MessagerAgent::agent()->addListener(agent);
00061 }
00062 //-----------------------------------------------------------------
00063 /**
00064  * Remove agent from pack
00065  * and remove his from listeners too.
00066  * @param name agent's name
00067  */
00068 void
00069 AgentPack::removeAgent(const std::string &name)
00070 {
00071     t_agents::iterator it = m_agents.find(name);
00072     if (m_agents.end() != it) {
00073         MessagerAgent::agent()->removeListener(name);
00074         delete it->second;
00075         m_agents.erase(it);
00076     }
00077 }
00078 //-----------------------------------------------------------------
00079 /**
00080  * Return agent according his name.
00081  * @param name agent's name
00082  * @return agent
00083  * @throws NameException when agent cannot be found
00084  * @throws LogicException when agent is not initialized
00085  */
00086     BaseAgent *
00087 AgentPack::getAgent(const std::string &name)
00088 {
00089     if (NULL == ms_singleton) {
00090         throw LogicException(ExInfo("AgentPack is not ready"));
00091     }
00092 
00093     t_agents::iterator it = ms_singleton->m_agents.find(name);
00094     if (ms_singleton->m_agents.end() == it) {
00095         throw NameException(ExInfo("cannot find agent")
00096                 .addInfo("name", name));
00097     }
00098 
00099     if (!it->second->isInitialized()) {
00100         throw LogicException(ExInfo("agent is not initialized")
00101                 .addInfo("name", name));
00102     }
00103     return it->second;
00104 }
00105 //-----------------------------------------------------------------
00106 /**
00107  * Init all agents bellow stopAgent.
00108  * Init all agents when stopAgent is not found.
00109  */
00110 void
00111 AgentPack::init(const std::string &stopAgent)
00112 {
00113     t_agents::iterator stop = m_agents.find(stopAgent);
00114 
00115     for (t_agents::iterator i = m_agents.begin(); i != stop; ++i) {
00116         if (!i->second->isInitialized()) {
00117             i->second->init();
00118         }
00119     }
00120 }
00121 //-----------------------------------------------------------------
00122 void
00123 AgentPack::update()
00124 {
00125     t_agents::iterator end = m_agents.end();
00126     for (t_agents::iterator i = m_agents.begin(); i != end; ++i) {
00127         i->second->update();
00128     }
00129 }
00130 //-----------------------------------------------------------------
00131 /**
00132  * Shutdown initialized agents in reverse order.
00133  */
00134 void
00135 AgentPack::shutdown()
00136 {
00137     t_agents::reverse_iterator rend = m_agents.rend();
00138     for (t_agents::reverse_iterator i = m_agents.rbegin();
00139             i != rend; ++i)
00140     {
00141         if (i->second->isInitialized()) {
00142             i->second->shutdown();
00143         }
00144     }
00145 }
00146 

Generated on Wed Jun 1 09:54:30 2005 for Fish Fillets - Next Generation by  doxygen 1.4.2