00001
00002
00003
00004
00005
00006
00007
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
00021 if (ms_singleton) {
00022 throw LogicException(ExInfo("AgentPack is singleton"));
00023 }
00024
00025 ms_singleton = this;
00026
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
00043
00044
00045
00046
00047
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
00065
00066
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
00081
00082
00083
00084
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
00108
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
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