/** \file server.cpp * * * $Id: server.cpp,v 1.1 2001/04/18 13:54:25 valignat Exp $ */ /* Copyright, 2001 Nevrax Ltd. * * This file is part of NEVRAX NeL Network Services. * NEVRAX NeL Network Services is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * NEVRAX NeL Network Services is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * You should have received a copy of the GNU General Public License * along with NEVRAX NeL Network Services; see the file COPYING. If not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, * MA 02111-1307, USA. */ #include #include "nel/ai/agent/agent.h" #include "nel/ai/script/codage.h" #include "nel/ai/script/interpret_object_agent.h" #include "nel/ai/agent/main_agent_script.h" #include "nel/ai/script/libcode.h" #include "nel/misc/thread.h" #include "server.h" const char logFile[] = "compile.log"; #include "filestream_n.h" #include "io_interface.h" #include "ag_compile.h" #include "init.h" namespace NLAISCRIPT { #ifdef NL_DEBUG extern bool NL_AI_DEBUG_SERVER; #endif } namespace SRV { std::fstream logStream; bool logFileState = false; SRV::IOTrace Interface; TAgentId getId(const NLAIAGENT::CAgentNumber &num) { TAgentId i; i.CreatorSId = num.CreatorId; i.CurrentSId = num.DynamicId; i.SerialNumber = num.AgentNumber; return i; } NLAIAGENT::CAgentNumber getAgentId(TBasicAgentId &num) { NLAIAGENT::CAgentNumber i; i.CreatorId = num.CreatorSId; i.AgentNumber = num.SerialNumber; return i; } CApplication::CApplication(const std::vector &args):IManager(&Interface),_Active(true),_Shell(NULL),_Runnable(NULL),_ManagerRun(NULL) { Interface.incRef(); SRV::CPyEditWin::PyServer = NLAIPYSERVER::initPythonLib(&Interface,"./pyscript"); Interface.incRef(); SRV::CPyEditWin::inputOutput = &Interface; Interface.incRef(); NLAISCRIPT::CCallPrint::inputOutput = &Interface; if(logFileState) logStream.open(logFile, std::ios_base::in | std::ios_base::out | std::ios_base::binary | std::ios_base::trunc); //init(); } bool CApplication::isActive() const { return _Active; } NLAIAGENT::IRefrence *CApplication::agentIsPresent( TBasicAgentId baid ) const { const NLAIAGENT::CAgentNumber &id = getAgentId(baid); return NLAIAGENT::CLocWordNumRef::getRef(NLAIAGENT::CNumericIndex(id)); } void CApplication::deliverMsg( NLAIAGENT::IRefrence *ag, NLNET::CMessage& inmsg, TAgentId src_aid ) { NLAIC::CIdentTypeAlloc msgFactory; inmsg.serial( msgFactory ); NLAIAGENT::IObjectIA *msg = (NLAIAGENT::IObjectIA *) msgFactory.allocClass(); inmsg.serial( *msg ); #ifdef NL_DEBUG NLAISCRIPT::NL_AI_DEBUG_SERVER = true; #endif ag->sendMessage(msg); } TAgentId CApplication::getAgentManagerId() const { const NLAIAGENT::CNumericIndex &id = ((const NLAIAGENT::IWordNumRef &)*this).getNumIdent(); return getId(id.getId()); } void CApplication::declareNewManager( const TAgentId& agtid ) { NLAIAGENT::CAgentNumber id; id.CreatorId = agtid.CreatorSId; id.DynamicId = agtid.CurrentSId; id.AgentNumber = agtid.SerialNumber; std::list::iterator itr = _AgentServerID.begin(); while(itr != _AgentServerID.end()) { if(*(itr ++) == id) return; } _AgentServerID.push_back(id); } void CApplication::releaseManager( const TAgentId& agtid ) { NLAIAGENT::CAgentNumber id; id.CreatorId = agtid.CreatorSId; id.DynamicId = agtid.CurrentSId; id.AgentNumber = agtid.SerialNumber; std::list::iterator itr = _AgentServerID.begin(); while(itr != _AgentServerID.end()) { if(*(itr ++) == id) { _AgentServerID.erase(itr); return; } } } void CApplication::correctAgentLocation( TAgentId new_aid ) { } NLAIAGENT::IObjectIA::CProcessResult CApplication::sendMessage(const NLAIAGENT::CAgentNumber &ref,NLAIAGENT::IObjectIA *m) { NLNET::CMessage msg("Ag"); msg.serial( (NLAIC::CIdentType &)m->getType() ); msg.serial(*m); const NLAIAGENT::CNumericIndex &id = ((const NLAIAGENT::IWordNumRef &)*this).getNumIdent(); CAgentService::sendToAgent(getId(id.getId()),getId(ref),msg); return NLAIAGENT::IObjectIA::CProcessResult(); } void CApplication::init(NLMISC::CSynchronized &thisSync) { initServer(); bool i = haveActivity(); _Runnable = new SRV::CVoidRun(thisSync, _MessageList); _ManagerRun = NLMISC::IThread::create(_Runnable); _Runnable->setThread(_ManagerRun); _Shell = new CPyEditWin(_MessageList,*_ManagerRun); _ManagerRun->start(); const NLAIAGENT::CNumericIndex &id = ((const NLAIAGENT::IWordNumRef &)*this).getNumIdent(); char text[1024]; id.getDebugString(text); _CodeContext->InputOutput->Echo("service %s is run\n",text); } clock_t start = clock() - 11; NLAIAGENT::IObjectIA::CProcessResult CApplication::runActivity() { /*NLNET::CMessage msg("PING"); CMessageService m("PINGS",new CObjectSerial(msg)); CMsgTranslater::MsgTranslater->sendMessage((CMessageService *)m.clone());*/ /*double time = ((double)(clock() - start))/(double)CLOCKS_PER_SEC; if(time >= 5) { NLAIAGENT::CGroupType g; NLAIAGENT::CStringType *name = new NLAIAGENT::CStringType(NLAIAGENT::CStringVarName("Mektoube")); name->incRef(); g.push(name); CCreatureHerbivore *x = new CCreatureHerbivore(this); x->incRef(); x->connect(); g.push(x); addDynamicAgent(&g); start = clock(); }*/ NLMISC::CSynchronized::CAccessor a(CMsgTranslater::MsgTranslater); a.value()->run(); return NLAIAGENT::IObjectIA::CProcessResult(); } NLAIAGENT::IMessageBase *CApplication::runKill(const NLAIAGENT::IMessageBase &m) { _Active = false; //_ManagerRun->terminate(); return (NLAIAGENT::IMessageBase *)m.clone(); } void CApplication::leave() { _ManagerRun->terminate(); } CApplication::~CApplication() { NLAIPYSERVER::endPythonInterface(SRV::CPyEditWin::PyServer); if(logFileState) logStream.close(); _Shell->release(); delete _Runnable; delete _ManagerRun; } }