|
Blender
V2.59
|
00001 /* 00002 * $Id: NG_NetworkScene.cpp 35172 2011-02-25 13:36:49Z jesterking $ 00003 * 00004 * ***** BEGIN GPL LICENSE BLOCK ***** 00005 * 00006 * This program is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU General Public License 00008 * as published by the Free Software Foundation; either version 2 00009 * of the License, or (at your option) any later version. 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 Foundation, 00018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00019 * 00020 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. 00021 * All rights reserved. 00022 * 00023 * The Original Code is: all of this file. 00024 * 00025 * Contributor(s): none yet. 00026 * 00027 * ***** END GPL LICENSE BLOCK ***** 00028 * NetworkSceneManagement generic implementation 00029 */ 00030 00035 #include <stdio.h> 00036 #include <MT_assert.h> 00037 #include <algorithm> 00038 00039 #include "NG_NetworkScene.h" 00040 #include "NG_NetworkDeviceInterface.h" 00041 #include "NG_NetworkMessage.h" 00042 #include "NG_NetworkObject.h" 00043 00044 NG_NetworkScene::NG_NetworkScene(NG_NetworkDeviceInterface* nic) 00045 { 00046 m_networkdevice = nic; 00047 } 00048 00049 NG_NetworkScene::~NG_NetworkScene() 00050 { 00051 ClearAllMessageMaps(); 00052 } 00053 00057 void NG_NetworkScene::proceed(double curtime) 00058 { 00059 if (!m_networkdevice) return; 00060 if (!m_networkdevice->IsOnline()) return; 00061 00062 ClearAllMessageMaps(); 00063 00064 // read all NetworkMessages from the device 00065 vector<NG_NetworkMessage*> messages = 00066 m_networkdevice->RetrieveNetworkMessages(); 00067 00068 vector<NG_NetworkMessage*>::iterator mesit = messages.begin(); 00069 for (; !(mesit == messages.end()); mesit++) { 00070 NG_NetworkMessage* message = (*mesit); 00071 vector<NG_NetworkMessage*>* tmplist=NULL; 00072 00073 vector<NG_NetworkMessage*>** tmplistptr = 00074 m_messagesByDestinationName[message->GetDestinationName()]; 00075 // if there is already a vector of messages, append, else create 00076 // a new vector and insert into map 00077 if (!tmplistptr) { 00078 tmplist = new vector<NG_NetworkMessage*>; 00079 m_messagesByDestinationName.insert(message->GetDestinationName(), 00080 tmplist); 00081 } else { 00082 tmplist = *tmplistptr; 00083 } 00084 message->AddRef(); 00085 tmplist->push_back(message); 00086 tmplist = NULL; 00087 00088 tmplistptr = m_messagesBySenderName[message->GetSenderName()]; 00089 // if there is already a vector of messages, append, else create 00090 // a new vector and insert into map 00091 if (!tmplistptr) { 00092 tmplist = new vector<NG_NetworkMessage*>; 00093 m_messagesBySenderName.insert(message->GetSenderName(), tmplist); 00094 } else { 00095 tmplist = *tmplistptr; 00096 } 00097 message->AddRef(); 00098 tmplist->push_back(message); 00099 tmplist = NULL; 00100 00101 tmplistptr = m_messagesBySubject[message->GetSubject()]; 00102 // if there is already a vector of messages, append, else create 00103 // a new vector and insert into map 00104 if (!tmplistptr) { 00105 tmplist = new vector<NG_NetworkMessage*>; 00106 m_messagesBySubject.insert(message->GetSubject(), tmplist); 00107 } else { 00108 tmplist = *tmplistptr; 00109 } 00110 message->AddRef(); 00111 tmplist->push_back(message); 00112 tmplist = NULL; 00113 } 00114 } 00115 00119 void NG_NetworkScene::AddObject(NG_NetworkObject* object) 00120 { 00121 if (! m_networkdevice->IsOnline()) return; 00122 00123 const STR_String& name = object->GetName(); 00124 m_networkObjects.insert(name, object); 00125 } 00126 00130 void NG_NetworkScene::RemoveObject(NG_NetworkObject* object) 00131 { 00132 if (! m_networkdevice->IsOnline()) return; 00133 00134 const STR_String& name = object->GetName(); 00135 m_networkObjects.remove(name); 00136 } 00137 00141 void NG_NetworkScene::RemoveAllObjects() 00142 { 00143 m_networkObjects.clear(); 00144 } 00145 00149 NG_NetworkObject* NG_NetworkScene::FindNetworkObject(const STR_String& objname) { 00150 NG_NetworkObject *nwobj = NULL; 00151 if (! m_networkdevice->IsOnline()) return nwobj; 00152 00153 NG_NetworkObject **nwobjptr = m_networkObjects[objname]; 00154 if (nwobjptr) { 00155 nwobj = *nwobjptr; 00156 } 00157 00158 return nwobj; 00159 } 00160 00161 bool NG_NetworkScene::ConstraintsAreValid( 00162 const STR_String& from, 00163 const STR_String& subject, 00164 NG_NetworkMessage* message) 00165 { 00166 vector<NG_NetworkMessage*>** fromlistptr = m_messagesBySenderName[from]; 00167 vector<NG_NetworkMessage*>** subjectlistptr = m_messagesBySubject[subject]; 00168 00169 vector<NG_NetworkMessage*>* fromlist = (fromlistptr ? *fromlistptr : NULL); 00170 vector<NG_NetworkMessage*>* subjectlist = (subjectlistptr ? *subjectlistptr : NULL); 00171 00172 return ( 00173 ( from.IsEmpty() || (!fromlist ? false : (!(std::find(fromlist->begin(), fromlist->end(), message) == fromlist->end()))) 00174 ) && 00175 ( subject.IsEmpty() || (!subjectlist ? false : (!(std::find(subjectlist->begin(), subjectlist->end(), message) == subjectlist->end()))) 00176 )); 00177 } 00178 00179 vector<NG_NetworkMessage*> NG_NetworkScene::FindMessages( 00180 const STR_String& to, 00181 const STR_String& from, 00182 const STR_String& subject, 00183 bool spamallowed) 00184 { 00185 vector<NG_NetworkMessage*> foundmessages; 00186 bool notfound = false; 00187 00188 // broad phase 00189 notfound = ((to.IsEmpty() || spamallowed) ? notfound : m_messagesByDestinationName[to] == NULL); 00190 if (!notfound) 00191 notfound = (from.IsEmpty() ? notfound : m_messagesBySenderName[from] == NULL); 00192 if (!notfound) 00193 notfound = (subject.IsEmpty() ? notfound : m_messagesBySubject[subject] == NULL); 00194 if (notfound) { 00195 // it's definitely NOT in the scene, so stop looking 00196 } else { // narrow phase 00197 // possibly it's there, but maybe not (false hit) 00198 if (to.IsEmpty()) { 00199 // take all messages, and check other fields 00200 MT_assert(!"objectnames that are empty are not valid, so make it a hobby project :)\n"); 00201 } else { 00202 //todo: find intersection of messages (that are in other 2 maps) 00203 vector<NG_NetworkMessage*>** tolistptr = m_messagesByDestinationName[to]; 00204 if (tolistptr) { 00205 vector<NG_NetworkMessage*>* tolist = *tolistptr; 00206 vector<NG_NetworkMessage*>::iterator listit; 00207 for (listit=tolist->begin();!(listit==tolist->end());listit++) { 00208 NG_NetworkMessage* message = *listit; 00209 if (ConstraintsAreValid(from, subject, message)) { 00210 message->AddRef(); 00211 foundmessages.push_back(message); 00212 } 00213 } 00214 } 00215 // TODO find intersection of messages (that are in other 2 maps) 00216 if (spamallowed) { 00217 tolistptr = m_messagesByDestinationName[""]; 00218 if (tolistptr) { 00219 vector<NG_NetworkMessage*>* tolist = *tolistptr; 00220 vector<NG_NetworkMessage*>::iterator listit; 00221 for (listit=tolist->begin();!(listit==tolist->end());listit++) { 00222 NG_NetworkMessage* message = *listit; 00223 if (ConstraintsAreValid(from, subject, message)) { 00224 message->AddRef(); 00225 foundmessages.push_back(message); 00226 } 00227 } 00228 } 00229 } 00230 } 00231 } 00232 return foundmessages; 00233 } 00234 00235 void NG_NetworkScene::SendMessage( 00236 const STR_String& to, 00237 const STR_String& from, 00238 const STR_String& subject, 00239 const STR_String& message) 00240 { 00241 NG_NetworkMessage* msg = new NG_NetworkMessage(to, from, subject, message); 00242 m_networkdevice->SendNetworkMessage(msg); 00243 msg->Release(); 00244 } 00245 00246 void NG_NetworkScene::ClearAllMessageMaps(void) 00247 { 00248 ClearMessageMap(m_messagesByDestinationName); 00249 ClearMessageMap(m_messagesBySenderName); 00250 ClearMessageMap(m_messagesBySubject); 00251 } 00252 00253 void NG_NetworkScene::ClearMessageMap(TMessageMap& map) 00254 { 00255 // Release the messages in the map 00256 for (int i = 0; i < map.size(); i++) { 00257 vector<NG_NetworkMessage*>* msglist; 00258 msglist = *(map.at(i)); 00259 00260 // Iterate through the current vector and release all it's messages 00261 vector<NG_NetworkMessage*>::iterator msgit; 00262 for (msgit = msglist->begin(); msgit != msglist->end(); msgit++) { 00263 (*msgit)->Release(); 00264 } 00265 00266 // Delete the actual vector 00267 delete (msglist); 00268 } 00269 00270 // Empty the map 00271 map.clear(); 00272 } 00273