RosTopicListener.cpp
Go to the documentation of this file.
00001 /*
00002  * RosTopicListener.cpp
00003  *
00004  *  Created on: Oct 27, 2013
00005  *      Author: blackpc
00006  */
00007 
00008 #include <scriptable_monitor/RosTopicListener.h>
00009 
00010 bool RosTopicListener::_running = false;
00011 boost::mutex RosTopicListener::_activeTopicsMutex;
00012 boost::thread* RosTopicListener::_listeningThread = NULL;
00013 map<string, string> RosTopicListener::_topicValues;
00014 set<string> RosTopicListener::_activeTopics;
00015 PyMethodDef RosTopicListener::RosTopicPythonLib[] =
00016 {
00017         {"internal_interruption_requested", &RosTopicListener::internalInterruptionRequested,           METH_NOARGS, "" },
00018         {"internal_topic_update",                       &RosTopicListener::internalTopicUpdate,                                 METH_VARARGS, ""},
00019         {"internal_get_topics",                         &RosTopicListener::internalGetTopics,                                   METH_NOARGS, "" },
00020         {NULL,                                  NULL,                                                                   0,              NULL}
00021 };
00022 
00023 PyObject* RosTopicListener::internalTopicUpdate(PyObject* self, PyObject* args)
00024 {
00025         string topicName  = PyString_AsString(PyTuple_GetItem(args, 0));
00026         string topicValue = PyString_AsString(PyTuple_GetItem(args, 1));
00027 
00028         _topicValues[topicName] = topicValue;
00029 
00030         return PyString_FromString("Ok");
00031 }
00032 
00033 PyObject* RosTopicListener::internalInterruptionRequested(PyObject* self, PyObject* args)
00034 {
00035         if (_listeningThread == NULL)
00036                 return PyBool_FromLong(0);
00037 
00038         return PyBool_FromLong((long)_listeningThread->interruption_requested());
00039 }
00040 
00041 PyObject* RosTopicListener::internalGetTopics(PyObject* self, PyObject* args)
00042 {
00043         lock(_activeTopicsMutex);
00044 
00045         PyObject* topicsList = PyList_New(_activeTopics.size());
00046 
00047         int i = 0;
00048         foreach(string topic, _activeTopics) {
00049                 PyList_SetItem(topicsList, i++, PyString_FromString(topic.c_str()));
00050         }
00051 
00052         return topicsList;
00053 }
00054 
00055 void RosTopicListener::start()
00056 {
00057         if (_running)
00058                 return;
00059 
00060         PythonExecuter::initialize();
00061         PythonExecuter::initModule("cogni_topic_listener", RosTopicPythonLib);
00062 
00063         _listeningThread = new boost::thread(listeningThread);
00064         sleep(1);
00065 
00066         _running = true;
00067 }
00068 
00069 void RosTopicListener::stop() {
00070 
00071         if (!_running)
00072                 return;
00073 
00074         cout << "Interrupting..." << endl;
00075         _listeningThread->interrupt();
00076         cout << "Joining..." << endl;
00077         _listeningThread->join();
00078         cout << "Joined" << endl;
00079         _running = false;
00080 }
00081 
00082 void RosTopicListener::listeningThread()
00083 {
00084         PythonExecuter::execute(ROS_TOPIC_LISTENER_SCRIPT);
00085 }
00086 
00087 
00088 void RosTopicListener::addTopic(string topicName)
00089 {
00090         lock(_activeTopicsMutex);
00091 
00092         if (_activeTopics.count(topicName) > 0)
00093                 return;
00094 
00095         _activeTopics.insert(topicName);
00096 }
00097 
00098 void RosTopicListener::removeTopic(string topicName)
00099 {
00100         lock(_activeTopicsMutex);
00101 
00102         if (_activeTopics.count(topicName) > 0)
00103                 return;
00104 
00105         _activeTopics.erase(topicName);
00106 }
00107 
00108 vector<string> RosTopicListener::getTopics()
00109 {
00110         lock(_activeTopicsMutex);
00111 
00112         vector<string> topics;
00113 
00114         foreach(string t, _activeTopics) {
00115                 topics.push_back(t);
00116         }
00117 
00118         return topics;
00119 }
00120 
00121 string RosTopicListener::getTopicValue(string topicName)
00122 {
00123         if (_topicValues.count(topicName) == 0)
00124                 return "";
00125 
00126         return _topicValues[topicName];
00127 }
00128 
00129 map<string, string> RosTopicListener::getTopicsValues()
00130 {
00131         return _topicValues;
00132 }
00133 
00134 bool RosTopicListener::hasTopicValue(string topicName)
00135 {
00136         return _topicValues.count(topicName) > 0;
00137 }


scriptable_monitor
Author(s):
autogenerated on Wed Aug 26 2015 16:21:30