aboutsummaryrefslogtreecommitdiff
path: root/cvs/cvsweb.cgi/~checkout~/code/nelns/agent_service/Attic/server.cpp?rev=1.1&content-type=text/plain&hideattic=0/index.html
blob: 9c27da39586ce679e30cc68eeaeb0ac3130bab19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/** \file server.cpp
 * <File description>
 *
 * $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 <time.h>
#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<std::string> &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<NLAIAGENT::CAgentNumber>::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<NLAIAGENT::CAgentNumber>::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<IManager *> &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<CMsgTranslater *>::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;
	}
}