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: monitorcontrol.cpp,v 1.4 2004/05/06 09:34:46 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 "monitorcontrol.h" 00021 #include "simulationserver.h" 00022 #include "netmessage.h" 00023 #include <zeitgeist/logserver/logserver.h> 00024 #include <oxygen/monitorserver/monitorserver.h> 00025 #include <oxygen/sceneserver/sceneserver.h> 00026 #include <oxygen/sceneserver/scene.h> 00027 00028 using namespace oxygen; 00029 using namespace zeitgeist; 00030 using namespace boost; 00031 using namespace std; 00032 00033 MonitorControl::MonitorControl() : NetControl() 00034 { 00035 mLocalAddr.setPort(3200); 00036 mMonitorInterval = 30; 00037 } 00038 00039 MonitorControl::~MonitorControl() 00040 { 00041 } 00042 00043 void MonitorControl::OnLink() 00044 { 00045 NetControl::OnLink(); 00046 shared_ptr<SimulationServer> sim = GetSimulationServer(); 00047 if (sim.get() == 0) 00048 { 00049 GetLog()->Error() 00050 << "(MonitorControl) ERROR: SimulationServer not found\n"; 00051 return; 00052 } 00053 00054 mMonitorServer = sim->GetMonitorServer(); 00055 } 00056 00057 void MonitorControl::OnUnlink() 00058 { 00059 NetControl::OnUnlink(); 00060 mMonitorServer.reset(); 00061 } 00062 00063 void MonitorControl::ClientConnect(shared_ptr<Client> client) 00064 { 00065 if ( 00066 (mMonitorServer.get() == 0) || 00067 (mNetMessage.get() == 0) 00068 ) 00069 { 00070 return; 00071 } 00072 00073 string header = mMonitorServer->GetMonitorHeaderInfo(); 00074 mNetMessage->PrepareToSend(header); 00075 SendMessage(client->addr,header); 00076 } 00077 00078 void MonitorControl::EndCycle() 00079 { 00080 NetControl::EndCycle(); 00081 00082 const int cycle = GetSimulationServer()->GetCycle(); 00083 if (cycle % mMonitorInterval) 00084 { 00085 return; 00086 } 00087 00088 if ( 00089 (mMonitorServer.get() == 0) || 00090 (mNetMessage.get() == 0) || 00091 (mClients.size() == 0) 00092 ) 00093 { 00094 return; 00095 } 00096 00097 // send updates to all connected monitors 00098 string info = mMonitorServer->GetMonitorInfo(); 00099 mNetMessage->PrepareToSend(info); 00100 00101 for ( 00102 TAddrMap::iterator iter = mClients.begin(); 00103 iter != mClients.end(); 00104 ++iter 00105 ) 00106 { 00107 SendMessage((*iter).second,info); 00108 } 00109 00110 // reset the modified flag for the active scene 00111 shared_ptr<SceneServer> sceneServer = 00112 GetSimulationServer()->GetSceneServer(); 00113 00114 if (sceneServer.get() !=0) 00115 { 00116 shared_ptr<Scene> scene = sceneServer->GetActiveScene(); 00117 if (scene.get() != 0) 00118 { 00119 scene->SetModified(false); 00120 } 00121 } 00122 } 00123 00124 void MonitorControl::StartCycle() 00125 { 00126 NetControl::StartCycle(); 00127 00128 if ( 00129 (mMonitorServer.get() == 0) || 00130 (mNetMessage.get() == 0) 00131 ) 00132 { 00133 return; 00134 } 00135 00136 // pass all received messages to the MonitorServer 00137 for ( 00138 TBufferMap::iterator iter = mBuffers.begin(); 00139 iter != mBuffers.end(); 00140 ++iter 00141 ) 00142 { 00143 shared_ptr<NetBuffer>& netBuff = (*iter).second; 00144 if ( 00145 (netBuff.get() == 0) || 00146 (netBuff->IsEmpty()) 00147 ) 00148 { 00149 continue; 00150 } 00151 00152 string message; 00153 while (mNetMessage->Extract(netBuff,message)) 00154 { 00155 mMonitorServer->ParseMonitorMessage(message); 00156 } 00157 } 00158 } 00159 00160 int MonitorControl::GetMonitorInterval() 00161 { 00162 return mMonitorInterval; 00163 } 00164 00165 void MonitorControl::SetMonitorInterval(int i) 00166 { 00167 mMonitorInterval = i; 00168 } 00169 00170