Blender V2.61 - r43446

NG_NetworkScene.cpp

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