Bugfix [#32410] Create New Group (CTRL+G) tried to activate even when nothing is
[blender.git] / source / gameengine / Network / NG_NetworkScene.cpp
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  * NetworkSceneManagement generic implementation
27  */
28
29 /** \file gameengine/Network/NG_NetworkScene.cpp
30  *  \ingroup bgenet
31  */
32
33 #include <stdio.h>
34 #include <MT_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)
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         const 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         const 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 {
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 definitely 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