86f114b2f51d36197db49241fff634c6572a307d
[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
95         if (m_BodyList) {
96                 m_BodyList->Release();
97                 m_BodyList = NULL;
98         }
99
100         if (m_SubjectList) {
101                 m_SubjectList->Release();
102                 m_SubjectList = NULL;
103         }
104
105         STR_String toname=GetParent()->GetName();
106         STR_String subject = this->m_subject;
107
108         vector<NG_NetworkMessage*> messages =
109                 m_NetworkScene->FindMessages(toname,"",subject,true);
110
111         m_frame_message_count = messages.size();
112
113         if (!messages.empty()) {
114 #ifdef NAN_NET_DEBUG
115                 printf("KX_NetworkMessageSensor found one or more messages\n");
116 #endif
117                 m_IsUp = true;
118                 m_BodyList = new CListValue();
119                 m_SubjectList = new CListValue();
120         }
121
122         vector<NG_NetworkMessage*>::iterator mesit;
123         for (mesit=messages.begin();mesit!=messages.end();mesit++)
124         {
125                 // save the body
126                 STR_String body = (*mesit)->GetMessageText();
127                 // save the subject
128                 STR_String messub = (*mesit)->GetSubject();
129 #ifdef NAN_NET_DEBUG
130                 if (body) {
131                         cout << "body [" << body << "]\n";
132                 }
133 #endif
134                 m_BodyList->Add(new CStringValue(body,"body"));
135                 // Store Subject
136                 m_SubjectList->Add(new CStringValue(messub,"subject"));
137
138                 // free the message
139                 (*mesit)->Release();
140         }
141         messages.clear();
142
143         result = (WasUp != m_IsUp);
144
145         // Return true if the message received state has changed. 
146         return result;
147 }
148
149 // return true for being up (no flank needed)
150 bool KX_NetworkMessageSensor::IsPositiveTrigger()
151 {
152 //      printf("KX_NetworkMessageSensor IsPositiveTrigger\n");
153         //attempt to fix [ #3809 ] IPO Actuator does not work with some Sensors
154         //a better solution is to properly introduce separate Edge and Level triggering concept
155
156         return m_IsUp;
157 }
158
159 /* --------------------------------------------------------------------- */
160 /* Python interface ---------------------------------------------------- */
161 /* --------------------------------------------------------------------- */
162
163 /* Integration hooks --------------------------------------------------- */
164 PyTypeObject KX_NetworkMessageSensor::Type = {
165         PyObject_HEAD_INIT(&PyType_Type)
166         0,
167         "KX_NetworkMessageSensor",
168         sizeof(KX_NetworkMessageSensor),
169         0,
170         PyDestructor,
171         0,
172         __getattr,
173         __setattr,
174         0, //&MyPyCompare,
175         __repr,
176         0, //&cvalue_as_number,
177         0,
178         0,
179         0,
180         0
181 };
182
183 PyParentObject KX_NetworkMessageSensor::Parents[] = {
184         &KX_NetworkMessageSensor::Type,
185         &SCA_ISensor::Type,
186         &SCA_ILogicBrick::Type,
187         &CValue::Type,
188         NULL
189 };
190
191 PyMethodDef KX_NetworkMessageSensor::Methods[] = {
192         {"setSubjectFilterText", (PyCFunction)
193                 KX_NetworkMessageSensor::sPySetSubjectFilterText, METH_VARARGS,
194                 SetSubjectFilterText_doc},
195         {"getFrameMessageCount", (PyCFunction)
196                 KX_NetworkMessageSensor::sPyGetFrameMessageCount, METH_VARARGS,
197                 GetFrameMessageCount_doc},
198         {"getBodies", (PyCFunction)
199                 KX_NetworkMessageSensor::sPyGetBodies, METH_VARARGS,
200                 GetBodies_doc},
201         {"getSubject", (PyCFunction)
202                 KX_NetworkMessageSensor::sPyGetSubject, METH_VARARGS,
203                 GetSubject_doc},
204         {"getSubjects", (PyCFunction)
205                 KX_NetworkMessageSensor::sPyGetSubjects, METH_VARARGS,
206                 GetSubjects_doc},
207         {NULL,NULL} //Sentinel
208 };
209
210 PyObject* KX_NetworkMessageSensor::_getattr(const STR_String& attr) {
211         _getattr_up(SCA_ISensor); // implicit return!
212 }
213
214 // 1. Set the message subject that this sensor listens for
215 char KX_NetworkMessageSensor::SetSubjectFilterText_doc[] = 
216 "\tsetSubjectFilterText(value)\n"
217 "\tChange the message subject text that this sensor is listening to.\n";
218
219 PyObject* KX_NetworkMessageSensor::PySetSubjectFilterText(
220         PyObject* self,
221         PyObject* args,
222         PyObject* kwds)
223 {
224         char* Subject;
225
226         if (PyArg_ParseTuple(args, "s", &Subject))
227         {
228              m_subject = Subject;
229         }
230
231         Py_Return;
232 }
233
234 // 2. Get the number of messages received since the last frame
235 char KX_NetworkMessageSensor::GetFrameMessageCount_doc[] =
236 "\tgetFrameMessageCount()\n"
237 "\tGet the number of messages received since the last frame.\n";
238
239 PyObject* KX_NetworkMessageSensor::PyGetFrameMessageCount(
240         PyObject* self,
241         PyObject* args,
242         PyObject* kwds)
243 {
244         return PyInt_FromLong(long(m_frame_message_count));
245 }
246
247 // 3. Get the message bodies
248 char KX_NetworkMessageSensor::GetBodies_doc[] =
249 "\tgetBodies()\n"
250 "\tGet the list of message bodies.\n";
251
252 PyObject* KX_NetworkMessageSensor::PyGetBodies(
253         PyObject* self,
254         PyObject* args,
255         PyObject* kwds)
256 {
257         if (m_BodyList) {
258                 return ((PyObject*) m_BodyList->AddRef());
259         }
260
261         Py_Return;
262 }
263
264 // 4. Get the message subject: field of the message sensor
265 char KX_NetworkMessageSensor::GetSubject_doc[] =
266 "\tgetSubject()\n"
267 "\tGet the subject: field of the message sensor.\n";
268
269 PyObject* KX_NetworkMessageSensor::PyGetSubject(
270         PyObject* self,
271         PyObject* args,
272         PyObject* kwds)
273 {
274         if (m_subject) {
275                 return PyString_FromString(m_subject);
276         }
277
278         Py_Return;
279 }
280
281 // 5. Get the message subjects
282 char KX_NetworkMessageSensor::GetSubjects_doc[] =
283 "\tgetSubjects()\n"
284 "\tGet list of message subjects.\n";
285
286 PyObject* KX_NetworkMessageSensor::PyGetSubjects(
287            PyObject* self,
288            PyObject* args,
289            PyObject* kwds)
290 {
291         if (m_SubjectList) {
292           return ((PyObject*) m_SubjectList->AddRef());
293           }
294
295         Py_Return;
296 }