Example #1
0
def test_broker():

    ctx = zmq.Context()

    # note: normally, app goes through zio.Node
    sport = zio.Port("server", zmq.SERVER);
    sport.bind(server_address)
    sport.do_binds()
    sport.online()

    client = ZActor(ctx, client_actor)

    factory = Factory(server_address)
    backend = ZActor(ctx, spawner, factory)
    broker = Broker(sport, backend.pipe)

    for count in range(30):
        print (f"main: poll [{count}]")
        ok = broker.poll(1000)      # client->handler
        if not ok:
            break

    print ("main: stopping")
    broker.stop()
    client.pipe.signal()
Example #2
0
def client_actor(ctx, pipe, *args):
    pipe.signal()
    
    port = zio.Port("client", zmq.CLIENT,'')
    port.connect(server_address)
    port.online(None)       # peer not needed if port only direct connects
    cflow = zio.flow.Flow(port)

    msg = zio.Message(label=json.dumps(dict(direction='extract',credit=2)))
    cflow.send_bot(msg)
    print ("client sent BOT:\n%s\n" % (msg,))
    msg = cflow.recv_bot(1000)
    assert(msg)
    print ("client got BOT:\n%s\n" % (msg,))

    for count in range(10):
        cflow.put(zio.Message())

    print ("client sent DAT")
    cflow.send_eot()
    print ("client sent EOT")
    msg = cflow.recv_eot()
    assert(msg)
    print ("client done")
    pipe.recv()                 # wait for signal to exit
Example #3
0
def client_actor(ctx, pipe, address):
    'An actor function talking to a broker on given address'
    pipe.signal()
    
    port = zio.Port("client", zmq.CLIENT,'')
    port.connect(address)
    port.online(None)       # peer not needed if port only direct connects
    log.debug ("made flow")

    direction='extract'
    credit=2
    cflow = Flow(port, direction, credit)

    bot = cflow.bot()
    log.debug (f'client did BOT: {bot}')
    assert(bot)
    cflow.begin()

    msg = zio.Message(form='FLOW', label_object={'flow':'DAT'})
    log.debug (f'client put DAT with {cflow.credit}/{cflow.total_credit} {msg}')
    cflow.put(msg)
    log.debug (f'client did DAT')
    cflow.eot()
    log.debug (f'client did EOT')

    pipe.recv()                 # wait for signal to exit
Example #4
0
def client_actor(ctx, pipe, address):
    'An actor function talking to a broker on given address'
    pipe.signal()

    port = zio.Port("client", zmq.CLIENT, '')
    port.connect(address)
    port.online(None)  # peer not needed if port only direct connects
    log.debug("made flow")
    cflow = zio.flow.Flow(port)

    msg = zio.Message(seqno=0,
                      label=json.dumps(dict(direction='extract', credit=2)))
    cflow.send_bot(msg)
    log.debug(f'client sent BOT: {msg}')
    msg = cflow.recv_bot(1000)
    assert (msg)
    log.debug(f'client got BOT: {msg}')
    msg = zio.Message(seqno=1)
    cflow.put(msg)
    log.debug(f'client sent {msg}')
    eot = zio.Message(seqno=2)
    cflow.send_eot(eot)
    log.debug(f'client sent {eot}')
    eot = cflow.recv_eot()
    assert (eot)
    log.debug(f'client done with {eot}')
    pipe.recv()  # wait for signal to exit
Example #5
0
def dumper(ctx, pipe, bot, address):
    '''
    A dump handler which may be used as an actor talking to a broker's botport.

    Parameters
    ----------
    bot : zio.Message
        Our initiating BOT message
    address : string
        A ZeroMQ address string for a bound broker SERVER socket
    '''
    poller = zmq.Poller()
    poller.register(pipe, zmq.POLLIN)
    pipe.signal()               # ready

    port = zio.Port("dumper", zmq.CLIENT,'')
    port.connect(address)
    port.online(None)       # peer not needed if port only direct connects

    fobj = bot.label_object
    direction=fobj["direction"]
    credit=fobj["credit"]
    flow = Flow(port, direction, credit)
    poller.register(flow.port.sock, zmq.POLLIN)

    log.debug (f'dumper: send {bot}')

    bot = flow.bot(bot)
    assert(bot)

    flow.begin()

    interupted = False
    keep_going = True
    while keep_going:

        for sock,_ in poller.poll():

            if sock == pipe:
                log.debug ("dumper: pipe hit")
                data = pipe.recv()
                if data == b'STOP':
                    log.debug ("dumper: got STOP")
                if len(data) == 0:
                    log.debug ("dumper: got signal")
                interupted = True
                return

            # got flow messages
            try:
                msg = flow.get()
            except TransmissionEnd:
                log.debug ("dumper: get gives EOT")
                flow.eotsend()
                poller.unregister(sock)
                keep_going = False
                break

            log.debug (f'dumper: sock hit: {msg}')

    log.debug("dumper: taking port offline")
    port.offline()
    if not interupted:
        log.debug("dumper: waiting for quit")
        pipe.recv()
    log.debug("dumper: done")
    return
Example #6
0
def dumper(ctx, pipe, bot, address):
    '''
    A dump handler which may be used as an actor talking to a broker's botport.

    Parameters
    ----------
    bot : zio.Message
        Our initiating BOT message
    address : string
        A ZeroMQ address string for a bound broker SERVER socket
    '''
    poller = zmq.Poller()
    poller.register(pipe, zmq.POLLIN)
    pipe.signal()  # ready

    port = zio.Port("client", zmq.CLIENT, '')
    port.connect(address)
    port.online(None)  # peer not needed if port only direct connects
    flow = zio.flow.Flow(port)
    poller.register(flow.port.sock, zmq.POLLIN)

    log.debug(f'dumper: send {bot}')

    flow.send_bot(bot)
    bot = flow.recv_bot(1000)
    assert (bot)

    # must explicitly break the deadlock of us waiting for DAT before
    # triggering the poll so that get() will implicitly send PAY so
    # that the other end can send DAT.  3rd base.
    flow.flush_pay()

    interupted = False
    keep_going = True
    while keep_going:

        for sock, _ in poller.poll():

            if sock == pipe:
                log.debug("dumper: pipe hit")
                data = pipe.recv()
                if data == b'STOP':
                    log.debug("dumper: got STOP")
                if len(data) == 0:
                    log.debug("dumper: got signal")
                interupted = True
                return

            # got flow messages
            msg = flow.get()
            log.debug(f'dumper: sock hit: {msg}')
            if msg is None:
                log.debug("dumper: null message from get, sending EOT")
                flow.send_eot()
                poller.unregister(sock)
                keep_going = False
                break

    log.debug("dumper: taking port offline")
    port.offline()
    if not interupted:
        log.debug("dumper: waiting for quit")
        pipe.recv()
    log.debug("dumper: done")
    return