Examples of ZFrame


Examples of org.zeromq.ZFrame

public class ZMTPFrameIntegrationTest {

  @Test
  public void testOneFrameREP() {
    final UUID remoteId = UUID.randomUUID();
    final ZFrame f = new ZFrame("test-frame".getBytes());

    final ZMTPTestConnector tester = new ZMTPTestConnector() {
      @Override
      public void preConnect(final ZMQ.Socket socket) {
        socket.setIdentity(ZMTPUtils.getBytesFromUUID(remoteId));
      }

      @Override
      public void afterConnect(final ZMQ.Socket socket, final ChannelFuture future) {
        f.sendAndKeep(socket);
      }

      @Override
      public boolean onMessage(final ZMTPIncomingMessage msg) {
        // Verify that we can parse the identity correctly
        assertArrayEquals(ZMTPUtils.getBytesFromUUID(remoteId),
                          msg.getSession().getRemoteIdentity());

        System.err.println(msg);

        // Verify that frames received is correct
        assertEquals(1, msg.getMessage().getContent().size());
        assertEquals(0, msg.getMessage().getEnvelope().size());
        assertArrayEquals(f.getData(), msg.getMessage().getContentFrame(0).getData());

        f.destroy();

        return true;
      }
    };
View Full Code Here

Examples of org.zeromq.ZFrame

            case SINGLE:

                if (store(zlog, msg)) {
                    if (flag > 0 && zlog.flushed()) {
                        response.add(new ZFrame(msg.buf().array()));
                        response.send(worker);
                    }
                } else
                    stop = true;
                if (!more)
View Full Code Here

Examples of org.zeromq.ZFrame

            ZContext ctx = new ZContext();
            Socket worker = ctx.createSocket(ZMQ.REQ);
            worker.connect(String.format("ipc://%s-localbe.ipc", self));

            //  Tell broker we're ready for work
            ZFrame frame = new ZFrame(WORKER_READY);
            frame.send(worker, 0);

            while (true) {
                //  Send request, get reply
                ZMsg msg = ZMsg.recvMsg(worker, 0);
                if (msg == null)
View Full Code Here

Examples of org.zeromq.ZFrame

                    break; // Interrupted
                if (items.pollin(0)) {
                    ZMsg msg = ZMsg.recvMsg(frontend);
                    if (msg == null)
                        break; // Interrupted
                    ZFrame address = msg.pop();
                    address.destroy();
                    msg.addFirst(new ZFrame("W"));
                    msg.send(backend);
                }
                if (items.pollin(1)) {

                    ZMsg msg = ZMsg.recvMsg(backend);
                    if (msg == null)
                        break; // Interrupted
                    ZFrame address = msg.pop();
                    address.destroy();
                    msg.addFirst(new ZFrame("C"));
                    msg.send(frontend);
                }
            }
            ctx.destroy();
        }
View Full Code Here

Examples of org.zeromq.ZFrame

            ZMsg msg = null;
            if (backends[0].isReadable()) {
                msg = ZMsg.recvMsg(localbe);
                if (msg == null)
                    break;          //  Interrupted
                ZFrame address = msg.unwrap();
                workers.add(address);
                capacity++;

                //  If it's READY, don't route the message any further
                ZFrame frame = msg.getFirst();
                if (new String(frame.getData()).equals(WORKER_READY)) {
                    msg.destroy();
                    msg = null;
                }
            }
            //  Or handle reply from peer broker
            else if (backends[1].isReadable()) {
                msg = ZMsg.recvMsg(cloudbe);
                if (msg == null)
                    break;          //  Interrupted
                //  We don't use peer broker address for anything
                ZFrame address = msg.unwrap();
                address.destroy();
            }
            //  Route reply to cloud if it's addressed to a broker
            for (argn = 1; msg != null && argn < argv.length; argn++) {
                byte[] data = msg.getFirst().getData();
                if (argv[argn].equals(new String(data))) {
                    msg.send(cloudfe);
                    msg = null;
                }
            }
            //  Route reply to client if we still need to
            if (msg != null)
                msg.send(localfe);

