~ubuntu-branches/ubuntu/saucy/pyzmq/saucy-proposed

« back to all changes in this revision

Viewing changes to zmq/core/_device.pyx

  • Committer: Package Import Robot
  • Author(s): Julian Taylor
  • Date: 2013-05-12 13:59:20 UTC
  • mfrom: (1.2.2)
  • Revision ID: package-import@ubuntu.com-20130512135920-1d2md9w425iq3sb6
Tags: 13.1.0-1
* New upstream release built with zeromq3 (Closes: #698830)
  - drop all patches
* workaround-gevent.patch: workaround issue with gevent < 1.0
* noncopysend-test.patch: avoid uninitialized values in tests
* update copyright

Show diffs side-by-side

added added

removed removed

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