4 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
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
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.
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.
23 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24 * All rights reserved.
26 * The Original Code is: all of this file.
28 * Contributor(s): none yet.
30 * ***** END GPL/BL DUAL LICENSE BLOCK *****
31 * Ketsji Logic Extenstion: Network Message Sensor generic implementation
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"
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,
59 SCA_ISensor(gameobj,eventmgr,T),
60 m_Networkeventmgr(eventmgr),
61 m_NetworkScene(NetworkScene),
63 m_frame_message_count (0),
70 KX_NetworkMessageSensor::~KX_NetworkMessageSensor()
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);
79 if (replica == NULL) return NULL;
81 // this will copy properties and so on...
82 CValue::AddDataToReplica(replica);
87 // Return true only for flank (UP and DOWN)
88 bool KX_NetworkMessageSensor::Evaluate(CValue* event)
95 m_BodyList->Release();
100 m_SubjectList->Release();
101 m_SubjectList = NULL;
104 STR_String toname=GetParent()->GetName();
105 STR_String subject = this->m_subject;
107 vector<NG_NetworkMessage*> messages =
108 m_NetworkScene->FindMessages(toname,"",subject,true);
110 m_frame_message_count = messages.size();
112 if (!messages.empty()) {
114 printf("KX_NetworkMessageSensor found one or more messages\n");
117 m_BodyList = new CListValue();
118 m_SubjectList = new CListValue();
121 vector<NG_NetworkMessage*>::iterator mesit;
122 for (mesit=messages.begin();mesit!=messages.end();mesit++)
125 STR_String body = (*mesit)->GetMessageText();
127 STR_String messub = (*mesit)->GetSubject();
130 cout << "body [" << body << "]\n";
133 m_BodyList->Add(new CStringValue(body,"body"));
135 m_SubjectList->Add(new CStringValue(messub,"subject"));
142 result = (WasUp != m_IsUp);
144 // Return true if the message received state has changed.
148 // return true for being up (no flank needed)
149 bool KX_NetworkMessageSensor::IsPositiveTrigger()
151 // printf("KX_NetworkMessageSensor IsPositiveTrigger\n");
152 //attempt to fix [ #3809 ] IPO Actuator does not work with some Sensors
153 //a better solution is to properly introduce separate Edge and Level triggering concept
155 return true;//m_IsUp;
158 /* --------------------------------------------------------------------- */
159 /* Python interface ---------------------------------------------------- */
160 /* --------------------------------------------------------------------- */
162 /* Integration hooks --------------------------------------------------- */
163 PyTypeObject KX_NetworkMessageSensor::Type = {
164 PyObject_HEAD_INIT(&PyType_Type)
166 "KX_NetworkMessageSensor",
167 sizeof(KX_NetworkMessageSensor),
175 0, //&cvalue_as_number,
182 PyParentObject KX_NetworkMessageSensor::Parents[] = {
183 &KX_NetworkMessageSensor::Type,
185 &SCA_ILogicBrick::Type,
190 PyMethodDef KX_NetworkMessageSensor::Methods[] = {
191 {"setSubjectFilterText", (PyCFunction)
192 KX_NetworkMessageSensor::sPySetSubjectFilterText, METH_VARARGS,
193 SetSubjectFilterText_doc},
194 {"getFrameMessageCount", (PyCFunction)
195 KX_NetworkMessageSensor::sPyGetFrameMessageCount, METH_VARARGS,
196 GetFrameMessageCount_doc},
197 {"getBodies", (PyCFunction)
198 KX_NetworkMessageSensor::sPyGetBodies, METH_VARARGS,
200 {"getSubject", (PyCFunction)
201 KX_NetworkMessageSensor::sPyGetSubject, METH_VARARGS,
203 {"getSubjects", (PyCFunction)
204 KX_NetworkMessageSensor::sPyGetSubjects, METH_VARARGS,
206 {NULL,NULL} //Sentinel
209 PyObject* KX_NetworkMessageSensor::_getattr(const STR_String& attr) {
210 _getattr_up(SCA_ISensor); // implicit return!
213 // 1. Set the message subject that this sensor listens for
214 char KX_NetworkMessageSensor::SetSubjectFilterText_doc[] =
215 "\tsetSubjectFilterText(value)\n"
216 "\tChange the message subject text that this sensor is listening to.\n";
218 PyObject* KX_NetworkMessageSensor::PySetSubjectFilterText(
225 if (PyArg_ParseTuple(args, "s", &Subject))
233 // 2. Get the number of messages received since the last frame
234 char KX_NetworkMessageSensor::GetFrameMessageCount_doc[] =
235 "\tgetFrameMessageCount()\n"
236 "\tGet the number of messages received since the last frame.\n";
238 PyObject* KX_NetworkMessageSensor::PyGetFrameMessageCount(
243 return PyInt_FromLong(long(m_frame_message_count));
246 // 3. Get the message bodies
247 char KX_NetworkMessageSensor::GetBodies_doc[] =
249 "\tGet the list of message bodies.\n";
251 PyObject* KX_NetworkMessageSensor::PyGetBodies(
257 return ((PyObject*) m_BodyList->AddRef());
263 // 4. Get the message subject: field of the message sensor
264 char KX_NetworkMessageSensor::GetSubject_doc[] =
266 "\tGet the subject: field of the message sensor.\n";
268 PyObject* KX_NetworkMessageSensor::PyGetSubject(
274 return PyString_FromString(m_subject);
280 // 5. Get the message subjects
281 char KX_NetworkMessageSensor::GetSubjects_doc[] =
283 "\tGet list of message subjects.\n";
285 PyObject* KX_NetworkMessageSensor::PyGetSubjects(
291 return ((PyObject*) m_SubjectList->AddRef());