Esempio n. 1
0
def create_sockets(ctx, deferred_socks, socks=None):
    if socks is None:
        socks = OrderedDict()

    for label, deferred_sock in deferred_socks.iteritems():
        socks[label] = zmq.Socket(ctx, deferred_sock.sock_type)

    # set sockopts (must be done first, in case of zmq.IDENTITY)
    for label, s in deferred_socks.iteritems():
        for opt, value in s.sockopts:
            socks[label].setsockopt(opt, value)

    for label, s in deferred_socks.iteritems():
        for bind_uri in s.binds:
            try:
                socks[label].bind(bind_uri)
            except:
                logging.getLogger(log_label()).error('Could not bind: %s',
                                                     bind_uri)
                raise
    for label, s in deferred_socks.iteritems():
        for connect_uri in s.connects:
            try:
                socks[label].connect(connect_uri)
            except:
                logging.getLogger(log_label()).error('Could not connect: %s',
                                                     connect_uri)
                raise
    return socks
Esempio n. 2
0
def create_streams(deferred_socks, socks, io_loop):
    streams = OrderedDict()
    logging.getLogger(log_label()).debug('socks = %s' % socks)
    for label, s in deferred_socks.iteritems():
        if label in socks and s.stream_callbacks:
            sock = socks[label]
            stream = ZMQStream(sock, io_loop)
            for stream_event, callback in s.stream_callbacks:
                logging.getLogger(log_label()).debug('%s %s %s %s' % (
                        label, stream, stream_event, callback))
                # Register callback for stream event
                #   e.g., stream_event='on_recv'
                env = OrderedDict([
                    ('ctx', sock.context),
                    ('io_loop', io_loop),
                    ('socks', socks),
                    ('streams', streams),
                ])
                f = functools.partial(callback, env)
                getattr(stream, stream_event)(f)
            streams[label] = stream
    return streams