Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

agentcontrol.cpp

Go to the documentation of this file.
00001 /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*-
00002    this file is part of rcssserver3D
00003    Fri May 9 2003
00004    Copyright (C) 2003 Koblenz University
00005    $Id: agentcontrol.cpp,v 1.2 2004/05/06 09:37:39 rollmark Exp $
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; version 2 of the License.
00010 
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014    GNU General Public License for more details.
00015 
00016    You should have received a copy of the GNU General Public License
00017    along with this program; if not, write to the Free Software
00018    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019 */
00020 #include "agentcontrol.h"
00021 #include "simulationserver.h"
00022 #include "netmessage.h"
00023 #include <zeitgeist/logserver/logserver.h>
00024 #include <oxygen/gamecontrolserver/gamecontrolserver.h>
00025 #include <oxygen/agentaspect/agentaspect.h>
00026 
00027 using namespace oxygen;
00028 using namespace zeitgeist;
00029 using namespace boost;
00030 using namespace std;
00031 
00032 AgentControl::AgentControl() : NetControl()
00033 {
00034     mLocalAddr.setPort(3100);
00035 }
00036 
00037 AgentControl::~AgentControl()
00038 {
00039 }
00040 
00041 void AgentControl::OnLink()
00042 {
00043     NetControl::OnLink();
00044     shared_ptr<SimulationServer> sim = GetSimulationServer();
00045     if (sim.get() == 0)
00046         {
00047             GetLog()->Error()
00048                 << "(AgentControl) ERROR: SimulationServer not found\n";
00049             return;
00050         }
00051 
00052     mGameControlServer = sim->GetGameControlServer();
00053 }
00054 
00055 void AgentControl::OnUnlink()
00056 {
00057     NetControl::OnUnlink();
00058     mGameControlServer.reset();
00059 }
00060 
00061 void AgentControl::ClientConnect(shared_ptr<Client> client)
00062 {
00063     if (mGameControlServer.get() == 0)
00064         {
00065             return;
00066         }
00067 
00068     mGameControlServer->AgentConnect(client->id);
00069 }
00070 
00071 void AgentControl::ClientDisconnect(shared_ptr<Client> client)
00072 {
00073     if (mGameControlServer.get() == 0)
00074         {
00075             return;
00076         }
00077 
00078     mGameControlServer->AgentDisappear(client->id);
00079 }
00080 
00081 void AgentControl::StartCycle()
00082 {
00083     NetControl::StartCycle();
00084 
00085     if (
00086         (mGameControlServer.get() == 0) ||
00087         (mNetMessage.get() == 0)
00088         )
00089         {
00090             return;
00091         }
00092 
00093     // pass all received messages on to the GameControlServer
00094     for (
00095          TBufferMap::iterator iter = mBuffers.begin();
00096          iter != mBuffers.end();
00097          ++iter
00098          )
00099         {
00100             shared_ptr<NetBuffer>& netBuff = (*iter).second;
00101             if (
00102                 (netBuff.get() == 0) ||
00103                 (netBuff->IsEmpty())
00104                 )
00105                 {
00106                     continue;
00107                 }
00108 
00109             // lookup the client entry corresponding for the buffer
00110             // entry
00111             TAddrMap::iterator clientIter = mClients.find(netBuff->GetAddr());
00112             if (clientIter == mClients.end())
00113                 {
00114                     continue;
00115                 }
00116             shared_ptr<Client>& client = (*clientIter).second;
00117 
00118             // lookup the AgentAspect node correspoding to the client
00119             shared_ptr<AgentAspect> agent =
00120                 mGameControlServer->GetAgentAspect(client->id);
00121             if (agent.get() == 0)
00122                 {
00123                     continue;
00124                 }
00125 
00126             // parse and immediately realize the action
00127             string message;
00128             while (mNetMessage->Extract(netBuff,message))
00129                 {
00130                     agent->RealizeActions
00131                         (mGameControlServer->Parse(client->id,message));
00132                 }
00133         }
00134 }
00135 
00136 void AgentControl::EndCycle()
00137 {
00138     NetControl::EndCycle();
00139 
00140     if (
00141         (mGameControlServer.get() == 0) ||
00142         (mNetMessage.get() == 0) ||
00143         (mClients.size() == 0)
00144         )
00145         {
00146             return;
00147         }
00148 
00149     shared_ptr<BaseParser> parser = mGameControlServer->GetParser();
00150     if (parser.get() == 0)
00151         {
00152             GetLog()->Error()
00153                 << "(AgentControl) ERROR:  got no parser from "
00154                 << " the GameControlServer" << endl;
00155             return;
00156         }
00157 
00158     // generate senses for all agents and send them to the
00159     // corresponding net clients
00160     for (
00161          TAddrMap::iterator iter = mClients.begin();
00162          iter != mClients.end();
00163          ++iter
00164          )
00165         {
00166             shared_ptr<Client>& client = (*iter).second;
00167 
00168             shared_ptr<AgentAspect> agent =
00169                 mGameControlServer->GetAgentAspect(client->id);
00170             if (agent.get() == 0)
00171                 {
00172                     continue;
00173                 }
00174 
00175             shared_ptr<PredicateList> senseList = agent->QueryPerceptors();
00176             string senses = parser->Generate(senseList);
00177             if (senses.empty())
00178                 {
00179                     continue;
00180                 }
00181 
00182             mNetMessage->PrepareToSend(senses);
00183             SendMessage(client,senses);
00184         }
00185 }
00186 

Generated on Thu Apr 6 15:25:37 2006 for rcssserver3d by  doxygen 1.4.4