From 254b51f46e81ed6ed0f21f026514848cdf73b6be Mon Sep 17 00:00:00 2001 From: Johnathan Corgan <johnathan@corganlabs.com> Date: Sun, 8 Dec 2013 13:29:07 -0800 Subject: zeromq: first try at enabling Python. Segfaults in SWIG code. --- gr-zeromq/python/zeromq/CMakeLists.txt | 45 +++++++++++++ gr-zeromq/python/zeromq/__init__.py | 36 ++++++++++ gr-zeromq/python/zeromq/probe_manager.py | 50 ++++++++++++++ gr-zeromq/python/zeromq/rpc_manager.py | 101 ++++++++++++++++++++++++++++ gr-zeromq/python/zmqblocks/CMakeLists.txt | 45 ------------- gr-zeromq/python/zmqblocks/__init__.py | 56 --------------- gr-zeromq/python/zmqblocks/probe_manager.py | 51 -------------- gr-zeromq/python/zmqblocks/rpc_manager.py | 101 ---------------------------- 8 files changed, 232 insertions(+), 253 deletions(-) create mode 100644 gr-zeromq/python/zeromq/CMakeLists.txt create mode 100644 gr-zeromq/python/zeromq/__init__.py create mode 100644 gr-zeromq/python/zeromq/probe_manager.py create mode 100644 gr-zeromq/python/zeromq/rpc_manager.py delete mode 100644 gr-zeromq/python/zmqblocks/CMakeLists.txt delete mode 100644 gr-zeromq/python/zmqblocks/__init__.py delete mode 100644 gr-zeromq/python/zmqblocks/probe_manager.py delete mode 100644 gr-zeromq/python/zmqblocks/rpc_manager.py (limited to 'gr-zeromq/python') diff --git a/gr-zeromq/python/zeromq/CMakeLists.txt b/gr-zeromq/python/zeromq/CMakeLists.txt new file mode 100644 index 000000000..918568259 --- /dev/null +++ b/gr-zeromq/python/zeromq/CMakeLists.txt @@ -0,0 +1,45 @@ +# Copyright 2011 Free Software Foundation, Inc. +# +# This file is part of GNU Radio +# +# GNU Radio is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3, or (at your option) +# any later version. +# +# GNU Radio is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with GNU Radio; see the file COPYING. If not, write to +# the Free Software Foundation, Inc., 51 Franklin Street, +# Boston, MA 02110-1301, USA. + +######################################################################## +# Include python install macros +######################################################################## +include(GrPython) +if(NOT PYTHONINTERP_FOUND) + return() +endif() + +######################################################################## +# Install python sources +######################################################################## +GR_PYTHON_INSTALL( + FILES + __init__.py + rpc_manager.py + probe_manager.py + DESTINATION ${GR_PYTHON_DIR}/gnuradio/zeromq +) + +######################################################################## +# Handle the unit tests +######################################################################## +include(GrTest) + +set(GR_TEST_TARGET_DEPS gnuradio-zeromq) +set(GR_TEST_PYTHON_DIRS ${CMAKE_BINARY_DIR}/swig) diff --git a/gr-zeromq/python/zeromq/__init__.py b/gr-zeromq/python/zeromq/__init__.py new file mode 100644 index 000000000..cab4b6764 --- /dev/null +++ b/gr-zeromq/python/zeromq/__init__.py @@ -0,0 +1,36 @@ +# +# Copyright 2013 Free Software Foundation, Inc. +# +# This file is part of GNU Radio +# +# GNU Radio is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3, or (at your option) +# any later version. +# +# GNU Radio is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with GNU Radio; see the file COPYING. If not, write to +# the Free Software Foundation, Inc., 51 Franklin Street, +# Boston, MA 02110-1301, USA. +# + +''' +Blocks for interfacing with ZeroMQ endpoints. +''' + +import os + +try: + from zeromq_swig import * +except ImportError: + dirname, filename = os.path.split(os.path.abspath(__file__)) + __path__.append(os.path.join(dirname, "..", "..", "swig")) + from zeromq_swig import * + +from probe_manager import probe_manager +from rpc_manager import rpc_manager diff --git a/gr-zeromq/python/zeromq/probe_manager.py b/gr-zeromq/python/zeromq/probe_manager.py new file mode 100644 index 000000000..c30e6eaab --- /dev/null +++ b/gr-zeromq/python/zeromq/probe_manager.py @@ -0,0 +1,50 @@ +# +# Copyright 2013 Free Software Foundation, Inc. +# +# This file is part of GNU Radio. +# +# This is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3, or (at your option) +# any later version. +# +# This software is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this software; see the file COPYING. If not, write to +# the Free Software Foundation, Inc., 51 Franklin Street, +# Boston, MA 02110-1301, USA. +# + +import zmq +import threading +import numpy + +class probe_manager(): + def __init__(self): + self.zmq_context = zmq.Context() + self.poller = zmq.Poller() + self.interfaces = [] + + def add_socket(self, address, data_type, callback_func): + socket = self.zmq_context.socket(zmq.SUB) + socket.setsockopt(zmq.SUBSCRIBE, "") + socket.connect(address) + # use a tuple to store interface elements + self.interfaces.append((socket, data_type, callback_func)) + self.poller.register(socket, zmq.POLLIN) + + def watcher(self): + poll = dict(self.poller.poll(0)) + for i in self.interfaces: + # i = (socket, data_type, callback_func) + if poll.get(i[0]) == zmq.POLLIN: + # receive data + msg_packed = i[0].recv() + # use numpy to unpack the data + msg_unpacked = numpy.fromstring(msg_packed, numpy.dtype(i[1])) + # invoke callback function + i[2](msg_unpacked) diff --git a/gr-zeromq/python/zeromq/rpc_manager.py b/gr-zeromq/python/zeromq/rpc_manager.py new file mode 100644 index 000000000..e50a3d62f --- /dev/null +++ b/gr-zeromq/python/zeromq/rpc_manager.py @@ -0,0 +1,101 @@ +# +# Copyright 2013 Institute for Theoretical Information Technology, +# RWTH Aachen University +# +# Authors: Johannes Schmitz <schmitz@ti.rwth-aachen.de> +# +# This is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3, or (at your option) +# any later version. +# +# This software is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this software; see the file COPYING. If not, write to +# the Free Software Foundation, Inc., 51 Franklin Street, +# Boston, MA 02110-1301, USA. +# + +import zmq +import pmt +import threading + + +class rpc_manager(): + def __init__(self): + self.zmq_context = zmq.Context() + self.poller_rep = zmq.Poller() + self.poller_req_out = zmq.Poller() + self.poller_req_in = zmq.Poller() + self.interfaces = dict() + + def __del__(self): + self.stop_watcher() + self.watcher_thread.join() + + def set_reply_socket(self, address): + self.rep_socket = self.zmq_context.socket(zmq.REP) + self.rep_socket.bind(address) + print "[RPC] reply socket bound to: ", address + self.poller_rep.register(self.rep_socket, zmq.POLLIN) + + def set_request_socket(self, address): + self.req_socket = self.zmq_context.socket(zmq.REQ) + self.req_socket.connect(address) + print "[RPC] request socket connected to: ", address + self.poller_req_out.register(self.req_socket, zmq.POLLOUT) + self.poller_req_in.register(self.req_socket, zmq.POLLIN) + + def add_interface(self, id_str, callback_func): + if not self.interfaces.has_key(id_str): + self.interfaces[id_str] = callback_func + print "[RPC] added reply interface:", id_str + else: + print "ERROR: duplicate id_str" + + def watcher(self): + self.keep_running = True + while self.keep_running: + # poll for calls + socks = dict(self.poller_rep.poll(10)) + if socks.get(self.rep_socket) == zmq.POLLIN: + # receive call + msg = self.rep_socket.recv() + (id_str, args) = pmt.to_python(pmt.deserialize_str(msg)) + print "[RPC] request:", id_str, ", args:", args + reply = self.callback(id_str, args) + self.rep_socket.send(pmt.serialize_str(pmt.to_pmt(reply))) + + def start_watcher(self): + self.watcher_thread = threading.Thread(target=self.watcher,args=()) + self.watcher_thread.daemon = True + self.watcher_thread.start() + + def stop_watcher(self): + self.keep_running = False + self.watcher_thread.join() + + def request(self, id_str, args=None): + socks = dict(self.poller_req_out.poll(10)) + if socks.get(self.req_socket) == zmq.POLLOUT: + self.req_socket.send(pmt.serialize_str(pmt.to_pmt((id_str,args)))) + socks = dict(self.poller_req_in.poll(10)) + if socks.get(self.req_socket) == zmq.POLLIN: + reply = pmt.to_python(pmt.deserialize_str(self.req_socket.recv())) + print "[RPC] reply:", reply + return reply + + def callback(self, id_str, args): + if self.interfaces.has_key(id_str): + callback_func = self.interfaces.get(id_str) + if not args == None: + return(callback_func(args)) + else: + return(callback_func()) + else: + print "[RPC] ERROR: id_str not found" + return 0 diff --git a/gr-zeromq/python/zmqblocks/CMakeLists.txt b/gr-zeromq/python/zmqblocks/CMakeLists.txt deleted file mode 100644 index b3175bcca..000000000 --- a/gr-zeromq/python/zmqblocks/CMakeLists.txt +++ /dev/null @@ -1,45 +0,0 @@ -# Copyright 2011 Free Software Foundation, Inc. -# -# This file is part of GNU Radio -# -# GNU Radio is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 3, or (at your option) -# any later version. -# -# GNU Radio is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with GNU Radio; see the file COPYING. If not, write to -# the Free Software Foundation, Inc., 51 Franklin Street, -# Boston, MA 02110-1301, USA. - -######################################################################## -# Include python install macros -######################################################################## -include(GrPython) -if(NOT PYTHONINTERP_FOUND) - return() -endif() - -######################################################################## -# Install python sources -######################################################################## -GR_PYTHON_INSTALL( - FILES - __init__.py - rpc_manager.py - probe_manager.py - DESTINATION ${GR_PYTHON_DIR}/zmqblocks -) - -######################################################################## -# Handle the unit tests -######################################################################## -include(GrTest) - -set(GR_TEST_TARGET_DEPS gnuradio-zmqblocks) -set(GR_TEST_PYTHON_DIRS ${CMAKE_BINARY_DIR}/swig) diff --git a/gr-zeromq/python/zmqblocks/__init__.py b/gr-zeromq/python/zmqblocks/__init__.py deleted file mode 100644 index b11d50aea..000000000 --- a/gr-zeromq/python/zmqblocks/__init__.py +++ /dev/null @@ -1,56 +0,0 @@ -# -# Copyright 2008,2009 Free Software Foundation, Inc. -# -# This application is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 3, or (at your option) -# any later version. -# -# This application is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License along -# with this program; if not, write to the Free Software Foundation, Inc., -# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -# - -# The presence of this file turns this directory into a Python package - -''' -This is the GNU Radio ZMQBLOCKS module. Place your Python package -description here (python/__init__.py). -''' - -# ---------------------------------------------------------------- -# Temporary workaround for ticket:181 (swig+python problem) -import sys -_RTLD_GLOBAL = 0 -try: - from dl import RTLD_GLOBAL as _RTLD_GLOBAL -except ImportError: - try: - from DLFCN import RTLD_GLOBAL as _RTLD_GLOBAL - except ImportError: - pass - -if _RTLD_GLOBAL != 0: - _dlopenflags = sys.getdlopenflags() - sys.setdlopenflags(_dlopenflags|_RTLD_GLOBAL) -# ---------------------------------------------------------------- - - -# import swig generated symbols into the zmqblocks namespace -from zmqblocks_swig import * - -# import any pure python here -from rpc_manager import rpc_manager -from probe_manager import probe_manager -# - -# ---------------------------------------------------------------- -# Tail of workaround -if _RTLD_GLOBAL != 0: - sys.setdlopenflags(_dlopenflags) # Restore original flags -# ---------------------------------------------------------------- diff --git a/gr-zeromq/python/zmqblocks/probe_manager.py b/gr-zeromq/python/zmqblocks/probe_manager.py deleted file mode 100644 index a402e622b..000000000 --- a/gr-zeromq/python/zmqblocks/probe_manager.py +++ /dev/null @@ -1,51 +0,0 @@ -# -# Copyright 2013 Institute for Theoretical Information Technology, -# RWTH Aachen University -# -# Authors: Johannes Schmitz <schmitz@ti.rwth-aachen.de> -# -# This is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 3, or (at your option) -# any later version. -# -# This software is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this software; see the file COPYING. If not, write to -# the Free Software Foundation, Inc., 51 Franklin Street, -# Boston, MA 02110-1301, USA. -# - -import zmq -import threading -import numpy - -class probe_manager(): - def __init__(self): - self.zmq_context = zmq.Context() - self.poller = zmq.Poller() - self.interfaces = [] - - def add_socket(self, address, data_type, callback_func): - socket = self.zmq_context.socket(zmq.SUB) - socket.setsockopt(zmq.SUBSCRIBE, "") - socket.connect(address) - # use a tuple to store interface elements - self.interfaces.append((socket, data_type, callback_func)) - self.poller.register(socket, zmq.POLLIN) - - def watcher(self): - poll = dict(self.poller.poll(0)) - for i in self.interfaces: - # i = (socket, data_type, callback_func) - if poll.get(i[0]) == zmq.POLLIN: - # receive data - msg_packed = i[0].recv() - # use numpy to unpack the data - msg_unpacked = numpy.fromstring(msg_packed, numpy.dtype(i[1])) - # invoke callback function - i[2](msg_unpacked) diff --git a/gr-zeromq/python/zmqblocks/rpc_manager.py b/gr-zeromq/python/zmqblocks/rpc_manager.py deleted file mode 100644 index 006dbc694..000000000 --- a/gr-zeromq/python/zmqblocks/rpc_manager.py +++ /dev/null @@ -1,101 +0,0 @@ -# -# Copyright 2013 Institute for Theoretical Information Technology, -# RWTH Aachen University -# -# Authors: Johannes Schmitz <schmitz@ti.rwth-aachen.de> -# -# This is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 3, or (at your option) -# any later version. -# -# This software is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this software; see the file COPYING. If not, write to -# the Free Software Foundation, Inc., 51 Franklin Street, -# Boston, MA 02110-1301, USA. -# - -import zmq -import pmt -import threading - - -class rpc_manager(): - def __init__(self): - self.zmq_context = zmq.Context() - self.poller_rep = zmq.Poller() - self.poller_req_out = zmq.Poller() - self.poller_req_in = zmq.Poller() - self.interfaces = dict() - - def __del__(self): - self.stop_watcher() - self.watcher_thread.join() - - def set_reply_socket(self, address): - self.rep_socket = self.zmq_context.socket(zmq.REP) - self.rep_socket.bind(address) - print "[RPC] reply socket bound to: ", address - self.poller_rep.register(self.rep_socket, zmq.POLLIN) - - def set_request_socket(self, address): - self.req_socket = self.zmq_context.socket(zmq.REQ) - self.req_socket.connect(address) - print "[RPC] request socket connected to: ", address - self.poller_req_out.register(self.req_socket, zmq.POLLOUT) - self.poller_req_in.register(self.req_socket, zmq.POLLIN) - - def add_interface(self, id_str, callback_func): - if not self.interfaces.has_key(id_str): - self.interfaces[id_str] = callback_func - print "[RPC] added reply interface:", id_str - else: - print "ERROR: duplicate id_str" - - def watcher(self): - self.keep_running = True - while self.keep_running: - # poll for calls - socks = dict(self.poller_rep.poll(10)) - if socks.get(self.rep_socket) == zmq.POLLIN: - # receive call - msg = self.rep_socket.recv() - (id_str, args) = pmt.to_python(pmt.deserialize_str(msg)) - print "[RPC] request:", id_str, ", args:", args - reply = self.callback(id_str, args) - self.rep_socket.send(pmt.serialize_str(pmt.to_pmt(reply))) - - def start_watcher(self): - self.watcher_thread = threading.Thread(target=self.watcher,args=()) - self.watcher_thread.daemon = True - self.watcher_thread.start() - - def stop_watcher(self): - self.keep_running = False - self.watcher_thread.join() - - def request(self, id_str, args=None): - socks = dict(self.poller_req_out.poll(10)) - if socks.get(self.req_socket) == zmq.POLLOUT: - self.req_socket.send(pmt.serialize_str(pmt.to_pmt((id_str,args)))) - socks = dict(self.poller_req_in.poll(10)) - if socks.get(self.req_socket) == zmq.POLLIN: - reply = pmt.to_python(pmt.deserialize_str(self.req_socket.recv())) - print "[RPC] reply:", reply - return reply - - def callback(self, id_str, args): - if self.interfaces.has_key(id_str): - callback_func = self.interfaces.get(id_str) - if not args == None: - return(callback_func(args)) - else: - return(callback_func()) - else: - print "[RPC] ERROR: id_str not found" - return 0 -- cgit v1.2.3