![]() |
User Manual, Developers Guide and API Documentation |
![]() |
00001 /******************************************************************************* 00002 * This file is part of openWNS (open Wireless Network Simulator) 00003 * _____________________________________________________________________________ 00004 * 00005 * Copyright (C) 2004-2007 00006 * Chair of Communication Networks (ComNets) 00007 * Kopernikusstr. 16, D-52074 Aachen, Germany 00008 * phone: ++49-241-80-27910, 00009 * fax: ++49-241-80-22242 00010 * email: info@openwns.org 00011 * www: http://www.openwns.org 00012 * _____________________________________________________________________________ 00013 * 00014 * openWNS is free software; you can redistribute it and/or modify it under the 00015 * terms of the GNU Lesser General Public License version 2 as published by the 00016 * Free Software Foundation; 00017 * 00018 * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY 00019 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 00020 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 00021 * details. 00022 * 00023 * You should have received a copy of the GNU Lesser General Public License 00024 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00025 * 00026 ******************************************************************************/ 00027 00028 #include <WNS/Python.hpp> 00029 #include <WNS/probe/bus/PythonProbeBus.hpp> 00030 00031 #include <sstream> 00032 00033 using namespace wns::probe::bus; 00034 00035 STATIC_FACTORY_REGISTER_WITH_CREATOR( 00036 PythonProbeBus, 00037 wns::probe::bus::ProbeBus, 00038 "PythonProbeBus", 00039 wns::PyConfigViewCreator); 00040 00041 PythonProbeBus::PythonProbeBus(const wns::pyconfig::View& pyco): 00042 pyco_(pyco), 00043 reportErrors_(pyco_.get<bool>("reportErrors")) 00044 { 00045 assure(pyco_.knows("accepts"), "Cannot retrieve method accept from PyConfig"); 00046 assure(pyco_.knows("onMeasurement"), "Cannot retrieve method on Measurement from PyConfig"); 00047 pyAcceptsMethod_ = pyco_.getObject("accepts"); 00048 pyOnMeasurementMethod_ = pyco_.getObject("onMeasurement"); 00049 pyOutputMethod_ = pyco_.getObject("output"); 00050 } 00051 00052 PythonProbeBus::~PythonProbeBus() 00053 { 00054 } 00055 00056 void 00057 PythonProbeBus::onMeasurement(const wns::simulator::Time& timestamp, 00058 const double& value, 00059 const IContext& reg) 00060 { 00061 PyObject* pyTimestamp = Py_BuildValue("d", timestamp); 00062 PyObject* pyValue = Py_BuildValue("d", value); 00063 00064 assure(pyValue != NULL, "Could not create Python Object"); 00065 00066 const Context* c = dynamic_cast<const Context*>(®); 00067 assure(c != NULL, "PythonProbeBus can only collaborate with 'Context' implementation of IContext"); 00068 00069 PyObject* result = PyObject_CallFunctionObjArgs(pyOnMeasurementMethod_.obj_, 00070 pyTimestamp, 00071 pyValue, 00072 c->pyDict_.obj_, 00073 NULL); 00074 if (result == NULL) 00075 this->showdown("Error when calling Python accepts method."); 00076 00077 Py_DECREF(result); 00078 Py_DECREF(pyTimestamp); 00079 Py_DECREF(pyValue); 00080 00081 // Post Condition: Python must be clean before we leave 00082 if (PyErr_Occurred()) 00083 { 00084 this->showdown("Error Python Context ist dirty"); 00085 } 00086 } 00087 00088 bool 00089 PythonProbeBus::accepts(const wns::simulator::Time& timestamp, 00090 const IContext& reg) 00091 { 00092 PyObject* pyTimestamp = Py_BuildValue("d", timestamp); 00093 00094 const Context* c = dynamic_cast<const Context*>(®); 00095 assure(c != NULL, "PythonProbeBus can only collaborate with 'Context' implementation of IContext"); 00096 00097 PyObject* result = PyObject_CallFunctionObjArgs(pyAcceptsMethod_.obj_, 00098 pyTimestamp, 00099 c->pyDict_.obj_, 00100 NULL); 00101 if (result == NULL) 00102 { 00103 this->showdown("Error when calling Python accepts method."); 00104 } 00105 00106 bool returnValue = (result==Py_True); 00107 00108 Py_DECREF(result); 00109 Py_DECREF(pyTimestamp); 00110 00111 // Post Condition: Python must be clean before we leave 00112 if (PyErr_Occurred()) 00113 { 00114 this->showdown("Error Python Context ist dirty"); 00115 } 00116 00117 return returnValue; 00118 } 00119 00120 void 00121 PythonProbeBus::output() 00122 { 00123 PyObject* result = PyObject_CallFunctionObjArgs(pyOutputMethod_.obj_, 00124 NULL); 00125 if (result == NULL) 00126 this->showdown("Error when calling Python output method."); 00127 00128 Py_DECREF(result); 00129 00130 // Post Condition: Python must be clean before we leave 00131 if (PyErr_Occurred()) 00132 { 00133 this->showdown("Error Python Context ist dirty"); 00134 } 00135 } 00136 00137 void 00138 PythonProbeBus::showdown(const std::string& reason) const 00139 { 00140 if (reportErrors_) 00141 { 00142 std::cerr << "\n" << reason << "\n"; 00143 std::cerr << "\nPython says:\n"; 00144 PyErr_Print(); 00145 std::cerr << "\n"; 00146 00147 } 00148 00149 PyErr_Clear(); 00150 00151 throw Exception(reason); 00152 } // showdown
1.5.5