rebranch from 2.5
[blender.git] / source / gameengine / Network / NG_NetworkScene.cpp
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  * NetworkSceneManagement generic implementation
29  */
30 #include <stdio.h>
31 #include <MT_assert.h>
32 #include <algorithm>
33
34 #include "NG_NetworkScene.h"
35 #include "NG_NetworkDeviceInterface.h"
36 #include "NG_NetworkMessage.h"
37 #include "NG_NetworkObject.h"
38
39 #ifdef HAVE_CONFIG_H
40 #include <config.h>
41 #endif
42
43 NG_NetworkScene::NG_NetworkScene(NG_NetworkDeviceInterface* nic)
44 {
45         m_networkdevice = nic;
46 }
47
48 NG_NetworkScene::~NG_NetworkScene()
49 {
50         ClearAllMessageMaps();
51 }
52
53 /**
54  * progress one frame, handle all network traffic
55  */
56 void NG_NetworkScene::proceed(double curtime)
57 {
58         if (!m_networkdevice) return;
59         if (!m_networkdevice->IsOnline()) return;
60
61         ClearAllMessageMaps();
62
63         // read all NetworkMessages from the device
64         vector<NG_NetworkMessage*> messages =
65                 m_networkdevice->RetrieveNetworkMessages();
66
67         vector<NG_NetworkMessage*>::iterator mesit = messages.begin();
68         for (; !(mesit == messages.end()); mesit++) {
69                 NG_NetworkMessage* message = (*mesit);
70                 vector<NG_NetworkMessage*>* tmplist=NULL;
71
72                 vector<NG_NetworkMessage*>** tmplistptr =
73                         m_messagesByDestinationName[message->GetDestinationName()];
74                 // if there is already a vector of messages, append, else create
75                 // a new vector and insert into map
76                 if (!tmplistptr) {
77                         tmplist = new vector<NG_NetworkMessage*>;
78                         m_messagesByDestinationName.insert(message->GetDestinationName(),
79                                                                                            tmplist);
80                 } else {
81                         tmplist = *tmplistptr;
82                 }
83                 message->AddRef();
84                 tmplist->push_back(message);
85                 tmplist = NULL;
86
87                 tmplistptr = m_messagesBySenderName[message->GetSenderName()];
88                 // if there is already a vector of messages, append, else create
89                 // a new vector and insert into map
90                 if (!tmplistptr) {
91                         tmplist = new vector<NG_NetworkMessage*>;
92                         m_messagesBySenderName.insert(message->GetSenderName(), tmplist);
93                 }  else {
94                         tmplist = *tmplistptr;
95                 }
96                 message->AddRef();
97                 tmplist->push_back(message);
98                 tmplist = NULL;
99                 
100                 tmplistptr = m_messagesBySubject[message->GetSubject()];
101                 // if there is already a vector of messages, append, else create
102                 // a new vector and insert into map
103                 if (!tmplistptr) {
104                         tmplist = new vector<NG_NetworkMessage*>;
105                         m_messagesBySubject.insert(message->GetSubject(), tmplist);
106                 }  else {
107                         tmplist = *tmplistptr;
108                 }
109                 message->AddRef();
110                 tmplist->push_back(message);
111                 tmplist = NULL;
112         }
113 }
114
115 /**
116  * add a network object to the network scene
117  */
118 void NG_NetworkScene::AddObject(NG_NetworkObject* object)
119 {
120         if (! m_networkdevice->IsOnline()) return;
121
122         STR_String name = object->GetName();
123         m_networkObjects.insert(name, object);
124 }
125
126 /**
127  * remove a network object from the network scene
128  */
129 void NG_NetworkScene::RemoveObject(NG_NetworkObject* object)
130 {
131         if (! m_networkdevice->IsOnline()) return;
132
133         STR_String name = object->GetName();
134         m_networkObjects.remove(name);
135 }
136
137 /**
138  * remove all network scene objects at once
139  */
140 void NG_NetworkScene::RemoveAllObjects()
141 {
142         m_networkObjects.clear();
143 }
144
145 /**
146  * get a single network object given its name
147  */
148 NG_NetworkObject* NG_NetworkScene::FindNetworkObject(const STR_String& objname) {
149         NG_NetworkObject *nwobj = NULL;
150         if (! m_networkdevice->IsOnline()) return nwobj;
151
152         NG_NetworkObject **nwobjptr = m_networkObjects[objname];
153         if (nwobjptr) {
154                 nwobj = *nwobjptr;
155         }
156
157         return nwobj;
158 }
159
160 bool NG_NetworkScene::ConstraintsAreValid(
161         const STR_String& from,
162         const STR_String& subject,
163         NG_NetworkMessage* message)
164 {
165         vector<NG_NetworkMessage*>** fromlistptr =  m_messagesBySenderName[from];
166         vector<NG_NetworkMessage*>** subjectlistptr =  m_messagesBySubject[subject];
167
168         vector<NG_NetworkMessage*>* fromlist = (fromlistptr ? *fromlistptr : NULL);
169         vector<NG_NetworkMessage*>* subjectlist = (subjectlistptr ? *subjectlistptr : NULL);
170         
171         return (
172                 ( from.IsEmpty()    || (!fromlist ? false    : (!(std::find(fromlist->begin(), fromlist->end(), message)       == fromlist->end())))
173                 ) &&
174                 ( subject.IsEmpty() || (!subjectlist ? false : (!(std::find(subjectlist->begin(), subjectlist->end(), message) == subjectlist->end())))
175                 ));
176 }
177
178 vector<NG_NetworkMessage*> NG_NetworkScene::FindMessages(
179         const STR_String& to,
180         const STR_String& from,
181         const STR_String& subject,
182         bool spamallowed)
183 {
184         vector<NG_NetworkMessage*> foundmessages;
185         bool notfound = false;
186
187         // broad phase
188         notfound = ((to.IsEmpty() || spamallowed) ? notfound : m_messagesByDestinationName[to] == NULL);
189         if (!notfound)
190                 notfound = (from.IsEmpty() ? notfound : m_messagesBySenderName[from] == NULL);
191         if (!notfound)
192                 notfound = (subject.IsEmpty() ? notfound : m_messagesBySubject[subject] == NULL);
193         if (notfound) {
194                 // it's definately NOT in the scene, so stop looking
195         } else { // narrow phase
196                 // possibly it's there, but maybe not (false hit)
197                 if (to.IsEmpty()) {
198                         // take all messages, and check other fields
199                         MT_assert(!"objectnames that are empty are not valid, so make it a hobby project :)\n");
200                 } else {
201                         //todo: find intersection of messages (that are in other 2 maps)
202                         vector<NG_NetworkMessage*>** tolistptr = m_messagesByDestinationName[to];
203                         if (tolistptr) {
204                                 vector<NG_NetworkMessage*>* tolist = *tolistptr;
205                                 vector<NG_NetworkMessage*>::iterator listit;
206                                 for (listit=tolist->begin();!(listit==tolist->end());listit++) {
207                                         NG_NetworkMessage* message = *listit;
208                                         if (ConstraintsAreValid(from, subject, message)) {
209                                                 message->AddRef();
210                                                 foundmessages.push_back(message);
211                                         }
212                                 } 
213                         }
214                         // TODO find intersection of messages (that are in other 2 maps)
215                         if (spamallowed) {
216                                 tolistptr = m_messagesByDestinationName[""];
217                                 if (tolistptr) {
218                                         vector<NG_NetworkMessage*>* tolist = *tolistptr;
219                                         vector<NG_NetworkMessage*>::iterator listit;
220                                         for (listit=tolist->begin();!(listit==tolist->end());listit++) {
221                                                 NG_NetworkMessage* message = *listit;
222                                                 if (ConstraintsAreValid(from, subject, message)) {
223                                                         message->AddRef();
224                                                         foundmessages.push_back(message);
225                                                 }
226                                         } 
227                                 }
228                         }
229                 }
230         } 
231         return foundmessages;
232 }
233
234 void NG_NetworkScene::SendMessage(
235         const STR_String& to,
236         const STR_String& from,
237         const STR_String& subject,
238         const STR_String& message)
239 {
240         NG_NetworkMessage* msg = new NG_NetworkMessage(to, from, subject, message);
241         m_networkdevice->SendNetworkMessage(msg);
242         msg->Release();
243 }
244
245 void NG_NetworkScene::ClearAllMessageMaps(void)
246 {
247         ClearMessageMap(m_messagesByDestinationName);
248         ClearMessageMap(m_messagesBySenderName);
249         ClearMessageMap(m_messagesBySubject);
250 }
251
252 void NG_NetworkScene::ClearMessageMap(TMessageMap& map)
253 {
254         // Release the messages in the map
255         for (int i = 0; i < map.size(); i++) {
256                 vector<NG_NetworkMessage*>* msglist;
257                 msglist = *(map.at(i));
258
259                 // Iterate through the current vector and release all it's messages
260                 vector<NG_NetworkMessage*>::iterator msgit;
261                 for (msgit = msglist->begin(); msgit != msglist->end(); msgit++) {
262                         (*msgit)->Release();
263                 }
264
265                 // Delete the actual vector
266                 delete (msglist);
267         }
268
269         // Empty the map
270         map.clear();
271 }
272