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