1aec3c8f4Simarom"""Python binding for 0MQ device function."""
2aec3c8f4Simarom
3aec3c8f4Simarom#
4aec3c8f4Simarom#    Copyright (c) 2010-2011 Brian E. Granger & Min Ragan-Kelley
5aec3c8f4Simarom#
6aec3c8f4Simarom#    This file is part of pyzmq.
7aec3c8f4Simarom#
8aec3c8f4Simarom#    pyzmq is free software; you can redistribute it and/or modify it under
9aec3c8f4Simarom#    the terms of the Lesser GNU General Public License as published by
10aec3c8f4Simarom#    the Free Software Foundation; either version 3 of the License, or
11aec3c8f4Simarom#    (at your option) any later version.
12aec3c8f4Simarom#
13aec3c8f4Simarom#    pyzmq is distributed in the hope that it will be useful,
14aec3c8f4Simarom#    but WITHOUT ANY WARRANTY; without even the implied warranty of
15aec3c8f4Simarom#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16aec3c8f4Simarom#    Lesser GNU General Public License for more details.
17aec3c8f4Simarom#
18aec3c8f4Simarom#    You should have received a copy of the Lesser GNU General Public License
19aec3c8f4Simarom#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20aec3c8f4Simarom#
21aec3c8f4Simarom
22aec3c8f4Simarom#-----------------------------------------------------------------------------
23aec3c8f4Simarom# Imports
24aec3c8f4Simarom#-----------------------------------------------------------------------------
25aec3c8f4Simarom
26aec3c8f4Simaromfrom libzmq cimport zmq_device, zmq_proxy, ZMQ_VERSION_MAJOR
27aec3c8f4Simaromfrom zmq.backend.cython.socket cimport Socket as cSocket
28aec3c8f4Simaromfrom zmq.backend.cython.checkrc cimport _check_rc
29aec3c8f4Simarom
30aec3c8f4Simarom#-----------------------------------------------------------------------------
31aec3c8f4Simarom# Basic device API
32aec3c8f4Simarom#-----------------------------------------------------------------------------
33aec3c8f4Simarom
34aec3c8f4Simaromdef device(int device_type, cSocket frontend, cSocket backend=None):
35aec3c8f4Simarom    """device(device_type, frontend, backend)
36aec3c8f4Simarom
37aec3c8f4Simarom    Start a zeromq device.
38aec3c8f4Simarom    
39aec3c8f4Simarom    .. deprecated:: libzmq-3.2
40aec3c8f4Simarom        Use zmq.proxy
41aec3c8f4Simarom
42aec3c8f4Simarom    Parameters
43aec3c8f4Simarom    ----------
44aec3c8f4Simarom    device_type : (QUEUE, FORWARDER, STREAMER)
45aec3c8f4Simarom        The type of device to start.
46aec3c8f4Simarom    frontend : Socket
47aec3c8f4Simarom        The Socket instance for the incoming traffic.
48aec3c8f4Simarom    backend : Socket
49aec3c8f4Simarom        The Socket instance for the outbound traffic.
50aec3c8f4Simarom    """
51aec3c8f4Simarom    if ZMQ_VERSION_MAJOR >= 3:
52aec3c8f4Simarom        return proxy(frontend, backend)
53aec3c8f4Simarom
54aec3c8f4Simarom    cdef int rc = 0
55aec3c8f4Simarom    with nogil:
56aec3c8f4Simarom        rc = zmq_device(device_type, frontend.handle, backend.handle)
57aec3c8f4Simarom    _check_rc(rc)
58aec3c8f4Simarom    return rc
59aec3c8f4Simarom
60aec3c8f4Simaromdef proxy(cSocket frontend, cSocket backend, cSocket capture=None):
61aec3c8f4Simarom    """proxy(frontend, backend, capture)
62aec3c8f4Simarom    
63aec3c8f4Simarom    Start a zeromq proxy (replacement for device).
64aec3c8f4Simarom    
65aec3c8f4Simarom    .. versionadded:: libzmq-3.2
66aec3c8f4Simarom    .. versionadded:: 13.0
67aec3c8f4Simarom    
68aec3c8f4Simarom    Parameters
69aec3c8f4Simarom    ----------
70aec3c8f4Simarom    frontend : Socket
71aec3c8f4Simarom        The Socket instance for the incoming traffic.
72aec3c8f4Simarom    backend : Socket
73aec3c8f4Simarom        The Socket instance for the outbound traffic.
74aec3c8f4Simarom    capture : Socket (optional)
75aec3c8f4Simarom        The Socket instance for capturing traffic.
76aec3c8f4Simarom    """
77aec3c8f4Simarom    cdef int rc = 0
78aec3c8f4Simarom    cdef void* capture_handle
79aec3c8f4Simarom    if isinstance(capture, cSocket):
80aec3c8f4Simarom        capture_handle = capture.handle
81aec3c8f4Simarom    else:
82aec3c8f4Simarom        capture_handle = NULL
83aec3c8f4Simarom    with nogil:
84aec3c8f4Simarom        rc = zmq_proxy(frontend.handle, backend.handle, capture_handle)
85aec3c8f4Simarom    _check_rc(rc)
86aec3c8f4Simarom    return rc
87aec3c8f4Simarom
88aec3c8f4Simarom__all__ = ['device', 'proxy']
89aec3c8f4Simarom
90