            //  Now we route as many client requests as we have worker capacity
            //  for. We may reroute requests from our local frontend, but not from //
            //  the cloud frontend. We reroute randomly now, just to test things
            //  out. In the next version we'll do this properly by calculating
            //  cloud capacity://

            while (capacity > 0) {
                PollItem frontends[] = {
                        new PollItem(localfe, Poller.POLLIN),
                        new PollItem(cloudfe, Poller.POLLIN)
                };
                rc = ZMQ.poll(frontends, 0);
                assert (rc >= 0);
                int reroutable = 0;
                //  We'll do peer brokers first, to prevent starvation
                if (frontends[1].isReadable()) {
                    msg = ZMsg.recvMsg(cloudfe);
                    reroutable = 0;
                } else if (frontends[0].isReadable()) {
                    msg = ZMsg.recvMsg(localfe);
                    reroutable = 1;
                } else
                    break;      //  No work, go back to backends

                //  If reroutable, send to cloud 20% of the time
                //  Here we'd normally use cloud status information
                //
                if (reroutable != 0 && argv.length > 1 && rand.nextInt(5) == 0) {
                    //  Route to random broker peer
                    int random_peer = rand.nextInt(argv.length - 1) + 1;
                    msg.push(argv[random_peer]);
                    msg.send(cloudbe);
                } else {
                    ZFrame frame = workers.remove(0);
                    msg.wrap(frame);
                    msg.send(localbe);
                    capacity--;
                }
            }
        }
        //  When we're done, clean up properly
        while (workers.size() > 0) {
            ZFrame frame = workers.remove(0);
            frame.destroy();
        }

        ctx.destroy();
    }
View Full Code Here

Examples of org.zeromq.ZFrame

                break;          //  Interrupted

            //  Fail nastily if run against wrong client
            assert (request.size() == 2);

            ZFrame identity = request.pop();
            request.destroy();

            ZMsg reply = new ZMsg();
            reply.add(identity);
            reply.add("OK");
View Full Code Here

Examples of org.zeromq.ZFrame

            ZMsg msg = null;
            if (primary[0].isReadable()) {
                msg = ZMsg.recvMsg(localbe);
                if (msg == null)
                    break;          //  Interrupted
                ZFrame address = msg.unwrap();
                workers.add(address);
                localCapacity++;

                //  If it's READY, don't route the message any further
                ZFrame frame = msg.getFirst();
                if (new String(frame.getData()).equals(WORKER_READY)) {
                    msg.destroy();
                    msg = null;
                }
            }
            //  Or handle reply from peer broker
            else if (primary[1].isReadable()) {
                msg = ZMsg.recvMsg(cloudbe);
                if (msg == null)
                    break;          //  Interrupted
                //  We don't use peer broker address for anything
                ZFrame address = msg.unwrap();
                address.destroy();
            }
            //  Route reply to cloud if it's addressed to a broker
            for (argn = 1; msg != null && argn < argv.length; argn++) {
                byte[] data = msg.getFirst().getData();
                if (argv[argn].equals(new String(data))) {
                    msg.send(cloudfe);
                    msg = null;
                }
            }
            //  Route reply to client if we still need to
            if (msg != null)
                msg.send(localfe);

            //  If we have input messages on our statefe or monitor sockets we
            //  can process these immediately:

            if (primary[2].isReadable()) {
                String peer = statefe.recvStr();
                String status = statefe.recvStr();
                cloudCapacity = Integer.parseInt(status);
            }
            if (primary[3].isReadable()) {
                String status = monitor.recvStr();
                System.out.println(status);
            }

            //  Now we route as many client requests as we have worker capacity
            //  for. We may reroute requests from our local frontend, but not from //
            //  the cloud frontend. We reroute randomly now, just to test things
            //  out. In the next version we'll do this properly by calculating
            //  cloud capacity://

            while (localCapacity + cloudCapacity > 0) {
                PollItem secondary[] = {
                        new PollItem(localfe, Poller.POLLIN),
                        new PollItem(cloudfe, Poller.POLLIN)
                };

                if (localCapacity > 0)
                    rc = ZMQ.poll(secondary, 2, 0);
                else
                    rc = ZMQ.poll(secondary, 1, 0);

                assert (rc >= 0);

                if (secondary[0].isReadable()) {
                    msg = ZMsg.recvMsg(localfe);
                } else if (secondary[1].isReadable()) {
                    msg = ZMsg.recvMsg(cloudfe);
                } else
                    break;      //  No work, go back to backends

                if (localCapacity > 0) {
                    ZFrame frame = workers.remove(0);
                    msg.wrap(frame);
                    msg.send(localbe);
                    localCapacity--;

                } else {
                    //  Route to random broker peer
                    int random_peer = rand.nextInt(argv.length - 1) + 1;
                    msg.push(argv[random_peer]);
                    msg.send(cloudbe);
                }
            }

            //  We broadcast capacity messages to other peers; to reduce chatter
            //  we do this only if our capacity changed.

            if (localCapacity != previous) {
                //  We stick our own address onto the envelope
                statebe.sendMore(self);
                //  Broadcast new capacity
                statebe.send(String.format("%d", localCapacity), 0);
            }
        }
        //  When we're done, clean up properly
        while (workers.size() > 0) {
            ZFrame frame = workers.remove(0);
            frame.destroy();
        }

        ctx.destroy();
    }
