Package org.zeromq

Examples of org.zeromq.ZMsg


        //  .split router messages
        //  This method processes one message from a connected
        //  server:
        private void routerMessage()
        {
            ZMsg reply = ZMsg.recvMsg(router);

            //  Frame 0 is server that replied
            String endpoint = reply.popString();
            Server server = servers.get(endpoint);
            assert (server != null);
            if (!server.alive) {
                actives.add(server);
                server.alive = true;
            }
            server.pingAt = System.currentTimeMillis() + PING_INTERVAL;
            server.expires = System.currentTimeMillis() + SERVER_TTL;

            //  Frame 1 may be sequence number for reply
            String sequenceStr = reply.popString();
            if (Integer.parseInt(sequenceStr) == sequence) {
                reply.push("OK");
                reply.send(pipe);
                request.destroy();
                request = null;
            }
            else
                reply.destroy();

        }
View Full Code Here


                            if (System.currentTimeMillis() >= server.expires) {
                                agent.actives.remove(0);
                                server.alive = false;
                            }
                            else {
                                ZMsg request = agent.request.duplicate();
                                request.push(server.endpoint);
                                request.send(agent.router);
                                break;
                            }
                        }
                    }
                }
View Full Code Here

                items.register(backend, ZMQ.Poller.POLLIN);
               
                if (items.poll() == -1)
                    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

            Socket worker = ctx.createSocket(ZMQ.DEALER);
            worker.setHWM (-1);
            worker.setIdentity("W".getBytes());
            worker.connect("tcp://localhost:5556");
            while (!Thread.currentThread().isInterrupted()) {
                ZMsg msg = ZMsg.recvMsg(worker);
                msg.send(worker);
            }

            ctx.destroy();

        }
View Full Code Here

        request.push("");

        //  Blast the request to all connected servers
        int server;
        for (server = 0; server < servers; server++) {
            ZMsg msg = request.duplicate();
            msg.send(socket);
        }
        //  Wait for a matching reply to arrive from anywhere
        //  Since we can poll several times, calculate each one
        ZMsg reply = null;
        long endtime = System.currentTimeMillis() + GLOBAL_TIMEOUT;
        while (System.currentTimeMillis() < endtime) {
            PollItem[] items = { new PollItem(socket, ZMQ.Poller.POLLIN) };
            ZMQ.poll(items, endtime - System.currentTimeMillis());
            if (items[0].isReadable()) {
                //  Reply is [empty][sequence][OK]
                reply = ZMsg.recvMsg(socket);
                assert (reply.size() == 3);
                reply.pop();
                String sequenceStr = reply.popString();
                int sequenceNbr = Integer.parseInt(sequenceStr);
                if (sequenceNbr == sequence)
                    break;
                reply.destroy();
            }
        }
        request.destroy();
        return reply;
View Full Code Here

        //  Send a bunch of name resolution 'requests', measure time
        int requests = 10000;
        long start = System.currentTimeMillis();
        while (requests-- > 0) {
            ZMsg request = new ZMsg();
            request.add("random name");
            ZMsg reply = client.request(request);
            if (reply == null) {
                System.out.printf("E: name service not available, aborting\n");
                break;
            }
            reply.destroy();
        }
        System.out.printf ("Average round trip cost: %d usec\n",
                (int) (System.currentTimeMillis() - start) / 10);

        client.destroy();
View Full Code Here

            int rc = ZMQ.poll(backends,
                    capacity > 0 ? 1000 : -1);
            if (rc == -1)
                break;              //  Interrupted
            //  Handle reply from local worker
            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
View Full Code Here

        System.out.printf("I: trying echo service at %s...\n", endpoint);
        Socket client = ctx.createSocket(ZMQ.REQ);
        client.connect(endpoint);

        //  Send request, wait safely for reply
        ZMsg msg = request.duplicate();
        msg.send(client);
        PollItem[] items = { new PollItem(client, ZMQ.Poller.POLLIN) };
        ZMQ.poll(items, REQUEST_TIMEOUT);
        ZMsg reply = null;
        if (items[0].isReadable())
            reply = ZMsg.recvMsg(client);

        //  Close socket in any case, we're done with it now
        ctx.destroySocket(client);
View Full Code Here

    //  once:

    public static void main (String[] argv)
    {
        ZContext ctx = new ZContext();
        ZMsg request = new ZMsg();
        request.add("Hello world");
        ZMsg reply = null;

        int endpoints = argv.length;
        if (endpoints == 0)
            System.out.printf ("I: syntax: flclient1 <endpoint> ...\n");
        else
        if (endpoints == 1) {
            //  For one endpoint, we retry N times
            int retries;
            for (retries = 0; retries < MAX_RETRIES; retries++) {
                String endpoint = argv [0];
                reply = tryRequest(ctx, endpoint, request);
                if (reply != null)
                    break;          //  Successful
                System.out.printf("W: no response from %s, retrying...\n", endpoint);
            }
        }
        else {
            //  For multiple endpoints, try each at most once
            int endpointNbr;
            for (endpointNbr = 0; endpointNbr < endpoints; endpointNbr++) {
                String endpoint = argv[endpointNbr];
                reply = tryRequest (ctx, endpoint, request);
                if (reply != null)
                    break;          //  Successful
                System.out.printf ("W: no response from %s\n", endpoint);
            }
        }
        if (reply != null) {
            System.out.printf ("Service is running OK\n");
            reply.destroy();
        }
        request.destroy();;
        ctx.destroy();
    }
View Full Code Here

    //  .split set method
    //  Set a new value in the shared hashmap. Sends a [SET][key][value][ttl]
    //  command through to the agent which does the actual work:
    public void set(String key, String value, int ttl)
    {
        ZMsg msg = new ZMsg();
        msg.add("SET");
        msg.add(key);
        msg.add(value);
        msg.add(String.format("%d", ttl));
        msg.send(pipe);
    }
View Full Code Here

TOP

Related Classes of org.zeromq.ZMsg

Copyright © 2018 www.massapicom. 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.