| 1 |
/* |
| 2 |
* Copyright (C) 2004 Ivo Danihelka ([email protected]) |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation; either version 2 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
*/ |
| 9 |
#include "AgentPack.h" |
| 10 |
|
| 11 |
#include "BaseAgent.h" |
| 12 |
#include "LogicException.h" |
| 13 |
#include "NameException.h" |
| 14 |
#include "MessagerAgent.h" |
| 15 |
|
| 16 |
AgentPack *AgentPack::ms_singleton = NULL; |
| 17 |
//----------------------------------------------------------------- |
| 18 |
AgentPack::AgentPack() |
| 19 |
{ |
| 20 |
//NOTE: this is not thread safe |
| 21 |
if (ms_singleton) { |
| 22 |
throw LogicException(ExInfo("AgentPack is singleton")); |
| 23 |
} |
| 24 |
|
| 25 |
ms_singleton = this; |
| 26 |
//NOTE: MessagerAgent must be the first |
| 27 |
MessagerAgent *messager = new MessagerAgent(); |
| 28 |
messager->init(); |
| 29 |
addAgent(messager); |
| 30 |
} |
| 31 |
//----------------------------------------------------------------- |
| 32 |
AgentPack::~AgentPack() |
| 33 |
{ |
| 34 |
t_agents::iterator end = m_agents.end(); |
| 35 |
for (t_agents::iterator i = m_agents.begin(); i != end; ++i) { |
| 36 |
delete i->second; |
| 37 |
} |
| 38 |
ms_singleton = NULL; |
| 39 |
} |
| 40 |
//----------------------------------------------------------------- |
| 41 |
/** |
| 42 |
* Insert agent to pack |
| 43 |
* and insert his to listeners too. |
| 44 |
* NOTE: agent->init() is not called yet |
| 45 |
* |
| 46 |
* @param agent agent |
| 47 |
* @throws NameEception if agent with this name already exists. |
| 48 |
*/ |
| 49 |
void |
| 50 |
AgentPack::addAgent(BaseAgent *agent) |
| 51 |
{ |
| 52 |
std::pair<t_agents::iterator,bool> status = |
| 53 |
m_agents.insert( |
| 54 |
std::pair<std::string,BaseAgent*>(agent->getName(), agent)); |
| 55 |
if (!status.second) { |
| 56 |
throw NameException(ExInfo("agent already exists") |
| 57 |
.addInfo("name", agent->getName())); |
| 58 |
} |
| 59 |
|
| 60 |
MessagerAgent::agent()->addListener(agent); |
| 61 |
} |
| 62 |
//----------------------------------------------------------------- |
| 63 |
/** |
| 64 |
* Remove agent from pack |
| 65 |
* and remove his from listeners too. |
| 66 |
* @param name agent's name |
| 67 |
*/ |
| 68 |
void |
| 69 |
AgentPack::removeAgent(const std::string &name) |
| 70 |
{ |
| 71 |
t_agents::iterator it = m_agents.find(name); |
| 72 |
if (m_agents.end() != it) { |
| 73 |
MessagerAgent::agent()->removeListener(name); |
| 74 |
delete it->second; |
| 75 |
m_agents.erase(it); |
| 76 |
} |
| 77 |
} |
| 78 |
//----------------------------------------------------------------- |
| 79 |
/** |
| 80 |
* Return agent according his name. |
| 81 |
* @param name agent's name |
| 82 |
* @return agent |
| 83 |
* @throws NameException when agent cannot be found |
| 84 |
* @throws LogicException when agent is not initialized |
| 85 |
*/ |
| 86 |
BaseAgent * |
| 87 |
AgentPack::getAgent(const std::string &name) |
| 88 |
{ |
| 89 |
if (NULL == ms_singleton) { |
| 90 |
throw LogicException(ExInfo("AgentPack is not ready")); |
| 91 |
} |
| 92 |
|
| 93 |
t_agents::iterator it = ms_singleton->m_agents.find(name); |
| 94 |
if (ms_singleton->m_agents.end() == it) { |
| 95 |
throw NameException(ExInfo("cannot find agent") |
| 96 |
.addInfo("name", name)); |
| 97 |
} |
| 98 |
|
| 99 |
if (!it->second->isInitialized()) { |
| 100 |
throw LogicException(ExInfo("agent is not initialized") |
| 101 |
.addInfo("name", name)); |
| 102 |
} |
| 103 |
return it->second; |
| 104 |
} |
| 105 |
//----------------------------------------------------------------- |
| 106 |
/** |
| 107 |
* Init all agents bellow stopAgent. |
| 108 |
* Init all agents when stopAgent is not found. |
| 109 |
*/ |
| 110 |
void |
| 111 |
AgentPack::init(const std::string &stopAgent) |
| 112 |
{ |
| 113 |
t_agents::iterator stop = m_agents.find(stopAgent); |
| 114 |
|
| 115 |
for (t_agents::iterator i = m_agents.begin(); i != stop; ++i) { |
| 116 |
if (!i->second->isInitialized()) { |
| 117 |
i->second->init(); |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
//----------------------------------------------------------------- |
| 122 |
void |
| 123 |
AgentPack::update() |
| 124 |
{ |
| 125 |
t_agents::iterator end = m_agents.end(); |
| 126 |
for (t_agents::iterator i = m_agents.begin(); i != end; ++i) { |
| 127 |
i->second->update(); |
| 128 |
} |
| 129 |
} |
| 130 |
//----------------------------------------------------------------- |
| 131 |
/** |
| 132 |
* Shutdown initialized agents in reverse order. |
| 133 |
*/ |
| 134 |
void |
| 135 |
AgentPack::shutdown() |
| 136 |
{ |
| 137 |
t_agents::reverse_iterator rend = m_agents.rend(); |
| 138 |
for (t_agents::reverse_iterator i = m_agents.rbegin(); |
| 139 |
i != rend; ++i) |
| 140 |
{ |
| 141 |
if (i->second->isInitialized()) { |
| 142 |
i->second->shutdown(); |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|