View Full Code Here

Examples of org.zeromq.ZFrame

            ZContext ctx = new ZContext();
            Socket worker = ctx.createSocket(ZMQ.REQ);
            worker.connect(String.format("ipc://%s-localbe.ipc", self));

            //  Tell broker we're ready for work
            ZFrame frame = new ZFrame(WORKER_READY);
            frame.send(worker, 0);

            while (true) {
                //  Send request, get reply
                ZMsg msg = ZMsg.recvMsg(worker, 0);
                if (msg == null)
View Full Code Here

Examples of org.zeromq.ZFrame

        Socket worker = ctx.createSocket(ZMQ.DEALER);
        worker.connect( "tcp://localhost:5556");

        //  Tell queue we're ready for work
        System.out.println ("I: worker ready\n");
        ZFrame frame = new ZFrame (PPP_READY);
        frame.send( worker, 0);

        return worker;
    }
View Full Code Here

Examples of org.zeromq.ZFrame

                }
                else
                //  When we get a heartbeat message from the queue, it means the
                //  queue was (recently) alive, so reset our liveness indicator:
                if (msg.size() == 1) {
                    ZFrame frame = msg.getFirst();
                    if (PPP_HEARTBEAT.equals(new String(frame.getData())))
                        liveness = HEARTBEAT_LIVENESS;
                    else {
                        System.out.println ("E: invalid message\n");
                        msg.dump(System.out);
                    }
                    msg.destroy();
                }
                else {
                    System.out.println ("E: invalid message\n");
                    msg.dump(System.out);
                }
                interval = INTERVAL_INIT;
            }
            else
            //  If the queue hasn't sent us heartbeats in a while, destroy the
            //  socket and reconnect. This is the simplest most brutal way of
            //  discarding any messages we might have sent in the meantime://
            if (--liveness == 0) {
                System.out.println ("W: heartbeat failure, can't reach queue\n");
                System.out.println (String.format("W: reconnecting in %zd msec\n", interval));
                try {
                    Thread.sleep(interval);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                if (interval < INTERVAL_MAX)
                    interval *= 2;
                ctx.destroySocket(worker);
                worker = worker_socket (ctx);
                liveness = HEARTBEAT_LIVENESS;
            }

            //  Send heartbeat to queue if it's time
            if (System.currentTimeMillis() > heartbeat_at) {
                heartbeat_at = System.currentTimeMillis() + HEARTBEAT_INTERVAL;
                System.out.println ("I: worker heartbeat\n");
                ZFrame frame = new ZFrame (PPP_HEARTBEAT);
                frame.send(worker, 0);
            }
        }
        ctx.destroy();
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.