Blender V2.61 - r43446

KX_NetworkMessageSensor.cpp

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00019  * All rights reserved.
00020  *
00021  * The Original Code is: all of this file.
00022  *
00023  * Contributor(s): none yet.
00024  *
00025  * ***** END GPL LICENSE BLOCK *****
00026  * Ketsji Logic Extenstion: Network Message Sensor generic implementation
00027  */
00028 
00034 #include <stddef.h>
00035 
00036 #include "KX_NetworkMessageSensor.h"
00037 #include "KX_NetworkEventManager.h"
00038 #include "NG_NetworkMessage.h"
00039 #include "NG_NetworkScene.h"
00040 #include "NG_NetworkObject.h"
00041 #include "SCA_IObject.h"    
00042 #include "InputParser.h"
00043 #include "ListValue.h"
00044 #include "StringValue.h"
00045 
00046 #ifdef NAN_NET_DEBUG
00047   #include <iostream>
00048 #endif
00049 
00050 KX_NetworkMessageSensor::KX_NetworkMessageSensor(
00051         class KX_NetworkEventManager* eventmgr, // our eventmanager
00052         class NG_NetworkScene *NetworkScene,    // our scene
00053         SCA_IObject* gameobj,                   // the sensor controlling object
00054         const STR_String &subject
00055         ) :
00056     SCA_ISensor(gameobj,eventmgr),
00057     m_NetworkScene(NetworkScene),
00058     m_subject(subject),
00059     m_frame_message_count (0),
00060     m_BodyList(NULL),
00061     m_SubjectList(NULL)
00062 {
00063     Init();
00064 }
00065 
00066 void KX_NetworkMessageSensor::Init()
00067 {
00068     m_IsUp = false;
00069 }
00070 
00071 KX_NetworkMessageSensor::~KX_NetworkMessageSensor()
00072 {
00073 }
00074 
00075 CValue* KX_NetworkMessageSensor::GetReplica() {
00076     // This is the standard sensor implementation of GetReplica
00077     // There may be more network message sensor specific stuff to do here.
00078     CValue* replica = new KX_NetworkMessageSensor(*this);
00079 
00080     if (replica == NULL) return NULL;
00081     replica->ProcessReplica();
00082 
00083     return replica;
00084 }
00085 
00086 // Return true only for flank (UP and DOWN)
00087 bool KX_NetworkMessageSensor::Evaluate()
00088 {
00089     bool result = false;
00090     bool WasUp = m_IsUp;
00091 
00092     m_IsUp = false;
00093 
00094     if (m_BodyList) {
00095         m_BodyList->Release();
00096         m_BodyList = NULL;
00097     }
00098 
00099     if (m_SubjectList) {
00100         m_SubjectList->Release();
00101         m_SubjectList = NULL;
00102     }
00103 
00104     STR_String& toname=GetParent()->GetName();
00105     STR_String& subject = this->m_subject;
00106 
00107     vector<NG_NetworkMessage*> messages =
00108         m_NetworkScene->FindMessages(toname,"",subject,true);
00109 
00110     m_frame_message_count = messages.size();
00111 
00112     if (!messages.empty()) {
00113 #ifdef NAN_NET_DEBUG
00114         printf("KX_NetworkMessageSensor found one or more messages\n");
00115 #endif
00116         m_IsUp = true;
00117         m_BodyList = new CListValue();
00118         m_SubjectList = new CListValue();
00119     }
00120 
00121     vector<NG_NetworkMessage*>::iterator mesit;
00122     for (mesit=messages.begin();mesit!=messages.end();mesit++)
00123     {
00124         // save the body
00125         const STR_String& body = (*mesit)->GetMessageText();
00126         // save the subject
00127         const STR_String& messub = (*mesit)->GetSubject();
00128 #ifdef NAN_NET_DEBUG
00129         if (body) {
00130             cout << "body [" << body << "]\n";
00131         }
00132 #endif
00133         m_BodyList->Add(new CStringValue(body,"body"));
00134         // Store Subject
00135         m_SubjectList->Add(new CStringValue(messub,"subject"));
00136 
00137         // free the message
00138         (*mesit)->Release();
00139     }
00140     messages.clear();
00141 
00142     result = (WasUp != m_IsUp);
00143 
00144     // Return always true if a message was received otherwise we can loose messages
00145     if (m_IsUp)
00146         return true;
00147     // Is it useful to return also true when the first frame without a message??
00148     // This will cause a fast on/off cycle that seems useless!
00149     return result;
00150 }
00151 
00152 // return true for being up (no flank needed)
00153 bool KX_NetworkMessageSensor::IsPositiveTrigger()
00154 {
00155 //  printf("KX_NetworkMessageSensor IsPositiveTrigger\n");
00156     //attempt to fix [ #3809 ] IPO Actuator does not work with some Sensors
00157     //a better solution is to properly introduce separate Edge and Level triggering concept
00158 
00159     return m_IsUp;
00160 }
00161 
00162 #ifdef WITH_PYTHON
00163 
00164 /* --------------------------------------------------------------------- */
00165 /* Python interface ---------------------------------------------------- */
00166 /* --------------------------------------------------------------------- */
00167 
00168 /* Integration hooks --------------------------------------------------- */
00169 PyTypeObject KX_NetworkMessageSensor::Type = {
00170     PyVarObject_HEAD_INIT(NULL, 0)
00171     "KX_NetworkMessageSensor",
00172     sizeof(PyObjectPlus_Proxy),
00173     0,
00174     py_base_dealloc,
00175     0,
00176     0,
00177     0,
00178     0,
00179     py_base_repr,
00180     0,0,0,0,0,0,0,0,0,
00181     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
00182     0,0,0,0,0,0,0,
00183     Methods,
00184     0,
00185     0,
00186     &SCA_ISensor::Type,
00187     0,0,0,0,0,0,
00188     py_base_new
00189 };
00190 
00191 PyMethodDef KX_NetworkMessageSensor::Methods[] = {
00192     {NULL,NULL} //Sentinel
00193 };
00194 
00195 PyAttributeDef KX_NetworkMessageSensor::Attributes[] = {
00196     KX_PYATTRIBUTE_STRING_RW("subject", 0, 100, false, KX_NetworkMessageSensor, m_subject),
00197     KX_PYATTRIBUTE_INT_RO("frameMessageCount", KX_NetworkMessageSensor, m_frame_message_count),
00198     KX_PYATTRIBUTE_RO_FUNCTION("bodies", KX_NetworkMessageSensor, pyattr_get_bodies),
00199     KX_PYATTRIBUTE_RO_FUNCTION("subjects", KX_NetworkMessageSensor, pyattr_get_subjects),
00200     { NULL }    //Sentinel
00201 };
00202 
00203 PyObject* KX_NetworkMessageSensor::pyattr_get_bodies(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
00204 {
00205     KX_NetworkMessageSensor *self = static_cast<KX_NetworkMessageSensor*>(self_v);
00206     if (self->m_BodyList) {
00207         return self->m_BodyList->GetProxy();
00208     } else {
00209         return (new CListValue())->NewProxy(true);
00210     }
00211 }
00212 
00213 PyObject* KX_NetworkMessageSensor::pyattr_get_subjects(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
00214 {
00215     KX_NetworkMessageSensor *self = static_cast<KX_NetworkMessageSensor*>(self_v);
00216     if (self->m_SubjectList) {
00217         return self->m_SubjectList->GetProxy();
00218     } else {
00219         return (new CListValue())->NewProxy(true);
00220     }
00221 }
00222 
00223 #endif // WITH_PYTHON