#!/usr/bin/python

import zmq
context = zmq.Context()
socket_rep = context.socket(zmq.REP)
socket_rep.bind('tcp://127.0.0.1:5050')
socket_sub = context.socket(zmq.SUB)
socket_sub.connect('tcp://127.0.0.1:5051')
socket_sub.setsockopt(zmq.SUBSCRIBE, '')
poller = zmq.Poller()
poller.register(socket_rep, zmq.POLLIN)
poller.register(socket_sub, zmq.POLLIN)
i_req = 0
i_pub = 0
while True:
    poll_results = poller.poll(1000)
    for socket, status in poll_results:
        if socket is socket_rep:
            i_req = i_req + 1
            req = socket.recv()
            print 'req-rep', req
            socket.send('ok')
        else:
            i_pub = i_pub + 1
            msg = socket.recv()
            print 'pub-sub', msg
        print 'nr req-rep %4i nr pub-sub %4i'%(i_req, i_pub)
