# These are simple samples, so for the sake of clarity, there's 
# not a whole lot of error checking. They eschew the more advanced
# features of 0mq in preference to demonstrating core concepts.
#
# This code demonstrates a ZMQ_ROUTER socket. For more information on
# the behavior of a ROUTER socket, check the zmq_socket
# man page: http://api.zeromq.org/2-1:zmq-socket under the
# heading for ZMQ_ROUTER
#
# Since CtrlC generally doesn't work, I recommend forking
# these from your shell (ie start zmqReq.py <port> on Windows
# or zmqReq.py <port> &)

if len(sys.argv) < 2:
    print "usage zmqRouter.py <bindPort>"
    cmdPromptUtils.waitExit()

# Boilerplate zmq code, create context and socket
context = zmq.Context()
port = int(sys.argv[1])

# Bind to the port specified in argv
frontend = context.socket(zmq.ROUTER)
print "Binding to port %i" % port
frontend.bind("tcp://*:%i" % port)

# A Router socket need is an asyncrhonous version of the REP
# socket. Instead of needing to serially process with recv/send/recv/send...
# A Router allows multiple simultaneous active requests. 
#
# To quote from the zmq_socket man page:
from cmdPromptUtils import waitExit
# These are simple samples, so for the sake of clarity, there's 
# not a whole lot of error checking. They eschew the more advanced
# features of 0mq in preference to demonstrating core concepts.
#
# This code demonstrates ZMQ_DEALER. For more information on
# the behavior of a DEALER socket, check the zmq_socket
# man page: http://api.zeromq.org/2-1:zmq-socket under the
# heading for ZMQ_DEALER
#
# This is a modification of the zmqDealer.py with a timeout waiting
# for a response.

if len(sys.argv) < 3:
    print "usage zmqDealerTimeoutWork.py <port0> ... <portN> echoTxt"
    waitExit()

ctx = zmq.Context()
sock = ctx.socket(zmq.DEALER)

for port in sys.argv[1:-1]:
    print "Connecting to port %i" % int(port)
    sock.connect("tcp://localhost:%i" % int(port))
echoText = sys.argv[-1]

class TimeoutError(Exception):
    pass

def timeoutRcv(sock,timeoutMsec):
    """ Timeout on work by using the timeout
        in poll"""