00001
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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 CSimpleLocalMailBox::CSimpleLocalMailBox(const IWordNumRef *parent) : IMailBox(parent), _Size(0)
00047 {
00048 }
00049
00050 CSimpleLocalMailBox::CSimpleLocalMailBox(NLMISC::IStream &is) : IMailBox(is), _Size(0)
00051 {
00052 load(is);
00053 }
00054
00055
00056 CSimpleLocalMailBox::CSimpleLocalMailBox(const CSimpleLocalMailBox &a) : IMailBox(a), _Size(0)
00057 {
00058 tListMessage::const_reverse_iterator i = a._ListMessage.rbegin();
00059 while(i != a._ListMessage.rend())
00060 {
00061 _ListMessage.push_back(*i++);
00062 ((IMessageBase *)_ListMessage.back())->incRef();
00063 _Size ++;
00064 }
00065 _RunState = a._RunState;
00066 }
00067
00068 CSimpleLocalMailBox::~CSimpleLocalMailBox()
00069 {
00070 while(_ListMessage.begin() != _ListMessage.end())
00071 {
00072 IMessageBase *m = (IMessageBase *)_ListMessage.back();
00073 m->release();
00074 _ListMessage.pop_back();
00075 }
00076 }
00077
00078 void CSimpleLocalMailBox::addMessage(IMessageBase *msg)
00079 {
00080 _ListMessage.push_front( msg );
00081 _Size ++;
00082 }
00083
00084 void CSimpleLocalMailBox::addMailBox(IMailBox *)
00085 {
00086 }
00087
00088 void CSimpleLocalMailBox::removeMailBox(IMailBox *)
00089 {
00090 }
00091
00092 const IMessageBase &CSimpleLocalMailBox::getMessage()
00093 {
00094 return *_ListMessage.back();
00095 }
00096
00097 void CSimpleLocalMailBox::popMessage()
00098 {
00099 ((IMessageBase *)_ListMessage.back())->release();
00100 _ListMessage.pop_back();
00101 _Size --;
00102 }
00103
00104 sint32 CSimpleLocalMailBox::getMessageCount() const
00105 {
00106 return _ListMessage.size();
00107 }
00108 bool CSimpleLocalMailBox::isEmpty() const
00109 {
00110 return _Size == 0;
00111 }
00112
00113 std::list<const IMessageBase *> *CSimpleLocalMailBox::pumpMessages() const
00114 {
00115 std::list<const IMessageBase *> *result = new std::list<const IMessageBase *>;
00116
00117 std::list<const IMessageBase *>::const_iterator i = _ListMessage.begin();
00118 while(i != _ListMessage.end() )
00119 {
00120 result->push_back( *i++ );
00121 }
00122 return result;
00123 }
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140 const IObjectIA::CProcessResult &CSimpleLocalMailBox::getState() const
00141 {
00142 return _RunState;
00143 }
00144
00145 void CSimpleLocalMailBox::setState(TProcessStatement state, IObjectIA *result)
00146 {
00147 _RunState.ResultState = state;
00148 _RunState.Result = result;
00149 }
00150
00151 const IObjectIA::CProcessResult &CSimpleLocalMailBox::run()
00152 {
00153 return IObjectIA::ProcessRun;
00154 }
00155
00156 bool CSimpleLocalMailBox::isEqual(const IBasicObjectIA &a) const
00157 {
00158 const IMailBox &m = (const IMailBox &)a;
00159 return (const IWordNumRef &)m == (const IWordNumRef &)*this;
00160 }
00161
00162 const NLAIC::IBasicType *CSimpleLocalMailBox::clone() const
00163 {
00164 NLAIC::IBasicType *a = new CSimpleLocalMailBox(*this);
00165 return a;
00166 }
00167
00168 const NLAIC::IBasicType *CSimpleLocalMailBox::newInstance() const
00169 {
00170 NLAIC::IBasicType *a;
00171 if(getParent() != NULL)
00172 a = new CSimpleLocalMailBox((const IWordNumRef *) getParent());
00173 else
00174 a = new CSimpleLocalMailBox((const IWordNumRef *) NULL);
00175 return a;
00176 }
00177
00178 const NLAIC::CIdentType &CSimpleLocalMailBox::getType() const
00179 {
00180 return IdSimpleLocalMailBox;
00181 }
00182
00183 void CSimpleLocalMailBox::getDebugString(std::string &t) const
00184 {
00185 t += "class CSimpleLocalMailBox";
00186 }
00187
00188 void CSimpleLocalMailBox::save(NLMISC::IStream &os)
00189 {
00190 IConnectIA::save(os);
00191 sint32 size = _ListMessage.size();
00192 os.serial( size );
00193
00194 tListMessage::const_iterator i = _ListMessage.begin();
00195 while(i != _ListMessage.end())
00196 {
00197 IMessageBase &m = (IMessageBase &)*(*i);
00198 os.serial( (NLAIC::CIdentType &) m.getType() );
00199
00200 m.save(os);
00201 }
00202
00203 }
00204
00205 void CSimpleLocalMailBox::load(NLMISC::IStream &is)
00206 {
00207 IConnectIA::load(is);
00208 while(_ListMessage.size())
00209 {
00210 ((IMessageBase *)_ListMessage.back())->release();
00211 _ListMessage.pop_back();
00212 }
00213 _Size = 0;
00214
00215 NLAIC::CIdentTypeAlloc id;
00216 sint32 i;
00217 is.serial(i);
00218 if(i)
00219 {
00220 while(i --)
00221 {
00222 is.serial(id);
00223 IMessageBase *m = (IMessageBase *)id.allocClass();
00224 is.serial( *m );
00225 _ListMessage.push_back(m);
00226 _Size ++;
00227 }
00228 }
00229 }
00230
00231 void CSimpleLocalMailBox::onKill(IConnectIA *)
00232 {
00233 }
00234
00235 IObjectIA::CProcessResult CSimpleLocalMailBox::sendMessage(IMessageBase *m)
00236 {
00237 m->release();
00238 return IObjectIA::ProcessRun;
00239 }
00240
00241 IConnectIA &CSimpleLocalMailBox::getConnection(IConnectIA &r)
00242 {
00243 addInConnectedList(&r);
00244 return *this;
00245 }
00246
00247 CLocalMailBox::CLocalMailBox (const IWordNumRef *parent):IMailBox(parent),_Size(0)
00248 {
00249 }
00250
00251 CLocalMailBox::CLocalMailBox(NLMISC::IStream &is):IMailBox(is),_Size(0)
00252 {
00253 load(is);
00254 }
00255
00256
00257 CLocalMailBox::CLocalMailBox (const CLocalMailBox &A):IMailBox(A),_RunState(A._RunState),_Size(0)
00258 {
00259
00260
00261
00262
00263
00264
00265 }
00266
00267 CLocalMailBox::~CLocalMailBox()
00268 {
00269 while(_ListMessageIn.begin() != _ListMessageIn.end())
00270 {
00271 IMessageBase *msg = (IMessageBase *)_ListMessageIn.back();
00272 #ifdef NL_DEBUG
00273 const char *mname = (const char *)msg->getType();
00274 #endif
00275 msg->release();
00276 _ListMessageIn.pop_back();
00277 }
00278
00279 while(_ListSharedMessage.begin() != _ListSharedMessage.end())
00280 {
00281 IMessageBase *msg = (IMessageBase *)_ListSharedMessage.back();
00282 msg->release();
00283 _ListSharedMessage.pop_back();
00284 }
00285
00286 }
00287
00288 void CLocalMailBox::shareMessage()
00289 {
00290 IMessageBase *msg = (IMessageBase *)_ListMessageIn.back();
00291 msg->incRef();
00292 _ListSharedMessage.push_back(msg);
00293 }
00294
00295 const IMessageBase &CLocalMailBox::getMessage()
00296 {
00297 return *_ListMessageIn.back();
00298 }
00299
00300 void CLocalMailBox::popMessage()
00301 {
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313 ((IMessageBase *)_ListMessageIn.back())->release();
00314 _ListMessageIn.pop_back();
00315 _Size --;
00316 }
00317
00318 sint32 CLocalMailBox::getMessageCount() const
00319 {
00320 return _Size;
00321 }
00322
00323 bool CLocalMailBox::isEmpty() const
00324 {
00325 return _Size == 0;
00326 }
00327
00328
00329
00330
00331
00332 IObjectIA::CProcessResult CLocalMailBox::sendMessage(IMessageBase *m)
00333 {
00334 m->release();
00335
00336 return IObjectIA::ProcessRun;
00337 }
00338
00339 void CLocalMailBox::addMessage(IMessageBase *msg)
00340 {
00341 _ListMessageIn.push_front(msg);
00342 _Size ++;
00343 }
00344
00345
00346 void CLocalMailBox::addMailBox(IMailBox *mail)
00347 {
00348 connect( mail );
00349 _ListMailBox.push_back((IMailBox *)mail);
00350
00351
00352
00353
00354
00355
00356
00357
00358 }
00359
00360
00361 void CLocalMailBox::removeMailBox(IMailBox *mail)
00362 {
00363 eraseFromList<IMailBox *>(&_ListMailBox,mail);
00364 removeConnection(mail);
00365 }
00366
00367 void CLocalMailBox::setName(const IVarName &)
00368 {
00369 }
00370
00371 const NLAIC::IBasicType *CLocalMailBox::clone() const
00372 {
00373 NLAIC::IBasicType *a = new CLocalMailBox(*this);
00374 return a;
00375 }
00376
00377 const NLAIC::IBasicType *CLocalMailBox::newInstance() const
00378 {
00379 NLAIC::IBasicType *a;
00380 if(getParent() != NULL)
00381 a = new CLocalMailBox((const IWordNumRef *)getParent());
00382 else
00383 a = new CLocalMailBox((const IWordNumRef *)NULL);
00384 return a;
00385 }
00386
00387 const NLAIC::CIdentType &CLocalMailBox::getType() const
00388 {
00389 return IdLocalMailBox;
00390 }
00391
00392 void CLocalMailBox::onKill(IConnectIA *a)
00393 {
00394 eraseFromList<IMailBox *>(&_ListMailBox,(IMailBox *)a);
00395
00396 tListMessageIter msgItr = _ListMessageIn.begin();
00397 while(msgItr != _ListMessageIn.end())
00398 {
00399 IMessageBase *msg = (IMessageBase *)*msgItr;
00400
00401 if(msg->getSender() == a || msg->getContinuation() == a)
00402 {
00403 tListMessageIter iTmp = msgItr++;
00404 _ListMessageIn.erase(iTmp);
00405 msg->release();
00406 _Size --;
00407 }
00408 else msgItr++;
00409 }
00410
00411 }
00412
00413 bool CLocalMailBox::isEqual(const IBasicObjectIA &a) const
00414 {
00415 const IMailBox &m = (const IMailBox &)a;
00416 return (const IWordNumRef &)m == (const IWordNumRef &)*this;
00417 }
00418
00419 void CLocalMailBox::save(NLMISC::IStream &os)
00420 {
00421 IMailBox::save(os);
00422 sint32 size = _ListMailBox.size();
00423 os.serial( size );
00424 tListMailBoxCstIter k = _ListMailBox.begin();
00425 while(k != _ListMailBox.end())
00426 {
00427 IMailBox *mail = *k++;
00428 ((IWordNumRef *)(const NLAIAGENT::IWordNumRef *)*mail)->save(os);
00429 }
00430
00431 size = _ListMessageIn.size();
00432 os.serial( size );
00433 tListMessageIter msgItr = _ListMessageIn.begin();
00434 while(msgItr != _ListMessageIn.end())
00435 {
00436 IMessageBase *msg = (IMessageBase *)*msgItr++;
00437 os.serial( (NLAIC::CIdentType &) (msg->getType()) );
00438 msg->save(os);
00439 }
00440
00441 size = _ListSharedMessage.size();
00442 os.serial( size );
00443 msgItr = _ListSharedMessage.begin();
00444 while(msgItr != _ListSharedMessage.end())
00445 {
00446 IMessageBase *msg = (IMessageBase *)*msgItr++;
00447 os.serial( (NLAIC::CIdentType &) (msg->getType()) );
00448 msg->save(os);
00449 }
00450
00451 }
00452
00453 void CLocalMailBox::load(NLMISC::IStream &is)
00454 {
00455 IMailBox::load(is);
00456 sint32 i;
00457 NLAIC::CIdentTypeAlloc id;
00458 is.serial(i);
00459 _Size = 0;
00460 if(i)
00461 {
00462 while(i --)
00463 {
00464 is.serial(id);
00465 IWordNumRef *num = (IWordNumRef *)id.allocClass();
00466 num->load(is);
00467 _ListMailBox.push_back((IMailBox *)((const IRefrence *)*num));
00468 _Size ++;
00469 delete num;
00470 }
00471 }
00472
00473
00474 }
00475
00476 const IObjectIA::CProcessResult &CLocalMailBox::getState() const
00477 {
00478 return _RunState;
00479 }
00480
00481 void CLocalMailBox::setState(TProcessStatement state, IObjectIA *result)
00482 {
00483 _RunState.ResultState = state;
00484 _RunState.Result = result;
00485 }
00486
00487 std::list<const IMessageBase *> *CLocalMailBox::pumpMessages() const
00488 {
00489
00490 if(_ListSharedMessage.begin() != _ListSharedMessage.end())
00491 {
00492 std::list<const IMessageBase *> *l = new tListMessage;
00493 tListMessageCstIter msgItr = _ListSharedMessage.begin();
00494 while(msgItr != _ListSharedMessage.end())
00495 {
00496 const IMessageBase *msg = *msgItr++;
00497 l->push_back((const IMessageBase *)msg->clone());
00498 }
00499 return l;
00500 }
00501 return NULL;
00502 }
00503
00504 const IObjectIA::CProcessResult &CLocalMailBox::run()
00505 {
00506
00507
00508
00509
00510
00511
00512
00513
00514 return getState();
00515 }
00516
00517 void CLocalMailBox::getDebugString(std::string &t) const
00518 {
00519 t += "class CLocalMailBox parent";
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529 }
00530
00531 void CLocalMailBox::fillMailBox()
00532 {
00533 tListMailBoxIter i_mbox = _ListMailBox.begin();
00534
00535 #ifdef NL_DEBUG
00536 int s = _ListMailBox.size();
00537 #endif
00538
00539 while ( i_mbox != _ListMailBox.end() )
00540 {
00541 IMailBox *box = (*i_mbox);
00542 std::list<const IMessageBase *> *msg_lst = box->pumpMessages();
00543 if ( msg_lst != NULL)
00544 {
00545 while ( msg_lst->begin() != msg_lst->end())
00546 {
00547 IMessageBase *m = (IMessageBase *) msg_lst->front();
00548 m->incRef();
00549 _ListMessageIn.push_back(m);
00550 m->release();
00551 msg_lst->pop_front();
00552 }
00553 delete msg_lst;
00554 }
00555 i_mbox++;
00556 }
00557 }
00558
00559
00560 CScriptMailBox::CScriptMailBox(const IWordNumRef *parent) : CSimpleLocalMailBox(parent)
00561 {
00562 }
00563
00564 CScriptMailBox::CScriptMailBox(NLMISC::IStream &is) : CSimpleLocalMailBox(is)
00565 {
00566 load(is);
00567 }
00568
00569 CScriptMailBox::CScriptMailBox(const CScriptMailBox &a) : CSimpleLocalMailBox(a)
00570 {
00571 tListMessage::const_reverse_iterator i = a._ListMessage.rbegin();
00572 while(i != a._ListMessage.rend())
00573 {
00574 _ListMessage.push_back(*i++);
00575 ((IMessageBase *)_ListMessage.back())->incRef();
00576 }
00577 _RunState = a._RunState;
00578 }
00579
00580 CScriptMailBox::~CScriptMailBox()
00581 {
00582 while(_ListMessage.begin() != _ListMessage.end())
00583 {
00584 IMessageBase *m = (IMessageBase *)_ListMessage.back();
00585 m->release();
00586 _ListMessage.pop_back();
00587 }
00588 }
00589
00590 const IObjectIA::CProcessResult &CScriptMailBox::run()
00591 {
00592
00593 fillMailBox();
00594 return IObjectIA::ProcessRun;
00595 }
00596
00598 void CScriptMailBox::fillMailBox()
00599 {
00600 }
00601
00602 void CScriptMailBox::setIndex(sint32 i)
00603 {
00604 _CIndex = i;
00605 }
00606
00607 sint32 CScriptMailBox::getIndex()
00608 {
00609 return _CIndex;
00610 }
00611
00612 const IMessageBase *CScriptMailBox::translateMsg(const IMessageBase *msg)
00613 {
00614 CAgentScript *father = (CAgentScript *) getParent()->getParent();
00615
00616 sint32 index = father->getChildMessageIndex( msg, _CIndex );
00617
00618 if ( index != -1 )
00619 {
00620 IMessageBase *translated_msg = (IMessageBase *) msg->clone();
00621 translated_msg->setMethodIndex( 0, index + ( (CAgentScript *) getParent())->getBaseMethodCount() );
00622 return translated_msg;
00623 }
00624 return NULL;
00625 }
00626
00627 const NLAIC::IBasicType *CScriptMailBox::clone() const
00628 {
00629 NLAIC::IBasicType *a = new CScriptMailBox(*this);
00630 return a;
00631 }
00632
00633 const NLAIC::IBasicType *CScriptMailBox::newInstance() const
00634 {
00635 NLAIC::IBasicType *a;
00636 if(getParent() != NULL)
00637 a = new CScriptMailBox((const IWordNumRef *)getParent());
00638 else
00639 a = new CScriptMailBox((const IWordNumRef *)NULL);
00640 return a;
00641 }
00642
00643 const NLAIC::CIdentType &CScriptMailBox::getType() const
00644 {
00645 return IdScriptMailBox;
00646 }
00647 }