~ubuntu-branches/ubuntu/wily/pyzmq/wily

« back to all changes in this revision

Viewing changes to zmq/core/device.pyx

  • Committer: Package Import Robot
  • Author(s): Julian Taylor
  • Date: 2013-02-24 19:23:15 UTC
  • mfrom: (1.2.1) (9 sid)
  • mto: This revision was merged to the branch mainline in revision 10.
  • Revision ID: package-import@ubuntu.com-20130224192315-qhmwp3m3ymk8r60d
Tags: 2.2.0.1-1
* New upstream release
* relicense debian packaging to LGPL-3
* update watch file to use github directly
  thanks to Bart Martens for the file
* add autopkgtests
* drop obsolete DM-Upload-Allowed
* bump standard to 3.9.4, no changes required

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
"""Python binding for 0MQ device function."""
2
2
 
3
3
#
4
 
#    Copyright (c) 2010 Brian E. Granger
 
4
#    Copyright (c) 2010-2011 Brian E. Granger & Min Ragan-Kelley
5
5
#
6
6
#    This file is part of pyzmq.
7
7
#
23
23
# Imports
24
24
#-----------------------------------------------------------------------------
25
25
 
26
 
from czmq cimport zmq_device
 
26
from libzmq cimport *
27
27
from zmq.core.socket cimport Socket as cSocket
 
28
from zmq.core.error import ZMQError
28
29
 
29
30
#-----------------------------------------------------------------------------
30
31
# Basic device API
44
45
    osocket : Socket
45
46
        The Socket instance for the outbound traffic.
46
47
    """
47
 
    cdef int result = 0
 
48
    cdef int rc = 0
48
49
    with nogil:
49
 
        result = zmq_device(device_type, isocket.handle, osocket.handle)
50
 
    return result
 
50
        if ZMQ_VERSION_MAJOR >= 3:
 
51
            rc = c_device(isocket.handle, osocket.handle)
 
52
        else:
 
53
            rc = zmq_device(device_type, isocket.handle, osocket.handle)
 
54
    if rc < 0:
 
55
        raise ZMQError()
 
56
    return rc
 
57
 
 
58
# inner loop inlined, to prevent code duplication for up/downstream
 
59
cdef inline int _relay(void * insocket, void *outsocket, zmq_msg_t msg) nogil:
 
60
    cdef int more=0
 
61
    cdef int label=0
 
62
    cdef int flags=0
 
63
    cdef size_t flagsz
 
64
    flagsz = sizeof (more)
 
65
 
 
66
    while (True):
 
67
 
 
68
        rc = zmq_recvmsg(insocket, &msg, 0)
 
69
        if (rc < 0):
 
70
            return -1
 
71
 
 
72
        flags = 0
 
73
        rc = zmq_getsockopt(insocket, ZMQ_RCVMORE, &more, &flagsz)
 
74
        if (rc < 0):
 
75
            return -1
 
76
        if more:
 
77
            flags = flags | ZMQ_SNDMORE
 
78
        
 
79
        # LABELs have been removed:
 
80
        # rc = zmq_getsockopt(insocket, ZMQ_RCVLABEL, &label, &flagsz)
 
81
        # if (rc < 0):
 
82
        #     return -1
 
83
        # if label:
 
84
        #     flags = flags | ZMQ_SNDLABEL
 
85
        
 
86
        rc = zmq_sendmsg(outsocket, &msg, flags)
 
87
 
 
88
        if (rc < 0):
 
89
            return -1
 
90
 
 
91
        if not (flags):
 
92
            break
 
93
    return 0
 
94
 
 
95
# c_device copied (and cythonized) from zmq_device in zeromq release-2.1.6
 
96
# used under LGPL
 
97
cdef inline int c_device (void * insocket, void *outsocket) nogil:
 
98
    if ZMQ_VERSION_MAJOR < 3:
 
99
        # shouldn't get here
 
100
        return -1
 
101
    cdef zmq_msg_t msg
 
102
    cdef int rc = zmq_msg_init (&msg)
 
103
 
 
104
    if (rc != 0):
 
105
        return -1
 
106
 
 
107
    cdef zmq_pollitem_t items [2]
 
108
    items [0].socket = insocket
 
109
    items [0].fd = 0
 
110
    items [0].events = ZMQ_POLLIN
 
111
    items [0].revents = 0
 
112
    items [1].socket = outsocket
 
113
    items [1].fd = 0
 
114
    items [1].events = ZMQ_POLLIN
 
115
    items [1].revents = 0
 
116
 
 
117
    while (True):
 
118
 
 
119
        #  Wait while there are either requests or replies to process.
 
120
        rc = zmq_poll (&items [0], 2, -1)
 
121
        if (rc < 0):
 
122
            return -1
 
123
        
 
124
 
 
125
        #  The algorithm below asumes ratio of request and replies processed
 
126
        #  under full load to be 1:1. Although processing requests replies
 
127
        #  first is tempting it is suspectible to DoS attacks (overloading
 
128
        #  the system with unsolicited replies).
 
129
 
 
130
        #  Process a request.
 
131
        if (items [0].revents & ZMQ_POLLIN):
 
132
            rc = _relay(insocket, outsocket, msg)
 
133
 
 
134
        #  Process a reply.
 
135
        if (items [1].revents & ZMQ_POLLIN):
 
136
            rc = _relay(outsocket, insocket, msg)
 
137
    return 0
51
138
 
52
139
 
53
140
__all__ = ['device']