Potential bugfix #4141
[blender.git] / source / gameengine / Ketsji / KXNetwork / KX_NetworkMessageSensor.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  * Ketsji Logic Extenstion: Network Message Sensor generic implementation
32  */
33
34 #include "KX_NetworkMessageSensor.h"
35 #include "KX_NetworkEventManager.h"
36 #include "NG_NetworkMessage.h"
37 #include "NG_NetworkScene.h"
38 #include "NG_NetworkObject.h"
39 #include "SCA_IObject.h"        
40 #include "InputParser.h"
41 #include "ListValue.h"
42 #include "StringValue.h"
43
44 #ifdef HAVE_CONFIG_H
45 #include <config.h>
46 #endif
47
48 #ifdef NAN_NET_DEBUG
49   #include <iostream>
50 #endif
51
52 KX_NetworkMessageSensor::KX_NetworkMessageSensor(
53         class KX_NetworkEventManager* eventmgr, // our eventmanager
54         class NG_NetworkScene *NetworkScene,    // our scene
55         SCA_IObject* gameobj,                                   // the sensor controlling object
56         const STR_String &subject,
57         PyTypeObject* T
58 ) :
59     SCA_ISensor(gameobj,eventmgr,T),
60     m_Networkeventmgr(eventmgr),
61     m_NetworkScene(NetworkScene),
62     m_subject(subject),
63     m_frame_message_count (0),
64     m_IsUp(false),
65     m_BodyList(NULL),
66     m_SubjectList(NULL)
67 {
68 }
69
70 KX_NetworkMessageSensor::~KX_NetworkMessageSensor()
71 {
72 }
73
74 CValue* KX_NetworkMessageSensor::GetReplica() {
75         // This is the standard sensor implementation of GetReplica
76         // There may be more network message sensor specific stuff to do here.
77         CValue* replica = new KX_NetworkMessageSensor(*this);
78
79         if (replica == NULL) return NULL;
80
81         // this will copy properties and so on...
82         CValue::AddDataToReplica(replica);
83
84         return replica;
85 }
86
87 // Return true only for flank (UP and DOWN)
88 bool KX_NetworkMessageSensor::Evaluate(CValue* event)
89 {
90         bool result = false;
91         bool WasUp = m_IsUp;
92
93         m_IsUp = false;
94         if (m_BodyList) {
95                 m_BodyList->Release();
96                 m_BodyList = NULL;
97         }
98
99         if (m_SubjectList) {
100                 m_SubjectList->Release();
101                 m_SubjectList = NULL;
102         }
103
104         STR_String toname=GetParent()->GetName();
105         STR_String subject = this->m_subject;
106
107         vector<NG_NetworkMessage*> messages =
108                 m_NetworkScene->FindMessages(toname,"",subject,true);
109
110         m_frame_message_count = messages.size();
111
112         if (!messages.empty()) {
113 #ifdef NAN_NET_DEBUG
114                 printf("KX_NetworkMessageSensor found one or more messages\n");
115 #endif
116                 m_IsUp = true;
117                 m_BodyList = new CListValue();
118                 m_SubjectList = new CListValue();
119         }
120
121         vector<NG_NetworkMessage*>::iterator mesit;
122         for (mesit=messages.begin();mesit!=messages.end();mesit++)
123         {
124                 // save the body
125                 STR_String body = (*mesit)->GetMessageText();
126                 // save the subject
127                 STR_String messub = (*mesit)->GetSubject();
128 #ifdef NAN_NET_DEBUG
129                 if (body) {
130                         cout << "body [" << body << "]\n";
131                 }
132 #endif
133                 m_BodyList->Add(new CStringValue(body,"body"));
134                 // Store Subject
135                 m_SubjectList->Add(new CStringValue(messub,"subject"));
136
137                 // free the message
138                 (*mesit)->Release();
139         }
140         messages.clear();
141
142         result = (WasUp != m_IsUp);
143
144         // Return true if the message received state has changed. 
145         return result;
146 }
147
148 // return true for being up (no flank needed)
149 bool KX_NetworkMessageSensor::IsPositiveTrigger()
150 {
151 //      printf("KX_NetworkMessageSensor IsPositiveTrigger\n");
152         return m_IsUp;
153 }
154
155 /* --------------------------------------------------------------------- */
156 /* Python interface ---------------------------------------------------- */
157 /* --------------------------------------------------------------------- */
158
159 /* Integration hooks --------------------------------------------------- */
160 PyTypeObject KX_NetworkMessageSensor::Type = {
161         PyObject_HEAD_INIT(&PyType_Type)
162         0,
163         "KX_NetworkMessageSensor",
164         sizeof(KX_NetworkMessageSensor),
165         0,
166         PyDestructor,
167         0,
168         __getattr,
169         __setattr,
170         0, //&MyPyCompare,
171         __repr,
172         0, //&cvalue_as_number,
173         0,
174         0,
175         0,
176         0
177 };
178
179 PyParentObject KX_NetworkMessageSensor::Parents[] = {
180         &KX_NetworkMessageSensor::Type,
181         &SCA_ISensor::Type,
182         &SCA_ILogicBrick::Type,
183         &CValue::Type,
184         NULL
185 };
186
187 PyMethodDef KX_NetworkMessageSensor::Methods[] = {
188         {"setSubjectFilterText", (PyCFunction)
189                 KX_NetworkMessageSensor::sPySetSubjectFilterText, METH_VARARGS,
190                 SetSubjectFilterText_doc},
191         {"getFrameMessageCount", (PyCFunction)
192                 KX_NetworkMessageSensor::sPyGetFrameMessageCount, METH_VARARGS,
193                 GetFrameMessageCount_doc},
194         {"getBodies", (PyCFunction)
195                 KX_NetworkMessageSensor::sPyGetBodies, METH_VARARGS,
196                 GetBodies_doc},
197         {"getSubject", (PyCFunction)
198                 KX_NetworkMessageSensor::sPyGetSubject, METH_VARARGS,
199                 GetSubject_doc},
200         {"getSubjects", (PyCFunction)
201                 KX_NetworkMessageSensor::sPyGetSubjects, METH_VARARGS,
202                 GetSubjects_doc},
203         {NULL,NULL} //Sentinel
204 };
205
206 PyObject* KX_NetworkMessageSensor::_getattr(const STR_String& attr) {
207         _getattr_up(SCA_ISensor); // implicit return!
208 }
209
210 // 1. Set the message subject that this sensor listens for
211 char KX_NetworkMessageSensor::SetSubjectFilterText_doc[] = 
212 "\tsetSubjectFilterText(value)\n"
213 "\tChange the message subject text that this sensor is listening to.\n";
214
215 PyObject* KX_NetworkMessageSensor::PySetSubjectFilterText(
216         PyObject* self,
217         PyObject* args,
218         PyObject* kwds)
219 {
220         char* Subject;
221
222         if (PyArg_ParseTuple(args, "s", &Subject))
223         {
224              m_subject = Subject;
225         }
226
227         Py_Return;
228 }
229
230 // 2. Get the number of messages received since the last frame
231 char KX_NetworkMessageSensor::GetFrameMessageCount_doc[] =
232 "\tgetFrameMessageCount()\n"
233 "\tGet the number of messages received since the last frame.\n";
234
235 PyObject* KX_NetworkMessageSensor::PyGetFrameMessageCount(
236         PyObject* self,
237         PyObject* args,
238         PyObject* kwds)
239 {
240         return PyInt_FromLong(long(m_frame_message_count));
241 }
242
243 // 3. Get the message bodies
244 char KX_NetworkMessageSensor::GetBodies_doc[] =
245 "\tgetBodies()\n"
246 "\tGet the list of message bodies.\n";
247
248 PyObject* KX_NetworkMessageSensor::PyGetBodies(
249         PyObject* self,
250         PyObject* args,
251         PyObject* kwds)
252 {
253         if (m_BodyList) {
254                 return ((PyObject*) m_BodyList->AddRef());
255         }
256
257         Py_Return;
258 }
259
260 // 4. Get the message subject: field of the message sensor
261 char KX_NetworkMessageSensor::GetSubject_doc[] =
262 "\tgetSubject()\n"
263 "\tGet the subject: field of the message sensor.\n";
264
265 PyObject* KX_NetworkMessageSensor::PyGetSubject(
266         PyObject* self,
267         PyObject* args,
268         PyObject* kwds)
269 {
270         if (m_subject) {
271                 return PyString_FromString(m_subject);
272         }
273
274         Py_Return;
275 }
276
277 // 5. Get the message subjects
278 char KX_NetworkMessageSensor::GetSubjects_doc[] =
279 "\tgetSubjects()\n"
280 "\tGet list of message subjects.\n";
281
282 PyObject* KX_NetworkMessageSensor::PyGetSubjects(
283            PyObject* self,
284            PyObject* args,
285            PyObject* kwds)
286 {
287         if (m_SubjectList) {
288           return ((PyObject*) m_SubjectList->AddRef());
289           }
290
291         Py_Return;
292 }