svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r22075:22099
[blender-staging.git] / source / gameengine / Ketsji / KXNetwork / KX_NetworkMessageSensor.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  * Ketsji Logic Extenstion: Network Message Sensor generic implementation
29  */
30
31 #include "KX_NetworkMessageSensor.h"
32 #include "KX_NetworkEventManager.h"
33 #include "NG_NetworkMessage.h"
34 #include "NG_NetworkScene.h"
35 #include "NG_NetworkObject.h"
36 #include "SCA_IObject.h"        
37 #include "InputParser.h"
38 #include "ListValue.h"
39 #include "StringValue.h"
40
41 #ifdef HAVE_CONFIG_H
42 #include <config.h>
43 #endif
44
45 #ifdef NAN_NET_DEBUG
46   #include <iostream>
47 #endif
48
49 KX_NetworkMessageSensor::KX_NetworkMessageSensor(
50         class KX_NetworkEventManager* eventmgr, // our eventmanager
51         class NG_NetworkScene *NetworkScene,    // our scene
52         SCA_IObject* gameobj,                                   // the sensor controlling object
53         const STR_String &subject
54 ) :
55     SCA_ISensor(gameobj,eventmgr),
56     m_Networkeventmgr(eventmgr),
57     m_NetworkScene(NetworkScene),
58     m_subject(subject),
59     m_frame_message_count (0),
60     m_BodyList(NULL),
61     m_SubjectList(NULL)
62 {
63         Init();
64 }
65
66 void KX_NetworkMessageSensor::Init()
67 {
68     m_IsUp = false;
69 }
70
71 KX_NetworkMessageSensor::~KX_NetworkMessageSensor()
72 {
73 }
74
75 CValue* KX_NetworkMessageSensor::GetReplica() {
76         // This is the standard sensor implementation of GetReplica
77         // There may be more network message sensor specific stuff to do here.
78         CValue* replica = new KX_NetworkMessageSensor(*this);
79
80         if (replica == NULL) return NULL;
81         replica->ProcessReplica();
82
83         return replica;
84 }
85
86 // Return true only for flank (UP and DOWN)
87 bool KX_NetworkMessageSensor::Evaluate()
88 {
89         bool result = false;
90         bool WasUp = m_IsUp;
91
92         m_IsUp = false;
93
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                 const STR_String& body = (*mesit)->GetMessageText();
126                 // save the subject
127                 const 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 always true if a message was received otherwise we can loose messages
145         if (m_IsUp)
146                 return true;
147         // Is it usefull to return also true when the first frame without a message?? 
148         // This will cause a fast on/off cycle that seems useless!
149         return result;
150 }
151
152 // return true for being up (no flank needed)
153 bool KX_NetworkMessageSensor::IsPositiveTrigger()
154 {
155 //      printf("KX_NetworkMessageSensor IsPositiveTrigger\n");
156         //attempt to fix [ #3809 ] IPO Actuator does not work with some Sensors
157         //a better solution is to properly introduce separate Edge and Level triggering concept
158
159         return m_IsUp;
160 }
161
162 /* --------------------------------------------------------------------- */
163 /* Python interface ---------------------------------------------------- */
164 /* --------------------------------------------------------------------- */
165
166 /* Integration hooks --------------------------------------------------- */
167 PyTypeObject KX_NetworkMessageSensor::Type = {
168 #if (PY_VERSION_HEX >= 0x02060000)
169         PyVarObject_HEAD_INIT(NULL, 0)
170 #else
171         /* python 2.5 and below */
172         PyObject_HEAD_INIT( NULL )  /* required py macro */
173         0,                          /* ob_size */
174 #endif
175         "KX_NetworkMessageSensor",
176         sizeof(PyObjectPlus_Proxy),
177         0,
178         py_base_dealloc,
179         0,
180         0,
181         0,
182         0,
183         py_base_repr,
184         0,0,0,0,0,0,0,0,0,
185         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
186         0,0,0,0,0,0,0,
187         Methods,
188         0,
189         0,
190         &SCA_ISensor::Type,
191         0,0,0,0,0,0,
192         py_base_new
193 };
194
195 PyMethodDef KX_NetworkMessageSensor::Methods[] = {
196         // Deprecated ----->
197         {"setSubjectFilterText", (PyCFunction)
198                 KX_NetworkMessageSensor::sPySetSubjectFilterText, METH_O,
199                 (PY_METHODCHAR)SetSubjectFilterText_doc},
200         {"getFrameMessageCount", (PyCFunction)
201                 KX_NetworkMessageSensor::sPyGetFrameMessageCount, METH_NOARGS,
202                 (PY_METHODCHAR)GetFrameMessageCount_doc},
203         {"getBodies", (PyCFunction)
204                 KX_NetworkMessageSensor::sPyGetBodies, METH_NOARGS,
205                 (PY_METHODCHAR)GetBodies_doc},
206         {"getSubject", (PyCFunction)
207                 KX_NetworkMessageSensor::sPyGetSubject, METH_NOARGS,
208                 (PY_METHODCHAR)GetSubject_doc},
209         {"getSubjects", (PyCFunction)
210                 KX_NetworkMessageSensor::sPyGetSubjects, METH_NOARGS,
211                 (PY_METHODCHAR)GetSubjects_doc},
212         // <-----
213         {NULL,NULL} //Sentinel
214 };
215
216 PyAttributeDef KX_NetworkMessageSensor::Attributes[] = {
217         KX_PYATTRIBUTE_STRING_RW("subject", 0, 100, false, KX_NetworkMessageSensor, m_subject),
218         KX_PYATTRIBUTE_INT_RO("frameMessageCount", KX_NetworkMessageSensor, m_frame_message_count),
219         KX_PYATTRIBUTE_RO_FUNCTION("bodies", KX_NetworkMessageSensor, pyattr_get_bodies),
220         KX_PYATTRIBUTE_RO_FUNCTION("subjects", KX_NetworkMessageSensor, pyattr_get_subjects),
221         { NULL }        //Sentinel
222 };
223
224 PyObject* KX_NetworkMessageSensor::pyattr_get_bodies(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
225 {
226         KX_NetworkMessageSensor *self = static_cast<KX_NetworkMessageSensor*>(self_v);
227         if (self->m_BodyList) {
228                 return self->m_BodyList->GetProxy();
229         } else {
230                 return (new CListValue())->NewProxy(true);
231         }
232 }
233
234 PyObject* KX_NetworkMessageSensor::pyattr_get_subjects(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
235 {
236         KX_NetworkMessageSensor *self = static_cast<KX_NetworkMessageSensor*>(self_v);
237         if (self->m_SubjectList) {
238                 return self->m_SubjectList->GetProxy();
239         } else {
240                 return (new CListValue())->NewProxy(true);
241         }
242 }
243
244 // Deprecated ----->
245 // 1. Set the message subject that this sensor listens for
246 const char KX_NetworkMessageSensor::SetSubjectFilterText_doc[] = 
247 "\tsetSubjectFilterText(value)\n"
248 "\tChange the message subject text that this sensor is listening to.\n";
249
250 PyObject* KX_NetworkMessageSensor::PySetSubjectFilterText(PyObject* value)
251 {
252         ShowDeprecationWarning("setSubjectFilterText()", "subject");
253         char* Subject = _PyUnicode_AsString(value);
254         if (Subject==NULL) {
255                 PyErr_SetString(PyExc_TypeError, "sensor.tsetSubjectFilterText(string): KX_NetworkMessageSensor, expected a string message");
256                 return NULL;
257         }
258         
259         m_subject = Subject;
260         Py_RETURN_NONE;
261 }
262
263 // 2. Get the number of messages received since the last frame
264 const char KX_NetworkMessageSensor::GetFrameMessageCount_doc[] =
265 "\tgetFrameMessageCount()\n"
266 "\tGet the number of messages received since the last frame.\n";
267
268 PyObject* KX_NetworkMessageSensor::PyGetFrameMessageCount()
269 {
270         ShowDeprecationWarning("getFrameMessageCount()", "frameMessageCount");
271         return PyLong_FromSsize_t(long(m_frame_message_count));
272 }
273
274 // 3. Get the message bodies
275 const char KX_NetworkMessageSensor::GetBodies_doc[] =
276 "\tgetBodies()\n"
277 "\tGet the list of message bodies.\n";
278
279 PyObject* KX_NetworkMessageSensor::PyGetBodies()
280 {
281         ShowDeprecationWarning("getBodies()", "bodies");
282         if (m_BodyList) {
283                 return m_BodyList->GetProxy();
284         } else {
285                 return (new CListValue())->NewProxy(true);
286         }
287 }
288
289 // 4. Get the message subject: field of the message sensor
290 const char KX_NetworkMessageSensor::GetSubject_doc[] =
291 "\tgetSubject()\n"
292 "\tGet the subject: field of the message sensor.\n";
293
294 PyObject* KX_NetworkMessageSensor::PyGetSubject()
295 {
296         ShowDeprecationWarning("getSubject()", "subject");
297         return PyUnicode_FromString(m_subject ? m_subject : "");
298 }
299
300 // 5. Get the message subjects
301 const char KX_NetworkMessageSensor::GetSubjects_doc[] =
302 "\tgetSubjects()\n"
303 "\tGet list of message subjects.\n";
304
305 PyObject* KX_NetworkMessageSensor::PyGetSubjects()
306 {
307         ShowDeprecationWarning("getSubjects()", "subjects");
308         if (m_SubjectList) {
309                 return m_SubjectList->GetProxy();
310         } else {
311                 return (new CListValue())->NewProxy(true);
312         }
313 }
314 // <----- Deprecated