00001 00006 /* Copyright, 2000 Nevrax Ltd. 00007 * 00008 * This file is part of NEVRAX NEL. 00009 * NEVRAX NEL is free software; you can redistribute it and/or modify 00010 * it under the terms of the GNU General Public License as published by 00011 * the Free Software Foundation; either version 2, or (at your option) 00012 * any later version. 00013 00014 * NEVRAX NEL is distributed in the hope that it will be useful, but 00015 * WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 * General Public License for more details. 00018 00019 * You should have received a copy of the GNU General Public License 00020 * along with NEVRAX NEL; see the file COPYING. If not, write to the 00021 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, 00022 * MA 02111-1307, USA. 00023 */ 00024 #include "nel/ai/agent/agent.h" 00025 #include "nel/ai/agent/agent_script.h" 00026 00027 namespace NLAIAGENT 00028 { 00029 00030 IMailBox::IMailBox(const IWordNumRef *parent):IConnectIA(parent) 00031 { 00032 } 00033 00034 IMailBox::IMailBox(const IMailBox &c) : IConnectIA(c) 00035 { 00036 } 00037 00038 IMailBox::IMailBox(NLMISC::IStream &is):IConnectIA(is) 00039 { 00040 } 00041 00042 IMailBox::~IMailBox() 00043 { 00044 } 00045 00046 CLocalMailBox::CLocalMailBox (const IWordNumRef *parent):IMailBox(parent),_Size(0) 00047 { 00048 } 00049 00050 CLocalMailBox::CLocalMailBox(NLMISC::IStream &is):IMailBox(is),_Size(0) 00051 { 00052 load(is); 00053 } 00054 00055 00056 CLocalMailBox::CLocalMailBox (const CLocalMailBox &A):IMailBox(A),_RunState(A._RunState),_Size(0) 00057 { 00058 } 00059 00060 CLocalMailBox::~CLocalMailBox() 00061 { 00062 while(_ListMessageIn.begin() != _ListMessageIn.end()) 00063 { 00064 IMessageBase *msg = (IMessageBase *)_ListMessageIn.back(); 00065 msg->release(); 00066 _ListMessageIn.pop_back(); 00067 } 00068 00069 } 00070 00071 const IMessageBase &CLocalMailBox::getMessage() 00072 { 00073 return *_ListMessageIn.back(); 00074 } 00075 00076 void CLocalMailBox::popMessage() 00077 { 00078 ((IMessageBase *)_ListMessageIn.back())->release(); 00079 _ListMessageIn.pop_back(); 00080 _Size --; 00081 } 00082 00083 sint32 CLocalMailBox::getMessageCount() const 00084 { 00085 return _Size; 00086 } 00087 00088 bool CLocalMailBox::isEmpty() const 00089 { 00090 return _Size == 0; 00091 } 00092 00093 IObjectIA::CProcessResult CLocalMailBox::sendMessage(IMessageBase *m) 00094 { 00095 m->release(); 00096 return IObjectIA::ProcessRun; 00097 } 00098 00099 void CLocalMailBox::addMessage(IMessageBase *msg) 00100 { 00101 _ListMessageIn.push_front(msg); 00102 _Size ++; 00103 } 00104 00105 // Ajoute une boite aux lettre dans la liste des boites aux lettres "source" (les bals à consulter). 00106 void CLocalMailBox::addMailBox(IMailBox *mail) 00107 { 00108 connect( mail ); 00109 _ListMailBox.push_back((IMailBox *)mail); 00110 00111 } 00112 00113 // Supprime une boite aux lettre dans la liste des boites aux lettres "source" (les bals à consulter). 00114 void CLocalMailBox::removeMailBox(IMailBox *mail) 00115 { 00116 eraseFromList<IMailBox *>(&_ListMailBox,mail); 00117 removeConnection(mail); 00118 } 00119 00120 00121 00122 const NLAIC::IBasicType *CLocalMailBox::clone() const 00123 { 00124 NLAIC::IBasicType *a = new CLocalMailBox(*this); 00125 return a; 00126 } 00127 00128 const NLAIC::IBasicType *CLocalMailBox::newInstance() const 00129 { 00130 NLAIC::IBasicType *a; 00131 if(getParent() != NULL) 00132 a = new CLocalMailBox((const IWordNumRef *)getParent()); 00133 else 00134 a = new CLocalMailBox((const IWordNumRef *)NULL); 00135 return a; 00136 } 00137 00138 const NLAIC::CIdentType &CLocalMailBox::getType() const 00139 { 00140 return IdLocalMailBox; 00141 } 00142 00143 void CLocalMailBox::onKill(IConnectIA *a) 00144 { 00145 eraseFromList<IMailBox *>(&_ListMailBox,(IMailBox *)a); 00146 00147 TListMessageIter msgItr = _ListMessageIn.begin(); 00148 while(msgItr != _ListMessageIn.end()) 00149 { 00150 IMessageBase *msg = (IMessageBase *)*msgItr; 00151 00152 if(msg->getSender() == a || msg->getContinuation() == a || msg->getReceiver() == a) 00153 { 00154 TListMessageIter iTmp = msgItr++; 00155 _ListMessageIn.erase(iTmp); 00156 msg->release(); 00157 _Size --; 00158 } 00159 else msgItr++; 00160 } 00161 00162 } 00163 00164 bool CLocalMailBox::isEqual(const IBasicObjectIA &a) const 00165 { 00166 const IMailBox &m = (const IMailBox &)a; 00167 return (const IWordNumRef &)m == (const IWordNumRef &)*this; 00168 } 00169 00170 void CLocalMailBox::save(NLMISC::IStream &os) 00171 { 00172 IMailBox::save(os); 00173 sint32 size = _ListMailBox.size(); 00174 os.serial( size ); 00175 TListMailBoxCstIter k = _ListMailBox.begin(); 00176 while(k != _ListMailBox.end()) 00177 { 00178 IMailBox *mail = *k++; 00179 ((IWordNumRef *)(const NLAIAGENT::IWordNumRef *)*mail)->save(os); 00180 } 00181 00182 size = _ListMessageIn.size(); 00183 os.serial( size ); 00184 TListMessageIter msgItr = _ListMessageIn.begin(); 00185 while(msgItr != _ListMessageIn.end()) 00186 { 00187 IMessageBase *msg = (IMessageBase *)*msgItr++; 00188 os.serial( (NLAIC::CIdentType &) (msg->getType()) ); 00189 msg->save(os); 00190 } 00191 00192 } 00193 00194 void CLocalMailBox::load(NLMISC::IStream &is) 00195 { 00196 IMailBox::load(is); 00197 sint32 i; 00198 NLAIC::CIdentTypeAlloc id; 00199 is.serial(i); 00200 _Size = 0; 00201 if(i) 00202 { 00203 while(i --) 00204 { 00205 is.serial(id); 00206 IWordNumRef *num = (IWordNumRef *)id.allocClass(); 00207 num->load(is); 00208 _ListMailBox.push_back((IMailBox *)((const IRefrence *)*num)); 00209 _Size ++; 00210 delete num; 00211 } 00212 } 00213 } 00214 00215 const IObjectIA::CProcessResult &CLocalMailBox::getState() const 00216 { 00217 return _RunState; 00218 } 00219 00220 void CLocalMailBox::setState(TProcessStatement state, IObjectIA *result) 00221 { 00222 _RunState.ResultState = state; 00223 _RunState.Result = result; 00224 } 00225 00226 const IObjectIA::CProcessResult &CLocalMailBox::run() 00227 { 00228 return getState(); 00229 } 00230 00231 void CLocalMailBox::getDebugString(std::string &t) const 00232 { 00233 t += "class CLocalMailBox parent"; 00234 00235 } 00236 }